-
Notifications
You must be signed in to change notification settings - Fork 1
/
SonicPiOSC.ts
453 lines (368 loc) · 13.7 KB
/
SonicPiOSC.ts
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* Sonic Pi OSC Extension for the micro:bit to allow a micro:bit to send
* OpenSound Control (OSC) messages directly to an instance of Sonic Pi.
*/
/**
* Custom blocks
*/
//% color="#FF1493" icon="\uf001" weight=90 block="Sonic Pi OSC"
namespace SonicPiOSC {
let initialised_state: boolean = false
let wifi_connected_state: boolean = false
let sonicpi_connected_state: boolean = false
let send_command_state: boolean = false
let number_of_retries: number = 1
let command_timeout: number = 10000
let address_buffer: Buffer
let tag_buffer: Buffer
let parameter_buffer: Buffer
/**
* Initialise the ESP8266
*/
//% block="initialise|TX = %tx|RX = %rx|baud rate = %baudrate"
//% tx.defl=SerialPin.P8
//% rx.defl=SerialPin.P12
//% baudrate.defl=BaudRate.BaudRate115200
export function initialise(tx: SerialPin, rx: SerialPin, baudrate: BaudRate) {
let retry_count = 0
initialised_state = false
wifi_connected_state = false
sonicpi_connected_state = false
send_command_state = false
while (!initialised_state && retry_count < number_of_retries) {
initialised_state = performInitialise(tx, rx, baudrate)
retry_count++
}
}
function performInitialise(tx: SerialPin, rx: SerialPin, baudrate: BaudRate): boolean {
serial.redirect(tx, rx, baudrate)
serial.setRxBufferSize(128)
serial.setTxBufferSize(128)
serial.writeString("AT+RESTORE\r\n")
serial.writeString("AT\r\n")
let startTime: number = input.runningTime()
let returnedMessage : string = ""
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("OK")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
serial.writeString("AT+RST\r\n")
startTime = input.runningTime()
returnedMessage = ""
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("ready")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
return true
}
/**
* Return the Initialised State
*/
//% block="initialised state"
export function initialisedState(): boolean {
return initialised_state
}
/**
* Connect to WiFi
*/
//% block="connect WiFi|name = %ssid|password = %password"
export function connectWiFi(ssid: string, password: string) {
wifi_connected_state = false
sonicpi_connected_state = false
send_command_state = false
if (initialised_state) {
let retry_count = 0
while (!wifi_connected_state && retry_count < number_of_retries) {
wifi_connected_state = performConnectWiFi(ssid, password)
retry_count++
}
}
}
function performConnectWiFi(ssid: string, password: string): boolean {
serial.writeString("AT+CWMODE=1\r\n")
let startTime: number = input.runningTime()
let returnedMessage : string = ""
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("OK")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
serial.writeString("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"\r\n")
startTime = input.runningTime()
returnedMessage = ""
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("WIFI CONNECTED")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("WIFI GOT IP")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("OK")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
return true
}
/**
* Return the WiFi Connected state
*/
//% block="wifi connected state"
export function wifiConnectedState(): boolean {
return wifi_connected_state
}
/**
* Connect to OSC
*/
//% block="connect Sonic Pi|server = %server|port = %port"
//% port.defl=4560
export function connectSonicPi(server: string, port: number) {
sonicpi_connected_state = false
send_command_state = false
if (initialised_state && wifi_connected_state) {
let retry_count = 0
while (!sonicpi_connected_state && retry_count < number_of_retries) {
sonicpi_connected_state = performConnectSonicPi(server, port)
retry_count++
}
}
}
function performConnectSonicPi(server: string, port: number): boolean {
serial.writeString("AT+CIPMUX=0\r\n")
let startTime: number = input.runningTime()
let returnedMessage : string = ""
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("OK")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
serial.writeString("AT+CIPSTART=\"UDP\",\"" + server + "\"," + port + "," + (port + 1) + ",0\r\n")
startTime = input.runningTime()
returnedMessage = ""
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("CONNECT")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("OK")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
return true
}
/**
* Return the Sonic Pi Connected State
*/
//% block="sonic pi connected state"
export function sonicPiOSCState(): boolean {
return sonicpi_connected_state
}
/**
* Start Command
*/
//% block="start command|address = %address"
export function startCommand(address: string) {
if (initialised_state && wifi_connected_state && sonicpi_connected_state) {
let address_buffer_length = (Math.trunc(address.length / 4) + 1) * 4
address_buffer = pins.createBuffer(address_buffer_length)
address_buffer.fill(0)
for (let buffer_position = 0; buffer_position < address.length; buffer_position++) {
address_buffer.setUint8(buffer_position, address.charCodeAt(buffer_position))
// address_buffer.setNumber(NumberFormat.Int8LE, buffer_position, address.charCodeAt(buffer_position))
}
tag_buffer = pins.createBuffer(1)
tag_buffer.setUint8(0, 44)
// tag_buffer.setNumber(NumberFormat.Int8LE, 0, 44)
parameter_buffer = pins.createBuffer(0)
}
}
/**
* Add String Parameter
*/
//% block="add string parameter|value = %value"
export function addStringParameter(value: string) {
if (initialised_state && wifi_connected_state && sonicpi_connected_state) {
let new_tag_buffer = pins.createBuffer(tag_buffer.length + 1)
new_tag_buffer.write(0, tag_buffer)
// new_tag_buffer.setNumber(NumberFormat.Int8LE, new_tag_buffer.length - 1, 115)
new_tag_buffer.setUint8(new_tag_buffer.length - 1, 115)
tag_buffer = new_tag_buffer
let parameter_value_length = (Math.trunc(value.length / 4) + 1) * 4
let new_parameter_buffer = pins.createBuffer(parameter_buffer.length + parameter_value_length)
new_parameter_buffer.fill(0)
new_parameter_buffer.write(0, parameter_buffer)
for (let buffer_position = 0; buffer_position < parameter_value_length; buffer_position++) {
// new_parameter_buffer.setNumber(NumberFormat.Int8LE, parameter_buffer.length + buffer_position, value.charCodeAt(buffer_position))
new_parameter_buffer.setUint8(parameter_buffer.length + buffer_position, value.charCodeAt(buffer_position))
}
parameter_buffer = new_parameter_buffer
}
}
/**
* Add Int Parameter
*/
//% block="add int parameter|value = %value"
export function addIntParameter(value: number) {
if (initialised_state && wifi_connected_state && sonicpi_connected_state) {
let new_tag_buffer = pins.createBuffer(tag_buffer.length + 1)
new_tag_buffer.write(0, tag_buffer)
// new_tag_buffer.setNumber(NumberFormat.Int8LE, new_tag_buffer.length - 1, 105)
new_tag_buffer.setUint8(new_tag_buffer.length - 1, 105)
tag_buffer = new_tag_buffer
let parameter_value_length = 4
let new_parameter_buffer = pins.createBuffer(parameter_buffer.length + parameter_value_length)
new_parameter_buffer.fill(0)
new_parameter_buffer.write(0, parameter_buffer)
new_parameter_buffer.setNumber(NumberFormat.Int32BE, parameter_buffer.length, value)
parameter_buffer = new_parameter_buffer
}
}
/**
* Add Float Parameter
*/
//% block="add float parameter|value = %value"
export function addFloatParameter(value: number) {
if (initialised_state && wifi_connected_state && sonicpi_connected_state) {
let new_tag_buffer = pins.createBuffer(tag_buffer.length + 1)
new_tag_buffer.write(0, tag_buffer)
// new_tag_buffer.setNumber(NumberFormat.Int8LE, new_tag_buffer.length - 1, 102)
new_tag_buffer.setUint8(new_tag_buffer.length - 1, 102)
tag_buffer = new_tag_buffer
let parameter_value_length = 4
let new_parameter_buffer = pins.createBuffer(parameter_buffer.length + parameter_value_length)
new_parameter_buffer.fill(0)
new_parameter_buffer.write(0, parameter_buffer)
new_parameter_buffer.setNumber(NumberFormat.Float32BE, parameter_buffer.length, value)
parameter_buffer = new_parameter_buffer
}
}
/**
* Send Command
*/
//% block="send command"
export function sendCommand() {
send_command_state = false
if (initialised_state && wifi_connected_state && sonicpi_connected_state) {
let retry_count = 0
while (!send_command_state && retry_count < number_of_retries) {
send_command_state = performSendCommand()
retry_count++
}
}
}
export function performSendCommand(): boolean {
let tag_buffer_length = (Math.trunc((tag_buffer.length - 1) / 4) + 1) * 4
// let send_buffer = pins.createBuffer(address_buffer.length + tag_buffer_length)
let send_buffer = pins.createBuffer(address_buffer.length + tag_buffer_length + parameter_buffer.length)
send_buffer.fill(0)
send_buffer.write(0, address_buffer)
send_buffer.write(address_buffer.length, tag_buffer)
send_buffer.write(address_buffer.length + tag_buffer_length, parameter_buffer)
serial.writeString("AT+CIPSEND=" + send_buffer.length + "\r\n")
let startTime: number = input.runningTime()
let returnedMessage : string = ""
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes("OK")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
while (true) {
returnedMessage += serial.readString()
if (returnedMessage.includes(">")) {
break
}
if (input.runningTime() - startTime > command_timeout) {
return false
}
}
serial.writeBuffer(send_buffer)
return true
}
/**
* Return the Send Command State
*/
//% block="send command state"
export function sendCommandState(): boolean {
return send_command_state
}
/**
* Return the number of retries
*/
//% block="number of retries"
//% advanced=true
export function numberOfRetries(): number {
return number_of_retries
}
/**
* Set the number of retries
*/
//% block="set number of retries|= %number"
//% %number.defl=1
//% advanced=true
export function setNumberOfRetries(number: number) {
number_of_retries = number
}
/**
* Return the command timeout
*/
//% block="command timeout (ms)"
//% advanced=true
export function commandTimeout(): number {
return command_timeout
}
/**
* Set the command timeout
*/
//% block="set command timeout (ms)|= %number"
//% %number.defl=10000
//% advanced=true
export function setCommandTimeout(number: number) {
command_timeout = number
}
}