Skip to content

Commit 451c210

Browse files
author
Eric
committed
Interactive Particles Responsive Made With ESP32 + INMP441 & Unity
1 parent 819ba48 commit 451c210

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+6526
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/////////////////////////////////////////////////////////////////
2+
/*
3+
Interactive Particles Responsive Made With ESP32 + INMP441 & Unity
4+
For More Information: https://youtu.be/lRj01J-cxew
5+
Created by Eric N. (ThatProject)
6+
*/
7+
/////////////////////////////////////////////////////////////////
8+
9+
#include <driver/i2s.h>
10+
11+
#define I2S_WS_RX 32
12+
#define I2S_SCK_RX 33
13+
#define I2S_SD_RX 15
14+
#define I2S_PORT_RX I2S_NUM_0
15+
16+
#define SAMPLE_RATE 16000
17+
#define SAMPLE_BITS 32
18+
#define DMA_BANKS 4
19+
#define DMA_BANK_SIZE 128
20+
21+
TaskHandle_t i2sReadTaskHandler = NULL;
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
i2s_RX_init();
26+
xTaskCreatePinnedToCore(
27+
i2s_reader_task,
28+
"i2s_reader_task",
29+
10000,
30+
NULL,
31+
1,
32+
&i2sReadTaskHandler,
33+
0);
34+
}
35+
36+
void loop() {}
37+
38+
void i2s_reader_task(void* parameter) {
39+
while (1) {
40+
int32_t samples[DMA_BANK_SIZE];
41+
size_t num_bytes_read = 0;
42+
i2s_read(I2S_PORT_RX, &samples, DMA_BANK_SIZE, &num_bytes_read, portMAX_DELAY);
43+
44+
int samples_read = num_bytes_read / 8;
45+
if (samples_read > 0) {
46+
float mean = 0;
47+
for (int i = 0; i < samples_read; ++i) {
48+
mean += (samples[i] >> 14);
49+
}
50+
mean /= samples_read;
51+
52+
float maxsample = -1e8, minsample = 1e8;
53+
for (int i = 0; i < samples_read; ++i) {
54+
minsample = min(minsample, samples[i] - mean);
55+
maxsample = max(maxsample, samples[i] - mean);
56+
}
57+
58+
// To narrow the range of the value
59+
float result = (maxsample - minsample) / 100000000;
60+
Serial.println(result);
61+
vTaskDelay(1);
62+
}
63+
}
64+
}
65+
66+
const i2s_config_t i2s_config_rx = {
67+
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
68+
.sample_rate = SAMPLE_RATE,
69+
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
70+
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
71+
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
72+
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
73+
.dma_buf_count = DMA_BANKS,
74+
.dma_buf_len = DMA_BANK_SIZE
75+
};
76+
77+
const i2s_pin_config_t pin_config_rx = {
78+
.bck_io_num = I2S_SCK_RX,
79+
.ws_io_num = I2S_WS_RX,
80+
.data_out_num = I2S_PIN_NO_CHANGE,
81+
.data_in_num = I2S_SD_RX
82+
};
83+
84+
void i2s_RX_init() {
85+
i2s_driver_install(I2S_PORT_RX, &i2s_config_rx, 0, NULL);
86+
i2s_set_pin(I2S_PORT_RX, &pin_config_rx);
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# This .gitignore file should be placed at the root of your Unity project directory
2+
#
3+
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
4+
#
5+
/[Ll]ibrary/
6+
/[Tt]emp/
7+
/[Oo]bj/
8+
/[Bb]uild/
9+
/[Bb]uilds/
10+
/[Ll]ogs/
11+
/[Uu]ser[Ss]ettings/
12+
13+
# MemoryCaptures can get excessive in size.
14+
# They also could contain extremely sensitive data
15+
/[Mm]emoryCaptures/
16+
17+
# Asset meta data should only be ignored when the corresponding asset is also ignored
18+
!/[Aa]ssets/**/*.meta
19+
20+
# Uncomment this line if you wish to ignore the asset store tools plugin
21+
# /[Aa]ssets/AssetStoreTools*
22+
23+
# Autogenerated Jetbrains Rider plugin
24+
/[Aa]ssets/Plugins/Editor/JetBrains*
25+
26+
# Visual Studio cache directory
27+
.vs/
28+
29+
# Gradle cache directory
30+
.gradle/
31+
32+
# Autogenerated VS/MD/Consulo solution and project files
33+
ExportedObj/
34+
.consulo/
35+
*.csproj
36+
*.unityproj
37+
*.sln
38+
*.suo
39+
*.tmp
40+
*.user
41+
*.userprefs
42+
*.pidb
43+
*.booproj
44+
*.svd
45+
*.pdb
46+
*.mdb
47+
*.opendb
48+
*.VC.db
49+
50+
# Unity3D generated meta files
51+
*.pidb.meta
52+
*.pdb.meta
53+
*.mdb.meta
54+
55+
# Unity3D generated file on crash reports
56+
sysinfo.txt
57+
58+
# Builds
59+
*.apk
60+
*.unitypackage
61+
62+
# Crashlytics generated file
63+
crashlytics-build.properties
64+
65+
# Packed Addressables
66+
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*
67+
68+
# Temporary auto-generated Android Assets
69+
/[Aa]ssets/[Ss]treamingAssets/aa.meta
70+
/[Aa]ssets/[Ss]treamingAssets/aa/*

Interactive_Particles_Responsive/Unity_VisualEffect/.idea/.idea.ParticleTest/.idea/.gitignore

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Interactive_Particles_Responsive/Unity_VisualEffect/.idea/.idea.ParticleTest/.idea/indexLayout.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/////////////////////////////////////////////////////////////////
2+
/*
3+
Interactive Particles Responsive Made With ESP32 + INMP441 & Unity
4+
For More Information: https://youtu.be/lRj01J-cxew
5+
Created by Eric N. (ThatProject)
6+
*/
7+
/////////////////////////////////////////////////////////////////
8+
9+
using UnityEngine;
10+
using UnityEngine.VFX;
11+
12+
public class EffectController : MonoBehaviour
13+
{
14+
VisualEffect visualEffect;
15+
16+
17+
// Start is called before the first frame update
18+
void Start()
19+
{
20+
visualEffect = GetComponent<VisualEffect>();
21+
}
22+
23+
// Update is called once per frame
24+
void Update()
25+
{
26+
27+
}
28+
29+
public void setForceX(float x)
30+
{
31+
visualEffect.SetFloat("ForceX", x * -1);
32+
}
33+
}

Interactive_Particles_Responsive/Unity_VisualEffect/Assets/EffectController.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/////////////////////////////////////////////////////////////////
2+
/*
3+
Interactive Particles Responsive Made With ESP32 + INMP441 & Unity
4+
For More Information: https://youtu.be/lRj01J-cxew
5+
Created by Eric N. (ThatProject)
6+
*/
7+
/////////////////////////////////////////////////////////////////
8+
9+
using System;
10+
using System.Collections;
11+
using System.IO.Ports;
12+
using UnityEngine;
13+
using UnityEngine.VFX;
14+
15+
public class MySerialOBJ : MonoBehaviour
16+
{
17+
private SerialPort serial;
18+
public int portSpeed = 115200;
19+
public string portName = "/dev/cu.SLAB_USBtoUART";
20+
21+
void OnEnable()
22+
{
23+
if (checkOpen())
24+
{
25+
Debug.Log("Opening serial port: " + portName + " at " + portSpeed + " bauds");
26+
}
27+
}
28+
29+
void Start()
30+
{
31+
}
32+
33+
// Update is called once per frame
34+
void Update()
35+
{
36+
if (serial != null)
37+
{
38+
checkOpen();
39+
StartCoroutine(ReadSerial());
40+
}
41+
}
42+
43+
private bool checkOpen()
44+
{
45+
if (serial == null)
46+
{
47+
if (serial != null && serial.IsOpen)
48+
{
49+
serial.Close();
50+
}
51+
52+
serial = new SerialPort(portName, portSpeed);
53+
}
54+
55+
if (!serial.IsOpen)
56+
{
57+
try
58+
{
59+
serial.Open();
60+
serial.DtrEnable = true;
61+
serial.DiscardInBuffer();
62+
}
63+
catch (Exception e)
64+
{
65+
Debug.LogError("System.Exception in serial.Open(): " + e);
66+
}
67+
}
68+
69+
return serial.IsOpen;
70+
}
71+
72+
public void OnApplicationQuit()
73+
{
74+
if (serial != null)
75+
{
76+
if (serial.IsOpen)
77+
{
78+
serial.Close();
79+
}
80+
81+
serial = null;
82+
}
83+
}
84+
85+
IEnumerator ReadSerial()
86+
{
87+
while (true)
88+
{
89+
if (!enabled)
90+
{
91+
yield break;
92+
}
93+
94+
try
95+
{
96+
while (serial.BytesToRead > 0)
97+
{
98+
string data = serial.ReadLine();
99+
Debug.Log("Sound Pressure from INMP441: " + data);
100+
101+
float soundPressure;
102+
if (float.TryParse(data, out soundPressure))
103+
{
104+
FindObjectOfType<VisualEffect>().GetComponent<EffectController>().setForceX(soundPressure);
105+
}
106+
}
107+
}
108+
catch (Exception e)
109+
{
110+
Debug.LogError("Exception in Serial ReadLine: " + e);
111+
}
112+
113+
if (serial.IsOpen && serial.BytesToRead == -1)
114+
{
115+
serial.Close();
116+
}
117+
118+
yield return null;
119+
}
120+
}
121+
}

Interactive_Particles_Responsive/Unity_VisualEffect/Assets/MySerialOBJ.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Interactive_Particles_Responsive/Unity_VisualEffect/Assets/Scenes.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Interactive_Particles_Responsive/Unity_VisualEffect/Assets/Scenes/SampleScene.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)