forked from happyhappysundays/SparkBox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSparkIO.ino
1353 lines (1154 loc) · 33.5 KB
/
SparkIO.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
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
#include "SparkIO.h"
/* SparkIO
*
* SparkIO handles communication to and from the Positive Grid Spark amp over bluetooth for ESP32 boards
*
* From the programmers perspective, you create and read two formats - a Spark Message or a Spark Preset.
* The Preset has all the data for a full preset (amps, effects, values) and can be sent or received from the amp.
* The Message handles all other changes - change amp, change effect, change value of an effect parameter, change hardware preset and so on
*
* The class is initialized by creating an instance such as:
*
* SparkClass sp;
*
* Conection is handled with the two commands:
*
* sp.start_bt();
* sp.connect_to_spark();
*
* The first starts up bluetooth, the second connects to the amp
*
*
* Messages and presets to and from the amp are then queued and processed.
* The essential thing is the have the process() function somewhere in loop() - this handles all the processing of the input and output queues
*
* loop() {
* ...
* sp.process()
* ...
* do something
* ...
* }
*
* Sending functions:
* void create_preset(SparkPreset *preset);
* void get_serial();
* void turn_effect_onoff(char *pedal, bool onoff);
* void change_hardware_preset(uint8_t preset_num);
* void change_effect(char *pedal1, char *pedal2);
* void change_effect_parameter(char *pedal, int param, float val);
*
* These all create a message or preset to be sent to the amp when they reach the front of the 'send' queue
*
* Receiving functions:
* bool get_message(unsigned int *cmdsub, SparkMessage *msg, SparkPreset *preset);
*
* This receives the front of the 'received' queue - if there is nothing it returns false
*
* Based on whatever was in the queue, it will populate fields of the msg parameter or the preset parameter.
* Eveything apart from a full preset sent from the amp will be a message.
*
* You can determine which by inspecting cmdsub - this will be 0x0301 for a preset.
*
* Other values are:
*
* cmdsub str1 str2 val param1 param2 onoff
* 0323 amp serial #
* 0337 effect name effect val effect number
* 0306 old effect new effect
* 0338 0 new hw preset (0-3)
*
*
*
*/
void dump_buf(char *hdr, uint8_t *buf, int len) {
Serial.print(hdr);
Serial.print(" <");
Serial.print(buf[18], HEX);
Serial.print("> ");
Serial.print(buf[len-2], HEX);
Serial.print(" ");
Serial.print(buf[len-1], HEX);
int i;
for (i = 0; i < len ; i++) {
if (buf[i]<16) Serial.print("0");
Serial.print(buf[i], HEX);
Serial.print(" ");
if (i % 16 == 15) Serial.println();
};
Serial.println();
}
// TO FIX
// *ok_to_send shared across classes, and also checked whilst sending to the app
// app_rc_seq / sp_rc_seq - anaylse and work out how it should work!!!
// UTILITY FUNCTIONS
void uint_to_bytes(unsigned int i, uint8_t *h, uint8_t *l) {
*h = (i & 0xff00) / 256;
*l = i & 0xff;
}
void bytes_to_uint(uint8_t h, uint8_t l,unsigned int *i) {
*i = (h & 0xff) * 256 + (l & 0xff);
}
// MAIN SparkIO CLASS
uint8_t chunk_header_from_spark[16]{0x01, 0xfe, 0x00, 0x00, 0x41, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t chunk_header_to_spark[16] {0x01, 0xfe, 0x00, 0x00, 0x53, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void spark_start(bool passthru) {
sp_bin.set(passthru, &sp_in_chunk, chunk_header_from_spark);
app_bin.set(passthru, &app_in_chunk, chunk_header_to_spark);
sp_cin.set(&sp_in_chunk, &sp_in_message, &sp_ok_to_send, &sp_rec_seq);
app_cin.set(&app_in_chunk, &app_in_message, &app_ok_to_send, &app_rec_seq);
spark_msg_in.set(&sp_in_message);
app_msg_in.set(&app_in_message);
spark_msg_out.set(&sp_out_message);
app_msg_out.set(&app_out_message);
sp_cout.set(&sp_out_chunk, &sp_out_message, &sp_rec_seq);
app_cout.set(&app_out_chunk, &app_out_message, &app_rec_seq);
sp_bout.set(&sp_out_chunk, chunk_header_to_spark, &sp_ok_to_send);
app_bout.set(&app_out_chunk, chunk_header_from_spark, &app_ok_to_send);
}
//
// Main processing routine
//
void spark_process()
{
// process inputs
sp_bin.process();
sp_cin.process();
if (!sp_ok_to_send && (millis() - sp_bout.last_sent_time > 500)) {
DEBUG("Timeout on send");
sp_ok_to_send = true;
}
// process outputs
sp_cout.process();
sp_bout.process();
}
void app_process()
{
// process inputs
app_bin.process();
app_cin.process();
// process outputs
app_cout.process();
app_bout.process();
}
// BLOCK INPUT ROUTINES
// Routine to read the block and put into the in_chunk ring buffer
//////////////////// TEMPORARY TO DEBUG BAD BLOCK PROCESSING
bool in_bad_block = false;
void BlockIn::process() {
uint8_t b;
bool boo;
while (data_available()) {
b = data_read();
// **** PASSTHROUGH APP AND AMP ****
if (pass_through) {
if (io_state == 0) {
if (b == 0x01) { // block passthough
io_state = 1;
}
else if (b == 0xf0) { // chunk passthrough (for Spark Mini)
io_state = 101;
io_buf[0] = 0xf0;
io_pos = 1;
io_len = MAX_IO_BUFFER;
}
}
else if (io_state == 1) {
if (b == 0xfe) {
io_state = 2;
io_buf[0] = 0x01;
io_buf[1] = 0xfe;
io_pos = 2;
io_len = MAX_IO_BUFFER;
}
else if (b == 0x01) {
io_state = 1;
}
else
io_state = 0;
}
else if (io_state == 2) {
if (io_pos == 6) {
io_len = b;
}
io_buf[io_pos++] = b;
if (io_pos >= io_len) { // could only be > if the earlier matching was fooled and len is <= 6
data_write(io_buf, io_pos);
io_pos = 0;
io_len = MAX_IO_BUFFER;
io_state = 0;
}
}
else if (io_state == 101) { // chunk passthrough only
io_buf[io_pos++] = b;
if (b == 0xf7) {
data_write(io_buf, io_pos);
io_pos = 0;
io_len = MAX_IO_BUFFER;
io_state = 0;
}
}
if (io_pos > MAX_IO_BUFFER) {
DEBUG("SPARKIO IO_PROCESS_IN_BLOCKS OVERRUN");
while (true);
}
}
// **** END PASSTHROUGH ****
// check the 7th byte which holds the block length
if (rb_state == 0 && b == 0xf0) { // chunk header not block header
rb_state = 101;
}
else if (rb_state == 6) {
rb_len = b - 16;
rb_state++;
} // check every other byte in the block header for a match to the header standard
else if (rb_state >= 0 && rb_state < 16) {
if (b == blk_hdr[rb_state]) {
rb_state++;
}
else {
DEB("Bad block header at position: ");
DEB(rb_state);
DEB(" data: ");
DEB(b, HEX);
DEB(" expected: ");
DEB(blk_hdr[rb_state], HEX);
DEBUG();
rb_state = 0;
if (b == blk_hdr[rb_state]) // in case we found 0x01, the start of the header
rb_state++;
}
}
// and once past the header just read the next bytes as defined by rb_len
// store these to the chunk buffer
else if (rb_state == 16) {
rb->add(b);
/*
if (b < 16) Serial.print("0");
Serial.print(b, HEX);
Serial.print("+");
*/
rb_len--;
if (rb_len == 0) {
rb_state = 0;
rb->commit();
// Serial.println();
}
}
if (rb_state == 101) {
rb->add(b);
/*
if (b < 16) Serial.print("0");
Serial.print(b, HEX);
Serial.print("-");
*/
if (b == 0xf7) {
rb_state == 0;
rb->commit();
// Serial.println();
}
}
}
}
void SparkBlockIn::set(bool pass, RingBuffer *ring_buffer, uint8_t *hdr) {
rb = ring_buffer;
blk_hdr = hdr;
pass_through = pass;
}
bool SparkBlockIn::data_available() {
return sp_available();
}
uint8_t SparkBlockIn::data_read(){
return sp_read();
}
void SparkBlockIn::data_write(uint8_t *buf, int len){
app_write(buf, len);
}
void AppBlockIn::set(bool pass, RingBuffer *ring_buffer, uint8_t *hdr) {
rb = ring_buffer;
blk_hdr = hdr;
pass_through = pass;
}
bool AppBlockIn::data_available() {
return app_available();
}
uint8_t AppBlockIn::data_read(){
return app_read();
}
void AppBlockIn::data_write(uint8_t *buf, int len){
sp_write(buf, len);
}
// CHUNK INPUT ROUTINES
// Routine to read chunks from the in_chunk ring buffer and copy to a in_message msgpack buffer
void SparkChunkIn::set(RingBuffer *chunks, RingBuffer *messages, bool *ok, uint8_t *seq) {
in_chunk = chunks;
in_message = messages;
ok_to_send = ok;
*ok_to_send = true;
rec_seq = seq;
}
void AppChunkIn::set(RingBuffer *chunks, RingBuffer *messages, bool *ok, uint8_t *seq) {
in_chunk = chunks;
in_message = messages;
ok_to_send = ok;
*ok_to_send = true;
rec_seq = seq;
}
void ChunkIn::process() {
uint8_t b;
bool boo;
unsigned int len;
uint8_t len_h, len_l;
while (!in_chunk->is_empty()) {
boo = in_chunk->get(&b);
if (!boo) DEBUG("Chunk is_empty was false but the buffer was empty!");
switch (rc_state) {
case 1:
if (b == 0x01)
rc_state++;
else
rc_state = 0;
break;
case 2:
rc_seq = b;
*rec_seq = b;
rc_state++;
break;
case 3:
rc_checksum = b;
rc_state++;
break;
case 4:
rc_cmd = b;
rc_state++;
break;
case 5:
rc_sub = b;
rc_state = 10;
// flow control for blocking sends - put here in case we want to check rc_sub too
// in here for amp responses - only triggered by the amp
if ((rc_cmd == 0x04 || rc_cmd == 0x05) && rc_sub == 0x01) {
////////////// THIS FEELS CLUNKY - HOW CAN THIS BE DONE BETTER? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if (*ok_to_send == false) {
*ok_to_send = true;
DEBUG("Unblocked");
}
}
// set up for the main data loop - rc_state 10
rc_bitmask = 0x80;
rc_calc_checksum = 0;
rc_data_pos = 0;
// check for multi-chunk
if (rc_sub == 1 && (rc_cmd == 3 || rc_cmd == 1))
rc_multi_chunk = true;
else {
rc_multi_chunk = false;
in_message_bad = false;
in_message->add(rc_cmd);
in_message->add(rc_sub);
in_message->add(0);
in_message->add(0);
}
break;
case 10: // the main loop which ends on an 0xf7
if (b == 0xf7) {
if (rc_calc_checksum != rc_checksum)
in_message_bad = true;
rc_state = 0;
if (!rc_multi_chunk || (rc_this_chunk == rc_total_chunks-1)) { //last chunk in message
if (in_message_bad) {
DEBUG("Bad message, dropped");
in_message->drop();
}
else {
len = in_message->get_len();
uint_to_bytes(len, &len_h, &len_l);
in_message->set_at_index(2, len_h);
in_message->set_at_index(3, len_l);
in_message->commit();
}
}
}
else if (rc_bitmask == 0x80) { // if the bitmask got to this value it is now a new bits
rc_calc_checksum ^= b;
rc_bits = b;
rc_bitmask = 1;
}
else {
rc_data_pos++;
rc_calc_checksum ^= b;
if (rc_bits & rc_bitmask)
b |= 0x80;
rc_bitmask *= 2;
if (rc_multi_chunk && rc_data_pos == 1)
rc_total_chunks = b;
else if (rc_multi_chunk && rc_data_pos == 2) {
rc_last_chunk = rc_this_chunk;
rc_this_chunk = b;
if (rc_this_chunk == 0) {
in_message_bad = false;
in_message->add(rc_cmd);
in_message->add(rc_sub);
in_message->add(0);
in_message->add(0);
}
else if (rc_this_chunk != rc_last_chunk+1)
in_message_bad = true;
}
else if (rc_multi_chunk && rc_data_pos == 3)
rc_chunk_len = b;
else {
in_message->add(b);
}
};
break;
}
// checking for rc_state 0 is done separately so that if a prior step finds a mismatch
// and resets the state to 0 it can be processed here for that byte - saves missing the
// first byte of the header if that was misplaced
if (rc_state == 0) {
if (b == 0xf0)
rc_state++;
}
}
}
// MESSAGE INPUT ROUTINES
// Routine to read messages from the in_message ring buffer and copy to a message C structurer
void SparkMessageIn::set(RingBuffer *messages) {
in_message = messages;
}
void AppMessageIn::set(RingBuffer *messages) {
in_message = messages;
}
void MessageIn::read_byte(uint8_t *b)
{
uint8_t a;
in_message->get(&a);
*b = a;
}
void MessageIn::read_uint(uint8_t *b)
{
uint8_t a;
in_message->get(&a);
if (a == 0xCC)
in_message->get(&a);
*b = a;
}
void MessageIn::read_string(char *str)
{
uint8_t a, len;
int i;
read_byte(&a);
if (a == 0xd9) {
read_byte(&len);
}
else if (a >= 0xa0) {
len = a - 0xa0;
}
else {
read_byte(&a);
if (a < 0xa0 || a >= 0xc0) DEBUG("Bad string");
len = a - 0xa0;
}
if (len > 0) {
// process whole string but cap it at STR_LEN-1
for (i = 0; i < len; i++) {
read_byte(&a);
if (a<0x20 || a>0x7e) a=0x20; // make sure it is in ASCII range - to cope with get_serial
if (i < STR_LEN -1) str[i]=a;
}
str[i > STR_LEN-1 ? STR_LEN-1 : i]='\0';
}
else {
str[0]='\0';
}
}
void MessageIn::read_prefixed_string(char *str)
{
uint8_t a, len;
int i;
read_byte(&a);
read_byte(&a);
if (a < 0xa0 || a >= 0xc0) DEBUG("Bad string");
len = a-0xa0;
if (len > 0) {
for (i = 0; i < len; i++) {
read_byte(&a);
if (a<0x20 || a>0x7e) a=0x20; // make sure it is in ASCII range - to cope with get_serial
if (i < STR_LEN -1) str[i]=a;
}
str[i > STR_LEN-1 ? STR_LEN-1 : i]='\0';
}
else {
str[0]='\0';
}
}
void MessageIn::read_float(float *f)
{
union {
float val;
byte b[4];
} conv;
uint8_t a;
int i;
read_byte(&a); // should be 0xca
if (a != 0xca) return;
// Seems this creates the most significant byte in the last position, so for example
// 120.0 = 0x42F00000 is stored as 0000F042
for (i=3; i>=0; i--) {
read_byte(&a);
conv.b[i] = a;
}
*f = conv.val;
}
void MessageIn::read_onoff(bool *b)
{
uint8_t a;
read_byte(&a);
if (a == 0xc3)
*b = true;
else // 0xc2
*b = false;
}
// The functions to get the message
bool MessageIn::get_message(unsigned int *cmdsub, SparkMessage *msg, SparkPreset *preset)
{
uint8_t cmd, sub, len_h, len_l;
unsigned int len;
unsigned int cs;
uint8_t junk;
int i, j;
uint8_t num;
if (in_message->is_empty()) return false;
read_byte(&cmd);
read_byte(&sub);
read_byte(&len_h);
read_byte(&len_l);
bytes_to_uint(len_h, len_l, &len);
bytes_to_uint(cmd, sub, &cs);
*cmdsub = cs;
switch (cs) {
// 0x02 series - requests
// get preset information
case 0x0201:
read_byte(&msg->param1);
read_byte(&msg->param2);
for (i=0; i < 30; i++) read_byte(&junk); // 30 bytes of 0x00
break;
// get current hardware preset number - this is a request with no payload
case 0x0210:
break;
// get amp name - no payload
case 0x0211:
break;
// get name - this is a request with no payload
case 0x0221:
break;
// get serial number - this is a request with no payload
case 0x0223:
break;
// the UNKNOWN command - 0x0224 00 01 02 03
case 0x0224:
case 0x022a:
case 0x032a:
// the data is a fixed array of four bytes (0x94 00 01 02 03)
read_byte(&junk);
read_uint(&msg->param1);
read_uint(&msg->param2);
read_uint(&msg->param3);
read_uint(&msg->param4);
// get firmware version - this is a request with no payload
case 0x022f:
break;
// change effect parameter
case 0x0104:
read_string(msg->str1);
read_byte(&msg->param1);
read_float(&msg->val);
break;
// change of effect model
case 0x0306:
case 0x0106:
read_string(msg->str1);
read_string(msg->str2);
break;
// get current hardware preset number
case 0x0310:
read_byte(&msg->param1);
read_byte(&msg->param2);
break;
// get name
case 0x0311:
read_string(msg->str1);
break;
// enable / disable an effect
// and 0x0128 amp info command
case 0x0315:
case 0x0115:
case 0x0128:
read_string(msg->str1);
read_onoff(&msg->onoff);
break;
// get serial number
case 0x0323:
read_string(msg->str1);
break;
// store into hardware preset
case 0x0327:
read_byte(&msg->param1);
read_byte(&msg->param2);
break;
// amp info
case 0x0328:
read_float(&msg->val);
break;
// firmware version number
case 0x032f:
// really this is a uint32 but it is just as easy to read into 4 uint8 - a bit of a cheat
read_byte(&junk); // this will be 0xce for a uint32
read_byte(&msg->param1);
read_byte(&msg->param2);
read_byte(&msg->param3);
read_byte(&msg->param4);
break;
// change of effect parameter
case 0x0337:
read_string(msg->str1);
read_byte(&msg->param1);
read_float(&msg->val);
break;
// tuner
case 0x0364:
read_byte(&msg->param1);
read_float(&msg->val);
break;
case 0x0365:
read_onoff(&msg->onoff);
break;
// change of preset number selected on the amp via the buttons
case 0x0338:
case 0x0138:
read_byte(&msg->param1);
read_byte(&msg->param2);
break;
// license key
case 0x0170:
for (i = 0; i < 64; i++)
read_uint(&license_key[i]);
break;
// response to a request for a full preset
case 0x0301:
case 0x0101:
read_byte(&preset->curr_preset);
read_byte(&preset->preset_num);
read_string(preset->UUID);
read_string(preset->Name);
read_string(preset->Version);
read_string(preset->Description);
read_string(preset->Icon);
read_float(&preset->BPM);
for (j=0; j<7; j++) {
read_string(preset->effects[j].EffectName);
read_onoff(&preset->effects[j].OnOff);
read_byte(&num);
preset->effects[j].NumParameters = num - 0x90;
for (i = 0; i < preset->effects[j].NumParameters; i++) {
read_byte(&junk);
read_byte(&junk);
read_float(&preset->effects[j].Parameters[i]);
}
}
read_byte(&preset->chksum);
break;
// tap tempo!
case 0x0363:
read_float(&msg->val);
break;
case 0x0470:
case 0x0428:
read_byte(&junk);
break;
// acks - no payload to read - no ack sent for an 0x0104
case 0x0401:
case 0x0501:
case 0x0406:
case 0x0415:
case 0x0438:
case 0x0465:
// Serial.print("Got an ack ");
// Serial.println(cs, HEX);
break;
default:
Serial.print("Unprocessed message SparkIO ");
Serial.print (cs, HEX);
Serial.print(":");
for (i = 0; i < len - 4; i++) {
read_byte(&junk);
Serial.print(junk, HEX);
Serial.print(" ");
}
Serial.println();
}
return true;
}
//
// Output routines
//
void MessageOut::start_message(int cmdsub)
{
int om_cmd, om_sub;
om_cmd = (cmdsub & 0xff00) >> 8;
om_sub = cmdsub & 0xff;
out_message->add(om_cmd);
out_message->add(om_sub);
out_message->add(0); // placeholder for length
out_message->add(0); // placeholder for length
out_msg_chksum = 0;
}
void MessageOut::end_message()
{
unsigned int len;
uint8_t len_h, len_l;
len = out_message->get_len();
uint_to_bytes(len, &len_h, &len_l);
out_message->set_at_index(2, len_h);
out_message->set_at_index(3, len_l);
out_message->commit();
}
void MessageOut::write_byte_no_chksum(byte b)
{
out_message->add(b);
}
void MessageOut::write_byte(byte b)
{
out_message->add(b);
out_msg_chksum += int(b);
}
void MessageOut::write_uint(byte b)
{
if (b > 127) {
out_message->add(0xCC);
out_msg_chksum += int(0xCC);
}
out_message->add(b);
out_msg_chksum += int(b);
}
void MessageOut::write_uint32(uint32_t w)
{
int i;
uint8_t b;
uint32_t mask;
mask = 0xFF000000;
out_message->add(0xCE);
out_msg_chksum += int(0xCE);
for (i = 3; i >= 0; i--) {
b = (w & mask) >> (8*i);
mask >>= 8;
write_uint(b);
// out_message->add(b);
// out_msg_chksum += int(b);
}
}
void MessageOut::write_prefixed_string(const char *str)
{
int len, i;
len = strnlen(str, STR_LEN);
write_byte(byte(len));
write_byte(byte(len + 0xa0));
for (i=0; i<len; i++)
write_byte(byte(str[i]));
}
void MessageOut::write_string(const char *str)
{
int len, i;
len = strnlen(str, STR_LEN);
write_byte(byte(len + 0xa0));
for (i=0; i<len; i++)
write_byte(byte(str[i]));
}
void MessageOut::write_long_string(const char *str)
{
int len, i;
len = strnlen(str, STR_LEN);
write_byte(byte(0xd9));
write_byte(byte(len));
for (i=0; i<len; i++)
write_byte(byte(str[i]));
}
void MessageOut::write_float (float flt)
{
union {
float val;
byte b[4];
} conv;
int i;
conv.val = flt;
// Seems this creates the most significant byte in the last position, so for example
// 120.0 = 0x42F00000 is stored as 0000F042
write_byte(0xca);
for (i=3; i>=0; i--) {
write_byte(byte(conv.b[i]));
}
}
void MessageOut::write_onoff (bool onoff)
{
byte b;
if (onoff)
// true is 'on'
b = 0xc3;
else
b = 0xc2;
write_byte(b);
}
void MessageOut::change_effect_parameter (char *pedal, int param, float val)
{
if (cmd_base == 0x0100)
start_message (cmd_base + 0x04);
else
start_message (cmd_base + 0x37);
write_prefixed_string (pedal);
write_byte (byte(param));
write_float(val);
end_message();
}
void MessageOut::change_effect (char *pedal1, char *pedal2)
{
start_message (cmd_base + 0x06);
write_prefixed_string (pedal1);
write_prefixed_string (pedal2);
end_message();
}
void MessageOut::change_hardware_preset (uint8_t curr_preset, uint8_t preset_num)
{
// preset_num is 0 to 3
start_message (cmd_base + 0x38);
write_byte (curr_preset);
write_byte (preset_num) ;
end_message();
}
void MessageOut::turn_effect_onoff (char *pedal, bool onoff)
{
start_message (cmd_base + 0x15);
write_prefixed_string (pedal);
write_onoff (onoff);
end_message();
}
void MessageOut::get_serial()
{
start_message (0x0223);
end_message();
}
void MessageOut::get_name()
{
start_message (0x0211);
end_message();
}
void MessageOut::get_hardware_preset_number()
{
start_message (0x0210);
end_message();
}
void MessageOut::get_checksum_info() {
start_message (0x022a);
write_byte(0x94);
write_uint(0);
write_uint(1);
write_uint(2);
write_uint(3);
end_message();
}
void MessageOut::get_firmware() {
start_message (0x022f);
end_message();
}
void MessageOut::save_hardware_preset(uint8_t curr_preset, uint8_t preset_num)