-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpianolights.ino
More file actions
430 lines (379 loc) · 12.8 KB
/
pianolights.ino
File metadata and controls
430 lines (379 loc) · 12.8 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// V1.03 Stable version.
// V1.02 Clean up a bit
// V1.01 Second attempt to scan for apple-midi devices
// V1.00 First production release
// V0.07 Attempt to connect to apple-midi devices on network don't seem to resolve mdns, anyway now hardcoded connect to ipad
// V0.06 Use table for colours
// V0.05 Velocity determines brightness
// V0.04 Add ability to save/recover/set password (through web / ap mode).
// V0.03 Add MDNS discovery
// V0.02 Integrate applemidi
// V0.01 First version
// Includes
extern "C" {
#include "user_interface.h"
}
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <mdns.h>
#include <EEPROM.h>
#include "AppleMidi.h"
#include <NeoPixelBus.h>
bool isConnected = false;
APPLEMIDI_CREATE_INSTANCE(WiFiUDP, AppleMIDI); // see definition in AppleMidi_Defs.h
#define QUESTION_SERVICE "_apple-midi._udp.local"
#define MAX_HOSTS 4
#define HOSTS_SERVICE_NAME 0
#define HOSTS_PORT 1
#define HOSTS_HOST_NAME 2
#define HOSTS_ADDRESS 3
String hosts[MAX_HOSTS][4]; // Array containing information about hosts received over mDNS.
#define MIDDLE_C 60
#define keysCount (72)
#define pixelCount (keysCount*2)
NeoPixelBus strip = NeoPixelBus(pixelCount, 2, NEO_GRB);
float keyrColor[12] = { 1.00, 1.00, 1.00, 1.00, 0.50, 0.00, 0.00, 0.00, 0.00, 0.75, 1.00, 1.00 };
float keygColor[12] = { 0.00, 0.50, 0.75, 1.00, 1.00, 1.00, 1.00, 1.00, 0.00, 0.00, 0.00, 0.00 };
float keybColor[12] = { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.75, 1.00, 1.00, 1.00, 1.00, 0.50 };
int r = 0;
int g = 0;
int b = 0;
struct Pixel {
byte r = 0;
byte g = 0;
byte b = 0;
};
struct Pixel hsv2rgb(double h, double s, double v) {
struct Pixel pixel;
double r, g, b;
int i = int(h * 6);
double f = h * 6 - i;
double p = v * (1 - s);
double q = v * (1 - f * s);
double t = v * (1 - (1 - f) * s);
switch(i % 6){
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
pixel.r = 255*r;
pixel.g = 255*g;
pixel.b = 255*b;
return pixel;
}
// ====================================================================================
// Event handlers for incoming MIDI messages
// ====================================================================================
// -----------------------------------------------------------------------------
// rtpMIDI session. Device connected
// -----------------------------------------------------------------------------
void OnAppleMidiConnected(uint32_t ssrc, char* name) {
Serial.print(name);
Serial.println(" connected");
isConnected = true;
// colours OFF
for (int i = 0; i < 144; i++)
strip.SetPixelColor(i, RgbColor(0,0,0));
strip.Show();
}
// -----------------------------------------------------------------------------
// rtpMIDI session. Device disconnected
// -----------------------------------------------------------------------------
void OnAppleMidiDisconnected(uint32_t ssrc) {
isConnected = false;
Serial.println("Disconnect");
}
byte NoteToLight(byte note) {
int light = keysCount + 2 * (note - MIDDLE_C);
if (note < MIDDLE_C)
light += 1;
if ((light < 0) || (light > 144))
light = 255;
return (byte) light;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void OnAppleMidiNoteOn(byte channel, byte note, byte velocity) {
int offset = constrain(note%12,0,12);
byte light = NoteToLight(note);
if (light != 255) {
strip.SetPixelColor(light , RgbColor(velocity*keyrColor[offset],velocity*keygColor[offset],velocity*keybColor[offset]));
strip.Show();
}
// Serial.println("Note On");
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void OnAppleMidiNoteOff(byte channel, byte note, byte velocity) {
byte light = NoteToLight(note);
if (light != 255) {
strip.SetPixelColor(light, RgbColor(0,0,0));
strip.Show();
}
// Serial.println("Note Off");
}
void parseBytes(const char* str, char sep, byte* bytes, int maxBytes, int base) {
for (int i = 0; i < maxBytes; i++) {
bytes[i] = strtoul(str, NULL, base); // Convert byte
str = strchr(str, sep); // Find next separator
if (str == NULL || *str == '\0') {
break; // No more separators, exit
}
str++; // Point to next character after separator
}
}
void answerCallback(const mdns::Answer* answer) {
// A typical PTR record matches service to a human readable name.
// eg:
// service: _mqtt._tcp.local
// name: Mosquitto MQTT server on twinkle.local
if (answer->rrtype == MDNS_TYPE_PTR and strstr(answer->name_buffer, QUESTION_SERVICE) != 0) {
unsigned int i = 0;
for (; i < MAX_HOSTS; ++i) {
if (hosts[i][HOSTS_SERVICE_NAME] == answer->rdata_buffer) {
// Already in hosts[][].
break;
}
if (hosts[i][HOSTS_SERVICE_NAME] == "") {
// This hosts[][] entry is still empty.
hosts[i][HOSTS_SERVICE_NAME] = answer->rdata_buffer;
break;
}
}
if (i == MAX_HOSTS) {
Serial.print(" ** ERROR ** No space in buffer for ");
Serial.print('"');
Serial.print(answer->name_buffer);
Serial.print('"');
Serial.print(" : ");
Serial.print('"');
Serial.println(answer->rdata_buffer);
Serial.print('"');
}
}
// A typical SRV record matches a human readable name to port and FQDN info.
// eg:
// name: Mosquitto MQTT server on twinkle.local
// data: p=0;w=0;port=1883;host=twinkle.local
if (answer->rrtype == MDNS_TYPE_SRV) {
unsigned int i = 0;
for (; i < MAX_HOSTS; ++i) {
if (hosts[i][HOSTS_SERVICE_NAME] == answer->name_buffer) {
// This hosts entry matches the name of the host we are looking for
// so parse data for port and hostname.
char* port_start = strstr(answer->rdata_buffer, "port=");
if (port_start) {
port_start += 5;
char* port_end = strchr(port_start, ';');
char port[1 + port_end - port_start];
strncpy(port, port_start, port_end - port_start);
port[port_end - port_start] = '\0';
if (port_end) {
char* host_start = strstr(port_end, "host=");
if (host_start) {
host_start += 5;
hosts[i][HOSTS_PORT] = port;
hosts[i][HOSTS_HOST_NAME] = host_start;
}
}
}
break;
}
}
if (i == MAX_HOSTS) {
Serial.print(" Did not find ");
Serial.print('"');
Serial.print(answer->name_buffer);
Serial.print('"');
Serial.println(" in hosts buffer.");
}
}
// A typical A record matches an FQDN to network ipv4 address.
// eg:
// name: twinkle.local
// address: 192.168.192.9
if (answer->rrtype == MDNS_TYPE_A) {
int i = 0;
for (; i < MAX_HOSTS; ++i) {
if (hosts[i][HOSTS_HOST_NAME] == answer->name_buffer) {
hosts[i][HOSTS_ADDRESS] = answer->rdata_buffer;
break;
}
}
if (i == MAX_HOSTS) {
Serial.print(" Did not find ");
Serial.print('"');
Serial.print(answer->name_buffer);
Serial.print('"');
Serial.println(" in hosts buffer.");
}
}
Serial.println();
for (int i = 0; i < MAX_HOSTS; ++i) {
if (hosts[i][HOSTS_SERVICE_NAME] != "") {
Serial.print("> ");
Serial.print(hosts[i][HOSTS_SERVICE_NAME]);
Serial.print(" ");
Serial.print(hosts[i][HOSTS_PORT]);
Serial.print(" ");
Serial.print(hosts[i][HOSTS_HOST_NAME]);
Serial.print(" ");
Serial.println(hosts[i][HOSTS_ADDRESS]);
byte ip[4];
parseBytes(hosts[i][HOSTS_ADDRESS].c_str(), '.', ip, 4, 10);
AppleMIDI.invite(IPAddress(ip[0],ip[1],ip[2],ip[3]));
}
}
}
mdns::MDns my_mdns(NULL, NULL, answerCallback);
void setup() {
// put your setup code here, to run once:
Serial.begin(76800);
Serial.println("All LEDs green");
// colours green
for (int key = 0; key < keysCount; key++)
{
for (int i = 0; i < 2; i++)
strip.SetPixelColor(i + (2*key), RgbColor(0,128,0));
}
strip.Show();
EEPROM.begin(512);
delay(10);
String esid = "";
for (int i = 0; i < 32; ++i)
{
esid += char(EEPROM.read(i));
}
String epass = "";
for (int i = 32; i < 96; ++i)
{
epass += char(EEPROM.read(i));
}
Serial.println("Connecting to previous access point");
WiFi.begin(esid.c_str(), epass.c_str());
int wificonnection = 144;
while ((WiFi.status() != WL_CONNECTED) && wificonnection > 0) {
delay(50);
Serial.print(".");
wificonnection --;
// shift to blue
strip.SetPixelColor(wificonnection, RgbColor(0,0,128));
strip.Show();
}
Serial.println();
if (wificonnection == 0) {
Serial.println("Starting web server to enter new wifi details");
Serial.println("All LEDs green");
// colours red
for (int i = 0; i < 144; i++)
strip.SetPixelColor(i, RgbColor(128,0,0));
strip.Show();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
String st = "<ul>";
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
st += "<li>";
st +=i + 1;
st += ": ";
st += WiFi.SSID(i);
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*";
st += "</li>";
}
st += "</ul>";
delay(100);
WiFi.softAP("piano", "");
WiFiServer server(80);
server.begin();
while (1) {
WiFiClient client = server.available();
if (client) {
while(client.connected() && !client.available()) {
delay(1);
}
String req = client.readStringUntil('\r');
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start != -1 && addr_end != -1) {
req = req.substring(addr_start + 1, addr_end);
client.flush();
String s;
if (req == "/") {
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Piano at ";
s += ipStr;
s += "<p>";
s += st;
s += "<form method='get' action='a'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
s += "</html>\r\n\r\n";
}
else if ( req.startsWith("/a?ssid=") ) {
for (int i = 0; i < 96; ++i) {
EEPROM.write(i, 0);
}
String qsid;
qsid = req.substring(8,req.indexOf('&'));
String qpass;
qpass = req.substring(req.lastIndexOf('=')+1);
for (int i = 0; i < qsid.length(); ++i) {
EEPROM.write(i, qsid[i]);
}
for (int i = 0; i < qpass.length(); ++i) {
EEPROM.write(32+i, qpass[i]);
}
EEPROM.commit();
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Piano ";
s += "Found ";
s += req;
s += "<p> saved to eeprom... reset to boot into new wifi</html>\r\n\r\n";
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
}
client.print(s);
}
}
}
}
Serial.println("All LEDs Coloured");
// show colours
for (int key = 0; key < keysCount; key++) {
int offset = constrain(key%12,0,12);
for (int i = 0; i < 2; i++)
strip.SetPixelColor(i + (2*key), RgbColor(128*keyrColor[offset],128*keygColor[offset],128*keybColor[offset]));
}
strip.Show();
my_mdns.Clear();
struct mdns::Query query_applemidi;
strncpy(query_applemidi.qname_buffer, QUESTION_SERVICE, MAX_MDNS_NAME_LEN);
query_applemidi.qtype = MDNS_TYPE_PTR;
query_applemidi.qclass = 1; // "INternet"
query_applemidi.unicast_response = 0;
my_mdns.AddQuery(query_applemidi);
my_mdns.Send();
Serial.println("setting up midi network");
AppleMIDI.begin("piano");
AppleMIDI.OnConnected(OnAppleMidiConnected);
AppleMIDI.OnDisconnected(OnAppleMidiDisconnected);
AppleMIDI.OnReceiveNoteOn(OnAppleMidiNoteOn);
AppleMIDI.OnReceiveNoteOff(OnAppleMidiNoteOff);
}
void loop() {
// put your main code here, to run repeatedly:
AppleMIDI.run();
my_mdns.Check();
}