-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfirmata.c
1872 lines (1797 loc) · 69.7 KB
/
pdfirmata.c
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
/*
PDFirmata: Firmata client for Pute Data
Copyright (C) 2017-2020 Malik Enes Safak
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "m_pd.h"
static t_class *pdfirmata_class;
typedef struct _pdfirmata{
t_object x_obj;
uint8_t * buffer;
uint32_t rawCounter;
int32_t rawType; //rawType 0 nothing, 1 sysex
t_inlet * raw;
t_outlet * rawOut, * decOut;
}t_pdfirmata;
// function protoypes
void writeBuffer(t_pdfirmata * x, uint8_t * buffer, uint16_t bytec);
uint8_t serialPort(const char * portName);
void pdfirmata_onRawData(t_pdfirmata * x, t_floatarg _f);
void pdfirmata_version(t_pdfirmata * x);
void pdfirmata_firmware(t_pdfirmata * x);
void pdfirmata_capability(t_pdfirmata * x);
void pdfirmata_sampling(t_pdfirmata * x, t_floatarg time);
void pdfirmata_pinMode(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void pdfirmata_digitalWrite(t_pdfirmata * x, t_floatarg pin, t_floatarg state);
void pdfirmata_digitalIn(t_pdfirmata * x, t_floatarg port, t_floatarg state);
void pdfirmata_analogMap(t_pdfirmata * x);
void pdfirmata_analogWrite(t_pdfirmata * x, t_floatarg pin, t_floatarg value);
void pdfirmata_analogIn(t_pdfirmata * x, t_floatarg pin, t_floatarg state);
void pdfirmata_pinState(t_pdfirmata * x, t_floatarg pin);
void pdfirmata_serial(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void pdfirmata_I2C(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void pdfirmata_servo(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void pdfirmata_encoder(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void pdfirmata_stepper(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void pdfirmata_multistepper(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void pdfirmata_onewire(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void pdfirmata_scheduler(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv);
void decSysex(t_pdfirmata *x);
// !function prototypes
/* Custom float and 7bit functions. These functions adapted from firmata.js implementation
firmata.js licensed under MIT https://github.com/firmata/firmata.js
Copyright (c) 2011-2015 Julian Gautier [email protected]
Copyright (c) 2015-2019 The Firmata.js Authors (see AUTHORS.md in above link) */
#define MAX_SIGNIFICAND pow(2, 23)
int encodeCustomFloat(float input){
int sign = input < 0 ? 1 : 0;
input *= input < 0 ? -1 : 1;
int base10 = floor(log10(input));
// Shift decimal to start of significand
int exponent = 0 + base10;
input /= pow(10, base10);
// Shift decimal to the right as far as we can
while ((input != (int)input) && (input < MAX_SIGNIFICAND)) {
exponent -= 1;
input *= 10;
}
// Reduce precision if necessary
while (input > MAX_SIGNIFICAND) {
exponent += 1;
input /= 10;
}
int result = (int)input;
exponent += 11;
result &= 0x7FFFFF;
result |= (exponent & 0x0F) << 23;
result |= (sign & 0x01) << 27;
return result;
}
float decodeCustomFloat(unsigned int input) {
int exponent = ((input >> 23) & 0x0F) - 11;
int sign = (input >> 27) & 0x01;
int result = input & 0x7FFFFF;
result *= sign == 1 ? -1 : 1;
float resultf = (float)result * pow(10, exponent);
return resultf;
}
uint8_t * to7bit(uint8_t * src, uint16_t length){
// we need 8 x 7-bit for every 7 x 8-bit words
uint16_t i_s = 0, i_d = 0;
uint16_t destLength = length + (length / 7);
uint8_t * buffer = (uint8_t *)malloc((destLength + 2) * sizeof(uint8_t));
buffer[i_d++] = destLength & 0xFF;
buffer[i_d++] = (destLength >> 8) & 0xFF;
uint8_t shift = 0;
uint8_t previous = 0;
while(i_s < length){
if(shift == 0){
buffer[i_d++] = src[i_s] & 0x7F;
shift++;
previous = src[i_s] >> 7;
}
else{
buffer[i_d++] = ((src[i_s] << shift) & 0x7f) | previous;
if(shift == 6){
buffer[i_d++] = src[i_s] >> 1;
shift = 0;
}
else{
shift++;
previous = src[i_s] >> (8 - shift);
}
}
i_s++;
}
if(shift > 0){
buffer[i_d++] = previous;
}
return buffer;
}
uint8_t * from7bit(uint8_t * src, uint16_t length){
uint16_t destLength = length - (length >> 3);
uint8_t * buffer = (uint8_t *)malloc((destLength + 2) * sizeof(uint8_t));
buffer[0] = destLength & 0xFF;
buffer[1] = (destLength >> 8) & 0xFF;
uint16_t i = 0;
for(i = 0; i < destLength; i++){
uint16_t j = i << 3;
uint16_t pos = (j / 7);
uint16_t shift = j % 7;
buffer[i + 2] = (src[pos] >> shift) | ((src[pos + 1] << (7 - shift)) & 0xFF);
}
return buffer;
}
// !Custom float and 7bit functions
// Constants
const char * pinModes[] = {
"INPUT",
"OUTPUT",
"ANALOG",
"PWM",
"SERVO",
"SHIFT",
"I2C",
"ONEWIRE",
"STEPPER",
"ENCODER",
"SERIAL",
"PULLUP"
};
const uint8_t pinModesLength = sizeof(pinModes) / sizeof(pinModes[0]);
const char * serialPorts[] = {
"HW0",
"HW1",
"HW2",
"HW3",
"HW4",
"HW5",
"HW6",
"HW7",
"SW0",
"SW1",
"SW2",
"SW3",
"SW4",
"SW5",
"SW6",
"SW7"
};
const uint8_t serialPortsLength = sizeof(serialPorts) / sizeof(serialPorts[0]);
// !Constants
void * pdfirmata_new(t_floatarg bufferSize){
t_pdfirmata * x = (t_pdfirmata *)pd_new(pdfirmata_class);
if(bufferSize < 1) bufferSize = 1024;
x->buffer = (uint8_t *)malloc(bufferSize * sizeof(uint8_t));
x->raw = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("rawdata"));
x->decOut = outlet_new(&x->x_obj, &s_list);
x->rawOut = outlet_new(&x->x_obj, &s_float);
post("pdfirmata: Firmata client for Pute Data");
post("Copyright (C) 2017-2020 Malik Enes Safak");
post("https://github.com/NullMember/PDFirmata");
return (void *)x;
}
void pdfirmata_free(t_pdfirmata *x){
free(x->buffer);
inlet_free(x->raw);
outlet_free(x->rawOut);
outlet_free(x->decOut);
}
void pdfirmata_setup(void){
pdfirmata_class = class_new(gensym("pdfirmata"),
(t_newmethod)pdfirmata_new,
(t_method)pdfirmata_free,
sizeof(t_pdfirmata),
CLASS_DEFAULT,
A_DEFFLOAT,
0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_onRawData, gensym("rawdata"), A_DEFFLOAT, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_version, gensym("version"), 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_firmware, gensym("firmware"), 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_capability, gensym("capability"), 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_sampling, gensym("sampling"), A_FLOAT, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_pinMode, gensym("pinMode"), A_GIMME, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_digitalWrite, gensym("digitalWrite"), A_FLOAT, A_FLOAT, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_digitalIn, gensym("digitalIn"), A_FLOAT, A_FLOAT, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_analogMap, gensym("analogMap"), 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_analogWrite, gensym("analogWrite"), A_FLOAT, A_FLOAT, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_analogIn, gensym("analogIn"), A_FLOAT, A_FLOAT, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_pinState, gensym("pinState"), A_FLOAT, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_serial, gensym("serial"), A_GIMME, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_I2C, gensym("I2C"), A_GIMME, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_servo, gensym("servo"), A_GIMME, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_encoder, gensym("encoder"), A_GIMME, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_stepper, gensym("stepper"), A_GIMME, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_multistepper, gensym("multistepper"), A_GIMME, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_onewire, gensym("onewire"), A_GIMME, 0);
class_addmethod(pdfirmata_class, (t_method)pdfirmata_scheduler, gensym("scheduler"), A_GIMME, 0);
}
void writeBuffer(t_pdfirmata * x, uint8_t * buffer, uint16_t bytec){
uint16_t i = 0;
while(i < bytec){
outlet_float(x->rawOut, buffer[i]);
i++;
}
}
uint8_t serialPort(const char * portName){
uint8_t i = 0;
while(i < serialPortsLength){
if(strcmp(portName, serialPorts[i]) == 0){
return i;
}
}
return 255;
}
void pdfirmata_onRawData(t_pdfirmata *x, t_floatarg f){
/* SysEx End */
if(f == 0xF7){
x->rawType = 0;
decSysex(x);
}
/* All bytes received */
if(x->rawType == 0){
x->rawCounter = 0;
}
/* Read incoming SysEx bytes */
else if(x->rawType == 1){
x->buffer[x->rawCounter] = f;
x->rawCounter++;
}
/* Read and output analog read command */
else if(x->rawType == 2){
x->buffer[x->rawCounter] = f;
x->rawCounter++;
if(x->rawCounter == 3){
t_atom buffer[3];
uint16_t value = x->buffer[1] & 0x7F;
value |= (x->buffer[2] & 0x7F) << 7;
SETSYMBOL(buffer, gensym("analog"));
SETFLOAT(buffer + 1, x->buffer[0]);
SETFLOAT(buffer + 2, value);
outlet_list(x->decOut, &s_list, 3, buffer);
x->rawType = 0;
}
}
/* Read and output digital read command */
else if(x->rawType == 3){
x->buffer[x->rawCounter] = f;
x->rawCounter++;
if(x->rawCounter == 3){
t_atom buffer[10];
uint16_t value = x->buffer[1] & 0x7F;
value |= (x->buffer[2] & 0x1) << 7;
SETSYMBOL(buffer, gensym("digital"));
SETFLOAT(buffer + 1, x->buffer[0]);
SETFLOAT(buffer + 2, value & 0x01);
SETFLOAT(buffer + 3, (value >> 1) & 0x01);
SETFLOAT(buffer + 4, (value >> 2) & 0x01);
SETFLOAT(buffer + 5, (value >> 3) & 0x01);
SETFLOAT(buffer + 6, (value >> 4) & 0x01);
SETFLOAT(buffer + 7, (value >> 5) & 0x01);
SETFLOAT(buffer + 8, (value >> 6) & 0x01);
SETFLOAT(buffer + 9, (value >> 7) & 0x01);
outlet_list(x->decOut, &s_list, 3, buffer);
x->rawType = 0;
}
}
/* Major and minor version numbers */
else if(x->rawType == 4){
x->buffer[x->rawCounter] = f;
x->rawCounter++;
if(x->rawCounter == 3){
t_atom buffer[3];
uint8_t major = x->buffer[1] & 0x7F;
uint8_t minor = x->buffer[2] & 0x7F;
SETSYMBOL(buffer, gensym("version"));
SETFLOAT(buffer + 1, major);
SETFLOAT(buffer + 2, minor);
outlet_list(x->decOut, &s_list, 3, buffer);
x->rawType = 0;
}
}
/* Beginning of SysEx command */
/* Type 1 */
if(f == 0xF0){
x->rawType = 1;
}
/* Beginning of analog command */
/* Type 2 */
else if(((int)f & 0xF0) == 0xE0){
x->buffer[x->rawCounter] = (int)f & 0x0F;
x->rawCounter++;
x->rawType = 2;
}
/* Beginning of digital command */
/* Type 3 */
else if(((int)f & 0xF0) == 0x90){
x->buffer[x->rawCounter] = (int)f & 0x0F;
x->rawCounter++;
x->rawType = 3;
}
/* Beginning of version number */
/* Type 4 */
else if((int)f == 0xF9){
x->buffer[x->rawCounter] = f;
x->rawCounter++;
x->rawType = 4;
}
}
void pdfirmata_version(t_pdfirmata * x){
uint8_t buffer = 0xF9;
writeBuffer(x, &buffer, 1);
}
void pdfirmata_firmware(t_pdfirmata * x){
uint8_t buffer[3];
buffer[0] = 0xF0;
buffer[1] = 0x79;
buffer[2] = 0xF7;
writeBuffer(x, buffer, 3);
}
void pdfirmata_capability(t_pdfirmata * x){
uint8_t buffer[3];
buffer[0] = 0xF0;
buffer[1] = 0x6B;
buffer[2] = 0xF7;
writeBuffer(x, buffer, 3);
}
void pdfirmata_sampling(t_pdfirmata * x, t_floatarg time){
int _time = time;
uint8_t buffer[5];
buffer[0] = 0xF0;
buffer[1] = 0x7A;
buffer[2] = _time & 0x7F;
buffer[3] = (_time >> 7) & 0x7F;
buffer[4] = 0xF7;
writeBuffer(x, buffer, 5);
}
void pdfirmata_pinMode(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv){
if(argc > 1){
int _pin = atom_getfloatarg(0, argc, argv);
if(_pin < 0 || _pin > 127){
error("Pin number must between 0-127");
return;
}
uint8_t buffer[3];
buffer[0] = 0xF4;
buffer[1] = _pin;
buffer[2] = 0xFF;
uint8_t i;
const char * modeName = atom_getsymbolarg(1, argc, argv)->s_name;
for(i = 0; i < pinModesLength; i++){
if(strcmp(modeName, pinModes[i]) == 0){
buffer[2] = i;
}
}
if(buffer[2] == 0xFF){
error("Unknown mode");
}
else{
writeBuffer(x, buffer, 3);
}
}
}
void pdfirmata_digitalWrite(t_pdfirmata * x, t_floatarg pin, t_floatarg state){
int _pin = pin;
int _state = state;
if(_pin < 0 || _pin > 127){
error("Pin number must between 0-127");
return;
}
if(_state < 0 || _state > 1){
error("State must 0 (LOW) or 1 (HIGH)");
return;
}
uint8_t buffer[3];
buffer[0] = 0xF5;
buffer[1] = _pin;
buffer[2] = _state;
writeBuffer(x, buffer, 3);
}
void pdfirmata_digitalIn(t_pdfirmata * x, t_floatarg port, t_floatarg state){
int _port = port;
int _state = state;
if(_port < 0 || _port > 15){
error("Port number must between 0-15");
return;
}
uint8_t buffer[2];
buffer[0] = 0xD0 + _port;
buffer[1] = _state > 0 ? 1 : 0;
writeBuffer(x, buffer, 2);
}
void pdfirmata_analogMap(t_pdfirmata * x){
uint8_t buffer[3];
buffer[0] = 0xF0;
buffer[1] = 0x69;
buffer[2] = 0xF7;
writeBuffer(x, buffer, 3);
}
void pdfirmata_analogWrite(t_pdfirmata * x, t_floatarg pin, t_floatarg value){
int _pin = pin;
int _value = value;
if(_pin < 0 || _pin > 127){
error("Pin number must between 0-127");
return;
}
if(_pin < 16 && _value < 0x4000){
uint8_t buffer[3];
buffer[0] = 0xE0 + _pin;
buffer[1] = _value & 0x7F;
buffer[2] = (_value >> 7) & 0x7F;
writeBuffer(x, buffer, 3);
}
else{
uint8_t buffer[8];
buffer[0] = 0xF0;
buffer[1] = 0x6F;
buffer[2] = _pin;
buffer[3] = _value & 127;
buffer[4] = (_value >> 7) & 127;
buffer[5] = (_value >> 14) & 127;
buffer[6] = (_value >> 21) & 127;
buffer[7] = 0xF7;
writeBuffer(x, buffer, 8);
}
}
void pdfirmata_analogIn(t_pdfirmata * x, t_floatarg pin, t_floatarg state){
int _pin = pin;
int _state = state;
if(_pin < 0 || _pin > 15){
error("Analog pin number must between 0-15");
return;
}
if(_state < 0 || _state > 1){
error("State must 0 (DISABLE) or 1 (ENABLE)");
return;
}
uint8_t buffer[2];
buffer[0] = 0xC0 + _pin;
buffer[1] = _state;
writeBuffer(x, buffer, 2);
}
void pdfirmata_pinState(t_pdfirmata * x, t_floatarg pin){
int _pin = pin;
if(_pin < 0 || _pin > 127){
error("Pin number must between 0-127");
return;
}
uint8_t buffer[4];
buffer[0] = 0xF0;
buffer[1] = 0x6D;
buffer[2] = _pin;
buffer[3] = 0xF7;
writeBuffer(x, buffer, 4);
}
void pdfirmata_serial(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv){
if(argc > 0){
const char * cmdName = atom_getsymbolarg(0, argc, argv)->s_name;
/* serial config PORT BAUD (RX) (TX) */
if(strcmp(cmdName, "config") == 0){
if(argc > 2){
uint8_t port = serialPort(atom_getsymbolarg(1, argc, argv)->s_name);
if(port == 255){
error("Unknown serial port name");
return;
}
uint32_t baud = atom_getint(argv + 2);
uint8_t rxPin;
uint8_t txPin;
if(argc > 4){
rxPin = atom_getint(argv + 3);
if(rxPin > 127){
error("Pin number must between 0-127");
return;
}
txPin = atom_getint(argv + 4);
if(txPin > 127){
error("Pin number must between 0-127");
return;
}
uint8_t buffer[9];
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x10 + port;
buffer[3] = baud & 0x7F;
buffer[4] = (baud >> 7) & 0x7F;
buffer[5] = (baud >> 14) & 0x7F;
buffer[6] = rxPin;
buffer[7] = txPin;
buffer[8] = 0xF7;
writeBuffer(x, buffer, 9);
}
else{
uint8_t buffer[7];
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x10 + port;
buffer[3] = baud & 0x7F;
buffer[4] = (baud >> 7) & 0x7F;
buffer[5] = (baud >> 14) & 0x7F;
buffer[6] = 0xF7;
writeBuffer(x, buffer, 7);
}
}
}
/* serial print PORT SYMBOL */
if(strcmp(cmdName, "print") == 0){
if(argc > 2){
uint8_t port = serialPort(atom_getsymbolarg(1, argc, argv)->s_name);
if(port == 255){
error("Unknown serial port name");
return;
}
const char ** stringNames = (const char **)malloc((argc - 2) * sizeof(const char **));
uint8_t stringLengths = 0;
uint16_t i = 2;
while(i < argc){
stringNames[i - 2] = atom_getsymbolarg(i, argc, argv)->s_name;
stringLengths += strlen(stringNames[i - 2]);
i++;
}
char * stringName = (char *)malloc(stringLengths * sizeof(char));
strcpy(stringName, stringNames[0]);
i = 1;
while(i < argc - 2){
strcat(stringName, " ");
strcat(stringName, stringNames[i]);
i++;
}
uint8_t stringLength = strlen(stringName);
uint8_t * buffer = (uint8_t *)malloc(((stringLength * 2) + 4) * sizeof(uint8_t));
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x20 | port;
for(i = 0; i < stringLength; i++){
buffer[3 + (i * 2)] = stringName[i] & 0x7F;
buffer[3 + ((i * 2) + 1)] = (stringName[i] >> 7) & 0x7F;
}
buffer[(stringLength * 2) + 3] = 0xF7;
writeBuffer(x, buffer, (stringLength * 2) + 4);
free(buffer);
free(stringNames);
free(stringName);
}
}
/* serial println PORT SYMBOL */
if(strcmp(cmdName, "println") == 0){
if(argc > 2){
uint8_t port = serialPort(atom_getsymbolarg(1, argc, argv)->s_name);
if(port == 255){
error("Unknown serial port name");
return;
}
const char ** stringNames = (const char **)malloc((argc - 2) * sizeof(const char **));
uint8_t stringLengths = 0;
uint16_t i = 2;
while(i < argc){
stringNames[i - 2] = atom_getsymbolarg(i, argc, argv)->s_name;
stringLengths += strlen(stringNames[i - 2]);
i++;
}
char * stringName = (char *)malloc(stringLengths * sizeof(char));
strcpy(stringName, stringNames[0]);
i = 1;
while(i < argc - 2){
strcat(stringName, " ");
strcat(stringName, stringNames[i]);
i++;
}
uint8_t stringLength = strlen(stringName);
uint8_t * buffer = (uint8_t *)malloc(((stringLength * 2) + 5) * sizeof(uint8_t));
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x20 | port;
for(i = 0; i < stringLength; i++){
buffer[3 + (i * 2)] = stringName[i] & 0x7F;
buffer[3 + ((i * 2) + 1)] = (stringName[i] >> 7) & 0x7F;
}
buffer[(stringLength * 2) + 3] = '\n';
buffer[(stringLength * 2) + 4] = 0xF7;
writeBuffer(x, buffer, (stringLength * 2) + 4);
free(buffer);
free(stringNames);
free(stringName);
}
}
/* serial write PORT ARG0 ARG1 ARG2 ... */
if(strcmp(cmdName, "write") == 0){
if(argc > 2){
uint8_t port = serialPort(atom_getsymbolarg(1, argc, argv)->s_name);
if(port == 255){
error("Unknown serial port name");
return;
}
uint8_t * buffer = (uint8_t *)malloc((((argc - 2) * 2) + 4) * sizeof(uint8_t));
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x20 | port;
uint16_t i = 2;
while(i < argc){
buffer[((i - 2) * 2) + 3] = atom_getint(argv + i) & 0x7F;
buffer[((i - 2) * 2) + 4] = (atom_getint(argv + i) >> 7) & 0x7F;
i++;
}
buffer[((argc - 2) * 2) + 3] = 0xF7;
writeBuffer(x, buffer, ((argc - 2) * 2) + 4);
free(buffer);
}
}
/* serial read PORT READMODE (MAXBYTES) */
if(strcmp(cmdName, "read") == 0){
if(argc > 2){
uint8_t port = serialPort(atom_getsymbolarg(1, argc, argv)->s_name);
if(port == 255){
error("Unknown serial port name");
return;
}
uint8_t readMode = atom_getint(argv + 2);
if(readMode > 1){
error("Read mode must 0 (Continuous) or 1 (Stop after read)");
return;
}
if(argc > 3){
uint32_t maxBytes = atom_getint(argv + 3);
uint8_t buffer[7];
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x30 | port;
buffer[3] = readMode;
buffer[4] = maxBytes & 0x7F;
buffer[5] = (maxBytes >> 7) & 0x7F;
buffer[6] = 0xF7;
writeBuffer(x, buffer, 7);
}
else{
uint8_t buffer[5];
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x30 | port;
buffer[3] = readMode;
buffer[4] = 0xF7;
writeBuffer(x, buffer, 5);
}
}
}
/* serial close PORT */
if(strcmp(cmdName, "close") == 0){
if(argc > 1){
uint8_t port = serialPort(atom_getsymbolarg(1, argc, argv)->s_name);
if(port == 255){
error("Unknown serial port name");
return;
}
uint8_t buffer[4];
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x50 | port;
buffer[3] = 0xF7;
writeBuffer(x, buffer, 4);
}
}
/* serial flush PORT */
if(strcmp(cmdName, "flush") == 0){
if(argc > 1){
uint8_t port = serialPort(atom_getsymbolarg(1, argc, argv)->s_name);
if(port == 255){
error("Unknown serial port name");
return;
}
uint8_t buffer[4];
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x60 | port;
buffer[3] = 0xF7;
writeBuffer(x, buffer, 4);
}
}
/* serial listen PORT */
if(strcmp(cmdName, "listen") == 0){
if(argc > 1){
uint8_t port = serialPort(atom_getsymbolarg(1, argc, argv)->s_name);
if(port == 255){
error("Unknown serial port name");
return;
}
uint8_t buffer[4];
buffer[0] = 0xF0;
buffer[1] = 0x60;
buffer[2] = 0x70 | port;
buffer[3] = 0xF7;
writeBuffer(x, buffer, 4);
}
}
}
}
void pdfirmata_I2C(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv){
if(argc > 0){
const char * cmdName = atom_getsymbolarg(0, argc, argv)->s_name;
/* I2C rw ADDRESS AUTORESTART RWMODE ARG0 (ARG1) (ARG2) (...) */
/* AUTORESTART = 0 : Stop, 1 : Restart */
/* RWMODE = wr : Write, ro : Read Only, rc : Read Continuously, sr : Stop Reading */
if(strcmp(cmdName, "rw") == 0){
if(argc > 4){
int addr = atom_getfloatarg(1, argc, argv);
int autoRestart = atom_getfloatarg(2, argc, argv);
const char * rwMode = atom_getsymbolarg(3, argc, argv)->s_name;
int rwModeInt = 0;
if(strcmp(rwMode, "wr") == 0){ rwModeInt = 0; }
else if(strcmp(rwMode, "ro") == 0){ rwModeInt = 1; }
else if(strcmp(rwMode, "rc") == 0){ rwModeInt = 2; }
else if(strcmp(rwMode, "sr") == 0){ rwModeInt = 3; }
else{ error("Unknown rw mode"); return; }
int LSB = addr & 0x7F;
int MSB = 0;
if(addr > 0x7F){ MSB = (addr >> 7) & 0x7; }
MSB |= (rwModeInt & 0x3) << 2;
if(addr > 0x7F) MSB |= 0x1 << 4;
MSB |= (autoRestart & 0x1) << 5;
uint8_t * buffer = (uint8_t *)malloc((((argc - 4) * 2) + 5) * sizeof(uint8_t));
buffer[0] = 0xF0;
buffer[1] = 0x76;
buffer[2] = LSB & 0x7F;
buffer[3] = MSB & 0x7F;
uint16_t i = 4;
while(i < argc){
buffer[((i - 4) * 2) + 4] = atom_getint(argv + i) & 0x7F;
buffer[((i - 4) * 2) + 5] = (atom_getint(argv + i) >> 7) & 0x7F;
i++;
}
buffer[((argc - 4) * 2) + 4] = 0xF7;
writeBuffer(x, buffer, ((argc - 4) * 2) + 5);
free(buffer);
}
}
/* I2C delay DELAY */
if(strcmp(cmdName, "delay") == 0){
if(argc > 1){
int delay = atom_getfloatarg(1, argc, argv);
uint8_t buffer[5];
buffer[0] = 0xF0;
buffer[1] = 0x78;
buffer[2] = delay & 0x7F;
buffer[3] = (delay >> 7) & 0x7F;
buffer[4] = 0xF7;
writeBuffer(x, buffer, 5);
}
}
/* I2C config ARG0 (ARG1) (ARG2) ... */
if(strcmp(cmdName, "config") == 0){
if(argc > 1){
uint8_t * buffer = (uint8_t *)malloc((argc + 3) * sizeof(uint8_t));
buffer[0] = 0xF0;
buffer[1] = 0x78;
uint16_t i = 1;
while(i < argc){
buffer[i + 1] = atom_getint(argv + i) & 0x7F;
i++;
}
buffer[argc + 2] = 0xF7;
writeBuffer(x, buffer, argc + 3);
free(buffer);
}
}
}
}
void pdfirmata_servo(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv){
if(argc > 0){
const char * cmdName = atom_getsymbolarg(0, argc, argv)->s_name;
/* servo config PIN MINPULSE MAXPULSE */
if(strcmp(cmdName, "config") == 0){
if(argc > 3){
int pin = atom_getfloatarg(1, argc, argv);
int minPulse = atom_getfloatarg(2, argc, argv);
int maxPulse = atom_getfloatarg(3, argc, argv);
uint8_t buffer[8];
buffer[0] = 0xF0;
buffer[1] = 0x70;
buffer[2] = pin & 0x7F;
buffer[3] = minPulse & 0x7F;
buffer[4] = (minPulse >> 7) & 0x7F;
buffer[5] = maxPulse & 0x7F;
buffer[6] = (maxPulse >> 7) & 0x7F;
buffer[7] = 0xF7;
writeBuffer(x, buffer, 8);
}
}
/* servo write PIN VALUE */
if(strcmp(cmdName, "write") == 0){
if(argc > 2){
int pin = atom_getfloatarg(1, argc, argv);
int value = atom_getfloatarg(2, argc, argv);
if(pin < 16 && value < 0x4000){
uint8_t buffer[3];
buffer[0] = 0xE0 + pin;
buffer[1] = value & 0x7F;
buffer[2] = (value >> 7) & 0x7F;
writeBuffer(x, buffer, 3);
}
else{
uint8_t buffer[8];
buffer[0] = 0xF0;
buffer[1] = 0x6F;
buffer[2] = pin;
buffer[3] = value & 127;
buffer[4] = (value >> 7) & 127;
buffer[5] = (value >> 14) & 127;
buffer[6] = (value >> 21) & 127;
buffer[7] = 0xF7;
writeBuffer(x, buffer, 8);
}
}
}
}
}
void pdfirmata_encoder(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv){
if(argc > 0){
/* encoder attach ENCODER PINA PINB */
const char * cmdName = atom_getsymbolarg(0, argc, argv)->s_name;
if(strcmp(cmdName, "attach") == 0){
if(argc > 3){
int encoder = atom_getfloatarg(1, argc, argv);
int pinA = atom_getfloatarg(2, argc, argv);
int pinB = atom_getfloatarg(3, argc, argv);
uint8_t buffer[7];
buffer[0] = 0xF0;
buffer[1] = 0x61;
buffer[2] = 0x00;
buffer[3] = encoder & 0x7F;
buffer[4] = pinA & 0x7F;
buffer[5] = pinB & 0x7F;
buffer[6] = 0xF7;
writeBuffer(x, buffer, 7);
}
}
/* encoder read ENCODER */
if(strcmp(cmdName, "read") == 0){
if(argc > 1){
int encoder = atom_getfloatarg(1, argc, argv);
uint8_t buffer[5];
buffer[0] = 0xF0;
buffer[1] = 0x61;
buffer[2] = 0x01;
buffer[3] = encoder & 0x7F;
buffer[4] = 0xF7;
writeBuffer(x, buffer, 5);
}
}
/* encoder readAll */
if(strcmp(cmdName, "readAll") == 0){
uint8_t buffer[4];
buffer[0] = 0xF0;
buffer[1] = 0x61;
buffer[2] = 0x02;
buffer[3] = 0xF7;
writeBuffer(x, buffer, 4);
}
/* encoder reset ENCODER */
if(strcmp(cmdName, "reset") == 0){
if(argc > 1){
int encoder = atom_getfloatarg(1, argc, argv);
uint8_t buffer[5];
buffer[0] = 0xF0;
buffer[1] = 0x61;
buffer[2] = 0x03;
buffer[3] = encoder & 0x7F;
buffer[4] = 0xF7;
writeBuffer(x, buffer, 5);
}
}
/* encoder report ENABLE */
if(strcmp(cmdName, "report") == 0){
if(argc > 1){
int enable = atom_getfloatarg(1, argc, argv);
uint8_t buffer[5];
buffer[0] = 0xF0;
buffer[1] = 0x61;
buffer[2] = 0x04;
buffer[3] = enable & 0x1;
buffer[4] = 0xF7;
writeBuffer(x, buffer, 5);
}
}
/* encoder detach ENCODER */
if(strcmp(cmdName, "detach") == 0){
if(argc > 1){
int encoder = atom_getfloatarg(1, argc, argv);
uint8_t buffer[5];
buffer[0] = 0xF0;
buffer[1] = 0x61;
buffer[2] = 0x05;
buffer[3] = encoder & 0x7F;
buffer[4] = 0xF7;
writeBuffer(x, buffer, 5);
}
}
}
}
void pdfirmata_stepper(t_pdfirmata * x, t_symbol * s, t_int argc, t_atom * argv){
if(argc > 0){
const char * cmdName = atom_getsymbolarg(0, argc, argv)->s_name;
/* stepper config motor interface step enable pin1 pin2 (pin3) (pin4) (enablePin) (invert) */
if(strcmp(cmdName, "config") == 0){
if(argc > 6){
uint8_t motor = atom_getfloatarg(1, argc, argv);
uint8_t interface = atom_getfloatarg(2, argc, argv);
uint8_t step = atom_getfloatarg(3, argc, argv);
uint8_t enable = atom_getfloatarg(4, argc, argv);
/* interface is driver or two pin */
if((interface == 1) | (interface == 2)){