-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266-osc_module.ino
239 lines (198 loc) · 4.64 KB
/
esp8266-osc_module.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
GPIO over OSC module v1.0
made by Hana Taki. @fataris
Licence : CC BY-NC-SA 4.0
*/
extern "C" {
#include "user_interface.h"
}
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
#include <Metro.h>
#include "types.h"
#define SAMPLING_RATE 25
#define ACTIVE_SIGNAL_INTERVAL 1000
#define GPIO_MODE_INPUT 0
#define GPIO_MODE_OUTPUT 1
static const int MODULE_ID = 1; //If you want to use more than one module, set id on each module.
char ssid[] = "YOURSSID"; // your network SSID (name)
char pass[] = "YOURPASS"; // your network password
WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
IPAddress hostAddress(224, 0, 0, 1); // remote IP of your computer
const unsigned int hostPort = 56789; // remote port to receive OSC
const unsigned int receivePort = 54321; // local port to listen to OSC packets
//To Receive
OSCErrorCode error;
//GPIO MODE 0 : Input, 1 : Output
boolean IO12_mode, IO13_mode, IO14_mode;
IO_Mode_t modes;
Input_Values_t inputs;
//Metro
Metro readMetro = Metro(SAMPLING_RATE);
Metro activeMetro = Metro(ACTIVE_SIGNAL_INTERVAL);
void sendMessage(OSCMessage &msg) {
Udp.beginPacket(hostAddress, hostPort);
msg.send(Udp);
Udp.endPacket();
msg.empty();
}
void digitalOut(OSCMessage &msg) {
int id = msg.getInt(0);
if (id == MODULE_ID) {
float ch = msg.getInt(1);
int val = msg.getFloat(2) * 1023;
analogWrite(ch, val);
}
}
void sendInput(int ch, int val) {
OSCMessage mes("/digital_in");
mes.add(MODULE_ID);
mes.add(ch);
mes.add(val);
sendMessage(mes);
}
void sendDigitalIn() {
int val = 0;
if (modes.io12 == INPUT) {
val = digitalRead(12);
if (val != inputs.io12) {
sendInput(12, val);
inputs.io12 = val;
}
}
if (modes.io13 == INPUT) {
val = digitalRead(13);
if (val != inputs.io13) {
sendInput(13, val);
inputs.io13 = val;
}
}
if (modes.io14 == INPUT) {
val = digitalRead(14);
if (val != inputs.io14) {
sendInput(14, val);
inputs.io14 = val;
}
}
if (modes.io16 == INPUT) {
val = digitalRead(16);
if (val != inputs.io16) {
sendInput(16, val);
inputs.io16 = val;
}
}
}
void sendAnalogIn() {
float val = system_adc_read();
val /= 1024.0; //rescale into 0.0 ~ 1.0
OSCMessage mes("/analog_in");
mes.add(MODULE_ID);
mes.add(val);
sendMessage(mes);
}
void setGpioMode(OSCMessage &msg) {
int ID = msg.getInt(0);
if (ID == MODULE_ID) {
int ch = msg.getInt(1);
int gpioMode = msg.getInt(2); //0 -- input. 1--output
switch (ch) {
case 12:
modes.io12 = gpioMode;
pinMode(12, modes.io12);
break;
case 13:
modes.io13 = gpioMode;
pinMode(13, modes.io13);
break;
case 14:
modes.io14 = gpioMode;
pinMode(14, modes.io14);
break;
case 16:
modes.io16 = gpioMode;
pinMode(16, modes.io16);
break;
default:
break;
}
}
}
void setHostAddress(OSCMessage &msg) {
int adr0 = msg.getInt(0);
int adr1 = msg.getInt(1);
int adr2 = msg.getInt(2);
int adr3 = msg.getInt(3);
hostAddress[0] = adr0;
hostAddress[1] = adr1;
hostAddress[2] = adr2;
hostAddress[3] = adr3;
}
void sendActiveSignal() {
OSCMessage mes("/active");
mes.add(MODULE_ID);
mes.add(1);
sendMessage(mes);
}
void setupWiFi() {
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void setupUdp() {
Udp.begin(receivePort);
}
void initGpio() {
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
inputs.io12 = digitalRead(12);
inputs.io13 = digitalRead(13);
inputs.io14 = digitalRead(14);
inputs.io16 = digitalRead(16);
modes.io12 = INPUT;
pinMode(12, modes.io12);
modes.io13 = INPUT;
pinMode(13, modes.io13);
modes.io14 = INPUT;
pinMode(14, modes.io14);
modes.io16 = INPUT;
pinMode(16, modes.io16);
}
void updateOsc() {
//Check receive
OSCMessage msgIn;
int size = Udp.parsePacket();
if (size > 0) {
while (size--) {
msgIn.fill(Udp.read());
}
if (msgIn.hasError()) {
error = msgIn.getError();
} else {
msgIn.dispatch("/gpio_mode", setGpioMode);
msgIn.dispatch("/digital_out", digitalOut);
msgIn.dispatch("/set_address", setHostAddress);
}
}
}
void setup() {
Serial.begin(115200);
setupWiFi();
setupUdp();
initGpio();
}
void loop() {
//Read OSC message
updateOsc();
//Send GPIO values
if (readMetro.check()) {
sendDigitalIn();
sendAnalogIn();
}
if (activeMetro.check()) {
sendActiveSignal();
}
}