forked from joan2937/pigpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pigpio.py
4130 lines (3252 loc) · 114 KB
/
pigpio.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
"""
pigpio is a Python module for the Raspberry which talks to
the pigpio daemon to allow control of the general purpose
input outputs (gpios).
[http://abyz.co.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 independent PWM on any of gpios 0-31 simultaneously
o independent servo pulses on any of gpios 0-31 simultaneously
o callbacks when any of gpios 0-31 change state
o creating and transmitting precisely timed waveforms
o reading/writing gpios and setting their modes
o wrappers for I2C, SPI, and serial links
o creating and running scripts on the pigpio daemon
*gpios*
ALL gpios 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 gpios 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
pigpio.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.
...
pigpio.exceptions = False
# Code where you want to test the error status.
pigpio.exceptions = True
...
*Usage*
This module uses the services of the C pigpio library. pigpio
must be running on the Pi(s) whose gpios are to be manipulated.
The normal way to start pigpio is as a daemon (during system
start).
sudo pigpiod
Your Python program must import pigpio and create one or more
instances of the pigpio.pi class. This class gives access to
a specified Pi's gpios.
...
pi1 = pigpio.pi() # pi1 accesses the local Pi's gpios
pi2 = pigpio.pi('tom') # pi2 accesses tom's gpios
pi3 = pigpio.pi('dick') # pi3 accesses dick's gpios
pi1.write(4, 0) # set local Pi's gpio 4 low
pi2.write(4, 1) # set tom's gpio 4 to high
pi3.read(4) # get level of dick's gpio 4
...
The later example code snippets assume that pi is an instance of
the pigpio.pi class.
OVERVIEW
Essential
pigpio.pi Initialise Pi connection
stop Stop a Pi connection
Beginner
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
set_PWM_dutycycle Start/stop PWM pulses on a gpio
get_PWM_dutycycle Get PWM dutycycle set on a gpio
set_servo_pulsewidth Start/Stop servo pulses on a gpio
get_servo_pulsewidth Get servo pulsewidth set on a gpio
callback Create gpio level change callback
wait_for_edge Wait for gpio level change
Intermediate
gpio_trigger Send a trigger pulse to a gpio
set_watchdog Set a watchdog on a gpio
set_filter Set an activity filter on a gpio
set_PWM_range Configure PWM range of a gpio
get_PWM_range Get configured PWM range of a gpio
set_PWM_frequency Set PWM frequency of a gpio
get_PWM_frequency Get PWM frequency of a gpio
read_bank_1 Read all bank 1 gpios
read_bank_2 Read all bank 2 gpios
clear_bank_1 Clear selected gpios in bank 1
clear_bank_2 Clear selected gpios in bank 2
set_bank_1 Set selected gpios in bank 1
set_bank_2 Set selected gpios in bank 2
Advanced
get_PWM_real_range Get underlying PWM range for a gpio
notify_open Request a notification handle
notify_begin Start notifications for selected gpios
notify_pause Pause notifications
notify_close Close a notification
bb_serial_read_open Open a gpio for bit bang serial reads
bb_serial_read Read bit bang serial data from a gpio
bb_serial_read_close Close a gpio for bit bang serial reads
bb_serial_invert Invert serial logic (1 invert, 0 normal)
hardware_clock Start hardware clock on supported gpios
hardware_PWM Start hardware PWM on supported gpios
set_glitch_filter Set a glitch filter on a gpio
set_noise_filter Set a noise filter on a gpio
Scripts
store_script Store a script
run_script Run a stored script
script_status Get script status and parameters
stop_script Stop a running script
delete_script Delete a stored script
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_delete Deletes one or more waveforms
wave_send_once Transmits a waveform once
wave_send_repeat Transmits a waveform repeatedly
wave_chain Transmits a chain of waveforms
wave_tx_busy Checks to see if a waveform has ended
wave_tx_stop Aborts the current waveform
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
wave_get_cbs Length in cbs of the current waveform
wave_get_max_cbs Absolute maximum allowed cbs
I2C
i2c_open Opens an I2C device
i2c_close Closes an I2C device
i2c_write_quick SMBus write quick
i2c_write_byte SMBus write byte
i2c_read_byte SMBus read byte
i2c_write_byte_data SMBus write byte data
i2c_write_word_data SMBus write word data
i2c_read_byte_data SMBus read byte data
i2c_read_word_data SMBus read word data
i2c_process_call SMBus process call
i2c_write_block_data SMBus write block data
i2c_read_block_data SMBus read block data
i2c_block_process_call SMBus block process call
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_zip Performs multiple I2C transactions
bb_i2c_open Opens gpios for bit banging I2C
bb_i2c_close Closes gpios for bit banging I2C
bb_i2c_zip Performs multiple bit banged I2C transactions
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
Serial
serial_open Opens a serial device (/dev/tty*)
serial_close Closes a serial device
serial_read Reads bytes from a serial device
serial_read_byte Reads a byte from a serial device
serial_write Writes bytes to a serial device
serial_write_byte Writes a byte to a serial device
serial_data_available Returns number of bytes ready to be read
CUSTOM
custom_1 User custom function 1
custom_2 User custom function 2
Utility
get_current_tick Get current tick (microseconds)
get_hardware_revision Get hardware revision
get_pigpio_version Get the pigpio version
pigpio.error_text Gets error text from error number
pigpio.tickDiff Returns difference between two ticks
"""
import sys
import socket
import struct
import time
import threading
import os
import atexit
VERSION = "1.24"
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_ALIVE = (1 << 6)
NTFY_FLAGS_WDOG = (1 << 5)
NTFY_FLAGS_GPIO = 31
# 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
# 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_NOT_HALTED =-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
# 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 > 3"],
[_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-6"],
[_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 gpios"],
[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_NOT_HALTED , "script already running or failed"],
[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 , "need a A+/B+/Pi2 for auxiliary SPI"],
[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 , "hardware PWM frequency not 1-125M"],
[PI_BAD_HPWM_DUTY , "hardware PWM dutycycle not 0-1M"],
[PI_BAD_HCLK_FREQ , "hardware clock frequency not 4689-250M"],
[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"],
]
class _socklock:
"""
A class to store socket and lock.
"""
def __init__(self):
self.s = None
self.l = threading.Lock()
class error(Exception):
"""pigpio module exception"""
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class pulse:
"""
A class to store pulse information.
"""
def __init__(self, gpio_on, gpio_off, delay):
"""
Initialises a pulse.
gpio_on:= the gpios to switch on at the start of the pulse.
gpio_off:= the gpios 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(pigpio.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(pigpio.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')
if sys.hexversion < 0x02070800:
def _str(x):
return buffer(x)
else:
def _str(x):
return x
def u2i(uint32):
"""
Converts a 32 bit unsigned number to signed.
uint32:= an unsigned 32 bit number
...
print(u2i(4294967272))
-24
print(u2i(37))
37
...
"""
mask = (2 ** 32) - 1
if uint32 & (1 << 31):
v = uint32 | ~mask
else:
v = uint32 & mask
return v
def _u2i(uint32):
"""
Converts a 32 bit unsigned number to signed. If the number
is negative it indicates an error. On error a pigpio
exception will be raised if exceptions is True.
"""
v = u2i(uint32)
if v < 0:
if exceptions:
raise error(error_text(v))
return v
def _pigpio_command(sl, cmd, p1, p2, rl=True):
"""
Runs a pigpio socket command.
sl:= command socket and lock.
cmd:= the command to be executed.
p1:= command parameter 1 (if applicable).
p2:= command parameter 2 (if applicable).
"""
sl.l.acquire()
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
dummy, res = struct.unpack('12sI', sl.s.recv(16))
if rl: sl.l.release()
return res
def _pigpio_command_ext(sl, cmd, p1, p2, p3, extents, rl=True):
"""
Runs an extended pigpio socket command.
sl:= command socket and lock.
cmd:= the command to be executed.
p1:= command parameter 1 (if applicable).
p2:= command parameter 2 (if applicable).
p3:= total size in bytes of following extents
extents:= additional data blocks
"""
ext = bytearray(struct.pack('IIII', cmd, p1, p2, p3))
for x in extents:
if type(x) == type(""):
ext.extend(_b(x))
else:
ext.extend(x)
sl.l.acquire()
sl.s.sendall(ext)
dummy, res = struct.unpack('12sI', sl.s.recv(16))
if rl: sl.l.release()
return res
class _callback_ADT:
"""An ADT class to hold callback information."""
def __init__(self, gpio, edge, func):
"""
Initialises a callback ADT.
gpio:= Broadcom gpio number.
edge:= EITHER_EDGE, RISING_EDGE, or FALLING_EDGE.
func:= a user function taking three arguments (gpio, level, tick).
"""
self.gpio = gpio
self.edge = edge
self.func = func
self.bit = 1<<gpio
class _callback_thread(threading.Thread):
"""A class to encapsulate pigpio notification callbacks."""
def __init__(self, control, host, port):
"""Initialises notifications."""
threading.Thread.__init__(self)
self.control = control
self.sl = _socklock()
self.go = False
self.daemon = True
self.monitor = 0
self.callbacks = []
self.sl.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sl.s.settimeout(None)
self.sl.s.connect((host, port))
self.handle = _pigpio_command(self.sl, _PI_CMD_NOIB, 0, 0)
self.go = True
self.start()
def stop(self):
"""Stops notifications."""
if self.go:
self.go = False
self.sl.s.send(struct.pack('IIII', _PI_CMD_NC, self.handle, 0, 0))
def append(self, callb):
"""Adds a callback to the notification thread."""
self.callbacks.append(callb)
self.monitor = self.monitor | callb.bit
_pigpio_command(self.control, _PI_CMD_NB, self.handle, self.monitor)
def remove(self, callb):
"""Removes a callback from the notification thread."""
if callb in self.callbacks:
self.callbacks.remove(callb)
newMonitor = 0
for c in self.callbacks:
newMonitor |= c.bit
if newMonitor != self.monitor:
self.monitor = newMonitor
_pigpio_command(
self.control, _PI_CMD_NB, self.handle, self.monitor)
def run(self):
"""Runs the notification thread."""
lastLevel = _pigpio_command(self.control, _PI_CMD_BR1, 0, 0)
MSG_SIZ = 12
while self.go:
buf = self.sl.s.recv(MSG_SIZ)
while self.go and len(buf) < MSG_SIZ:
buf += self.sl.s.recv(MSG_SIZ-len(buf))
if self.go:
seq, flags, tick, level = (struct.unpack('HHII', buf))
if flags == 0:
changed = level ^ lastLevel
lastLevel = level
for cb in self.callbacks:
if cb.bit & changed:
newLevel = 0
if cb.bit & level:
newLevel = 1
if (cb.edge ^ newLevel):
cb.func(cb.gpio, newLevel, tick)
else:
if flags & NTFY_FLAGS_WDOG:
gpio = flags & NTFY_FLAGS_GPIO
for cb in self.callbacks:
if cb.gpio == gpio:
cb.func(cb.gpio, TIMEOUT, tick)
self.sl.s.close()
class _callback:
"""A class to provide gpio level change callbacks."""
def __init__(self, notify, user_gpio, edge=RISING_EDGE, func=None):
"""
Initialise a callback and adds it to the notification thread.
"""
self._notify = notify
self.count=0
if func is None:
func=self._tally
self.callb = _callback_ADT(user_gpio, edge, func)
self._notify.append(self.callb)
def cancel(self):
"""Cancels a callback by removing it from the notification thread."""
self._notify.remove(self.callb)
def _tally(self, user_gpio, level, tick):
"""Increment the callback called count."""
self.count += 1
def tally(self):
"""
Provides a count of how many times the default tally
callback has triggered.
The count will be zero if the user has supplied their own
callback function.
"""
return self.count
class _wait_for_edge:
"""Encapsulates waiting for gpio edges."""
def __init__(self, notify, gpio, edge, timeout):
"""Initialises a wait_for_edge."""
self._notify = notify
self.callb = _callback_ADT(gpio, edge, self.func)
self.trigger = False
self._notify.append(self.callb)
self.start = time.time()
while (self.trigger == False) and ((time.time()-self.start) < timeout):
time.sleep(0.05)
self._notify.remove(self.callb)