forked from spthm/asyncpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncpio.py
5902 lines (4587 loc) · 166 KB
/
asyncpio.py
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
"""
asyncpio is a Python module for the Raspberry which talks to
the pigpio daemon to allow control of the general purpose
input outputs (GPIO).
[http://abyz.me.uk/rpi/pigpio/python.html]
*Features*
o the pigpio Python module can run on Windows, Macs, or Linux
o controls one or more Pi's
o hardware timed PWM on any of GPIO 0-31
o hardware timed servo pulses on any of GPIO 0-31
o callbacks when any of GPIO 0-31 change state
o creating and transmitting precisely timed waveforms
o reading/writing GPIO and setting their modes
o wrappers for I2C, SPI, and serial links
o creating and running scripts on the pigpio daemon
*GPIO*
ALL GPIO are identified by their Broadcom number.
*Notes*
Transmitted waveforms are accurate to a microsecond.
Callback level changes are time-stamped and will be
accurate to within a few microseconds.
*Settings*
A number of settings are determined when the pigpio daemon is started.
o the sample rate (1, 2, 4, 5, 8, or 10 us, default 5 us).
o the set of GPIO which may be updated (generally written to). The
default set is those available on the Pi board revision.
o the available PWM frequencies (see [*set_PWM_frequency*]).
*Exceptions*
By default a fatal exception is raised if you pass an invalid
argument to a pigpio function.
If you wish to handle the returned status yourself you should set
asyncpio.exceptions to False.
You may prefer to check the returned status in only a few parts
of your code. In that case do the following:
...
asyncpio.exceptions = False
# Code where you want to test the error status.
asyncpio.exceptions = True
...
*Usage*
This module uses the services of the C pigpio library. pigpio
must be running on the Pi(s) whose GPIO are to be manipulated.
The normal way to start pigpio is as a daemon (during system
start).
sudo pigpiod
Your Python program must import asyncpio and create one or more
instances of the asyncpio.pi class. This class gives access to
a specified Pi's GPIO.
...
pi1 = asyncpio.pi()
await pi1.connect() # pi1 accesses the local Pi's GPIO
pi2 = asyncpio.pi()
await pi2.connect('tom') # pi2 accesses tom's GPIO
pi3 = asyncpio.pi()
await pi3.connect('dick') # pi3 accesses dick's GPIO
await pi1.write(4, 0) # set local Pi's GPIO 4 low
await pi2.write(4, 1) # set tom's GPIO 4 to high
await pi3.read(4) # get level of dick's GPIO 4
...
The later example code snippets assume that pi is an instance of
the asyncpio.pi class.
OVERVIEW
ESSENTIAL
asyncpio.pi Initialise Pi connection
stop Stop a Pi connection
BASIC
set_mode Set a GPIO mode
get_mode Get a GPIO mode
set_pull_up_down Set/clear GPIO pull up/down resistor
read Read a GPIO
write Write a GPIO
PWM_(overrides_servo_commands_on_same_GPIO)
set_PWM_dutycycle Start/stop PWM pulses on a GPIO
set_PWM_frequency Set PWM frequency of a GPIO
set_PWM_range Configure PWM range of a GPIO
get_PWM_dutycycle Get PWM dutycycle set on a GPIO
get_PWM_frequency Get PWM frequency of a GPIO
get_PWM_range Get configured PWM range of a GPIO
get_PWM_real_range Get underlying PWM range for a GPIO
Servo_(overrides_PWM_commands_on_same_GPIO)
set_servo_pulsewidth Start/Stop servo pulses on a GPIO
get_servo_pulsewidth Get servo pulsewidth set on a GPIO
INTERMEDIATE
gpio_trigger Send a trigger pulse to a GPIO
set_watchdog Set a watchdog on a GPIO
read_bank_1 Read all bank 1 GPIO
read_bank_2 Read all bank 2 GPIO
clear_bank_1 Clear selected GPIO in bank 1
clear_bank_2 Clear selected GPIO in bank 2
set_bank_1 Set selected GPIO in bank 1
set_bank_2 Set selected GPIO in bank 2
callback Create GPIO level change callback
wait_for_edge Wait for GPIO level change
ADVANCED
notify_open Request a notification handle
notify_begin Start notifications for selected GPIO
notify_pause Pause notifications
notify_close Close a notification
hardware_clock Start hardware clock on supported GPIO
hardware_PWM Start hardware PWM on supported GPIO
set_glitch_filter Set a glitch filter on a GPIO
set_noise_filter Set a noise filter on a GPIO
set_pad_strength Sets a pads drive strength
get_pad_strength Gets a pads drive strength
shell Executes a shell command
Custom
custom_1 User custom function 1
custom_2 User custom function 2
Events
event_callback Sets a callback for an event
event_trigger Triggers an event
wait_for_event Wait for an event
Scripts
store_script Store a script
run_script Run a stored script
update_script Set a scripts parameters
script_status Get script status and parameters
stop_script Stop a running script
delete_script Delete a stored script
I2C
i2c_open Opens an I2C device
i2c_close Closes an I2C device
i2c_write_quick SMBus write quick
i2c_read_byte SMBus read byte
i2c_write_byte SMBus write byte
i2c_read_byte_data SMBus read byte data
i2c_write_byte_data SMBus write byte data
i2c_read_word_data SMBus read word data
i2c_write_word_data SMBus write word data
i2c_read_block_data SMBus read block data
i2c_write_block_data SMBus write block data
i2c_read_i2c_block_data SMBus read I2C block data
i2c_write_i2c_block_data SMBus write I2C block data
i2c_read_device Reads the raw I2C device
i2c_write_device Writes the raw I2C device
i2c_process_call SMBus process call
i2c_block_process_call SMBus block process call
i2c_zip Performs multiple I2C transactions
I2C_BIT_BANG
bb_i2c_open Opens GPIO for bit banging I2C
bb_i2c_close Closes GPIO for bit banging I2C
bb_i2c_zip Performs multiple bit banged I2C transactions
I2C/SPI_SLAVE
bsc_xfer I2C/SPI as slave transfer
bsc_i2c I2C as slave transfer
SERIAL
serial_open Opens a serial device
serial_close Closes a serial device
serial_read_byte Reads a byte from a serial device
serial_write_byte Writes a byte to a serial device
serial_read Reads bytes from a serial device
serial_write Writes bytes to a serial device
serial_data_available Returns number of bytes ready to be read
SERIAL_BIT_BANG_(read_only)
bb_serial_read_open Open a GPIO for bit bang serial reads
bb_serial_read_close Close a GPIO for bit bang serial reads
bb_serial_invert Invert serial logic (1 invert, 0 normal)
bb_serial_read Read bit bang serial data from a GPIO
SPI
spi_open Opens a SPI device
spi_close Closes a SPI device
spi_read Reads bytes from a SPI device
spi_write Writes bytes to a SPI device
spi_xfer Transfers bytes with a SPI device
SPI_BIT_BANG
bb_spi_open Opens GPIO for bit banging SPI
bb_spi_close Closes GPIO for bit banging SPI
bb_spi_xfer Transfers bytes with bit banging SPI
FILES
file_open Opens a file
file_close Closes a file
file_read Reads bytes from a file
file_write Writes bytes to a file
file_seek Seeks to a position within a file
file_list List files which match a pattern
WAVES
wave_clear Deletes all waveforms
wave_add_new Starts a new waveform
wave_add_generic Adds a series of pulses to the waveform
wave_add_serial Adds serial data to the waveform
wave_create Creates a waveform from added data
wave_create_and_pad Creates a waveform of fixed size from added data
wave_delete Deletes a waveform
wave_send_once Transmits a waveform once
wave_send_repeat Transmits a waveform repeatedly
wave_send_using_mode Transmits a waveform in the chosen mode
wave_chain Transmits a chain of waveforms
wave_tx_at Returns the current transmitting waveform
wave_tx_busy Checks to see if a waveform has ended
wave_tx_stop Aborts the current waveform
wave_get_cbs Length in cbs of the current waveform
wave_get_max_cbs Absolute maximum allowed cbs
wave_get_micros Length in microseconds of the current waveform
wave_get_max_micros Absolute maximum allowed micros
wave_get_pulses Length in pulses of the current waveform
wave_get_max_pulses Absolute maximum allowed pulses
UTILITIES
get_current_tick Get current tick (microseconds)
get_hardware_revision Get hardware revision
get_pigpio_version Get the pigpio version
asyncpio.error_text Gets error text from error number
asyncpio.tickDiff Returns difference between two ticks
"""
import sys
import socket
import struct
import asyncio
import os
import atexit
__version__ = '0.78.0' # asyncpio version, sync minor number to pigpio version
VERSION = "1.78" # Upstream pigpio version.
exceptions = True
# GPIO levels
OFF = 0
LOW = 0
CLEAR = 0
ON = 1
HIGH = 1
SET = 1
TIMEOUT = 2
# GPIO edges
RISING_EDGE = 0
FALLING_EDGE = 1
EITHER_EDGE = 2
# GPIO modes
INPUT = 0
OUTPUT = 1
ALT0 = 4
ALT1 = 5
ALT2 = 6
ALT3 = 7
ALT4 = 3
ALT5 = 2
# GPIO Pull Up Down
PUD_OFF = 0
PUD_DOWN = 1
PUD_UP = 2
# script run status
PI_SCRIPT_INITING=0
PI_SCRIPT_HALTED =1
PI_SCRIPT_RUNNING=2
PI_SCRIPT_WAITING=3
PI_SCRIPT_FAILED =4
# notification flags
NTFY_FLAGS_EVENT = (1 << 7)
NTFY_FLAGS_ALIVE = (1 << 6)
NTFY_FLAGS_WDOG = (1 << 5)
NTFY_FLAGS_GPIO = 31
# wave modes
WAVE_MODE_ONE_SHOT =0
WAVE_MODE_REPEAT =1
WAVE_MODE_ONE_SHOT_SYNC=2
WAVE_MODE_REPEAT_SYNC =3
WAVE_NOT_FOUND = 9998 # Transmitted wave not found.
NO_TX_WAVE = 9999 # No wave being transmitted.
FILE_READ=1
FILE_WRITE=2
FILE_RW=3
FILE_APPEND=4
FILE_CREATE=8
FILE_TRUNC=16
FROM_START=0
FROM_CURRENT=1
FROM_END=2
SPI_MODE_0 = 0
SPI_MODE_1 = 1
SPI_MODE_2 = 2
SPI_MODE_3 = 3
SPI_CPHA = 1 << 0
SPI_CPOL = 1 << 1
SPI_CS_HIGH_ACTIVE = 1 << 2
SPI_TX_LSBFIRST = 1 << 14
SPI_RX_LSBFIRST = 1 << 15
EVENT_BSC = 31
_SOCK_CMD_LEN = 16
# pigpio command numbers
_PI_CMD_MODES= 0
_PI_CMD_MODEG= 1
_PI_CMD_PUD= 2
_PI_CMD_READ= 3
_PI_CMD_WRITE= 4
_PI_CMD_PWM= 5
_PI_CMD_PRS= 6
_PI_CMD_PFS= 7
_PI_CMD_SERVO= 8
_PI_CMD_WDOG= 9
_PI_CMD_BR1= 10
_PI_CMD_BR2= 11
_PI_CMD_BC1= 12
_PI_CMD_BC2= 13
_PI_CMD_BS1= 14
_PI_CMD_BS2= 15
_PI_CMD_TICK= 16
_PI_CMD_HWVER=17
_PI_CMD_NO= 18
_PI_CMD_NB= 19
_PI_CMD_NP= 20
_PI_CMD_NC= 21
_PI_CMD_PRG= 22
_PI_CMD_PFG= 23
_PI_CMD_PRRG= 24
_PI_CMD_HELP= 25
_PI_CMD_PIGPV=26
_PI_CMD_WVCLR=27
_PI_CMD_WVAG= 28
_PI_CMD_WVAS= 29
_PI_CMD_WVGO= 30
_PI_CMD_WVGOR=31
_PI_CMD_WVBSY=32
_PI_CMD_WVHLT=33
_PI_CMD_WVSM= 34
_PI_CMD_WVSP= 35
_PI_CMD_WVSC= 36
_PI_CMD_TRIG= 37
_PI_CMD_PROC= 38
_PI_CMD_PROCD=39
_PI_CMD_PROCR=40
_PI_CMD_PROCS=41
_PI_CMD_SLRO= 42
_PI_CMD_SLR= 43
_PI_CMD_SLRC= 44
_PI_CMD_PROCP=45
_PI_CMD_MICRO=46
_PI_CMD_MILLI=47
_PI_CMD_PARSE=48
_PI_CMD_WVCRE=49
_PI_CMD_WVDEL=50
_PI_CMD_WVTX =51
_PI_CMD_WVTXR=52
_PI_CMD_WVNEW=53
_PI_CMD_I2CO =54
_PI_CMD_I2CC =55
_PI_CMD_I2CRD=56
_PI_CMD_I2CWD=57
_PI_CMD_I2CWQ=58
_PI_CMD_I2CRS=59
_PI_CMD_I2CWS=60
_PI_CMD_I2CRB=61
_PI_CMD_I2CWB=62
_PI_CMD_I2CRW=63
_PI_CMD_I2CWW=64
_PI_CMD_I2CRK=65
_PI_CMD_I2CWK=66
_PI_CMD_I2CRI=67
_PI_CMD_I2CWI=68
_PI_CMD_I2CPC=69
_PI_CMD_I2CPK=70
_PI_CMD_SPIO =71
_PI_CMD_SPIC =72
_PI_CMD_SPIR =73
_PI_CMD_SPIW =74
_PI_CMD_SPIX =75
_PI_CMD_SERO =76
_PI_CMD_SERC =77
_PI_CMD_SERRB=78
_PI_CMD_SERWB=79
_PI_CMD_SERR =80
_PI_CMD_SERW =81
_PI_CMD_SERDA=82
_PI_CMD_GDC =83
_PI_CMD_GPW =84
_PI_CMD_HC =85
_PI_CMD_HP =86
_PI_CMD_CF1 =87
_PI_CMD_CF2 =88
_PI_CMD_NOIB =99
_PI_CMD_BI2CC=89
_PI_CMD_BI2CO=90
_PI_CMD_BI2CZ=91
_PI_CMD_I2CZ =92
_PI_CMD_WVCHA=93
_PI_CMD_SLRI =94
_PI_CMD_CGI =95
_PI_CMD_CSI =96
_PI_CMD_FG =97
_PI_CMD_FN =98
_PI_CMD_WVTXM=100
_PI_CMD_WVTAT=101
_PI_CMD_PADS =102
_PI_CMD_PADG =103
_PI_CMD_FO =104
_PI_CMD_FC =105
_PI_CMD_FR =106
_PI_CMD_FW =107
_PI_CMD_FS =108
_PI_CMD_FL =109
_PI_CMD_SHELL=110
_PI_CMD_BSPIC=111
_PI_CMD_BSPIO=112
_PI_CMD_BSPIX=113
_PI_CMD_BSCX =114
_PI_CMD_EVM =115
_PI_CMD_EVT =116
_PI_CMD_PROCU=117
_PI_CMD_WVCAP=118
# pigpio error numbers
_PI_INIT_FAILED =-1
PI_BAD_USER_GPIO =-2
PI_BAD_GPIO =-3
PI_BAD_MODE =-4
PI_BAD_LEVEL =-5
PI_BAD_PUD =-6
PI_BAD_PULSEWIDTH =-7
PI_BAD_DUTYCYCLE =-8
_PI_BAD_TIMER =-9
_PI_BAD_MS =-10
_PI_BAD_TIMETYPE =-11
_PI_BAD_SECONDS =-12
_PI_BAD_MICROS =-13
_PI_TIMER_FAILED =-14
PI_BAD_WDOG_TIMEOUT =-15
_PI_NO_ALERT_FUNC =-16
_PI_BAD_CLK_PERIPH =-17
_PI_BAD_CLK_SOURCE =-18
_PI_BAD_CLK_MICROS =-19
_PI_BAD_BUF_MILLIS =-20
PI_BAD_DUTYRANGE =-21
_PI_BAD_SIGNUM =-22
_PI_BAD_PATHNAME =-23
PI_NO_HANDLE =-24
PI_BAD_HANDLE =-25
_PI_BAD_IF_FLAGS =-26
_PI_BAD_CHANNEL =-27
_PI_BAD_PRIM_CHANNEL=-27
_PI_BAD_SOCKET_PORT =-28
_PI_BAD_FIFO_COMMAND=-29
_PI_BAD_SECO_CHANNEL=-30
_PI_NOT_INITIALISED =-31
_PI_INITIALISED =-32
_PI_BAD_WAVE_MODE =-33
_PI_BAD_CFG_INTERNAL=-34
PI_BAD_WAVE_BAUD =-35
PI_TOO_MANY_PULSES =-36
PI_TOO_MANY_CHARS =-37
PI_NOT_SERIAL_GPIO =-38
_PI_BAD_SERIAL_STRUC=-39
_PI_BAD_SERIAL_BUF =-40
PI_NOT_PERMITTED =-41
PI_SOME_PERMITTED =-42
PI_BAD_WVSC_COMMND =-43
PI_BAD_WVSM_COMMND =-44
PI_BAD_WVSP_COMMND =-45
PI_BAD_PULSELEN =-46
PI_BAD_SCRIPT =-47
PI_BAD_SCRIPT_ID =-48
PI_BAD_SER_OFFSET =-49
PI_GPIO_IN_USE =-50
PI_BAD_SERIAL_COUNT =-51
PI_BAD_PARAM_NUM =-52
PI_DUP_TAG =-53
PI_TOO_MANY_TAGS =-54
PI_BAD_SCRIPT_CMD =-55
PI_BAD_VAR_NUM =-56
PI_NO_SCRIPT_ROOM =-57
PI_NO_MEMORY =-58
PI_SOCK_READ_FAILED =-59
PI_SOCK_WRIT_FAILED =-60
PI_TOO_MANY_PARAM =-61
PI_SCRIPT_NOT_READY =-62
PI_BAD_TAG =-63
PI_BAD_MICS_DELAY =-64
PI_BAD_MILS_DELAY =-65
PI_BAD_WAVE_ID =-66
PI_TOO_MANY_CBS =-67
PI_TOO_MANY_OOL =-68
PI_EMPTY_WAVEFORM =-69
PI_NO_WAVEFORM_ID =-70
PI_I2C_OPEN_FAILED =-71
PI_SER_OPEN_FAILED =-72
PI_SPI_OPEN_FAILED =-73
PI_BAD_I2C_BUS =-74
PI_BAD_I2C_ADDR =-75
PI_BAD_SPI_CHANNEL =-76
PI_BAD_FLAGS =-77
PI_BAD_SPI_SPEED =-78
PI_BAD_SER_DEVICE =-79
PI_BAD_SER_SPEED =-80
PI_BAD_PARAM =-81
PI_I2C_WRITE_FAILED =-82
PI_I2C_READ_FAILED =-83
PI_BAD_SPI_COUNT =-84
PI_SER_WRITE_FAILED =-85
PI_SER_READ_FAILED =-86
PI_SER_READ_NO_DATA =-87
PI_UNKNOWN_COMMAND =-88
PI_SPI_XFER_FAILED =-89
_PI_BAD_POINTER =-90
PI_NO_AUX_SPI =-91
PI_NOT_PWM_GPIO =-92
PI_NOT_SERVO_GPIO =-93
PI_NOT_HCLK_GPIO =-94
PI_NOT_HPWM_GPIO =-95
PI_BAD_HPWM_FREQ =-96
PI_BAD_HPWM_DUTY =-97
PI_BAD_HCLK_FREQ =-98
PI_BAD_HCLK_PASS =-99
PI_HPWM_ILLEGAL =-100
PI_BAD_DATABITS =-101
PI_BAD_STOPBITS =-102
PI_MSG_TOOBIG =-103
PI_BAD_MALLOC_MODE =-104
_PI_TOO_MANY_SEGS =-105
_PI_BAD_I2C_SEG =-106
PI_BAD_SMBUS_CMD =-107
PI_NOT_I2C_GPIO =-108
PI_BAD_I2C_WLEN =-109
PI_BAD_I2C_RLEN =-110
PI_BAD_I2C_CMD =-111
PI_BAD_I2C_BAUD =-112
PI_CHAIN_LOOP_CNT =-113
PI_BAD_CHAIN_LOOP =-114
PI_CHAIN_COUNTER =-115
PI_BAD_CHAIN_CMD =-116
PI_BAD_CHAIN_DELAY =-117
PI_CHAIN_NESTING =-118
PI_CHAIN_TOO_BIG =-119
PI_DEPRECATED =-120
PI_BAD_SER_INVERT =-121
_PI_BAD_EDGE =-122
_PI_BAD_ISR_INIT =-123
PI_BAD_FOREVER =-124
PI_BAD_FILTER =-125
PI_BAD_PAD =-126
PI_BAD_STRENGTH =-127
PI_FIL_OPEN_FAILED =-128
PI_BAD_FILE_MODE =-129
PI_BAD_FILE_FLAG =-130
PI_BAD_FILE_READ =-131
PI_BAD_FILE_WRITE =-132
PI_FILE_NOT_ROPEN =-133
PI_FILE_NOT_WOPEN =-134
PI_BAD_FILE_SEEK =-135
PI_NO_FILE_MATCH =-136
PI_NO_FILE_ACCESS =-137
PI_FILE_IS_A_DIR =-138
PI_BAD_SHELL_STATUS =-139
PI_BAD_SCRIPT_NAME =-140
PI_BAD_SPI_BAUD =-141
PI_NOT_SPI_GPIO =-142
PI_BAD_EVENT_ID =-143
PI_CMD_INTERRUPTED =-144
PI_NOT_ON_BCM2711 =-145
PI_ONLY_ON_BCM2711 =-146
# pigpio error text
_errors=[
[_PI_INIT_FAILED , "pigpio initialisation failed"],
[PI_BAD_USER_GPIO , "GPIO not 0-31"],
[PI_BAD_GPIO , "GPIO not 0-53"],
[PI_BAD_MODE , "mode not 0-7"],
[PI_BAD_LEVEL , "level not 0-1"],
[PI_BAD_PUD , "pud not 0-2"],
[PI_BAD_PULSEWIDTH , "pulsewidth not 0 or 500-2500"],
[PI_BAD_DUTYCYCLE , "dutycycle not 0-range (default 255)"],
[_PI_BAD_TIMER , "timer not 0-9"],
[_PI_BAD_MS , "ms not 10-60000"],
[_PI_BAD_TIMETYPE , "timetype not 0-1"],
[_PI_BAD_SECONDS , "seconds < 0"],
[_PI_BAD_MICROS , "micros not 0-999999"],
[_PI_TIMER_FAILED , "gpioSetTimerFunc failed"],
[PI_BAD_WDOG_TIMEOUT , "timeout not 0-60000"],
[_PI_NO_ALERT_FUNC , "DEPRECATED"],
[_PI_BAD_CLK_PERIPH , "clock peripheral not 0-1"],
[_PI_BAD_CLK_SOURCE , "DEPRECATED"],
[_PI_BAD_CLK_MICROS , "clock micros not 1, 2, 4, 5, 8, or 10"],
[_PI_BAD_BUF_MILLIS , "buf millis not 100-10000"],
[PI_BAD_DUTYRANGE , "dutycycle range not 25-40000"],
[_PI_BAD_SIGNUM , "signum not 0-63"],
[_PI_BAD_PATHNAME , "can't open pathname"],
[PI_NO_HANDLE , "no handle available"],
[PI_BAD_HANDLE , "unknown handle"],
[_PI_BAD_IF_FLAGS , "ifFlags > 4"],
[_PI_BAD_CHANNEL , "DMA channel not 0-14"],
[_PI_BAD_SOCKET_PORT , "socket port not 1024-30000"],
[_PI_BAD_FIFO_COMMAND , "unknown fifo command"],
[_PI_BAD_SECO_CHANNEL , "DMA secondary channel not 0-14"],
[_PI_NOT_INITIALISED , "function called before gpioInitialise"],
[_PI_INITIALISED , "function called after gpioInitialise"],
[_PI_BAD_WAVE_MODE , "waveform mode not 0-1"],
[_PI_BAD_CFG_INTERNAL , "bad parameter in gpioCfgInternals call"],
[PI_BAD_WAVE_BAUD , "baud rate not 50-250000(RX)/1000000(TX)"],
[PI_TOO_MANY_PULSES , "waveform has too many pulses"],
[PI_TOO_MANY_CHARS , "waveform has too many chars"],
[PI_NOT_SERIAL_GPIO , "no bit bang serial read in progress on GPIO"],
[PI_NOT_PERMITTED , "no permission to update GPIO"],
[PI_SOME_PERMITTED , "no permission to update one or more GPIO"],
[PI_BAD_WVSC_COMMND , "bad WVSC subcommand"],
[PI_BAD_WVSM_COMMND , "bad WVSM subcommand"],
[PI_BAD_WVSP_COMMND , "bad WVSP subcommand"],
[PI_BAD_PULSELEN , "trigger pulse length not 1-100"],
[PI_BAD_SCRIPT , "invalid script"],
[PI_BAD_SCRIPT_ID , "unknown script id"],
[PI_BAD_SER_OFFSET , "add serial data offset > 30 minute"],
[PI_GPIO_IN_USE , "GPIO already in use"],
[PI_BAD_SERIAL_COUNT , "must read at least a byte at a time"],
[PI_BAD_PARAM_NUM , "script parameter id not 0-9"],
[PI_DUP_TAG , "script has duplicate tag"],
[PI_TOO_MANY_TAGS , "script has too many tags"],
[PI_BAD_SCRIPT_CMD , "illegal script command"],
[PI_BAD_VAR_NUM , "script variable id not 0-149"],
[PI_NO_SCRIPT_ROOM , "no more room for scripts"],
[PI_NO_MEMORY , "can't allocate temporary memory"],
[PI_SOCK_READ_FAILED , "socket read failed"],
[PI_SOCK_WRIT_FAILED , "socket write failed"],
[PI_TOO_MANY_PARAM , "too many script parameters (> 10)"],
[PI_SCRIPT_NOT_READY , "script initialising"],
[PI_BAD_TAG , "script has unresolved tag"],
[PI_BAD_MICS_DELAY , "bad MICS delay (too large)"],
[PI_BAD_MILS_DELAY , "bad MILS delay (too large)"],
[PI_BAD_WAVE_ID , "non existent wave id"],
[PI_TOO_MANY_CBS , "No more CBs for waveform"],
[PI_TOO_MANY_OOL , "No more OOL for waveform"],
[PI_EMPTY_WAVEFORM , "attempt to create an empty waveform"],
[PI_NO_WAVEFORM_ID , "No more waveform ids"],
[PI_I2C_OPEN_FAILED , "can't open I2C device"],
[PI_SER_OPEN_FAILED , "can't open serial device"],
[PI_SPI_OPEN_FAILED , "can't open SPI device"],
[PI_BAD_I2C_BUS , "bad I2C bus"],
[PI_BAD_I2C_ADDR , "bad I2C address"],
[PI_BAD_SPI_CHANNEL , "bad SPI channel"],
[PI_BAD_FLAGS , "bad i2c/spi/ser open flags"],
[PI_BAD_SPI_SPEED , "bad SPI speed"],
[PI_BAD_SER_DEVICE , "bad serial device name"],
[PI_BAD_SER_SPEED , "bad serial baud rate"],
[PI_BAD_PARAM , "bad i2c/spi/ser parameter"],
[PI_I2C_WRITE_FAILED , "I2C write failed"],
[PI_I2C_READ_FAILED , "I2C read failed"],
[PI_BAD_SPI_COUNT , "bad SPI count"],
[PI_SER_WRITE_FAILED , "ser write failed"],
[PI_SER_READ_FAILED , "ser read failed"],
[PI_SER_READ_NO_DATA , "ser read no data available"],
[PI_UNKNOWN_COMMAND , "unknown command"],
[PI_SPI_XFER_FAILED , "SPI xfer/read/write failed"],
[_PI_BAD_POINTER , "bad (NULL) pointer"],
[PI_NO_AUX_SPI , "no auxiliary SPI on Pi A or B"],
[PI_NOT_PWM_GPIO , "GPIO is not in use for PWM"],
[PI_NOT_SERVO_GPIO , "GPIO is not in use for servo pulses"],
[PI_NOT_HCLK_GPIO , "GPIO has no hardware clock"],
[PI_NOT_HPWM_GPIO , "GPIO has no hardware PWM"],
[PI_BAD_HPWM_FREQ , "invalid hardware PWM frequency"],
[PI_BAD_HPWM_DUTY , "hardware PWM dutycycle not 0-1M"],
[PI_BAD_HCLK_FREQ , "invalid hardware clock frequency"],
[PI_BAD_HCLK_PASS , "need password to use hardware clock 1"],
[PI_HPWM_ILLEGAL , "illegal, PWM in use for main clock"],
[PI_BAD_DATABITS , "serial data bits not 1-32"],
[PI_BAD_STOPBITS , "serial (half) stop bits not 2-8"],
[PI_MSG_TOOBIG , "socket/pipe message too big"],
[PI_BAD_MALLOC_MODE , "bad memory allocation mode"],
[_PI_TOO_MANY_SEGS , "too many I2C transaction segments"],
[_PI_BAD_I2C_SEG , "an I2C transaction segment failed"],
[PI_BAD_SMBUS_CMD , "SMBus command not supported"],
[PI_NOT_I2C_GPIO , "no bit bang I2C in progress on GPIO"],
[PI_BAD_I2C_WLEN , "bad I2C write length"],
[PI_BAD_I2C_RLEN , "bad I2C read length"],
[PI_BAD_I2C_CMD , "bad I2C command"],
[PI_BAD_I2C_BAUD , "bad I2C baud rate, not 50-500k"],
[PI_CHAIN_LOOP_CNT , "bad chain loop count"],
[PI_BAD_CHAIN_LOOP , "empty chain loop"],
[PI_CHAIN_COUNTER , "too many chain counters"],
[PI_BAD_CHAIN_CMD , "bad chain command"],
[PI_BAD_CHAIN_DELAY , "bad chain delay micros"],
[PI_CHAIN_NESTING , "chain counters nested too deeply"],
[PI_CHAIN_TOO_BIG , "chain is too long"],
[PI_DEPRECATED , "deprecated function removed"],
[PI_BAD_SER_INVERT , "bit bang serial invert not 0 or 1"],
[_PI_BAD_EDGE , "bad ISR edge value, not 0-2"],
[_PI_BAD_ISR_INIT , "bad ISR initialisation"],
[PI_BAD_FOREVER , "loop forever must be last chain command"],
[PI_BAD_FILTER , "bad filter parameter"],
[PI_BAD_PAD , "bad pad number"],
[PI_BAD_STRENGTH , "bad pad drive strength"],
[PI_FIL_OPEN_FAILED , "file open failed"],
[PI_BAD_FILE_MODE , "bad file mode"],
[PI_BAD_FILE_FLAG , "bad file flag"],
[PI_BAD_FILE_READ , "bad file read"],
[PI_BAD_FILE_WRITE , "bad file write"],
[PI_FILE_NOT_ROPEN , "file not open for read"],
[PI_FILE_NOT_WOPEN , "file not open for write"],
[PI_BAD_FILE_SEEK , "bad file seek"],
[PI_NO_FILE_MATCH , "no files match pattern"],
[PI_NO_FILE_ACCESS , "no permission to access file"],
[PI_FILE_IS_A_DIR , "file is a directory"],
[PI_BAD_SHELL_STATUS , "bad shell return status"],
[PI_BAD_SCRIPT_NAME , "bad script name"],
[PI_BAD_SPI_BAUD , "bad SPI baud rate, not 50-500k"],
[PI_NOT_SPI_GPIO , "no bit bang SPI in progress on GPIO"],
[PI_BAD_EVENT_ID , "bad event id"],
[PI_CMD_INTERRUPTED , "pigpio command interrupted"],
[PI_NOT_ON_BCM2711 , "not available on BCM2711"],
[PI_ONLY_ON_BCM2711 , "only available on BCM2711"],
]
_except_a = "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n{}"
_except_z = "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
_except_1 = """
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{}
Did you start the pigpio daemon? E.g. sudo pigpiod
Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888
Did you specify the correct Pi host/port in the
asyncpio.pi.connect() function? E.g. await pi.connect('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"""
_except_2 = """
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{}
Do you have permission to access the pigpio daemon?
Perhaps it was started with sudo pigpiod -nlocalhost
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"""
_except_3 = """
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't create callback handler.
Perhaps too many simultaneous pigpio connections.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"""
class _cosocket:
"""An asynchronous socket."""
def __init__(self, loop, sock):
sock.setblocking(False)
self._loop = loop
self._socket = sock
def close(self):
self._socket.close()
async def recv(self, nbytes):
return await self._loop.sock_recv(self._socket, nbytes)
async def send(self, data):
await self._loop.sock_sendall(self._socket, data)
async def sendall(self, data):
await self._loop.sock_sendall(self._socket, data)
class _socklock:
"""
A class to store socket and lock.
"""
def __init__(self):
self.s = None
self.l = asyncio.Lock()
class error(Exception):
"""pigpio module exception"""
pass
class pulse:
"""
A class to store pulse information.
"""
def __init__(self, gpio_on, gpio_off, delay):
"""
Initialises a pulse.
gpio_on:= the GPIO to switch on at the start of the pulse.
gpio_off:= the GPIO to switch off at the start of the pulse.
delay:= the delay in microseconds before the next pulse.
"""
self.gpio_on = gpio_on
self.gpio_off = gpio_off
self.delay = delay
def error_text(errnum):
"""
Returns a text description of a pigpio error.
errnum:= <0, the error number
...
print(asyncpio.error_text(-5))
level not 0-1
...
"""
for e in _errors:
if e[0] == errnum:
return e[1]
return "unknown error ({})".format(errnum)
def tickDiff(t1, t2):
"""
Returns the microsecond difference between two ticks.
t1:= the earlier tick
t2:= the later tick
...
print(asyncpio.tickDiff(4294967272, 12))
36
...
"""
tDiff = t2 - t1
if tDiff < 0:
tDiff += (1 << 32)
return tDiff
# A couple of hacks to cope with different string handling
# between various Python versions
# 3 != 2.7.8 != 2.7.3
if sys.hexversion < 0x03000000:
def _b(x):
return x
else:
def _b(x):
return x.encode('latin-1')