forked from OpenBCI/OpenBCI_WIFI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WifiShieldOSC.ino
255 lines (234 loc) · 6.96 KB
/
WifiShieldOSC.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//f.olofsson2019
//install esp8266 board manager (version 2.5.0)
//install OpenBCI_Wifi, OSC, ArduinoJson libraries (json need to be the older 5.13.4)
//select board 'Generic ESP8266 Module' and set cpu freq to "80 MHz"
//set Builtin Led to "2" and Flash Size to "4M (1M SPIFFS)"
//hold PRG (gpio0) on power up to upload new firmware
//TODO:
//adapt for Ganglion and CytonDaisy
#define ARDUINO_ARCH_ESP8266
#define ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include "SPISlave.h"
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCData.h>
#include <Ticker.h>
#include "OpenBCI_Wifi_Definitions.h"
#include "OpenBCI_Wifi.h"
#define DEFAULT_LATENCY 5000 //override OpenBCI_Wifi_Definitions
#define MAX_PACKETS_PER_SEND_OSC 39 //ensure no segmented packages
#define OSCINPORT 13999 //EDIT input osc port
unsigned int udpPort = 57120; //EDIT output osc port (supercollider by default)
const char *espname = "OpenBCI_WifiShieldOSC";
unsigned long lastSendToClient;
WiFiUDP clientUDP;
Ticker ticker;
void tick() {
int state = digitalRead(LED_BUILTIN);
digitalWrite(LED_BUILTIN, !state);
}
void configModeCallback(WiFiManager *myWiFiManager) {
ticker.attach(0.15, tick);
}
void setup() {
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
ticker.attach(0.6, tick);
WiFi.hostname(espname);
wifi_station_set_hostname(espname);
WiFiManager wifiManager;
wifiManager.setAPCallback(configModeCallback);
if (!wifiManager.autoConnect(espname)) {
ESP.reset();
delay(1000);
}
MDNS.begin(espname); //make .local work
clientUDP.begin(OSCINPORT);
SPISlave.onData([](uint8_t * data, size_t len) {
wifi.spiProcessPacket(data);
});
SPISlave.onDataSent([]() {
wifi.spiOnDataSent();
SPISlave.setData(wifi.passthroughBuffer, BYTES_PER_SPI_PACKET);
});
SPISlave.onStatusSent([]() {
SPISlave.setStatus(209);
});
SPISlave.begin();
SPISlave.setStatus(209);
SPISlave.setData(wifi.passthroughBuffer, BYTES_PER_SPI_PACKET);
wifi.setLatency(DEFAULT_LATENCY);
ticker.detach();
digitalWrite(LED_BUILTIN, HIGH);
}
void oscStart(OSCMessage &msg) {
wifi.passthroughCommands("b"); //start streaming
SPISlave.setData(wifi.passthroughBuffer, BYTES_PER_SPI_PACKET);
}
void oscStop(OSCMessage &msg) {
wifi.passthroughCommands("s"); //stop streaming
SPISlave.setData(wifi.passthroughBuffer, BYTES_PER_SPI_PACKET);
}
void oscPort(OSCMessage &msg) {
udpPort = getIntData(msg, 0);
}
void oscLatency(OSCMessage &msg) {
uint16_t latency = getIntData(msg, 0);
wifi.setLatency(latency);
}
void oscReset(OSCMessage &msg) {
wifi.passthroughCommands("s"); //stop streaming
SPISlave.setData(wifi.passthroughBuffer, BYTES_PER_SPI_PACKET);
delay(1);
wifi.passthroughCommands("d"); //set default channel settings
SPISlave.setData(wifi.passthroughBuffer, BYTES_PER_SPI_PACKET);
delay(1);
wifi.passthroughCommands("~6"); //set default sample rate
SPISlave.setData(wifi.passthroughBuffer, BYTES_PER_SPI_PACKET);
delay(1);
wifi.setLatency(DEFAULT_LATENCY);
OSCMessage rpl("/ready");
rpl.add(OSCINPORT);
sendMsg(rpl);
rpl.empty();
}
void oscCommand(OSCMessage &msg) {
uint8_t len = msg.getDataLength(0);
char str[len];
msg.getString(0, str, len);
uint8_t retVal = wifi.passthroughCommands(str);
if (retVal < PASSTHROUGH_PASS) {
OSCMessage rpl("/reply");
switch (retVal) {
case PASSTHROUGH_FAIL_TOO_MANY_CHARS:
rpl.add(501);
rpl.add("Error: Sent more than 31 chars");
break;
case PASSTHROUGH_FAIL_NO_CHARS:
rpl.add(505);
rpl.add("Error: No characters found for key 'command'");
break;
case PASSTHROUGH_FAIL_QUEUE_FILLED:
rpl.add(503);
rpl.add("Error: Queue is full, please wait 20ms and try again.");
break;
default:
rpl.add(504);
rpl.add("Error: Unknown error");
}
sendMsg(rpl);
rpl.empty();
}
}
void oscVersion(OSCMessage &msg) {
OSCMessage rpl("/version");
rpl.add(wifi.getVersion().c_str());
sendMsg(rpl);
rpl.empty();
}
void oscName(OSCMessage &msg) {
OSCMessage rpl("/name");
rpl.add(wifi.getName().c_str());
sendMsg(rpl);
rpl.empty();
}
void oscBoard(OSCMessage &msg) {
OSCMessage rpl("/board");
rpl.add(wifi.getInfoBoard().c_str());
sendMsg(rpl);
rpl.empty();
}
void oscAll(OSCMessage &msg) {
OSCMessage rpl("/all");
rpl.add(wifi.getInfoAll().c_str());
sendMsg(rpl);
rpl.empty();
}
void sendMsg(OSCMessage &msg) {
clientUDP.beginPacket(clientUDP.remoteIP(), udpPort);
msg.send(clientUDP);
clientUDP.endPacket();
yield();
}
int getIntData(OSCMessage &msg, int val) {
if (msg.isInt(val)) {
return msg.getInt(val);
}
return int(msg.getFloat(val));
}
void loop() {
MDNS.update();
int packetSize = clientUDP.parsePacket();
if (packetSize) {
OSCMessage oscMsg;
while (packetSize--) {
oscMsg.fill(clientUDP.read());
}
if (!oscMsg.hasError()) {
oscMsg.dispatch("/start", oscStart);
oscMsg.dispatch("/stop", oscStop);
oscMsg.dispatch("/port", oscPort);
oscMsg.dispatch("/latency", oscLatency);
oscMsg.dispatch("/reset", oscReset);
oscMsg.dispatch("/command", oscCommand);
oscMsg.dispatch("/version", oscVersion);
oscMsg.dispatch("/name", oscName);
oscMsg.dispatch("/board", oscBoard);
oscMsg.dispatch("/all", oscAll);
}
}
if (wifi.clientWaitingForResponseFullfilled) {
wifi.clientWaitingForResponseFullfilled = false;
OSCMessage rpl("/reply");
switch (wifi.curClientResponse) {
case wifi.CLIENT_RESPONSE_OUTPUT_STRING:
rpl.add(wifi.outputString.c_str());
wifi.outputString = "";
break;
case wifi.CLIENT_RESPONSE_NONE:
default:
rpl.add("");
break;
}
sendMsg(rpl);
rpl.empty();
}
if (wifi.clientWaitingForResponse && (millis() > (wifi.timePassthroughBufferLoaded + 2000))) {
wifi.clientWaitingForResponse = false;
OSCMessage rpl("/reply");
rpl.add(502);
rpl.add("Error: timeout getting command response, be sure board is fully connected");
sendMsg(rpl);
rpl.empty();
wifi.outputString = "";
}
int packetsToSend = wifi.rawBufferHead - wifi.rawBufferTail;
if (packetsToSend < 0) {
packetsToSend = NUM_PACKETS_IN_RING_BUFFER_RAW + packetsToSend; // for wrap around
}
if (packetsToSend > MAX_PACKETS_PER_SEND_OSC) {
packetsToSend = MAX_PACKETS_PER_SEND_OSC;
digitalWrite(LED_BUILTIN, LOW);
}
if ((micros() > (lastSendToClient + wifi.getLatency()) || packetsToSend == MAX_PACKETS_PER_SEND_OSC) && (packetsToSend > 0)) {
OSCMessage msg("/data");
uint32_t taily = wifi.rawBufferTail;
for (uint8_t i = 0; i < packetsToSend; i++) {
if (taily >= NUM_PACKETS_IN_RING_BUFFER_RAW) {
taily = 0;
}
msg.add(wifi.rawBuffer[taily], BYTES_PER_SPI_PACKET);
taily += 1;
}
sendMsg(msg);
msg.empty();
lastSendToClient = micros();
wifi.rawBufferTail = taily;
digitalWrite(LED_BUILTIN, HIGH);
}
delay(1);
}