-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcharlie.device.nut
1299 lines (1053 loc) · 38.2 KB
/
charlie.device.nut
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2013 electric imp, inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* ----------------[ PIN CONFIGURATION ]----------------------------------------
Pinout:
1 = Wake / SPI CLK
2 = Sampler (Audio In)
5 = DAC (Audio Out)
6 =
7 = SPI CS_L
8 = SPI MOSI
9 = SPI MISO
A = Battery Check (ADC) (Enabled on Mic Enable)
B = Speaker Enable
C = Mic Enable
D = Clapper motor
E =
*/
// =============================================================================
const RECORDING_TIME = 10;
const TIMEOUT_POLICY = 1; // SUSPEND_ON_ERROR = 0, RETURN_ON_ERROR = 1
const FIXED_FREQ_DAC = "fixedfrequencydac";
const DAC_BUFFER_SIZE = 1024;
const DAC_SAMPLE_RATE = 16000;
const SPI_CLOCK_SPEED_FLASH = 15000; // 15mbps
const LOAD_BUFFER_SIZE = 4096; // 4kb
const FAT_SIZE = 65536; // 1 block reserved for FAT
const TOTAL_MEMORY = 4194304; // 4 Megabytes
const CONNECTION_TIMEOUT = 30;
const RETRY_TIMEOUT = 600; // Ten minutes
const CHECKIN_TIMEOUT = 86395; // Daily
// =============================================================================
serializer <- {
// Serialize a variable of any type into a blob
serialize = function(obj) {
local str = _serialize(obj);
local len = str.len();
local crc = LRC8(str);
return format("%c%c%c", len >> 8 & 0xFF, len & 0xFF, crc) + str;
},
_serialize = function(obj) {
switch (typeof obj) {
case "integer":
local str = format("%d", obj);
local len = str.len();
return format("%c%c%c%s", 'i', len >> 8 & 0xFF, len & 0xFF, str);
case "float":
local str = format("%f", obj);
local len = str.len();
return format("%c%c%c%s", 'f', len >> 8 & 0xFF, len & 0xFF, str);
case "null":
return format("%c", 'n');
case "bool":
return format("%c%c", 'b', obj ? 1 : 0);
case "blob":
local len = obj.len();
return format("%c%c%c%s", 'B', len >> 8 & 0xFF, len & 0xFF, obj.tostring());
case "string":
local len = obj.len();
return format("%c%c%c%s", 's', len >> 8 & 0xFF, len & 0xFF, obj);
case "table":
case "array":
local t = (typeof obj == "table") ? 't' : 'a';
local len = obj.len();
local tbl = format("%c%c%c", t, len >> 8 & 0xFF, len & 0xFF);
foreach ( k,v in obj ) {
// server.log("Serializing " + k)
tbl += _serialize(k) + _serialize(v);
}
return tbl;
case "function":
// Silently setting this to null
return format("%c", 'n');
default:
throw ("Can't serialize " + typeof obj);
// server.log("Can't serialize " + typeof obj);
}
},
// Deserialize a string into a variable
deserialize = function(s) {
// Should not have the length at the start
return _deserialize(s).val;
},
_deserialize = function(s, p = 0) {
for (local i = p; i < s.len(); i++) {
local t = s[i];
switch (t) {
case 'n': // Null
return { val = null, len = 1 };
case 'i': // Integer
local len = s[i+1] << 8 | s[i+2];
local val = s.slice(i+3, i+3+len);
return { val = val.tointeger(), len = 3+len };
case 'f': // Float
local len = s[i+1] << 8 | s[i+2];
local val = s.slice(i+3, i+3+len);
return { val = val.tofloat(), len = 3+len };
case 'b': // Bool
local val = s[i+1];
return { val = (val == 1), len = 2 };
case 'B': // Blob
local len = s[i+1] << 8 | s[i+2];
local val = blob(len);
for (local j = 0; j < len; j++) {
val[j] = s[i+3+j];
}
return { val = val, len = 3+len };
case 's': // String
local len = s[i+1] << 8 | s[i+2];
local val = s.slice(i+3, i+3+len);
return { val = val, len = 3+len };
case 't': // Table
case 'a': // Array
local len = 0;
local nodes = s[i+1] << 8 | s[i+2];
i += 3;
local tab = null;
if (t == 'a') {
// server.log("Array with " + nodes + " nodes");
tab = [];
}
if (t == 't') {
// server.log("Table with " + nodes + " nodes");
tab = {};
}
for (; nodes > 0; nodes--) {
local k = _deserialize(s, i);
// server.log("Key = '" + k.val + "' (" + k.len + ")");
i += k.len;
len += k.len;
local v = _deserialize(s, i);
// server.log("Val = '" + v.val + "' [" + (typeof v.val) + "] (" + v.len + ")");
i += v.len;
len += v.len;
if (t == 'a') tab.push(v.val);
else tab[k.val] <- v.val;
}
return { val = tab, len = len+3 };
default:
throw format("Unknown type: 0x%02x at %d", t, i);
}
}
},
LRC8 = function(data) {
local LRC = 0x00;
for (local i = 0; i < data.len(); i++) {
LRC = (LRC + data[i]) & 0xFF;
}
return ((LRC ^ 0xFF) + 1) & 0xFF;
}
}
// =============================================================================
class audio {
pin_speaker = null;
pin_speaker_enable = null;
pin_mic = null;
pin_mic_enable = null;
flash = null;
index = 0;
endat = 0;
playing = false;
play_callback = null;
play_all_callback = null;
play_all_files = null;
recording = false;
record_callback = null;
recording_filename = null;
// -------------------------------------------------------------------------
constructor(_pin_speaker, _pin_speaker_enable, _pin_mic, _pin_mic_enable, _flash) {
pin_speaker = _pin_speaker;
pin_speaker_enable = _pin_speaker_enable;
pin_mic = _pin_mic;
pin_mic_enable = _pin_mic_enable;
flash = _flash;
pin_speaker_enable.configure(DIGITAL_OUT);
pin_speaker_enable.write(0);
pin_mic_enable.configure(DIGITAL_OUT);
pin_mic_enable.write(1);
}
// -------------------------------------------------------------------------
function play(filename, callback = null)
{
if (FIXED_FREQ_DAC in hardware && "fat" in flash && filename in flash.fat) {
stop_play();
index = flash.fat[filename].start;
endat = flash.fat[filename].finish;
local filesize = endat - index;
local dur = 1.0 * filesize / DAC_SAMPLE_RATE;
server.log(format("Playing audio file: '%s', size = %d bytes (%0.2fs), with available memory = %d", filename, filesize, dur, imp.getmemoryfree()));
local buffer1 = index;
local buffer2 = index + DAC_BUFFER_SIZE;
index += (2 * DAC_BUFFER_SIZE);
playing = true;
play_callback = callback;
hardware.fixedfrequencydac.configure(pin_speaker,
DAC_SAMPLE_RATE,
[ flash.readBlob(buffer1, DAC_BUFFER_SIZE),
flash.readBlob(buffer2, DAC_BUFFER_SIZE)],
_buffer_empty.bindenv(this),
A_LAW_DECOMPRESS);
pin_speaker_enable.write(1);
hardware.fixedfrequencydac.start();
} else if (!("fat" in flash)) {
server.log("Flash FAT not initialised yet.");
if (callback) callback();
} else if (!(filename in flash.fat)) {
server.log("File not found: " + filename);
if (callback) callback();
} else {
server.log("Hardware.fixedfrequencydac not present");
if (callback) callback();
}
}
// -------------------------------------------------------------------------
function _buffer_empty(buffer)
{
if (!playing) {
return;
}
if (!buffer) {
server.error("Audio buffer underrun");
return;
}
if (index < endat) {
// server.log("Reading more audio from flash");
local samples_remaining = endat - index;
local samples = DAC_BUFFER_SIZE > samples_remaining ? samples_remaining : DAC_BUFFER_SIZE;
buffer = null;
local old_index = index;
index += samples;
// server.log("Reading " + samples + " bytes at " + index);
hardware.fixedfrequencydac.addbuffer(flash.readBlob(index, samples));
} else {
// server.log("Finished playing audio");
stop_play();
}
}
// -------------------------------------------------------------------------
function stop_play()
{
playing = false;
pin_speaker_enable.write(0);
if (FIXED_FREQ_DAC in hardware) hardware.fixedfrequencydac.stop();
if (play_callback) {
local callback = play_callback;
play_callback = null;
callback();
}
}
// -------------------------------------------------------------------------
function play_all(_callback = null) {
if (_callback) play_all_callback = _callback;
if (play_all_files == null) {
// Make a play list
play_all_files = [];
foreach (file in flash.fat) {
if ("filename" in file && flash.file_exists(file.filename)) {
play_all_files.push(file.filename);
}
}
if (play_all_files.len() == 0) beep();
}
// Play all the audio files recursively
if (play_all_files.len() > 0) {
// Load this file
local filename = play_all_files.pop();
play(filename, function () {
// Now skip to the next one
imp.wakeup(1, play_all.bindenv(this));
}.bindenv(this))
} else {
// Finished playing all files
play_all_files = null;
if (play_all_callback) {
local callback = play_all_callback;
play_all_callback = null;
callback();
}
}
}
// -------------------------------------------------------------------------
function record(callback = null) {
// set the recording flag
recording = true;
record_callback = callback;
recording_filename = format("%d", time());
// set the record pointer to zero; this points filled buffers to the proper area in flash
flash.new_file(recording_filename);
// enable the microphone preamp
pin_mic_enable.write(0);
imp.sleep(0.05);
// configure the sampler
hardware.sampler.configure(pin_mic,
DAC_SAMPLE_RATE,
[ blob(DAC_BUFFER_SIZE),
blob(DAC_BUFFER_SIZE),
blob(DAC_BUFFER_SIZE) ],
_buffer_full.bindenv(this),
NORMALISE | A_LAW_COMPRESS);
// start the sampler
hardware.sampler.start();
server.log("Recording now ...");
}
// -------------------------------------------------------------------------
function _buffer_full(buffer, length) {
heartbeat = time();
if (length > 0) {
if (flash.append_file(recording_filename, buffer, length)) {
stop_record();
}
} else {
server.error("Sampler buffer overrun - stopping record");
stop_record();
}
}
// -------------------------------------------------------------------------
function stop_record() {
if (recording) {
// clear the recording flag
recording = false;
// stop the sampler
hardware.sampler.stop();
// disable the microphone preamp
pin_mic_enable.write(1);
server.log("... recording has stopped");
// the sampler will immediately call _buffer_full to empty its last buffer
// following _buffer_full, the imp will idle, and _finish_recording will be called
imp.onidle(_finish_recording.bindenv(this));
}
}
// -------------------------------------------------------------------------
function _finish_recording() {
// reconfigure the sampler to free the memory allocated for sampler buffers
hardware.sampler.configure(pin_mic, DAC_SAMPLE_RATE, [ blob(2) ], _buffer_full.bindenv(this));
// Write out the FAT changes
local callback = record_callback;
record_callback = null;
flash.close_file(recording_filename, callback);
}
// -------------------------------------------------------------------------
// http://www.howstuffworks.com/guitar2.htm
function beep(frequency = 297, duration = 0.1) {
pin_speaker.configure(PWM_OUT, 1.0/frequency, 0.5);
pin_speaker_enable.write(1);
imp.wakeup(duration, function() {
pin_speaker_enable.write(0);
pin_speaker.configure(DIGITAL_OUT);
}.bindenv(this));
}
}
// =============================================================================
const WREN = "\x06"; // write enable
const WRDI = "\x04"; // write disable
const RDID = "\x9F"; // read identification
const RDSR = "\x05"; // read status register
const READ = "\x03"; // read data
const FASTREAD = "\x0B"; // fast read data
const RDSFDP = "\x5A"; // read SFDP
const RES = "\xAB"; // read electronic ID
const REMS = "\x90"; // read electronic mfg & device ID
const DREAD = "\x3B"; // double output mode, which we don't use
const SE = "\x20"; // sector erase (Any 4kbyte sector set to 0xff)
const BE = "\x52"; // block erase (Any 64kbyte sector set to 0xff)
const CE = "\x60"; // chip erase (full device set to 0xff)
const PP = "\x02"; // page program
const RDSCUR = "\x2B"; // read security register
const WRSCUR = "\x2F"; // write security register
const ENSO = "\xB1"; // enter secured OTP
const EXSO = "\xC1"; // exit secured OTP
const DP = "\xB9"; // deep power down
const RDP = "\xAB"; // release from deep power down
const FILE_STATUS_INIT = 1;
const FILE_STATUS_READY = 2;
const FILE_STATUS_DELETED = 3;
const FILE_STATUS_SENDING = 4;
// MX25L3206E SPI Flash
class spiFlash {
// 64 blocks of 64k each = 4mb
static totalBlocks = 64;
// spi interface
spi = null;
cs_l = null;
// The file allocation table (FAT)
fat = null;
config = null;
load_pos = 0;
// The callbacks
init_callback = null;
load_files_files = [];
load_files_callback = null;
sync_config_callback = null;
// Status
busy = false;
// -------------------------------------------------------------------------
// constructor takes in pre-configured spi interface object and chip select GPIO
constructor(spiBus, csPin) {
spi = spiBus;
cs_l = csPin;
// Setup the event handlers
agent.on("flash.load.start", load_start.bindenv(this));
agent.on("flash.load.data", load_data.bindenv(this));
agent.on("flash.load.finish", load_finish.bindenv(this));
agent.on("flash.load.error", load_error.bindenv(this));
agent.on("flash.save.finish", save_finish.bindenv(this));
agent.on("flash.save.error", save_error.bindenv(this));
agent.on("config.sync", sync_config.bindenv(this));
// Check the flash is alive by readin the manufacturer details
cs_l.write(0);
local i = 0;
for (i = 0; i <= 100; i++) {
configure();
spi.write(RDID);
local data = spi.readblob(3);
if (data[0] != 0x0 && data[0] != 0xFF) {
// server.log(format("SPI Flash version: %d.%d", data[0], (data[1] << 8) | data[2]));
break;
} else {
imp.sleep(0.01);
}
}
cs_l.write(1);
if (i == 100) {
throw "SPI Flash didn't boot in time";
}
}
// -------------------------------------------------------------------------
function configure() {
spi.configure(CLOCK_IDLE_LOW | MSB_FIRST, SPI_CLOCK_SPEED_FLASH);
cs_l.configure(DIGITAL_OUT);
}
// -------------------------------------------------------------------------
function init(callback = null, clobber = false) {
// Prepare the initialisation callback
if (callback) init_callback = callback;
// Initialise the FAT
if (!clobber && load_fat()) {
if (init_callback) imp.wakeup(0, init_callback);
} else {
busy = true;
server.log("Start erasing the flash")
eraseChip(function () {
server.log("Formatting the FAT")
format_fat(function() {
busy = false;
if (init_callback) imp.wakeup(0, init_callback);
}.bindenv(this));
}.bindenv(this));
}
}
// -------------------------------------------------------------------------
function wrenable() {
configure();
cs_l.write(0);
spi.write(WREN);
cs_l.write(1);
}
// -------------------------------------------------------------------------
// pages should be pre-erased before writing
function write(addr, data) {
wrenable();
// check the status register's write enabled bit
if (!(getStatus() & 0x02)) {
server.log("Flash write not Enabled");
return 1;
}
cs_l.write(0);
spi.write(PP);
spi.write(format("%c%c%c", (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF));
spi.write(data);
cs_l.write(1);
// wait for the status register to show write complete
// typical 1.4 ms, max 5 ms
local timeout = 50000; // time in us
local start = hardware.micros();
while (getStatus() & 0x01) {
if ((hardware.micros() - start) > timeout) {
server.log("Timed out waiting for write to finish");
return 1;
}
}
return 0;
}
// -------------------------------------------------------------------------
// allow data chunks greater than one flash page to be written in a single op
function writeString(addr, data, callback = null) {
// separate the chunk into pages
for (local i = 0; i < data.len(); i+=256) {
local leftInBuffer = data.len() - i;
if (leftInBuffer < 256) {
write(addr+i, data.slice(i));
} else {
write(addr+i, data.slice(i, i+256));
}
}
if (callback) callback();
}
// -------------------------------------------------------------------------
// allow data chunks greater than one flash page to be written in a single op
function writeBlob(addr, data, callback = null) {
// separate the chunk into pages
local drb = data.readblob.bindenv(data);
data.seek(0,'b');
for (local i = 0; i < data.len(); i+=256) {
local leftInBuffer = data.len() - i;
if (leftInBuffer < 256) {
write((addr+i), drb(leftInBuffer));
} else {
write((addr+i), drb(256));
}
}
if (callback) callback();
}
// -------------------------------------------------------------------------
function readBlob(addr, bytes) {
// to read, send the read command and a 24-bit address
configure();
cs_l.write(0);
spi.write(READ);
spi.write(format("%c%c%c", (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF));
local readBlob = spi.readblob(bytes);
cs_l.write(1);
return readBlob;
}
// -------------------------------------------------------------------------
function getStatus() {
configure();
cs_l.write(0);
spi.write(RDSR);
local status = spi.readblob(1);
cs_l.write(1);
return status[0];
}
// -------------------------------------------------------------------------
// set any 64kbyte block of flash to all 0xff
// takes a starting address, 24-bit, MSB-first
function eraseBlock(addr, callback = null) {
wrenable();
cs_l.write(0);
spi.write(BE);
spi.write(format("%c%c%c", (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF));
cs_l.write(1);
imp.wakeup(0, checkStatus(callback).bindenv(this));
}
// -------------------------------------------------------------------------
// set the entire flash to all 0xff
function eraseChip(callback = null) {
wrenable();
cs_l.write(0);
spi.write(CE);
cs_l.write(1);
imp.wakeup(0, checkStatus(callback).bindenv(this));
}
// -------------------------------------------------------------------------
// Checks the status of the last command (well returns a function that does)
function checkStatus(callback = null, interval = 0.25, mask = 0x01, timeout = 120) {
return function() {
local status = getStatus() & mask;
if (status) {
imp.wakeup(interval, checkStatus(callback, interval, mask, timeout).bindenv(this));
} else {
callback();
}
}
}
// -------------------------------------------------------------------------
// Initialise the FAT
function format_fat(callback = null) {
fat = {};
fat.root <- {};
fat.root.start <- 0;
fat.root.finish <- FAT_SIZE-1;
fat.root.free <- FAT_SIZE;
fat.root.files <- 0;
write_fat(callback);
}
// -------------------------------------------------------------------------
// Loads the FAT if its valid
function load_fat() {
// Read the length
local hexlen = readBlob(0, 3);
local len = (hexlen[0] << 8 | (hexlen[1] & 0xFF));
local crc = hexlen[2];
server.log(format("Reading FAT Length = %d (0x%02x 0x%02x), CRC = 0x%02x", len, hexlen[0], hexlen[1], hexlen[2]));
if (len == 0x0 || len == 0xFFFF) {
server.log("Flash has no FAT data");
return false;
}
// Read and deserialize the data
local chunk = readBlob(3, len);
try {
local chunkcrc = serializer.LRC8(chunk);
if (chunkcrc == crc) {
chunk = serializer.deserialize(chunk.tostring());
} else {
server.log(format("CRC mismatch when loading FAT (0x%02x != 0x%02x)", crc, chunkcrc));
return false;
}
} catch (e) {
server.log("Exception: " + e);
return false;
}
if (!("fat" in chunk && "root" in chunk.fat)) {
server.log("No FAT or CONFIG loaded.")
fat = {};
config = { updated = 0, email = null };
return false;
}
fat = chunk.fat;
server.log("Loaded FAT. It contains " + fat.root.files + " files")
if ("config" in chunk && typeof chunk.config == "table") {
config = chunk.config;
server.log("Loaded config. It contains " + config.len() + " entries")
// server.log("CONFIG loaded from flash with: " + config.email);
} else {
config = { updated = 0, email = null };
}
return true;
}
// -------------------------------------------------------------------------
// Writes the FAT table to the first block
function write_fat(callback = null) {
local chunk = serializer.serialize({"fat": fat, "config": config});
server.log(format("Writing FAT and CONFIG, length = %d (0x%02x 0x%02x), CRC = 0x%02x", chunk.len()-3, chunk[0], chunk[1], chunk[2]));
// server.log("CONFIG written to flash with: " + config.email);
// Erase the FAT and write the new one
eraseBlock(0, function() {
writeString(0, chunk, function() {
if (callback) callback();
}.bindenv(this));
}.bindenv(this));
}
// -------------------------------------------------------------------------
// Marks the start of a new recording
function new_file(filename) {
fat[filename] <- {};
fat[filename].filename <- filename;
fat[filename].start <- fat.root.free;
fat[filename].finish <- fat.root.free;
fat[filename].status <- 0; // Init
fat.root.files++;
}
// -------------------------------------------------------------------------
// Writes a new buffer of data to the file system
function append_file(filename, buffer, length) {
local rtrn = false;
if (fat.root.free + length >= TOTAL_MEMORY) {
local oldlength = length;
length = TOTAL_MEMORY - fat.root.free;
server.log("Cropping the last sample from " + oldlength + " to " + length);
rtrn = true;
}
if (length > 0) {
// server.log("Data: " + length + " bytes for " + filename + ":" + fat[filename].finish);
writeBlob(fat[filename].finish, buffer.readblob(length));
fat[filename].finish += length;
fat[filename].status = FILE_STATUS_INIT;
fat.root.free += length;
}
return rtrn;
}
// -------------------------------------------------------------------------
// Finalises the recording of a file
function close_file(filename, callback) {
// Align the next free pointer to the next page boundary
local realignment = (fat.root.free % 256 == 0) ? 0 : (256-(fat.root.free % 256));
fat.root.free += realignment;
fat[filename].status = FILE_STATUS_READY;
write_fat(function() {
if (callback) callback(filename);
}.bindenv(this))
}
// -------------------------------------------------------------------------
// Asks the agent to load a file
function load(filename, url, callback = null) {
if (file_exists(filename)) {
// server.log("Skipping existing file '" + filename + "' from '" + url + "'");
if (callback) callback(filename);
return;
}
server.log("Loading file '" + filename + "' from '" + url + "'");
busy = true;
new_file(filename);
fat[filename].callback <- callback;
local request = {};
request.filename <- filename;
request.url <- url;
request.start <- 0;
request.finish <- LOAD_BUFFER_SIZE - 1;
agent.send("flash.load", request);
}
// -------------------------------------------------------------------------
// Initiation of a audio upload by the agent instead of the device
function load_start(request) {
if ("filename" in request && "url" in request) {
if (file_exists(request.filename)) {
// server.log("Skipping requested existing file '" + filename + "' from '" + url + "'");
return;
}
server.log("Receiving file '" + request.filename + "' from agent");
busy = true;
new_file(request.filename);
fat[request.filename].callback <- null;
local response = {};
response.filename <- request.filename;
response.url <- request.url;
response.start <- 0;
response.finish <- LOAD_BUFFER_SIZE - 1;
agent.send("flash.load", response);
}
}
// -------------------------------------------------------------------------
// Handle the loading of a new data chunk from the agent
function load_data(response) {
append_file(response.filename, response.chunk, response.chunk.len());
response.start += response.chunk.len();
response.finish += response.chunk.len();
agent.send("flash.load", response);
}
// -------------------------------------------------------------------------
// Handle the finish of an entire file from the agent
function load_finish(response) {
server.log("Finished writing " + (fat[response.filename].finish - fat[response.filename].start) + " bytes of " + response.filename + " to flash");
busy = false;
local callback = fat[response.filename].callback;
delete fat[response.filename].callback;
close_file(response.filename, callback);
}
// -------------------------------------------------------------------------
// Handle an error loading a file from the agent
function load_error(response) {
server.log("Error loading '" + response.filename + "' for flash: " + response.err);
busy = false;
if (fat[response.filename].callback) {
local callback = fat[response.filename].callback;
delete fat[response.filename].callback;
callback(response.filename);
}
delete fat[response.filename];
fat.root.files--;
write_fat();
}
// -------------------------------------------------------------------------
// Checks if a file exists
function file_exists(filename, status = FILE_STATUS_READY) {
return (filename in fat) && ("status" in fat[filename]) && (fat[filename].status == status);
}
// -------------------------------------------------------------------------
// Delete a file (mark it as deleted)
function unlink(filename, callback = null) {
if (file_exists(filename, FILE_STATUS_SENDING)) {
server.log("Deleting: " + filename);
fat[filename].status = FILE_STATUS_DELETED;
fat.root.files--;
write_fat(callback);
} else {
if (callback) callback();
}
}
// -------------------------------------------------------------------------
function load_files(_files = null, callback = null) {
if (callback) load_files_callback = callback;
if (_files) load_files_files = _files;
// Load all the audio files recursively
if (load_pos < load_files_files.len()) {
local file = load_files_files[load_pos];
load_pos++;
if (file_exists(file.filename)) {
// Skip this file
imp.wakeup(0, load_files.bindenv(this));
} else {
// Load this file
server.log("Started loading audio file: " + file.filename);
load(file.filename, file.url, function (filename) {
// Now skip to the next one
imp.wakeup(0, load_files.bindenv(this));
}.bindenv(this))
}
} else {
server.log("Finished loading " + load_pos + " files into flash.")
load_pos = 0;
load_files_files = [];
if (load_files_callback) {
local callback = load_files_callback;
load_files_callback = null;
callback();
}
}
}
// -------------------------------------------------------------------------
function save_files(callback = null) {
busy = true;
foreach (filename,file in fat) {
if (file_exists(filename)) {
fat[filename].status = FILE_STATUS_SENDING;
save_file(filename, function(success) {
if (success) {
unlink(filename, function() {
save_files(callback);
}.bindenv(this));
} else {
save_files(callback);
}
}.bindenv(this));
return;