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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649 | class TelemetrixUnoR4WiFi(threading.Thread):
"""
This class exposes and implements the telemetrix API.
It uses threading to accommodate concurrency.
It includes the public API methods as well as
a set of private methods.
"""
# noinspection PyPep8,PyPep8,PyPep8
def __init__(self, com_port=None, arduino_instance_id=1,
arduino_wait=1, sleep_tune=0.000001,
shutdown_on_exception=True, hard_reset_on_shutdown=True,
transport_address=None, ip_port=31336, transport_type=0):
"""
:param com_port: e.g. COM3 or /dev/ttyACM0.
Only use if you wish to bypass auto com port
detection.
:param arduino_instance_id: Match with the value installed on the
arduino-telemetrix sketch.
:param arduino_wait: Amount of time to wait for an Arduino to
fully reset itself.
:param sleep_tune: A tuning parameter (typically not changed by user)
:param shutdown_on_exception: call shutdown before raising
a RunTimeError exception, or
receiving a KeyboardInterrupt exception
:param hard_reset_on_shutdown: reset the board on shutdown
:param transport_address: ip address of tcp/ip connected device.
:param ip_port: ip port of tcp/ip connected device
:param transport_type: 0 = WiFI
1 = USBSerial
2 = BLE
"""
# initialize threading parent
threading.Thread.__init__(self)
# create the threads and set them as daemons so
# that they stop when the program is closed
# create a thread to interpret received serial data
self.the_reporter_thread = threading.Thread(target=self._reporter)
self.the_reporter_thread.daemon = True
self.transport_address = transport_address
self.ip_port = ip_port
if transport_type not in [0, 1, 2]:
raise RuntimeError("Valid transport_type value is 0, 1, or 2")
self.transport_type = transport_type
if transport_type == 0:
if not transport_address:
raise RuntimeError("An IP address must be specified.")
if not self.transport_address:
self.the_data_receive_thread = threading.Thread(target=self._serial_receiver)
else:
self.the_data_receive_thread = threading.Thread(target=self._tcp_receiver)
self.the_data_receive_thread.daemon = True
# flag to allow the reporter and receive threads to run.
self.run_event = threading.Event()
# check to make sure that Python interpreter is version 3.7 or greater
python_version = sys.version_info
if python_version[0] >= 3:
if python_version[1] >= 7:
pass
else:
raise RuntimeError("ERROR: Python 3.7 or greater is "
"required for use of this program.")
# save input parameters as instance variables
self.com_port = com_port
self.arduino_instance_id = arduino_instance_id
self.arduino_wait = arduino_wait
self.sleep_tune = sleep_tune
self.shutdown_on_exception = shutdown_on_exception
self.hard_reset_on_shutdown = hard_reset_on_shutdown
# create a deque to receive and process data from the arduino
self.the_deque = deque()
# The report_dispatch dictionary is used to process
# incoming report messages by looking up the report message
# and executing its associated processing method.
self.report_dispatch = {}
# To add a command to the command dispatch table, append here.
self.report_dispatch.update(
{PrivateConstants.LOOP_COMMAND: self._report_loop_data})
self.report_dispatch.update(
{PrivateConstants.DEBUG_PRINT: self._report_debug_data})
self.report_dispatch.update(
{PrivateConstants.DIGITAL_REPORT: self._digital_message})
self.report_dispatch.update(
{PrivateConstants.ANALOG_REPORT: self._analog_message})
self.report_dispatch.update(
{PrivateConstants.FIRMWARE_REPORT: self._firmware_message})
self.report_dispatch.update({PrivateConstants.I_AM_HERE_REPORT: self._i_am_here})
self.report_dispatch.update(
{PrivateConstants.SERVO_UNAVAILABLE: self._servo_unavailable})
self.report_dispatch.update(
{PrivateConstants.I2C_READ_REPORT: self._i2c_read_report})
self.report_dispatch.update(
{PrivateConstants.I2C_TOO_FEW_BYTES_RCVD: self._i2c_too_few})
self.report_dispatch.update(
{PrivateConstants.I2C_TOO_MANY_BYTES_RCVD: self._i2c_too_many})
self.report_dispatch.update(
{PrivateConstants.SONAR_DISTANCE: self._sonar_distance_report})
self.report_dispatch.update({PrivateConstants.DHT_REPORT: self._dht_report})
self.report_dispatch.update(
{PrivateConstants.SPI_REPORT: self._spi_report})
self.report_dispatch.update(
{PrivateConstants.ONE_WIRE_REPORT: self._onewire_report})
self.report_dispatch.update(
{PrivateConstants.STEPPER_DISTANCE_TO_GO:
self._stepper_distance_to_go_report})
self.report_dispatch.update(
{PrivateConstants.STEPPER_TARGET_POSITION:
self._stepper_target_position_report})
self.report_dispatch.update(
{PrivateConstants.STEPPER_CURRENT_POSITION:
self._stepper_current_position_report})
self.report_dispatch.update(
{PrivateConstants.STEPPER_RUNNING_REPORT:
self._stepper_is_running_report})
self.report_dispatch.update(
{PrivateConstants.STEPPER_RUN_COMPLETE_REPORT:
self._stepper_run_complete_report})
self.report_dispatch.update(
{PrivateConstants.STEPPER_DISTANCE_TO_GO:
self._stepper_distance_to_go_report})
self.report_dispatch.update(
{PrivateConstants.STEPPER_TARGET_POSITION:
self._stepper_target_position_report})
self.report_dispatch.update(
{PrivateConstants.FEATURES:
self._features_report})
# dictionaries to store the callbacks for each pin
self.analog_callbacks = {}
self.digital_callbacks = {}
self.i2c_callback = None
self.i2c_callback2 = None
self.i2c_1_active = False
self.i2c_2_active = False
self.spi_callback = None
self.onewire_callback = None
self.cs_pins_enabled = []
# the trigger pin will be the key to retrieve
# the callback for a specific HC-SR04
self.sonar_callbacks = {}
self.sonar_count = 0
self.dht_callbacks = {}
self.dht_count = 0
# serial port in use
self.serial_port = None
# socket for tcp/ip communications
self.sock = None
# flag to indicate we are in shutdown mode
self.shutdown_flag = False
# debug loopback callback method
self.loop_back_callback = None
# flag to indicate the start of a new report
# self.new_report_start = True
# firmware version to be stored here
self.firmware_version = []
# reported arduino instance id
self.reported_arduino_id = []
# reported features
self.reported_features = 0
# flag to indicate if i2c was previously enabled
self.i2c_enabled = False
# flag to indicate if spi is initialized
self.spi_enabled = False
# flag to indicate if onewire is initialized
self.onewire_enabled = False
# stepper motor variables
# updated when a new motor is added
# self.next_stepper_assigned = 0
#
# # valid list of stepper motor interface types
# self.valid_stepper_interfaces = [1, 2, 3, 4, 6, 8]
#
# # maximum number of steppers supported
# self.max_number_of_steppers = 4
#
# # number of steppers created - not to exceed the maximum
# self.number_of_steppers = 0
#
# # dictionary to hold stepper motor information
# self.stepper_info = {'instance': False, 'is_running': None,
# 'maximum_speed': 1, 'speed': 0, 'acceleration': 0,
# 'distance_to_go_callback': None,
# 'target_position_callback': None,
# 'current_position_callback': None,
# 'is_running_callback': None,
# 'motion_complete_callback': None,
# 'acceleration_callback': None}
#
# # build a list of stepper motor info items
# self.stepper_info_list = []
# # a list of dictionaries to hold stepper information
# for motor in range(self.max_number_of_steppers):
# self.stepper_info_list.append(self.stepper_info.copy())
self.the_reporter_thread.start()
self.the_data_receive_thread.start()
print(f"telemetrix_uno_r4_wifi: Version"
f" {PrivateConstants.TELEMETRIX_VERSION}\n\n"
f"Copyright (c) 2023 Alan Yorinks All Rights Reserved.\n")
# using the serial link
if not self.transport_address:
if not self.com_port:
# user did not specify a com_port
try:
self._find_arduino()
except KeyboardInterrupt:
if self.shutdown_on_exception:
self.shutdown()
else:
# com_port specified - set com_port and baud rate
try:
self._manual_open()
except KeyboardInterrupt:
if self.shutdown_on_exception:
self.shutdown()
if self.serial_port:
print(
f"Arduino compatible device found and connected to {self.serial_port.port}")
self.serial_port.reset_input_buffer()
self.serial_port.reset_output_buffer()
self.disable_scroll_message()
# no com_port found - raise a runtime exception
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('No Arduino Found or User Aborted Program')
else:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.transport_address, self.ip_port))
print(f'Successfully connected to: {self.transport_address}:{self.ip_port}')
# allow the threads to run
self._run_threads()
print(f'Waiting for Arduino to reset')
print(f'Reset Complete')
# get telemetrix firmware version and print it
print('\nRetrieving Telemetrix4UnoR4WiFi firmware ID...')
self._get_firmware_version()
if not self.firmware_version:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'Telemetrix4UnoR4WiFi firmware version')
else:
print(f'Telemetrix4UnoR4WiFi firmware version: {self.firmware_version[0]}.'
f'{self.firmware_version[1]}.{self.firmware_version[2]}')
command = [PrivateConstants.ENABLE_ALL_REPORTS]
self._send_command(command)
# get the features list
command = [PrivateConstants.GET_FEATURES]
self._send_command(command)
time.sleep(.2)
# Have the server reset its data structures
command = [PrivateConstants.RESET]
self._send_command(command)
time.sleep(.2)
def _find_arduino(self):
"""
This method will search all potential serial ports for an Arduino
containing a sketch that has a matching arduino_instance_id as
specified in the input parameters of this class.
This is used explicitly with the Telemetrix4Arduino sketch.
"""
# a list of serial ports to be checked
serial_ports = []
print('Opening all potential serial ports...')
the_ports_list = list_ports.comports()
for port in the_ports_list:
if port.pid is None:
continue
try:
self.serial_port = serial.Serial(port.device, 115200,
timeout=1, writeTimeout=0)
except SerialException:
continue
# create a list of serial ports that we opened
serial_ports.append(self.serial_port)
# display to the user
print('\t' + port.device)
# clear out any possible data in the input buffer
# wait for arduino to reset
print(
f'\nWaiting {self.arduino_wait} seconds(arduino_wait) for Arduino devices to '
'reset...')
# temporary for testing
time.sleep(self.arduino_wait)
self._run_threads()
for serial_port in serial_ports:
self.serial_port = serial_port
self._get_arduino_id()
if self.reported_arduino_id != self.arduino_instance_id:
continue
else:
print('Valid Arduino ID Found.')
self.serial_port.reset_input_buffer()
self.serial_port.reset_output_buffer()
return
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'Incorrect Arduino ID: {self.reported_arduino_id}')
def _manual_open(self):
"""
Com port was specified by the user - try to open up that port
"""
# if port is not found, a serial exception will be thrown
try:
print(f'Opening {self.com_port}...')
self.serial_port = serial.Serial(self.com_port, 115200,
timeout=1, writeTimeout=0)
print(
f'\nWaiting {self.arduino_wait} seconds(arduino_wait) for Arduino devices to '
'reset...')
self._run_threads()
time.sleep(self.arduino_wait)
self._get_arduino_id()
if self.reported_arduino_id != self.arduino_instance_id:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'Incorrect Arduino ID: {self.reported_arduino_id}')
print('Valid Arduino ID Found.')
# get arduino firmware version and print it
print('\nRetrieving Telemetrix4Arduino firmware ID...')
self._get_firmware_version()
if not self.firmware_version:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
f'Telemetrix4Arduino Sketch Firmware Version Not Found')
else:
print(f'Telemetrix4UnoR4 firmware version: {self.firmware_version[0]}.'
f'{self.firmware_version[1]}')
except KeyboardInterrupt:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('User Hit Control-C')
def analog_write(self, pin, value):
"""
Set the specified pin to the specified value.
:param pin: arduino pin number
:param value: pin value (maximum 16 bits)
"""
value_msb = value >> 8
value_lsb = value & 0xff
command = [PrivateConstants.ANALOG_WRITE, pin, value_msb, value_lsb]
self._send_command(command)
def digital_write(self, pin, value):
"""
Set the specified pin to the specified value.
:param pin: arduino pin number
:param value: pin value (1 or 0)
"""
command = [PrivateConstants.DIGITAL_WRITE, pin, value]
self._send_command(command)
def disable_all_reporting(self):
"""
Disable reporting for all digital and analog input pins
"""
command = [PrivateConstants.MODIFY_REPORTING,
PrivateConstants.REPORTING_DISABLE_ALL, 0]
self._send_command(command)
def disable_analog_reporting(self, pin):
"""
Disables analog reporting for a single analog pin.
:param pin: Analog pin number. For example for A0, the number is 0.
"""
command = [PrivateConstants.MODIFY_REPORTING,
PrivateConstants.REPORTING_ANALOG_DISABLE, pin]
self._send_command(command)
def disable_digital_reporting(self, pin):
"""
Disables digital reporting for a single digital input.
:param pin: Pin number.
"""
command = [PrivateConstants.MODIFY_REPORTING,
PrivateConstants.REPORTING_DIGITAL_DISABLE, pin]
self._send_command(command)
def enable_analog_reporting(self, pin):
"""
Enables analog reporting for the specified pin.
:param pin: Analog pin number. For example for A0, the number is 0.
"""
command = [PrivateConstants.MODIFY_REPORTING,
PrivateConstants.REPORTING_ANALOG_ENABLE, pin]
self._send_command(command)
def enable_digital_reporting(self, pin):
"""
Enable reporting on the specified digital pin.
:param pin: Pin number.
"""
command = [PrivateConstants.MODIFY_REPORTING,
PrivateConstants.REPORTING_DIGITAL_ENABLE, pin]
self._send_command(command)
def _get_arduino_id(self):
"""
Retrieve arduino-telemetrix arduino id
"""
command = [PrivateConstants.ARE_U_THERE]
self._send_command(command)
# provide time for the reply
time.sleep(.5)
def _get_firmware_version(self):
"""
This method retrieves the
arduino-telemetrix firmware version
"""
command = [PrivateConstants.GET_FIRMWARE_VERSION]
self._send_command(command)
# provide time for the reply
time.sleep(.5)
def i2c_read(self, address, register, number_of_bytes,
callback=None, i2c_port=0,
write_register=True):
"""
Read the specified number of bytes from the
specified register for the i2c device.
:param address: i2c device address
:param register: i2c register (or None if no register
selection is needed)
:param number_of_bytes: number of bytes to be read
:param callback: Required callback function to report
i2c data as a result of read command
:param i2c_port: 0 = default, 1 = secondary
:param write_register: If True, the register is written
before read
Else, the write is suppressed
callback returns a data list:
[I2C_READ_REPORT, i2c_port, number of bytes read, address, register,
bytes read..., time-stamp]
"""
self._i2c_read_request(address, register, number_of_bytes,
callback=callback, i2c_port=i2c_port,
write_register=write_register)
def i2c_read_restart_transmission(self, address, register,
number_of_bytes,
callback=None, i2c_port=0,
write_register=True):
"""
Read the specified number of bytes from the specified
register for the i2c device. This restarts the transmission
after the read. It is required for some i2c devices such as the MMA8452Q
accelerometer.
:param address: i2c device address
:param register: i2c register (or None if no register
selection is needed)
:param number_of_bytes: number of bytes to be read
:param callback: Required callback function to report i2c
data as a result of read command
:param i2c_port: 0 = default 1 = secondary
:param write_register: If True, the register is written before read
Else, the write is suppressed
callback returns a data list:
[I2C_READ_REPORT, i2c_port, number of bytes read, address, register,
bytes read..., time-stamp]
"""
self._i2c_read_request(address, register, number_of_bytes,
stop_transmission=False,
callback=callback, i2c_port=i2c_port,
write_register=write_register)
def _i2c_read_request(self, address, register, number_of_bytes,
stop_transmission=True, callback=None, i2c_port=0,
write_register=True):
"""
This method requests the read of an i2c device. Results are retrieved
via callback.
:param address: i2c device address
:param register: register number (or None if no register selection is needed)
:param number_of_bytes: number of bytes expected to be returned
:param stop_transmission: stop transmission after read
:param callback: Required callback function to report i2c data as a
result of read command.
:param write_register: If True, the register is written before read
Else, the write is suppressed
"""
if not i2c_port:
if not self.i2c_1_active:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
'I2C Read: set_pin_mode i2c never called for i2c port 1.')
if i2c_port:
if not self.i2c_2_active:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
'I2C Read: set_pin_mode i2c never called for i2c port 2.')
if not callback:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('I2C Read: A callback function must be specified.')
if not i2c_port:
self.i2c_callback = callback
else:
self.i2c_callback2 = callback
if not register:
register = 0
if write_register:
write_register = 1
else:
write_register = 0
# message contains:
# 1. address
# 2. register
# 3. number of bytes
# 4. restart_transmission - True or False
# 5. i2c port
# 6. suppress write flag
command = [PrivateConstants.I2C_READ, address, register, number_of_bytes,
stop_transmission, i2c_port, write_register]
self._send_command(command)
def i2c_write(self, address, args, i2c_port=0):
"""
Write data to an i2c device.
:param address: i2c device address
:param i2c_port: 0= port 1, 1 = port 2
:param args: A variable number of bytes to be sent to the device
passed in as a list
"""
if not i2c_port:
if not self.i2c_1_active:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
'I2C Write: set_pin_mode i2c never called for i2c port 1.')
if i2c_port:
if not self.i2c_2_active:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
'I2C Write: set_pin_mode i2c never called for i2c port 2.')
command = [PrivateConstants.I2C_WRITE, len(args), address, i2c_port]
for item in args:
command.append(item)
self._send_command(command)
def loop_back(self, start_character, callback=None):
"""
This is a debugging method to send a character to the
Arduino device, and have the device loop it back.
:param start_character: The character to loop back. It should be
an integer.
:param callback: Looped back character will appear in the callback method
"""
command = [PrivateConstants.LOOP_COMMAND, ord(start_character)]
self.loop_back_callback = callback
self._send_command(command)
def set_analog_scan_interval(self, interval):
"""
Set the analog scanning interval.
:param interval: value of 0 - 255 - milliseconds
"""
if 0 <= interval <= 255:
command = [PrivateConstants.SET_ANALOG_SCANNING_INTERVAL, interval]
self._send_command(command)
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('Analog interval must be between 0 and 255')
def set_pin_mode_analog_output(self, pin_number):
"""
Set a pin as a pwm (analog output) pin.
:param pin_number:arduino pin number
"""
self._set_pin_mode(pin_number, PrivateConstants.AT_OUTPUT)
def set_pin_mode_analog_input(self, pin_number, differential=0, callback=None):
"""
Set a pin as an analog input.
:param pin_number: arduino pin number
:param differential: difference in previous to current value before
report will be generated
:param callback: callback function
callback returns a data list:
[pin_type, pin_number, pin_value, raw_time_stamp]
The pin_type for analog input pins = 3
"""
self._set_pin_mode(pin_number, PrivateConstants.AT_ANALOG, differential,
callback)
def set_pin_mode_digital_input(self, pin_number, callback=None):
"""
Set a pin as a digital input.
:param pin_number: arduino pin number
:param callback: callback function
callback returns a data list:
[pin_type, pin_number, pin_value, raw_time_stamp]
The pin_type for all digital input pins = 2
"""
self._set_pin_mode(pin_number, PrivateConstants.AT_INPUT, callback=callback)
def set_pin_mode_digital_input_pullup(self, pin_number, callback=None):
"""
Set a pin as a digital input with pullup enabled.
:param pin_number: arduino pin number
:param callback: callback function
callback returns a data list:
[pin_type, pin_number, pin_value, raw_time_stamp]
The pin_type for all digital input pins = 2
"""
self._set_pin_mode(pin_number, PrivateConstants.AT_INPUT_PULLUP,
callback=callback)
def set_pin_mode_digital_output(self, pin_number):
"""
Set a pin as a digital output pin.
:param pin_number: arduino pin number
"""
self._set_pin_mode(pin_number, PrivateConstants.AT_OUTPUT)
def set_pin_mode_i2c(self, i2c_port=0):
"""
Establish the standard Arduino i2c pins for i2c utilization.
:param i2c_port: 0 = i2c1, 1 = i2c2
NOTES: 1. THIS METHOD MUST BE CALLED BEFORE ANY I2C REQUEST IS MADE
2. Callbacks are set within the individual i2c read methods of this
API.
See i2c_read, or i2c_read_restart_transmission.
"""
# test for i2c port 2
if i2c_port:
# if not previously activated set it to activated
# and the send a begin message for this port
if not self.i2c_2_active:
self.i2c_2_active = True
else:
return
# port 1
else:
if not self.i2c_1_active:
self.i2c_1_active = True
else:
return
command = [PrivateConstants.I2C_BEGIN, i2c_port]
self._send_command(command)
def set_pin_mode_dht(self, pin, callback=None, dht_type=22):
"""
:param pin: connection pin
:param callback: callback function
:param dht_type: either 22 for DHT22 or 11 for DHT11
Error Callback: [DHT REPORT Type, DHT_ERROR_NUMBER, PIN, DHT_TYPE, Time]
Valid Data Callback: DHT REPORT Type, DHT_DATA=, PIN, DHT_TYPE, Humidity,
Temperature,
Time]
DHT_REPORT_TYPE = 12
"""
if self.reported_features & PrivateConstants.DHT_FEATURE:
if not callback:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('set_pin_mode_dht: A Callback must be specified')
if self.dht_count < PrivateConstants.MAX_DHTS - 1:
self.dht_callbacks[pin] = callback
self.dht_count += 1
if dht_type != 22 and dht_type != 11:
dht_type = 22
command = [PrivateConstants.DHT_NEW, pin, dht_type]
self._send_command(command)
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
f'Maximum Number Of DHTs Exceeded - set_pin_mode_dht fails for pin {pin}')
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'The DHT feature is disabled in the server.')
# noinspection PyRedundantParentheses
def set_pin_mode_servo(self, pin_number, min_pulse=544, max_pulse=2400):
"""
Attach a pin to a servo motor
:param pin_number: pin
:param min_pulse: minimum pulse width
:param max_pulse: maximum pulse width
"""
if self.reported_features & PrivateConstants.SERVO_FEATURE:
minv = (min_pulse).to_bytes(2, byteorder="big")
maxv = (max_pulse).to_bytes(2, byteorder="big")
command = [PrivateConstants.SERVO_ATTACH, pin_number,
minv[0], minv[1], maxv[0], maxv[1]]
self._send_command(command)
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'The SERVO feature is disabled in the server.')
def set_pin_mode_sonar(self, trigger_pin, echo_pin,
callback=None):
"""
:param trigger_pin:
:param echo_pin:
:param callback: callback
callback data: [PrivateConstants.SONAR_DISTANCE, trigger_pin, distance_value, time_stamp]
"""
if self.reported_features & PrivateConstants.SONAR_FEATURE:
if not callback:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('set_pin_mode_sonar: A Callback must be specified')
if self.sonar_count < PrivateConstants.MAX_SONARS - 1:
self.sonar_callbacks[trigger_pin] = callback
self.sonar_count += 1
command = [PrivateConstants.SONAR_NEW, trigger_pin, echo_pin]
self._send_command(command)
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
f'Maximum Number Of Sonars Exceeded - set_pin_mode_sonar fails for pin {trigger_pin}')
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'The SONAR feature is disabled in the server.')
def set_pin_mode_spi(self, chip_select_list=None):
"""
Specify the list of chip select pins.
Standard Arduino MISO, MOSI and CLK pins are used for the board in use.
Chip Select is any digital output capable pin.
:param chip_select_list: this is a list of pins to be used for chip select.
The pins will be configured as output, and set to high
ready to be used for chip select.
NOTE: You must specify the chips select pins here!
command message: [command, [cs pins...]]
"""
if self.reported_features & PrivateConstants.SPI_FEATURE:
if type(chip_select_list) is not list:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('chip_select_list must be in the form of a list')
if not chip_select_list:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('Chip select pins were not specified')
self.spi_enabled = True
command = [PrivateConstants.SPI_INIT, len(chip_select_list)]
for pin in chip_select_list:
command.append(pin)
self.cs_pins_enabled.append(pin)
self._send_command(command)
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'The SPI feature is disabled in the server.')
# def set_pin_mode_stepper(self, interface=1, pin1=2, pin2=3, pin3=4,
# pin4=5, enable=True):
# """
# Stepper motor support is implemented as a proxy for the
# the AccelStepper library for the Arduino.
#
# This feature is compatible with the TB6600 Motor Driver
#
# Note: It may not work for other driver types!
#
# https://github.com/waspinator/AccelStepper
#
# Instantiate a stepper motor.
#
# Initialize the interface and pins for a stepper motor.
#
# :param interface: Motor Interface Type:
#
# 1 = Stepper Driver, 2 driver pins required
#
# 2 = FULL2WIRE 2 wire stepper, 2 motor pins required
#
# 3 = FULL3WIRE 3 wire stepper, such as HDD spindle,
# 3 motor pins required
#
# 4 = FULL4WIRE, 4 wire full stepper, 4 motor pins
# required
#
# 6 = HALF3WIRE, 3 wire half stepper, such as HDD spindle,
# 3 motor pins required
#
# 8 = HALF4WIRE, 4 wire half stepper, 4 motor pins required
#
# :param pin1: Arduino digital pin number for motor pin 1
#
# :param pin2: Arduino digital pin number for motor pin 2
#
# :param pin3: Arduino digital pin number for motor pin 3
#
# :param pin4: Arduino digital pin number for motor pin 4
#
# :param enable: If this is true, the output pins at construction time.
#
# :return: Motor Reference number
# """
# if self.reported_features & PrivateConstants.STEPPERS_FEATURE:
#
# if self.number_of_steppers == self.max_number_of_steppers:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('Maximum number of steppers has already been assigned')
#
# if interface not in self.valid_stepper_interfaces:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('Invalid stepper interface')
#
# self.number_of_steppers += 1
#
# motor_id = self.next_stepper_assigned
# self.next_stepper_assigned += 1
# self.stepper_info_list[motor_id]['instance'] = True
#
# # build message and send message to server
# command = [PrivateConstants.SET_PIN_MODE_STEPPER, motor_id, interface, pin1,
# pin2, pin3, pin4, enable]
# self._send_command(command)
#
# # return motor id
# return motor_id
# else:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'The Stepper feature is disabled in the server.')
def servo_write(self, pin_number, angle):
"""
Set a servo attached to a pin to a given angle.
:param pin_number: pin
:param angle: angle (0-180)
"""
command = [PrivateConstants.SERVO_WRITE, pin_number, angle]
self._send_command(command)
def servo_detach(self, pin_number):
"""
Detach a servo for reuse
:param pin_number: attached pin
"""
command = [PrivateConstants.SERVO_DETACH, pin_number]
self._send_command(command)
# def stepper_move_to(self, motor_id, position):
# """
# Set an absolution target position. If position is positive, the movement is
# clockwise, else it is counter-clockwise.
#
# The run() function (below) will try to move the motor (at most one step per call)
# from the current position to the target position set by the most
# recent call to this function. Caution: moveTo() also recalculates the
# speed for the next step.
# If you are trying to use constant speed movements, you should call setSpeed()
# after calling moveTo().
#
# :param motor_id: motor id: 0 - 3
#
# :param position: target position. Maximum value is 32 bits.
# """
# if position < 0:
# polarity = 1
# else:
# polarity = 0
# position = abs(position)
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_move_to: Invalid motor_id.')
#
# position_bytes = list(position.to_bytes(4, 'big', signed=True))
#
# command = [PrivateConstants.STEPPER_MOVE_TO, motor_id]
# for value in position_bytes:
# command.append(value)
# command.append(polarity)
# self._send_command(command)
#
# def stepper_move(self, motor_id, relative_position):
# """
# Set the target position relative to the current position.
#
# :param motor_id: motor id: 0 - 3
#
# :param relative_position: The desired position relative to the current
# position. Negative is anticlockwise from
# the current position. Maximum value is 32 bits.
# """
# if relative_position < 0:
# polarity = 1
# else:
# polarity = 0
#
# relative_position = abs(relative_position)
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_move: Invalid motor_id.')
#
# position_bytes = list(relative_position.to_bytes(4, 'big', signed=True))
#
# command = [PrivateConstants.STEPPER_MOVE, motor_id]
# for value in position_bytes:
# command.append(value)
# command.append(polarity)
# self._send_command(command)
#
# def stepper_run(self, motor_id, completion_callback=None):
# """
# This method steps the selected motor based on the current speed.
#
# Once called, the server will continuously attempt to step the motor.
#
# :param motor_id: 0 - 3
#
# :param completion_callback: call back function to receive motion complete
# notification
#
# callback returns a data list:
#
# [report_type, motor_id, raw_time_stamp]
#
# The report_type = 19
# """
# if not completion_callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_run: A motion complete callback must be '
# 'specified.')
#
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_run: Invalid motor_id.')
#
# self.stepper_info_list[motor_id]['motion_complete_callback'] = completion_callback
# command = [PrivateConstants.STEPPER_RUN, motor_id]
# self._send_command(command)
#
# def stepper_run_speed(self, motor_id):
# """
# This method steps the selected motor based at a constant speed as set by the most
# recent call to stepper_set_max_speed(). The motor will run continuously.
#
# Once called, the server will continuously attempt to step the motor.
#
# :param motor_id: 0 - 3
#
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_run_speed: Invalid motor_id.')
#
# command = [PrivateConstants.STEPPER_RUN_SPEED, motor_id]
# self._send_command(command)
#
# def stepper_set_max_speed(self, motor_id, max_speed):
# """
# Sets the maximum permitted speed. The stepper_run() function will accelerate
# up to the speed set by this function.
#
# Caution: the maximum speed achievable depends on your processor and clock speed.
# The default maxSpeed is 1 step per second.
#
# Caution: Speeds that exceed the maximum speed supported by the processor may
# result in non-linear accelerations and decelerations.
#
# :param motor_id: 0 - 3
#
# :param max_speed: 1 - 1000
# """
#
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_max_speed: Invalid motor_id.')
#
# if not 1 < max_speed <= 1000:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_max_speed: Speed range is 1 - 1000.')
#
# self.stepper_info_list[motor_id]['max_speed'] = max_speed
# max_speed_msb = (max_speed & 0xff00) >> 8
# max_speed_lsb = max_speed & 0xff
#
# command = [PrivateConstants.STEPPER_SET_MAX_SPEED, motor_id, max_speed_msb,
# max_speed_lsb]
# self._send_command(command)
#
# def stepper_get_max_speed(self, motor_id):
# """
# Returns the maximum speed configured for this stepper
# that was previously set by stepper_set_max_speed()
#
# Value is stored in the client, so no callback is required.
#
# :param motor_id: 0 - 3
#
# :return: The currently configured maximum speed.
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_max_speed: Invalid motor_id.')
#
# return self.stepper_info_list[motor_id]['max_speed']
#
# def stepper_set_acceleration(self, motor_id, acceleration):
# """
# Sets the acceleration/deceleration rate.
#
# :param motor_id: 0 - 3
#
# :param acceleration: The desired acceleration in steps per second
# per second. Must be > 0.0. This is an
# expensive call since it requires a square
# root to be calculated on the server.
# Dont call more often than needed.
#
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_acceleration: Invalid motor_id.')
#
# if not 1 < acceleration <= 1000:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_acceleration: Acceleration range is 1 - '
# '1000.')
#
# self.stepper_info_list[motor_id]['acceleration'] = acceleration
#
# max_accel_msb = acceleration >> 8
# max_accel_lsb = acceleration & 0xff
#
# command = [PrivateConstants.STEPPER_SET_ACCELERATION, motor_id, max_accel_msb,
# max_accel_lsb]
# self._send_command(command)
#
# def stepper_set_speed(self, motor_id, speed):
# """
# Sets the desired constant speed for use with stepper_run_speed().
#
# :param motor_id: 0 - 3
#
# :param speed: 0 - 1000 The desired constant speed in steps per
# second. Positive is clockwise. Speeds of more than 1000 steps per
# second are unreliable. Speed accuracy depends on the Arduino
# crystal. Jitter depends on how frequently you call the
# stepper_run_speed() method.
# The speed will be limited by the current value of
# stepper_set_max_speed().
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_speed: Invalid motor_id.')
#
# if not 0 < speed <= 1000:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_speed: Speed range is 0 - '
# '1000.')
#
# self.stepper_info_list[motor_id]['speed'] = speed
#
# speed_msb = speed >> 8
# speed_lsb = speed & 0xff
#
# command = [PrivateConstants.STEPPER_SET_SPEED, motor_id, speed_msb, speed_lsb]
# self._send_command(command)
#
# def stepper_get_speed(self, motor_id):
# """
# Returns the most recently set speed.
# that was previously set by stepper_set_speed();
#
# Value is stored in the client, so no callback is required.
#
# :param motor_id: 0 - 3
#
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_get_speed: Invalid motor_id.')
#
# return self.stepper_info_list[motor_id]['speed']
#
# def stepper_get_distance_to_go(self, motor_id, distance_to_go_callback):
# """
# Request the distance from the current position to the target position
# from the server.
#
# :param motor_id: 0 - 3
#
# :param distance_to_go_callback: required callback function to receive report
#
# :return: The distance to go is returned via the callback as a list:
#
# [REPORT_TYPE=15, motor_id, distance in steps, time_stamp]
#
# A positive distance is clockwise from the current position.
#
# """
# if not distance_to_go_callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_get_distance_to_go Read: A callback function must be specified.')
#
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_get_distance_to_go: Invalid motor_id.')
# self.stepper_info_list[motor_id][
# 'distance_to_go_callback'] = distance_to_go_callback
# command = [PrivateConstants.STEPPER_GET_DISTANCE_TO_GO, motor_id]
# self._send_command(command)
#
# def stepper_get_target_position(self, motor_id, target_callback):
# """
# Request the most recently set target position from the server.
#
# :param motor_id: 0 - 3
#
# :param target_callback: required callback function to receive report
#
# :return: The distance to go is returned via the callback as a list:
#
# [REPORT_TYPE=16, motor_id, target position in steps, time_stamp]
#
# Positive is clockwise from the 0 position.
#
# """
# if not target_callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(
# 'stepper_get_target_position Read: A callback function must be specified.')
#
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_get_target_position: Invalid motor_id.')
#
# self.stepper_info_list[motor_id][
# 'target_position_callback'] = target_callback
#
# command = [PrivateConstants.STEPPER_GET_TARGET_POSITION, motor_id]
# self._send_command(command)
#
# def stepper_get_current_position(self, motor_id, current_position_callback):
# """
# Request the current motor position from the server.
#
# :param motor_id: 0 - 3
#
# :param current_position_callback: required callback function to receive report
#
# :return: The current motor position returned via the callback as a list:
#
# [REPORT_TYPE=17, motor_id, current position in steps, time_stamp]
#
# Positive is clockwise from the 0 position.
# """
# if not current_position_callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(
# 'stepper_get_current_position Read: A callback function must be specified.')
#
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_get_current_position: Invalid motor_id.')
#
# self.stepper_info_list[motor_id]['current_position_callback'] = current_position_callback
#
# command = [PrivateConstants.STEPPER_GET_CURRENT_POSITION, motor_id]
# self._send_command(command)
#
# def stepper_set_current_position(self, motor_id, position):
# """
# Resets the current position of the motor, so that wherever the motor
# happens to be right now is considered to be the new 0 position. Useful
# for setting a zero position on a stepper after an initial hardware
# positioning move.
#
# Has the side effect of setting the current motor speed to 0.
#
# :param motor_id: 0 - 3
#
# :param position: Position in steps. This is a 32 bit value
# """
#
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_current_position: Invalid motor_id.')
# position_bytes = list(position.to_bytes(4, 'big', signed=True))
#
# command = [PrivateConstants.STEPPER_SET_CURRENT_POSITION, motor_id]
# for value in position_bytes:
# command.append(value)
# self._send_command(command)
#
# def stepper_run_speed_to_position(self, motor_id, completion_callback=None):
# """
# Runs the motor at the currently selected speed until the target position is
# reached.
#
# Does not implement accelerations.
#
# :param motor_id: 0 - 3
#
# :param completion_callback: call back function to receive motion complete
# notification
#
# callback returns a data list:
#
# [report_type, motor_id, raw_time_stamp]
#
# The report_type = 19
# """
# if not completion_callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_run_speed_to_position: A motion complete '
# 'callback must be '
# 'specified.')
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_run_speed_to_position: Invalid motor_id.')
#
# self.stepper_info_list[motor_id]['motion_complete_callback'] = completion_callback
# command = [PrivateConstants.STEPPER_RUN_SPEED_TO_POSITION, motor_id]
# self._send_command(command)
#
# def stepper_stop(self, motor_id):
# """
# Sets a new target position that causes the stepper
# to stop as quickly as possible, using the current speed and
# acceleration parameters.
#
# :param motor_id: 0 - 3
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_stop: Invalid motor_id.')
#
# command = [PrivateConstants.STEPPER_STOP, motor_id]
# self._send_command(command)
#
# def stepper_disable_outputs(self, motor_id):
# """
# Disable motor pin outputs by setting them all LOW.
#
# Depending on the design of your electronics this may turn off
# the power to the motor coils, saving power.
#
# This is useful to support Arduino low power modes: disable the outputs
# during sleep and then re-enable with enableOutputs() before stepping
# again.
#
# If the enable Pin is defined, sets it to OUTPUT mode and clears
# the pin to disabled.
#
# :param motor_id: 0 - 3
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_disable_outputs: Invalid motor_id.')
#
# command = [PrivateConstants.STEPPER_DISABLE_OUTPUTS, motor_id]
# self._send_command(command)
#
# def stepper_enable_outputs(self, motor_id):
# """
# Enable motor pin outputs by setting the motor pins to OUTPUT
# mode.
#
# If the enable Pin is defined, sets it to OUTPUT mode and sets
# the pin to enabled.
#
# :param motor_id: 0 - 3
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_enable_outputs: Invalid motor_id.')
#
# command = [PrivateConstants.STEPPER_ENABLE_OUTPUTS, motor_id]
# self._send_command(command)
#
# def stepper_set_min_pulse_width(self, motor_id, minimum_width):
# """
# Sets the minimum pulse width allowed by the stepper driver.
#
# The minimum practical pulse width is approximately 20 microseconds.
#
# Times less than 20 microseconds will usually result in 20 microseconds or so.
#
# :param motor_id: 0 -3
#
# :param minimum_width: A 16 bit unsigned value expressed in microseconds.
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_min_pulse_width: Invalid motor_id.')
#
# if not 0 < minimum_width <= 0xff:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_min_pulse_width: Pulse width range = '
# '0-0xffff.')
#
# width_msb = minimum_width >> 8
# width_lsb = minimum_width & 0xff
#
# command = [PrivateConstants.STEPPER_SET_MINIMUM_PULSE_WIDTH, motor_id, width_msb,
# width_lsb]
# self._send_command(command)
#
# def stepper_set_enable_pin(self, motor_id, pin=0xff):
# """
# Sets the enable pin number for stepper drivers.
# 0xFF indicates unused (default).
#
# Otherwise, if a pin is set, the pin will be turned on when
# enableOutputs() is called and switched off when disableOutputs()
# is called.
#
# :param motor_id: 0 - 4
# :param pin: 0-0xff
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_enable_pin: Invalid motor_id.')
#
# if not 0 < pin <= 0xff:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_enable_pin: Pulse width range = '
# '0-0xff.')
# command = [PrivateConstants.STEPPER_SET_ENABLE_PIN, motor_id, pin]
#
# self._send_command(command)
#
# def stepper_set_3_pins_inverted(self, motor_id, direction=False, step=False,
# enable=False):
# """
# Sets the inversion for stepper driver pins.
#
# :param motor_id: 0 - 3
#
# :param direction: True=inverted or False
#
# :param step: True=inverted or False
#
# :param enable: True=inverted or False
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_3_pins_inverted: Invalid motor_id.')
#
# command = [PrivateConstants.STEPPER_SET_3_PINS_INVERTED, motor_id, direction,
# step, enable]
#
# self._send_command(command)
#
# def stepper_set_4_pins_inverted(self, motor_id, pin1_invert=False, pin2_invert=False,
# pin3_invert=False, pin4_invert=False, enable=False):
# """
# Sets the inversion for 2, 3 and 4 wire stepper pins
#
# :param motor_id: 0 - 3
#
# :param pin1_invert: True=inverted or False
#
# :param pin2_invert: True=inverted or False
#
# :param pin3_invert: True=inverted or False
#
# :param pin4_invert: True=inverted or False
#
# :param enable: True=inverted or False
# """
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_set_4_pins_inverted: Invalid motor_id.')
#
# command = [PrivateConstants.STEPPER_SET_4_PINS_INVERTED, motor_id, pin1_invert,
# pin2_invert, pin3_invert, pin4_invert, enable]
#
# self._send_command(command)
#
# def stepper_is_running(self, motor_id, callback):
# """
# Checks to see if the motor is currently running to a target.
#
# Callback return True if the speed is not zero or not at the target position.
#
# :param motor_id: 0-4
#
# :param callback: required callback function to receive report
#
# :return: The current running state returned via the callback as a list:
#
# [REPORT_TYPE=18, motor_id, True or False for running state, time_stamp]
# """
# if not callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(
# 'stepper_is_running: A callback function must be specified.')
#
# if not self.stepper_info_list[motor_id]['instance']:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('stepper_is_running: Invalid motor_id.')
#
# self.stepper_info_list[motor_id]['is_running_callback'] = callback
#
# command = [PrivateConstants.STEPPER_IS_RUNNING, motor_id]
# self._send_command(command)
def _set_pin_mode(self, pin_number, pin_state, differential=0, callback=None):
"""
A private method to set the various pin modes.
:param pin_number: arduino pin number
:param pin_state: INPUT/OUTPUT/ANALOG/PWM/PULLUP
For SERVO use: set_pin_mode_servo
For DHT use: set_pin_mode_dht
:param differential: for analog inputs - threshold
value to be achieved for report to
be generated
:param callback: A reference to a call back function to be
called when pin data value changes
"""
if callback:
if pin_state == PrivateConstants.AT_INPUT:
self.digital_callbacks[pin_number] = callback
elif pin_state == PrivateConstants.AT_INPUT_PULLUP:
self.digital_callbacks[pin_number] = callback
elif pin_state == PrivateConstants.AT_ANALOG:
self.analog_callbacks[pin_number] = callback
else:
print('{} {}'.format('set_pin_mode: callback ignored for '
'pin state:', pin_state))
if pin_state == PrivateConstants.AT_INPUT:
command = [PrivateConstants.SET_PIN_MODE, pin_number,
PrivateConstants.AT_INPUT, 1]
elif pin_state == PrivateConstants.AT_INPUT_PULLUP:
command = [PrivateConstants.SET_PIN_MODE, pin_number,
PrivateConstants.AT_INPUT_PULLUP, 1]
elif pin_state == PrivateConstants.AT_OUTPUT:
command = [PrivateConstants.SET_PIN_MODE, pin_number,
PrivateConstants.AT_OUTPUT]
elif pin_state == PrivateConstants.AT_ANALOG:
command = [PrivateConstants.SET_PIN_MODE, pin_number,
PrivateConstants.AT_ANALOG,
differential >> 8, differential & 0xff, 1]
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('Unknown pin state')
if command:
self._send_command(command)
def shutdown(self):
"""
This method attempts an orderly shutdown
If any exceptions are thrown, they are ignored.
"""
self.shutdown_flag = True
self._stop_threads()
try:
command = [PrivateConstants.STOP_ALL_REPORTS]
self._send_command(command)
time.sleep(.5)
if self.hard_reset_on_shutdown:
self.r4_hard_reset()
if self.transport_address:
try:
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
except Exception:
pass
else:
try:
self.serial_port.reset_input_buffer()
self.serial_port.reset_output_buffer()
self.serial_port.close()
except (RuntimeError, SerialException, OSError):
# ignore error on shutdown
pass
except Exception:
# raise RuntimeError('Shutdown failed - could not send stop streaming
# message')
pass
def sonar_disable(self):
"""
Disable sonar scanning for all sonar sensors
"""
command = [PrivateConstants.SONAR_DISABLE]
self._send_command(command)
def sonar_enable(self):
"""
Enable sonar scanning for all sonar sensors
"""
command = [PrivateConstants.SONAR_ENABLE]
self._send_command(command)
def spi_cs_control(self, chip_select_pin, select):
"""
Control an SPI chip select line
:param chip_select_pin: pin connected to CS
:param select: 0=select, 1=deselect
"""
if not self.spi_enabled:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'spi_cs_control: SPI interface is not enabled.')
if chip_select_pin not in self.cs_pins_enabled:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'spi_cs_control: chip select pin never enabled.')
command = [PrivateConstants.SPI_CS_CONTROL, chip_select_pin, select]
self._send_command(command)
def spi_read_blocking(self, chip_select, register_selection, number_of_bytes_to_read,
call_back=None):
"""
Read the specified number of bytes from the specified SPI port and
call the callback function with the reported data.
:param chip_select: chip select pin
:param register_selection: Register to be selected for read.
:param number_of_bytes_to_read: Number of bytes to read
:param call_back: Required callback function to report spi data as a
result of read command
callback returns a data list:
[SPI_READ_REPORT, chip select pin, SPI Register, count of data bytes read,
data bytes, time-stamp]
SPI_READ_REPORT = 13
"""
if not self.spi_enabled:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'spi_read_blocking: SPI interface is not enabled.')
if not call_back:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('spi_read_blocking: A Callback must be specified')
self.spi_callback = call_back
command = [PrivateConstants.SPI_READ_BLOCKING, chip_select,
number_of_bytes_to_read,
register_selection]
self._send_command(command)
def spi_set_format(self, clock_divisor, bit_order, data_mode):
"""
Configure how the SPI serializes and de-serializes data on the wire.
See Arduino SPI reference materials for details.
:param clock_divisor: 1 - 255
:param bit_order:
LSBFIRST = 0
MSBFIRST = 1 (default)
:param data_mode:
SPI_MODE0 = 0x00 (default)
SPI_MODE1 = 1
SPI_MODE2 = 2
SPI_MODE3 = 3
"""
if not self.spi_enabled:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'spi_set_format: SPI interface is not enabled.')
if not 0 < clock_divisor <= 255:
raise RuntimeError(f'spi_set_format: illegal clock divisor selected.')
if bit_order not in [0, 1]:
raise RuntimeError(f'spi_set_format: illegal bit_order selected.')
if data_mode not in [0, 1, 2, 3]:
raise RuntimeError(f'spi_set_format: illegal data_order selected.')
command = [PrivateConstants.SPI_SET_FORMAT, clock_divisor, bit_order,
data_mode]
self._send_command(command)
def spi_write_blocking(self, chip_select, bytes_to_write):
"""
Write a list of bytes to the SPI device.
:param chip_select: chip select pin
:param bytes_to_write: A list of bytes to write. This must
be in the form of a list.
"""
if not self.spi_enabled:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(f'spi_write_blocking: SPI interface is not enabled.')
if type(bytes_to_write) is not list:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('spi_write_blocking: bytes_to_write must be a list.')
command = [PrivateConstants.SPI_WRITE_BLOCKING, chip_select, len(bytes_to_write)]
for data in bytes_to_write:
command.append(data)
self._send_command(command)
# def set_pin_mode_one_wire(self, pin):
# """
# Initialize the one wire serial bus.
#
# :param pin: Data pin connected to the OneWire device
# """
# if self.reported_features & PrivateConstants.ONEWIRE_FEATURE:
# self.onewire_enabled = True
# command = [PrivateConstants.ONE_WIRE_INIT, pin]
# self._send_command(command)
# else:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'The OneWire feature is disabled in the server.')
#
# def onewire_reset(self, callback=None):
# """
# Reset the onewire device
#
# :param callback: required function to report reset result
#
# callback returns a list:
# [ReportType = 14, Report Subtype = 25, reset result byte,
# timestamp]
# """
# if not self.onewire_enabled:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'onewire_reset: OneWire interface is not enabled.')
# if not callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('onewire_reset: A Callback must be specified')
#
# self.onewire_callback = callback
#
# command = [PrivateConstants.ONE_WIRE_RESET]
# self._send_command(command)
#
# def onewire_select(self, device_address):
# """
# Select a device based on its address
# :param device_address: A bytearray of 8 bytes
# """
# if not self.onewire_enabled:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'onewire_select: OneWire interface is not enabled.')
#
# if type(device_address) is not list:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('onewire_select: device address must be an array of 8 '
# 'bytes.')
#
# if len(device_address) != 8:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('onewire_select: device address must be an array of 8 '
# 'bytes.')
# command = [PrivateConstants.ONE_WIRE_SELECT]
# for data in device_address:
# command.append(data)
# self._send_command(command)
#
# def onewire_skip(self):
# """
# Skip the device selection. This only works if you have a
# single device, but you can avoid searching and use this to
# immediately access your device.
# """
# if not self.onewire_enabled:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'onewire_skip: OneWire interface is not enabled.')
#
# command = [PrivateConstants.ONE_WIRE_SKIP]
# self._send_command(command)
#
# def onewire_write(self, data, power=0):
# """
# Write a byte to the onewire device. If 'power' is one
# then the wire is held high at the end for
# parasitically powered devices. You
# are responsible for eventually de-powering it by calling
# another read or write.
#
# :param data: byte to write.
# :param power: power control (see above)
# """
# if not self.onewire_enabled:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'onewire_write: OneWire interface is not enabled.')
# if 0 < data < 255:
# command = [PrivateConstants.ONE_WIRE_WRITE, data, power]
# self._send_command(command)
# else:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('onewire_write: Data must be no larger than 255')
#
# def onewire_read(self, callback=None):
# """
# Read a byte from the onewire device
# :param callback: required function to report onewire data as a
# result of read command
#
#
# callback returns a data list:
# [ONEWIRE_REPORT, ONEWIRE_READ=29, data byte, time-stamp]
#
# ONEWIRE_REPORT = 14
# """
# if not self.onewire_enabled:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'onewire_read: OneWire interface is not enabled.')
#
# if not callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('onewire_read A Callback must be specified')
#
# self.onewire_callback = callback
#
# command = [PrivateConstants.ONE_WIRE_READ]
# self._send_command(command)
# time.sleep(.2)
#
# def onewire_reset_search(self):
# """
# Begin a new search. The next use of search will begin at the first device
# """
#
# if not self.onewire_enabled:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'onewire_reset_search: OneWire interface is not '
# f'enabled.')
# else:
# command = [PrivateConstants.ONE_WIRE_RESET_SEARCH]
# self._send_command(command)
#
# def onewire_search(self, callback=None):
# """
# Search for the next device. The device address will returned in the callback.
# If a device is found, the 8 byte address is contained in the callback.
# If no more devices are found, the address returned contains all elements set
# to 0xff.
#
# :param callback: required function to report a onewire device address
#
# callback returns a data list:
# [ONEWIRE_REPORT, ONEWIRE_SEARCH=31, 8 byte address, time-stamp]
#
# ONEWIRE_REPORT = 14
# """
# if not self.onewire_enabled:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'onewire_search: OneWire interface is not enabled.')
#
# if not callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('onewire_read A Callback must be specified')
#
# self.onewire_callback = callback
#
# command = [PrivateConstants.ONE_WIRE_SEARCH]
# self._send_command(command)
#
# def onewire_crc8(self, address_list, callback=None):
# """
# Compute a CRC check on an array of data.
# :param address_list:
#
# :param callback: required function to report a onewire device address
#
# callback returns a data list:
# [ONEWIRE_REPORT, ONEWIRE_CRC8=32, CRC, time-stamp]
#
# ONEWIRE_REPORT = 14
#
# """
#
# if not self.onewire_enabled:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError(f'onewire_crc8: OneWire interface is not enabled.')
#
# if not callback:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('onewire_crc8 A Callback must be specified')
#
# if type(address_list) is not list:
# if self.shutdown_on_exception:
# self.shutdown()
# raise RuntimeError('onewire_crc8: address list must be a list.')
#
# self.onewire_callback = callback
#
# address_length = len(address_list)
#
# command = [PrivateConstants.ONE_WIRE_CRC8, address_length - 1]
#
# for data in address_list:
# command.append(data)
#
# self._send_command(command)
def r4_hard_reset(self):
"""
Place the r4 into hard reset
"""
command = [PrivateConstants.RESET, 1]
self._send_command(command)
time.sleep(.5)
command = [PrivateConstants.BOARD_HARD_RESET, 1]
self._send_command(command)
def enable_scroll_message(self, message, scroll_speed=50):
"""
:param message: Message with maximum length of 25
:param scroll_speed: in milliseconds (maximum of 255)
"""
if len(message) > 25:
raise RuntimeError("Scroll message size is maximum of 25 characters.")
if scroll_speed > 255:
raise RuntimeError("Scroll speed maximum of 255 milliseconds.")
message = message.encode()
command = [PrivateConstants.SCROLL_MESSAGE_ON, len(message), scroll_speed]
for x in message:
command.append(x)
self._send_command(command)
def disable_scroll_message(self):
"""
Turn off a scrolling message
"""
command = [PrivateConstants.SCROLL_MESSAGE_OFF]
self._send_command(command)
'''
report message handlers
'''
def _analog_message(self, data):
"""
This is a private message handler method.
It is a message handler for analog messages.
:param data: message data
"""
pin = data[0]
value = (data[1] << 8) + data[2]
# set the current value in the pin structure
time_stamp = time.time()
# self.digital_pins[pin].event_time = time_stamp
if self.analog_callbacks[pin]:
message = [PrivateConstants.ANALOG_REPORT, pin, value, time_stamp]
try:
self.analog_callbacks[pin](message)
except KeyError:
pass
def _dht_report(self, data):
"""
This is the dht report handler method.
:param data: data[0] = report error return
No Errors = 0
Checksum Error = 1
Timeout Error = 2
Invalid Value = 999
data[1] = pin number
data[2] = dht type 11 or 22
data[3] = humidity positivity flag
data[4] = temperature positivity value
data[5] = humidity integer
data[6] = humidity fractional value
data[7] = temperature integer
data[8] = temperature fractional value
"""
if data[0]: # DHT_ERROR
# error report
# data[0] = report sub type, data[1] = pin, data[2] = error message
if self.dht_callbacks[data[1]]:
# Callback 0=DHT REPORT, DHT_ERROR, PIN, Time
message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2],
time.time()]
try:
self.dht_callbacks[data[1]](message)
except KeyError:
pass
else:
# got valid data DHT_DATA
f_humidity = float(data[5] + data[6] / 100)
if data[3]:
f_humidity *= -1.0
f_temperature = float(data[7] + data[8] / 100)
if data[4]:
f_temperature *= -1.0
message = [PrivateConstants.DHT_REPORT, data[0], data[1], data[2],
f_humidity, f_temperature, time.time()]
try:
self.dht_callbacks[data[1]](message)
except KeyError:
pass
def _digital_message(self, data):
"""
This is a private message handler method.
It is a message handler for Digital Messages.
:param data: digital message
"""
pin = data[0]
value = data[1]
time_stamp = time.time()
if self.digital_callbacks[pin]:
message = [PrivateConstants.DIGITAL_REPORT, pin, value, time_stamp]
self.digital_callbacks[pin](message)
def _firmware_message(self, data):
"""
Telemetrix4Arduino firmware version message
:param data: data[0] = major number, data[1] = minor number.
data[2] = patch number
"""
self.firmware_version = [data[0], data[1], data[2]]
def _i2c_read_report(self, data):
"""
Execute callback for i2c reads.
:param data: [I2C_READ_REPORT, i2c_port, number of bytes read, address, register, bytes read..., time-stamp]
"""
# we receive [# data bytes, address, register, data bytes]
# number of bytes of data returned
# data[0] = number of bytes
# data[1] = i2c_port
# data[2] = number of bytes returned
# data[3] = address
# data[4] = register
# data[5] ... all the data bytes
cb_list = [PrivateConstants.I2C_READ_REPORT, data[0], data[1]] + data[2:]
cb_list.append(time.time())
if cb_list[1]:
self.i2c_callback2(cb_list)
else:
self.i2c_callback(cb_list)
def _i2c_too_few(self, data):
"""
I2c reports too few bytes received
:param data: data[0] = device address
"""
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
f'i2c too few bytes received from i2c port {data[0]} i2c address {data[1]}')
def _i2c_too_many(self, data):
"""
I2c reports too few bytes received
:param data: data[0] = device address
"""
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
f'i2c too many bytes received from i2c port {data[0]} i2c address {data[1]}')
def _i_am_here(self, data):
"""
Reply to are_u_there message
:param data: arduino id
"""
self.reported_arduino_id = data[0]
def _spi_report(self, report):
cb_list = [PrivateConstants.SPI_REPORT, report[0]] + report[1:]
cb_list.append(time.time())
self.spi_callback(cb_list)
def _onewire_report(self, report):
cb_list = [PrivateConstants.ONE_WIRE_REPORT, report[0]] + report[1:]
cb_list.append(time.time())
self.onewire_callback(cb_list)
def _report_debug_data(self, data):
"""
Print debug data sent from Arduino
:param data: data[0] is a byte followed by 2
bytes that comprise an integer
:return:
"""
value = (data[1] << 8) + data[2]
print(f'DEBUG ID: {data[0]} Value: {value}')
def _report_loop_data(self, data):
"""
Print data that was looped back
:param data: byte of loop back data
:return:
"""
if self.loop_back_callback:
self.loop_back_callback(data)
def _send_command(self, command):
"""
This is a private utility method.
:param command: command data in the form of a list
"""
# the length of the list is added at the head
command.insert(0, len(command))
send_message = bytes(command)
if self.serial_port:
try:
self.serial_port.write(send_message)
except SerialException:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('write fail in _send_command')
elif self.transport_address:
self.sock.sendall(send_message)
else:
raise RuntimeError('No serial port or ip address set.')
def _servo_unavailable(self, report):
"""
Message if no servos are available for use.
:param report: pin number
"""
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
f'Servo Attach For Pin {report[0]} Failed: No Available Servos')
def _sonar_distance_report(self, report):
"""
:param report: data[0] = trigger pin, data[1] and data[2] = distance
callback report format: [PrivateConstants.SONAR_DISTANCE, trigger_pin, distance_value, time_stamp]
"""
# get callback from pin number
cb = self.sonar_callbacks[report[0]]
# build report data
cb_list = [PrivateConstants.SONAR_DISTANCE, report[0],
((report[1] << 8) + report[2]), time.time()]
cb(cb_list)
def _stepper_distance_to_go_report(self, report):
return # for now
# """
# Report stepper distance to go.
#
# :param report: data[0] = motor_id, data[1] = steps MSB, data[2] = steps byte 1,
# data[3] = steps bytes 2, data[4] = steps LSB
#
# callback report format: [PrivateConstants.STEPPER_DISTANCE_TO_GO, motor_id
# steps, time_stamp]
# """
#
# # get callback
# cb = self.stepper_info_list[report[0]]['distance_to_go_callback']
#
# # isolate the steps bytes and covert list to bytes
# steps = bytes(report[1:])
#
# # get value from steps
# num_steps = int.from_bytes(steps, byteorder='big', signed=True)
#
# cb_list = [PrivateConstants.STEPPER_DISTANCE_TO_GO, report[0], num_steps,
# time.time()]
#
# cb(cb_list)
#
def _stepper_target_position_report(self, report):
return # for now
# """
# Report stepper target position to go.
#
# :param report: data[0] = motor_id, data[1] = target position MSB,
# data[2] = target position byte MSB+1
# data[3] = target position byte MSB+2
# data[4] = target position LSB
#
# callback report format: [PrivateConstants.STEPPER_TARGET_POSITION, motor_id
# target_position, time_stamp]
# """
#
# # get callback
# cb = self.stepper_info_list[report[0]]['target_position_callback']
#
# # isolate the steps bytes and covert list to bytes
# target = bytes(report[1:])
#
# # get value from steps
# target_position = int.from_bytes(target, byteorder='big', signed=True)
#
# cb_list = [PrivateConstants.STEPPER_TARGET_POSITION, report[0], target_position,
# time.time()]
#
# cb(cb_list)
#
def _stepper_current_position_report(self, report):
return # for now
# """
# Report stepper current position.
#
# :param report: data[0] = motor_id, data[1] = current position MSB,
# data[2] = current position byte MSB+1
# data[3] = current position byte MSB+2
# data[4] = current position LSB
#
# callback report format: [PrivateConstants.STEPPER_CURRENT_POSITION, motor_id
# current_position, time_stamp]
# """
#
# # get callback
# cb = self.stepper_info_list[report[0]]['current_position_callback']
#
# # isolate the steps bytes and covert list to bytes
# position = bytes(report[1:])
#
# # get value from steps
# current_position = int.from_bytes(position, byteorder='big', signed=True)
#
# cb_list = [PrivateConstants.STEPPER_CURRENT_POSITION, report[0], current_position,
# time.time()]
#
# cb(cb_list)
#
def _stepper_is_running_report(self, report):
return # for now
# """
# Report if the motor is currently running
#
# :param report: data[0] = motor_id, True if motor is running or False if it is not.
#
# callback report format: [18, motor_id,
# running_state, time_stamp]
# """
#
# # get callback
# cb = self.stepper_info_list[report[0]]['is_running_callback']
#
# cb_list = [PrivateConstants.STEPPER_RUNNING_REPORT, report[0], time.time()]
#
# cb(cb_list)
#
def _stepper_run_complete_report(self, report):
return # for now
# """
# The motor completed it motion
#
# :param report: data[0] = motor_id
#
# callback report format: [PrivateConstants.STEPPER_RUN_COMPLETE_REPORT, motor_id,
# time_stamp]
# """
#
# # get callback
# cb = self.stepper_info_list[report[0]]['motion_complete_callback']
#
# cb_list = [PrivateConstants.STEPPER_RUN_COMPLETE_REPORT, report[0],
# time.time()]
#
# cb(cb_list)
def _features_report(self, report):
self.reported_features = report[0]
def _run_threads(self):
self.run_event.set()
def _is_running(self):
return self.run_event.is_set()
def _stop_threads(self):
self.run_event.clear()
def _reporter(self):
"""
This is the reporter thread. It continuously pulls data from
the deque. When a full message is detected, that message is
processed.
"""
self.run_event.wait()
while self._is_running() and not self.shutdown_flag:
if len(self.the_deque):
# response_data will be populated with the received data for the report
response_data = []
packet_length = self.the_deque.popleft()
# print(f'packet_length {packet_length}')
if packet_length:
# get all the data for the report and place it into response_data
for i in range(packet_length):
while not len(self.the_deque):
time.sleep(self.sleep_tune)
data = self.the_deque.popleft()
response_data.append(data)
# print(f'response_data {response_data}')
# get the report type and look up its dispatch method
# here we pop the report type off of response_data
report_type = response_data.pop(0)
# print(f' reported type {report_type}')
# retrieve the report handler from the dispatch table
dispatch_entry = self.report_dispatch.get(report_type)
# if there is additional data for the report,
# it will be contained in response_data
# noinspection PyArgumentList
dispatch_entry(response_data)
continue
else:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError(
'A report with a packet length of zero was received.')
else:
time.sleep(self.sleep_tune)
def _serial_receiver(self):
"""
Thread to continuously check for incoming data.
When a byte comes in, place it onto the deque.
"""
self.run_event.wait()
# Don't start this thread if using a tcp/ip transport
if self.transport_address:
return
while self._is_running() and not self.shutdown_flag:
# we can get an OSError: [Errno9] Bad file descriptor when shutting down
# just ignore it
try:
if self.serial_port.inWaiting():
c = self.serial_port.read()
self.the_deque.append(ord(c))
# print(ord(c))
else:
time.sleep(self.sleep_tune)
# continue
except OSError:
pass
def _tcp_receiver(self):
"""
Thread to continuously check for incoming data.
When a byte comes in, place it onto the deque.
"""
self.run_event.wait()
# Start this thread only if transport_address is set
if self.transport_address:
while self._is_running() and not self.shutdown_flag:
try:
payload = self.sock.recv(1)
self.the_deque.append(ord(payload))
except Exception:
pass
else:
return
|