-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratchdaemon.cpp
1318 lines (1226 loc) · 35.3 KB
/
scratchdaemon.cpp
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
/*
* Program to connect scratch to Firmata
* In scratch use:
* Variables:
* pinNN 0/1/on/off/high/low
* allpins 0/1/on/off/high/low
* pwmNN xx
* servoNN xx
* configNN xx (xx=out/in)
* motorNN xx (motorA = motor11 / motorB = motor12, xx is %)
* powerNN xx (synonym for motor)
* Broadcasts:
* pinNNon
* pinNNoff
* configNNout
* configNNin
* adcNN (enable ADC reporting for pin NN)
* adcNNoff
* allon
* alloff
*
* TODO:
* test allon
* test servo
*
* http://simplesi.net/scratchgpio/scratchgpio-1st-project/
* broadcast supports:
* invert (to flip sense of io)
* setpins (to set selected as inputs/outputs)
* setpinslow (to turn all outputs off)
* setpinsnone (to set all as input)
* setpinshigh
* sonarNN (to put pin NN into sonar mode)
* ultraNN (to put pin NN into ultrasonic mode)
* pinpatternBBBBB... (to set pins to list of states)
* 1coil/2coil/halfstep (set stepper motor mode)
* map pin 40, switch (rename pin 40 to "switch")
* stepper (puts motor support into stepper mode, xx becomes steps)
*
* variable supports:
* dac xx (no firmata?)
* stepdelay xx (time between stepper motor steps)
*
* also from firmata:
* stepper motor support
*
* also add direct support for TB6612FNG on specified pins
*
*/
#include <iostream>
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <string>
#include <cstdlib>
#include <netdb.h>
#include <algorithm>
#include <cctype>
#include <map>
#include <limits.h>
#include <functional>
#include "firmata.h"
#ifndef NO_BLUETOOTH
#include "firmble.h"
#endif
#include "firmserial.h"
bool s_debug = 0;
#define DBG(__x...) \
if (s_debug) { \
std::ostringstream __s; \
__s << "DBG:" << syscall(SYS_gettid) << ":" << __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ":" << __x << std::endl; \
std::cerr <<__s.str(); \
}
#define ERR(__x...) \
if (1) { \
std::ostringstream __s; \
size_t __t; \
__s << "ERR:" << syscall(SYS_gettid) << ":" << __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ":"; \
__t = __s.str().size(); \
__s << __x; \
report_error(__s.str().substr(__t)); \
__s << std::endl; \
std::cerr <<__s.str(); \
}
bool stopping = false;
void do_stop(int sig)
{
std::cout << "Shutting down..." << std::endl;
stopping = true;
}
int scratch_fd = -1;
std::string scratch_host("127.0.0.1");
struct sockaddr_in scratch_addr;
int scratch_port = 42001;
// milliseconds
int samplingInterval = 100;
int numPins = -1;
bool myPins[256];
bool reportingset[256];
struct timeval tv;
typedef std::function<int (const std::string&, const std::string&)> cmdfunc;
std::map<std::string,cmdfunc> custom_commands;
firmata::Firmata<firmata::Base, firmata::I2C>* f = nullptr;
#ifndef NO_BLUETOOTH
firmata::FirmBle* bleio = nullptr;
#endif
firmata::FirmSerial* serialio = nullptr;
// report an error back to scratch, if possible
void write_scratch_message(const std::string &msgtype, const std::string &label, const std::string &value);
void report_error(const std::string & msg)
{
if (scratch_fd != -1)
{
write_scratch_message("sensor-update", "error-message", msg);
}
}
// reset timer to send next sensor updates
void reset_timeout()
{
tv.tv_sec = (samplingInterval / 1000);
tv.tv_usec = (samplingInterval * 1000) % 1000000;
}
// read all current pin modes
void read_pinstates()
{
// no actual action required as firmata lib does this for us
numPins = f->getNumPins();
DBG("Found "<<numPins<<" pins");
for (int pin=0; pin<numPins; ++pin)
{
const std::vector<uint8_t> &caps(f->getPinCaps(pin));
std::string comma;
std::cout << pin << ": ";
std::vector<uint8_t>::const_iterator i = caps.begin();
while (i != caps.end())
{
std::cout << comma;
switch (*i)
{
case 0: std::cout << "Input"; break;
case 1: std::cout << "Output"; break;
case 2: std::cout << "Analog"; break;
case 3: std::cout << "PWM"; break;
case 4: std::cout << "Servo"; break;
case 5: std::cout << "Shift"; break;
case 6: std::cout << "I2C"; break;
case 7: std::cout << "Onewire"; break;
case 8: std::cout << "Stepper"; break;
case 10: std::cout << "Serial"; break;
case 11: std::cout << "Pullup"; break;
case 127: std::cout << "Ignore"; break;
default: std::cout << "(" << (int)(*i)<<")"; break;
}
comma=",";
++i;
}
std::cout << std::endl;
}
}
void wait_for_scratch()
{
if (scratch_fd == -1)
{
scratch_fd = socket(AF_INET, SOCK_STREAM, 0);
DBG("scratch socket is "<<scratch_fd);
}
while ((!stopping) &&
(connect(scratch_fd, (sockaddr *)&scratch_addr, sizeof(scratch_addr)) < 0))
{
// not there, keep waiting
DBG("waiting for scratch, errno "<<strerror(errno));
sleep(10);
}
ERR("Connected to scratch");
}
bool connected_to_firmata()
{
if (f == nullptr)
{
DBG("no firmata");
return false;
}
#ifndef NO_BLUETOOTH
if ((bleio != nullptr) && (!bleio->isOpen()))
{
DBG("bluetooth not connected");
return false;
}
#endif
if (!f->ready())
{
DBG("firmata not ready");
return false;
}
return true;
}
void disconnect_firmata()
{
if (connected_to_firmata()) {
DBG("Disconnecting firmata");
std::vector<unsigned char> r;
r.push_back(FIRMATA_SYSTEM_RESET);
f->standardCommand(r);
}
if (f != nullptr) {
DBG("Deleting firmata");
// the act of deleting the firmata object will also destroy
// the IO object too
delete(f);
f = nullptr;
#ifndef NO_BLUETOOTH
bleio = nullptr;
#endif
serialio = nullptr;
}
}
// connect to firmata
// p1 = conn type, 1 = serial, 2/3 = Bluetooth
// p2 = port
bool connect_firmata(int type, const std::string & port)
{
// ensure properly disconnected first
disconnect_firmata();
// setup bleio/serialio
switch (type)
{
case 1: // serial
DBG("connecting to serial port "<<port);
try
{
if (serialio) {
delete serialio;
}
serialio = new firmata::FirmSerial(port.c_str());
}
catch (...)
{
ERR("Failed to open serial port "<<port);
}
break;
#ifndef NO_BLUETOOTH
case 3: // first bluetooth (port already set up)
case 2: // specified bluetooth
DBG("connecting to "<<port);
try
{
if (bleio) {
delete bleio;
}
bleio = new firmata::FirmBle(port.c_str());
if (s_debug) {
DBG("enabling debug");
bleio->enableDebug();
}
}
catch (...)
{
ERR("Failed to connect to Bluetooth device "<<port);
}
break;
#endif
}
if (f) {
delete f;
}
ERR("Opening firmata");
#ifndef NO_BLUETOOTH
if (bleio != nullptr)
{
f = new firmata::Firmata<firmata::Base, firmata::I2C>(bleio);
}
#endif
if (serialio != nullptr)
{
f = new firmata::Firmata<firmata::Base, firmata::I2C>(serialio);
}
// firmata constructor called open()
if (f == nullptr)
{
ERR("failed to instantiate firmata connection");
return false;
}
#ifndef NO_BLUETOOTH
if (bleio != nullptr)
{
if (!bleio->isOpen())
{
ERR("Connecting Bluetooth");
bleio->open();
if (bleio->isOpen())
{
ERR("Bluetooth now connected");
f->init();
} else {
ERR("Bluetooth connect failed");
return false;
}
}
}
#endif
if (serialio != nullptr)
{
// can this happen?
if (!serialio->isOpen())
{
ERR("Connecting serial");
serialio->open();
if (serialio->isOpen())
{
ERR("Serial now connected");
f->init();
} else {
ERR("Serial connect failed");
return false;
}
}
}
ERR("Firmata connected and ready");
f->setSamplingInterval(samplingInterval);
read_pinstates();
sleep(1);
reset_timeout();
return true;
}
void disconnect_scratch()
{
if (scratch_fd != -1)
{
close(scratch_fd);
scratch_fd = -1;
}
}
int do_poll()
{
fd_set write_set, read_set;
int result;
FD_ZERO(&read_set);
FD_ZERO(&write_set);
int fd_max = -1;
FD_SET(scratch_fd, &read_set);
fd_max = scratch_fd;
result = select(fd_max+1, &read_set, &write_set, nullptr, &tv);
if ((result < 0) && (errno != EINTR))
{
ERR("select failed, "<<strerror(errno));
disconnect_scratch();
} else if (result == 0) {
reset_timeout();
}
return result;
}
//////////////////////////////////////////////////////////////////////////
//
// Handling commands from scratch
// set pin mode in firmata if required
void pinmode(uint8_t pin, uint8_t mode)
{
bool setreporting = false;
DBG("pin "<<(int)pin<<" mode "<<(int)mode);
if (f->getPinMode(pin) != mode)
{
const std::vector<uint8_t> &caps(f->getPinCaps(pin));
std::vector<uint8_t>::const_iterator i = caps.begin();
while (i != caps.end())
{
if (*i == mode)
{
break;
}
++i;
}
if (i != caps.end())
{
DBG("setting pin mode");
f->pinMode(pin, mode);
setreporting = true;
}
else
{
ERR("pin "<<(int)pin<<" does not support mode "<<(int)mode);
return;
}
}
// the first time thru we must always set reporting correctly
// even if the pin is already in the desired mode
if (reportingset[pin] == false)
{
setreporting = true;
}
if (setreporting)
{
// set the reporting state accordingly
if ((mode == MODE_INPUT) || (mode == MODE_PULLUP))
{
DBG("enable digital reporting");
f->reportDigitalPin(pin,1);
} else if (mode == MODE_ANALOG)
{
DBG("enable analog reporting");
f->reportAnalog(f->getPinAnalogChannel(pin),1);
} else {
DBG("disable reporting");
f->reportDigitalPin(pin,0);
uint8_t apin = f->getPinAnalogChannel(pin);
if (apin < 128) {
DBG("disable analog reporting on "<<(int)apin);
f->reportAnalog(pin,0);
}
}
reportingset[pin] = true;
}
}
// parse the pin number from a subset of the string
// returns maxint if not a valid pin number or does not end
// at the given position
#define BADNUMBER (UINT_MAX)
#define BADCMD (UINT_MAX - 1)
unsigned int getpin(const std::string &s, size_t ofs, size_t end = std::string::npos)
{
size_t newend = std::string::npos;
std::string n(s.substr(ofs,end-ofs));
unsigned int ret;
try
{
ret = std::stoul(n,&newend);
}
catch (...)
{
DBG("Failed to parse pin from "<<n);
return BADNUMBER;
}
if (newend != n.size()) {
DBG("only consumed "<<newend<<" chars but wanted to use "<<n.size()<<" "<< end<<" chars from "<<n);
return BADCMD;
}
DBG("from "<<s<<" got pin "<<ret);
return ret;
}
#define ends_in(__h,__n) (__h.substr(__h.length()-strlen(#__n)) == #__n)
// set digital output pin state
// pin1on / pin9 off
// p1=cmd p2=value
// value absent = parse from number
int process_pin(const std::string &t1, const std::string &t2)
{
unsigned int value = UINT_MAX;
int ret = 2; // assume consume 2 tokens
size_t end = std::string::npos;
DBG("Parsing from "<<t1<<" "<<t2);
if (ends_in(t1,off)) {
value = 0;
end = t1.size() - 3;
ret = 1;
} else if (ends_in(t1,on)) {
value = 1;
end = t1.size() - 2;
ret = 1;
}
unsigned int pin = getpin(t1,3,end);
if (pin == BADNUMBER) {
ERR("Failed to parse required pin state from "<<t1);
return 0;
} else if (pin == BADCMD) {
ERR("Not a valid command from "<<t1);
return 0;
}
if (value == UINT_MAX) {
// need second token
if ((t2 == "off") || (t2 == "low") || (t2 == "0")) {
value = 0;
} else if ((t2 == "on") || (t2 == "high") || (t2 == "1")) {
value = 1;
} else if (!t2.empty()) {
ERR("Failed to parse required pin state from "<<t2);
return 0;
}
}
DBG("pin "<<pin<<" set to "<<value);
pinmode(pin, MODE_OUTPUT);
myPins[pin] = false;
f->digitalWrite(pin,value);
return ret;
}
// enable reporting for ADC pin
// adcN / adcNoff
// p1=cmd p2=value
// value absent = parse from number
// pin provided is analog pin we must map to digital
int process_adc(const std::string &t1, const std::string &t2)
{
unsigned int value = UINT_MAX;
int ret = 2;
size_t end = std::string::npos;
DBG("Parsing from "<<t1<<" "<<t2);
if (ends_in(t1,off)) {
value = 0;
end = t1.size() - 3;
ret = 1;
}
unsigned int apin = getpin(t1,3,end);
if (apin == BADNUMBER) {
ERR("Failed to parse required pin state from "<<t1);
return 0;
} else if (apin == BADCMD) {
ERR("Not a valid command from "<<t1);
return 0;
}
if (value == UINT_MAX) {
// if no clue yet then try second arg
if (t2 == "off") {
value = 0;
} else if (t2 == "on") {
value = 1;
} else {
// assume command without parameters
value = 1;
ret = 1;
}
}
unsigned int pin = f->getPinFromAnalogChannel(apin);
DBG("pin "<<pin<<" apin "<<apin<<" value "<<value);
pinmode(pin, MODE_ANALOG);
myPins[pin] = (value==1)?true:false;
f->reportAnalog(apin,value);
return ret;
}
// set ddr
// config1in / config2 out
// p1=cmd p2=value
// value absent = parse from number
int process_config(const std::string &t1, const std::string &t2)
{
unsigned int value = UINT_MAX;
int ret = 2;
size_t end = std::string::npos;
DBG("Parsing from "<<t1<<" "<<t2);
if (ends_in(t1,out)) {
value = MODE_OUTPUT;
end = t1.size() - 3;
ret = 1;
} else if (ends_in(t1,pu)) {
value = MODE_PULLUP;
end = t1.size() - 2;
ret = 1;
} else if (ends_in(t1,in)) {
value = MODE_INPUT;
end = t1.size() - 2;
ret = 1;
}
unsigned int pin = getpin(t1,6,end);
if (pin == BADNUMBER) {
ERR("Failed to parse required pin state from "<<t1);
return 0;
} else if (pin == BADCMD) {
ERR("Not a valid command from "<<t1);
return 0;
}
if (value == UINT_MAX) {
if (t2 == "out") {
value = MODE_OUTPUT;
} else if (t2 == "in") {
value = MODE_INPUT;
} else if (t2 == "pu") {
value = MODE_PULLUP;
} else if (!t2.empty()) {
ERR("Failed to parse required pin state from "<<t2);
return 0;
} else {
ERR("Failed to parse required pin state from "<<t1);
return 0;
}
}
DBG("pin "<<pin<<" value "<<value);
pinmode(pin, value);
if ((value == MODE_INPUT) || (value == MODE_PULLUP)) {
myPins[pin] = true;
}
return ret;
}
// set pwm value
// pwmNN val
// p1=number p2=value%
int process_pwm(const std::string &t1, const std::string &t2)
{
DBG("Parsing from "<<t1<<" "<<t2);
unsigned int pin = getpin(t1,3);
if (pin == BADNUMBER) {
ERR("Failed to parse required pin state from "<<t1);
return 0;
} else if (pin == BADCMD) {
ERR("Not a valid command from "<<t1);
return 0;
}
unsigned int value = std::stoul(t2);
DBG("pin "<<pin<<" value "<<value);
pinmode(pin, MODE_PWM);
f->analogWrite(pin,value);
return 2;
}
// set servo value
// servoNN val
// p1=number p2=value%
int process_servo(const std::string &t1, const std::string &t2)
{
DBG("Parsing from "<<t1<<" "<<t2);
unsigned int pin = getpin(t1,5);
if (pin == BADNUMBER) {
ERR("Failed to parse required pin state from "<<t1);
return 0;
} else if (pin == BADCMD) {
ERR("Not a valid command from "<<t1);
return 0;
}
unsigned int value = std::stoul(t2);
DBG("pin "<<pin);
pinmode(pin, MODE_SERVO);
f->analogWrite(pin,value);
return 2;
}
int process_pin_percent(int pin, const std::string &t2, uint8_t mode)
{
int value = std::stoul(t2);
DBG("pin "<<pin<<" raw value "<<value);
uint32_t resolution = f->getPinCapResolution(pin,mode);
if (resolution > 0)
{
uint32_t max = (1<<resolution);
uint32_t scaled = (max * abs(value)) / 100;
if (scaled >= max)
{
scaled = max-1;
}
DBG("resolution "<<resolution<<" scaled "<<scaled);
pinmode(pin, mode);
f->analogWrite(pin,scaled);
return value;
}
else
{
ERR("No pin capability for mode "<<(int)mode);
}
return 0;
}
void process_pin_percent(const std::string &t1, const std::string &t2, size_t baseLen, uint8_t mode)
{
DBG("Parsing from "<<t1<<" "<<t2);
unsigned int pin;
if (t1[baseLen] == 'a')
{
pin = 11;
}
else if (t1[baseLen] == 'b')
{
pin = 12;
}
else {
pin = getpin(t1,baseLen);
if (pin == BADNUMBER) {
ERR("Failed to parse required pin state from "<<t1);
return;
} else if (pin == BADCMD) {
ERR("Not a valid command from "<<t1);
return;
}
}
process_pin_percent(pin, t2, mode);
}
// motor speed - alias for pwm
// motorA = motor11
// motorB = motor12
// motorNN val
// p1=number p2=value
int process_motor(const std::string &t1, const std::string &t2)
{
process_pin_percent(t1, t2, 5, MODE_PWM);
return 2;
}
// power - alias for pwm
// powerA = power11
// powerB = power12
// powerNN val
// p1=number p2=value
int process_power(const std::string &t1, const std::string &t2)
{
process_pin_percent(t1, t2, 5, MODE_PWM);
return 2;
}
// set all pins
// allpins value
int process_allpins(const std::string &t1, const std::string &t2)
{
unsigned int value;
size_t end = std::string::npos;
DBG("Parsing from "<<t1<<" "<<t2);
if ((t2 == "off") || (t2 == "low") || (t2 == "0")) {
value = 0;
} else if ((t2 == "on") || (t2 == "high") || (t2 == "1")) {
value = 1;
} else if (!t2.empty()) {
ERR("Failed to parse required pin state from "<<t2);
return 0;
}
// find all digital IO pins and iterate over them
for (uint8_t pin = 0; pin<numPins; ++pin)
{
// only update pins which are digital outputs
if (f->getPinMode(pin) == MODE_OUTPUT)
{
DBG("pin "<<pin<<" value "<<value);
f->digitalWrite(pin, value);
}
}
return 2;
}
// set all pins on
// allpins value
int process_allon(const std::string &t1, const std::string &t2)
{
process_allpins("allpins","on");
return 1;
}
// set all pins off
// allpins value
int process_alloff(const std::string &t1, const std::string &t2)
{
process_allpins("allpins","off");
return 1;
}
typedef struct
{
uint8_t in1;
uint8_t in2;
uint8_t pwm;
} tb6612fng;
std::map<std::string,tb6612fng> tb6612fng_list;
// motorname = custom name for motor
// motorname % or motorname -% or motorname stop or motorname brake
int process_setmotor(const std::string &t1, const std::string &t2)
{
std::map<std::string,tb6612fng>::iterator i = tb6612fng_list.find(t1);
if (i == tb6612fng_list.end())
{
ERR("Failed to find motor entry "<<t1);
return 0;
}
DBG("Setting motor "<<t1<<" to "<<t2);
if (t2 == "stop")
{
f->digitalWrite(i->second.in1,0);
f->digitalWrite(i->second.in2,0);
f->analogWrite(i->second.pwm,0);
}
else if (t2 == "brake")
{
f->digitalWrite(i->second.in1,1);
f->digitalWrite(i->second.in2,1);
f->analogWrite(i->second.pwm,0);
}
else
{
int speed = process_pin_percent(i->second.pwm, t2, MODE_PWM);
if (speed == 0)
{
f->digitalWrite(i->second.in1,0);
f->digitalWrite(i->second.in2,0);
}
else if (speed > 0)
{
f->digitalWrite(i->second.in1,1);
f->digitalWrite(i->second.in2,0);
}
else
{
f->digitalWrite(i->second.in1,0);
f->digitalWrite(i->second.in2,1);
}
}
return 2;
}
// define a motor controlled by a TB6612FNG
// defmotor "motorname,pwmPin,in1Pin,in2Pin"
int process_defmotor(const std::string &t1, const std::string &t2)
{
DBG("t1 "<<t1<<" t2 "<<t2);
int j = 0;
std::string token;
std::string name;
int pwm, pin1, pin2;
size_t start = 0, end = 0;
while (end != std::string::npos) {
end = t2.find(',', start);
token.assign(t2.substr( start, (end == std::string::npos) ? std::string::npos : end - start));
start = ( end > (std::string::npos - 1)) ? std::string::npos : (end + 1);
DBG("Token: " << token);
switch (j)
{
case 0: // motorname
name.assign(token);
DBG("Motor "<<name);
break;
case 1: // pwm pin
pwm = stoul(token);
DBG("pwm "<<pwm);
break;
case 2: // pin1
pin1 = stoul(token);
DBG("pin1 "<<pin1);
break;
case 3: // pin2
pin2 = stoul(token);
DBG("pin2 "<<pin2);
break;
}
++j;
}
if (j != 4)
{
ERR("Failed to parse motor definition from "<<t2);
return 0;
}
tb6612fng_list[name].in1 = pin1;
tb6612fng_list[name].in2 = pin2;
tb6612fng_list[name].pwm = pwm;
DBG("Motor "<<pin1<<" "<<pin2<<" "<<pwm);
custom_commands[name] = process_setmotor;
pinmode(pin1, MODE_OUTPUT);
pinmode(pin2, MODE_OUTPUT);
pinmode(pwm, MODE_PWM);
return 2;
}
// check for custom commands
int process_custom(const std::string &t1, const std::string &t2 = "")
{
std::map<std::string,cmdfunc>::const_iterator i = custom_commands.find(t1);
if (i != custom_commands.end())
{
return i->second(t1,t2);
return true;
}
return 0;
}
// process a single request from scratch
#define process_thing(__x,__y,__z) if (__x.find(#__y) == 0) return process_##__y(__x,__z)
int process_scratch(const std::string &t1, const std::string &t2 = "")
{
DBG("t1 "<<t1<<" t2 "<<t2);
int ret = process_custom(t1,t2);
if (ret > 0)
{
DBG("matched custom command "<<t1);
return ret;
}
process_thing(t1,pin,t2);
process_thing(t1,adc,t2);
process_thing(t1,config,t2);
process_thing(t1,pwm,t2);
process_thing(t1,servo,t2);
process_thing(t1,allpins,t2);
process_thing(t1,allon,t2);
process_thing(t1,alloff,t2);
process_thing(t1,motor,t2);
process_thing(t1,power,t2);
process_thing(t1,defmotor,t2);
return 0;
}
void read_scratch_message()
{
// msg format is
// XXXX:msgtype "label" [value]
// msgtype generally == broadcast
// label can be a quoted list of pins to process (e.g. pin1on pin2off)
// or can be a single name with value as a separate arg
int i;
unsigned char c[4];
if (read(scratch_fd,&c,4) != 4)
{
// something bad happened
ERR("failed to read from scratch, "<<strerror(errno));
disconnect_scratch();
return;
}
unsigned int msglen = (c[0]*16777216) + (c[1]*65536) + (c[2]*256) + c[3];
DBG("msglen is "<<msglen);
unsigned char * msgbuf = (unsigned char *)malloc(msglen+1);
if (read(scratch_fd, msgbuf, msglen) != msglen)
{
ERR("failed to read from scratch, "<<strerror(errno));
free(msgbuf);
disconnect_scratch();
return;
}
msgbuf[msglen]=0;
DBG("msg is '"<<msgbuf<<"'");
std::string token;
i = 0;
int j = 0;
bool in_quotes = false;
bool multivalue = false;
std::vector <std::string> tokens;
// broadcast message requires splitting on space within spaces
bool broadcast = false ; // assume sensor-update for now
while (i < msglen)
{
//DBG("char "<<i<<" = \""<<msgbuf[i]<<"\"");
if (msgbuf[i] == ' ')
{
if (in_quotes)
{
if (broadcast)
{
// space within quotes in a broadcast message has to be
// split to a new token
std::transform(token.begin(), token.end(), token.begin(), ::tolower);
tokens.push_back(token);
token.clear();
++j;
} else {
// otherwise we preserve the whitespace within the token
token.append(1, msgbuf[i]);
}
} else {
// outside quotes, space means move to next token
DBG("token "<<j<<" is \""<<token<<"\"");
std::transform(token.begin(), token.end(), token.begin(), ::tolower);
if (j == 0) {
// message type, broadcast or sensor-update
if (token == "broadcast") {
broadcast = true;
DBG("Is broadcast");
} else if (token != "sensor-update") {
ERR("Failed to find message type in "<<token);
break;
}
}