pigpio.py
Go to the documentation of this file.
1 """
2 pigpio is a Python module for the Raspberry which talks to
3 the pigpio daemon to allow control of the general purpose
4 input outputs (GPIO).
5 
6 [http://abyz.me.uk/rpi/pigpio/python.html]
7 
8 *Features*
9 
10 o the pigpio Python module can run on Windows, Macs, or Linux
11 
12 o controls one or more Pi's
13 
14 o hardware timed PWM on any of GPIO 0-31
15 
16 o hardware timed servo pulses on any of GPIO 0-31
17 
18 o callbacks when any of GPIO 0-31 change state
19 
20 o creating and transmitting precisely timed waveforms
21 
22 o reading/writing GPIO and setting their modes
23 
24 o wrappers for I2C, SPI, and serial links
25 
26 o creating and running scripts on the pigpio daemon
27 
28 *GPIO*
29 
30 ALL GPIO are identified by their Broadcom number.
31 
32 *Notes*
33 
34 Transmitted waveforms are accurate to a microsecond.
35 
36 Callback level changes are time-stamped and will be
37 accurate to within a few microseconds.
38 
39 *Settings*
40 
41 A number of settings are determined when the pigpio daemon is started.
42 
43 o the sample rate (1, 2, 4, 5, 8, or 10 us, default 5 us).
44 
45 o the set of GPIO which may be updated (generally written to). The
46  default set is those available on the Pi board revision.
47 
48 o the available PWM frequencies (see [*set_PWM_frequency*]).
49 
50 *Exceptions*
51 
52 By default a fatal exception is raised if you pass an invalid
53 argument to a pigpio function.
54 
55 If you wish to handle the returned status yourself you should set
56 pigpio.exceptions to False.
57 
58 You may prefer to check the returned status in only a few parts
59 of your code. In that case do the following:
60 
61 ...
62 pigpio.exceptions = False
63 
64 # Code where you want to test the error status.
65 
66 pigpio.exceptions = True
67 ...
68 
69 *Usage*
70 
71 This module uses the services of the C pigpio library. pigpio
72 must be running on the Pi(s) whose GPIO are to be manipulated.
73 
74 The normal way to start pigpio is as a daemon (during system
75 start).
76 
77 sudo pigpiod
78 
79 Your Python program must import pigpio and create one or more
80 instances of the pigpio.pi class. This class gives access to
81 a specified Pi's GPIO.
82 
83 ...
84 pi1 = pigpio.pi() # pi1 accesses the local Pi's GPIO
85 pi2 = pigpio.pi('tom') # pi2 accesses tom's GPIO
86 pi3 = pigpio.pi('dick') # pi3 accesses dick's GPIO
87 
88 pi1.write(4, 0) # set local Pi's GPIO 4 low
89 pi2.write(4, 1) # set tom's GPIO 4 to high
90 pi3.read(4) # get level of dick's GPIO 4
91 ...
92 
93 The later example code snippets assume that pi is an instance of
94 the pigpio.pi class.
95 
96 OVERVIEW
97 
98 Essential
99 
100 pigpio.pi Initialise Pi connection
101 stop Stop a Pi connection
102 
103 Beginner
104 
105 set_mode Set a GPIO mode
106 get_mode Get a GPIO mode
107 set_pull_up_down Set/clear GPIO pull up/down resistor
108 
109 read Read a GPIO
110 write Write a GPIO
111 
112 set_PWM_dutycycle Start/stop PWM pulses on a GPIO
113 get_PWM_dutycycle Get PWM dutycycle set on a GPIO
114 
115 set_servo_pulsewidth Start/Stop servo pulses on a GPIO
116 get_servo_pulsewidth Get servo pulsewidth set on a GPIO
117 
118 callback Create GPIO level change callback
119 wait_for_edge Wait for GPIO level change
120 
121 Intermediate
122 
123 gpio_trigger Send a trigger pulse to a GPIO
124 
125 set_watchdog Set a watchdog on a GPIO
126 
127 set_PWM_range Configure PWM range of a GPIO
128 get_PWM_range Get configured PWM range of a GPIO
129 
130 set_PWM_frequency Set PWM frequency of a GPIO
131 get_PWM_frequency Get PWM frequency of a GPIO
132 
133 read_bank_1 Read all bank 1 GPIO
134 read_bank_2 Read all bank 2 GPIO
135 
136 clear_bank_1 Clear selected GPIO in bank 1
137 clear_bank_2 Clear selected GPIO in bank 2
138 
139 set_bank_1 Set selected GPIO in bank 1
140 set_bank_2 Set selected GPIO in bank 2
141 
142 Advanced
143 
144 get_PWM_real_range Get underlying PWM range for a GPIO
145 
146 notify_open Request a notification handle
147 notify_begin Start notifications for selected GPIO
148 notify_pause Pause notifications
149 notify_close Close a notification
150 
151 bb_serial_read_open Open a GPIO for bit bang serial reads
152 bb_serial_read Read bit bang serial data from a GPIO
153 bb_serial_read_close Close a GPIO for bit bang serial reads
154 bb_serial_invert Invert serial logic (1 invert, 0 normal)
155 
156 hardware_clock Start hardware clock on supported GPIO
157 hardware_PWM Start hardware PWM on supported GPIO
158 
159 set_glitch_filter Set a glitch filter on a GPIO
160 set_noise_filter Set a noise filter on a GPIO
161 
162 get_pad_strength Gets a pads drive strength
163 set_pad_strength Sets a pads drive strength
164 
165 shell Executes a shell command
166 
167 Scripts
168 
169 store_script Store a script
170 run_script Run a stored script
171 update_script Set a scripts parameters
172 script_status Get script status and parameters
173 stop_script Stop a running script
174 delete_script Delete a stored script
175 
176 Waves
177 
178 wave_clear Deletes all waveforms
179 
180 wave_add_new Starts a new waveform
181 wave_add_generic Adds a series of pulses to the waveform
182 wave_add_serial Adds serial data to the waveform
183 
184 wave_create Creates a waveform from added data
185 wave_delete Deletes a waveform
186 
187 wave_send_once Transmits a waveform once
188 wave_send_repeat Transmits a waveform repeatedly
189 wave_send_using_mode Transmits a waveform in the chosen mode
190 
191 wave_chain Transmits a chain of waveforms
192 
193 wave_tx_at Returns the current transmitting waveform
194 wave_tx_busy Checks to see if a waveform has ended
195 wave_tx_stop Aborts the current waveform
196 
197 wave_get_micros Length in microseconds of the current waveform
198 wave_get_max_micros Absolute maximum allowed micros
199 wave_get_pulses Length in pulses of the current waveform
200 wave_get_max_pulses Absolute maximum allowed pulses
201 wave_get_cbs Length in cbs of the current waveform
202 wave_get_max_cbs Absolute maximum allowed cbs
203 
204 I2C
205 
206 i2c_open Opens an I2C device
207 i2c_close Closes an I2C device
208 
209 i2c_write_quick SMBus write quick
210 i2c_write_byte SMBus write byte
211 i2c_read_byte SMBus read byte
212 i2c_write_byte_data SMBus write byte data
213 i2c_write_word_data SMBus write word data
214 i2c_read_byte_data SMBus read byte data
215 i2c_read_word_data SMBus read word data
216 i2c_process_call SMBus process call
217 i2c_write_block_data SMBus write block data
218 i2c_read_block_data SMBus read block data
219 i2c_block_process_call SMBus block process call
220 
221 i2c_read_i2c_block_data SMBus read I2C block data
222 i2c_write_i2c_block_data SMBus write I2C block data
223 
224 i2c_read_device Reads the raw I2C device
225 i2c_write_device Writes the raw I2C device
226 
227 i2c_zip Performs multiple I2C transactions
228 
229 bb_i2c_open Opens GPIO for bit banging I2C
230 bb_i2c_close Closes GPIO for bit banging I2C
231 bb_i2c_zip Performs multiple bit banged I2C transactions
232 
233 SPI
234 
235 spi_open Opens a SPI device
236 spi_close Closes a SPI device
237 
238 spi_read Reads bytes from a SPI device
239 spi_write Writes bytes to a SPI device
240 spi_xfer Transfers bytes with a SPI device
241 
242 bb_spi_open Opens GPIO for bit banging SPI
243 bb_spi_close Closes GPIO for bit banging SPI
244 bb_spi_xfer Transfers bytes with bit banging SPI
245 
246 I2C/SPI_Slave
247 
248 bsc_xfer I2C/SPI as slave transfer
249 bsc_i2c I2C as slave transfer
250 
251 Serial
252 
253 serial_open Opens a serial device
254 serial_close Closes a serial device
255 
256 serial_read Reads bytes from a serial device
257 serial_read_byte Reads a byte from a serial device
258 
259 serial_write Writes bytes to a serial device
260 serial_write_byte Writes a byte to a serial device
261 
262 serial_data_available Returns number of bytes ready to be read
263 
264 Files
265 
266 file_open Opens a file
267 file_close Closes a file
268 file_read Reads bytes from a file
269 file_write Writes bytes to a file
270 file_seek Seeks to a position within a file
271 file_list List files which match a pattern
272 
273 Events
274 
275 event_callback Sets a callback for an event
276 event_trigger Triggers an event
277 wait_for_event Wait for an event
278 
279 Custom
280 
281 custom_1 User custom function 1
282 custom_2 User custom function 2
283 
284 Utility
285 
286 get_current_tick Get current tick (microseconds)
287 
288 get_hardware_revision Get hardware revision
289 get_pigpio_version Get the pigpio version
290 
291 pigpio.error_text Gets error text from error number
292 pigpio.tickDiff Returns difference between two ticks
293 """
294 
295 import sys
296 import socket
297 import struct
298 import time
299 import threading
300 import os
301 import atexit
302 
303 VERSION = "1.41"
304 
305 exceptions = True
306 
307 # GPIO levels
308 
309 OFF = 0
310 LOW = 0
311 CLEAR = 0
312 
313 ON = 1
314 HIGH = 1
315 SET = 1
316 
317 TIMEOUT = 2
318 
319 # GPIO edges
320 
321 RISING_EDGE = 0
322 FALLING_EDGE = 1
323 EITHER_EDGE = 2
324 
325 # GPIO modes
326 
327 INPUT = 0
328 OUTPUT = 1
329 ALT0 = 4
330 ALT1 = 5
331 ALT2 = 6
332 ALT3 = 7
333 ALT4 = 3
334 ALT5 = 2
335 
336 # GPIO Pull Up Down
337 
338 PUD_OFF = 0
339 PUD_DOWN = 1
340 PUD_UP = 2
341 
342 # script run status
343 
344 PI_SCRIPT_INITING=0
345 PI_SCRIPT_HALTED =1
346 PI_SCRIPT_RUNNING=2
347 PI_SCRIPT_WAITING=3
348 PI_SCRIPT_FAILED =4
349 
350 # notification flags
351 
352 NTFY_FLAGS_EVENT = (1 << 7)
353 NTFY_FLAGS_ALIVE = (1 << 6)
354 NTFY_FLAGS_WDOG = (1 << 5)
355 NTFY_FLAGS_GPIO = 31
356 
357 # wave modes
358 
359 WAVE_MODE_ONE_SHOT =0
360 WAVE_MODE_REPEAT =1
361 WAVE_MODE_ONE_SHOT_SYNC=2
362 WAVE_MODE_REPEAT_SYNC =3
363 
364 WAVE_NOT_FOUND = 9998 # Transmitted wave not found.
365 NO_TX_WAVE = 9999 # No wave being transmitted.
366 
367 FILE_READ=1
368 FILE_WRITE=2
369 FILE_RW=3
370 
371 FILE_APPEND=4
372 FILE_CREATE=8
373 FILE_TRUNC=16
374 
375 FROM_START=0
376 FROM_CURRENT=1
377 FROM_END=2
378 
379 SPI_MODE_0 = 0
380 SPI_MODE_1 = 1
381 SPI_MODE_2 = 2
382 SPI_MODE_3 = 3
383 
384 SPI_CPHA = 1 << 0
385 SPI_CPOL = 1 << 1
386 
387 SPI_CS_HIGH_ACTIVE = 1 << 2
388 
389 SPI_TX_LSBFIRST = 1 << 14
390 SPI_RX_LSBFIRST = 1 << 15
391 
392 EVENT_BSC = 31
393 
394 _SOCK_CMD_LEN = 16
395 
396 # pigpio command numbers
397 
398 _PI_CMD_MODES= 0
399 _PI_CMD_MODEG= 1
400 _PI_CMD_PUD= 2
401 _PI_CMD_READ= 3
402 _PI_CMD_WRITE= 4
403 _PI_CMD_PWM= 5
404 _PI_CMD_PRS= 6
405 _PI_CMD_PFS= 7
406 _PI_CMD_SERVO= 8
407 _PI_CMD_WDOG= 9
408 _PI_CMD_BR1= 10
409 _PI_CMD_BR2= 11
410 _PI_CMD_BC1= 12
411 _PI_CMD_BC2= 13
412 _PI_CMD_BS1= 14
413 _PI_CMD_BS2= 15
414 _PI_CMD_TICK= 16
415 _PI_CMD_HWVER=17
416 
417 _PI_CMD_NO= 18
418 _PI_CMD_NB= 19
419 _PI_CMD_NP= 20
420 _PI_CMD_NC= 21
421 
422 _PI_CMD_PRG= 22
423 _PI_CMD_PFG= 23
424 _PI_CMD_PRRG= 24
425 _PI_CMD_HELP= 25
426 _PI_CMD_PIGPV=26
427 
428 _PI_CMD_WVCLR=27
429 _PI_CMD_WVAG= 28
430 _PI_CMD_WVAS= 29
431 _PI_CMD_WVGO= 30
432 _PI_CMD_WVGOR=31
433 _PI_CMD_WVBSY=32
434 _PI_CMD_WVHLT=33
435 _PI_CMD_WVSM= 34
436 _PI_CMD_WVSP= 35
437 _PI_CMD_WVSC= 36
438 
439 _PI_CMD_TRIG= 37
440 
441 _PI_CMD_PROC= 38
442 _PI_CMD_PROCD=39
443 _PI_CMD_PROCR=40
444 _PI_CMD_PROCS=41
445 
446 _PI_CMD_SLRO= 42
447 _PI_CMD_SLR= 43
448 _PI_CMD_SLRC= 44
449 
450 _PI_CMD_PROCP=45
451 _PI_CMD_MICRO=46
452 _PI_CMD_MILLI=47
453 _PI_CMD_PARSE=48
454 
455 _PI_CMD_WVCRE=49
456 _PI_CMD_WVDEL=50
457 _PI_CMD_WVTX =51
458 _PI_CMD_WVTXR=52
459 _PI_CMD_WVNEW=53
460 
461 _PI_CMD_I2CO =54
462 _PI_CMD_I2CC =55
463 _PI_CMD_I2CRD=56
464 _PI_CMD_I2CWD=57
465 _PI_CMD_I2CWQ=58
466 _PI_CMD_I2CRS=59
467 _PI_CMD_I2CWS=60
468 _PI_CMD_I2CRB=61
469 _PI_CMD_I2CWB=62
470 _PI_CMD_I2CRW=63
471 _PI_CMD_I2CWW=64
472 _PI_CMD_I2CRK=65
473 _PI_CMD_I2CWK=66
474 _PI_CMD_I2CRI=67
475 _PI_CMD_I2CWI=68
476 _PI_CMD_I2CPC=69
477 _PI_CMD_I2CPK=70
478 
479 _PI_CMD_SPIO =71
480 _PI_CMD_SPIC =72
481 _PI_CMD_SPIR =73
482 _PI_CMD_SPIW =74
483 _PI_CMD_SPIX =75
484 
485 _PI_CMD_SERO =76
486 _PI_CMD_SERC =77
487 _PI_CMD_SERRB=78
488 _PI_CMD_SERWB=79
489 _PI_CMD_SERR =80
490 _PI_CMD_SERW =81
491 _PI_CMD_SERDA=82
492 
493 _PI_CMD_GDC =83
494 _PI_CMD_GPW =84
495 
496 _PI_CMD_HC =85
497 _PI_CMD_HP =86
498 
499 _PI_CMD_CF1 =87
500 _PI_CMD_CF2 =88
501 
502 _PI_CMD_NOIB =99
503 
504 _PI_CMD_BI2CC=89
505 _PI_CMD_BI2CO=90
506 _PI_CMD_BI2CZ=91
507 
508 _PI_CMD_I2CZ =92
509 
510 _PI_CMD_WVCHA=93
511 
512 _PI_CMD_SLRI =94
513 
514 _PI_CMD_CGI =95
515 _PI_CMD_CSI =96
516 
517 _PI_CMD_FG =97
518 _PI_CMD_FN =98
519 
520 _PI_CMD_WVTXM=100
521 _PI_CMD_WVTAT=101
522 
523 _PI_CMD_PADS =102
524 _PI_CMD_PADG =103
525 
526 _PI_CMD_FO =104
527 _PI_CMD_FC =105
528 _PI_CMD_FR =106
529 _PI_CMD_FW =107
530 _PI_CMD_FS =108
531 _PI_CMD_FL =109
532 _PI_CMD_SHELL=110
533 
534 _PI_CMD_BSPIC=111
535 _PI_CMD_BSPIO=112
536 _PI_CMD_BSPIX=113
537 
538 _PI_CMD_BSCX =114
539 
540 _PI_CMD_EVM =115
541 _PI_CMD_EVT =116
542 
543 _PI_CMD_PROCU=117
544 
545 # pigpio error numbers
546 
547 _PI_INIT_FAILED =-1
548 PI_BAD_USER_GPIO =-2
549 PI_BAD_GPIO =-3
550 PI_BAD_MODE =-4
551 PI_BAD_LEVEL =-5
552 PI_BAD_PUD =-6
553 PI_BAD_PULSEWIDTH =-7
554 PI_BAD_DUTYCYCLE =-8
555 _PI_BAD_TIMER =-9
556 _PI_BAD_MS =-10
557 _PI_BAD_TIMETYPE =-11
558 _PI_BAD_SECONDS =-12
559 _PI_BAD_MICROS =-13
560 _PI_TIMER_FAILED =-14
561 PI_BAD_WDOG_TIMEOUT =-15
562 _PI_NO_ALERT_FUNC =-16
563 _PI_BAD_CLK_PERIPH =-17
564 _PI_BAD_CLK_SOURCE =-18
565 _PI_BAD_CLK_MICROS =-19
566 _PI_BAD_BUF_MILLIS =-20
567 PI_BAD_DUTYRANGE =-21
568 _PI_BAD_SIGNUM =-22
569 _PI_BAD_PATHNAME =-23
570 PI_NO_HANDLE =-24
571 PI_BAD_HANDLE =-25
572 _PI_BAD_IF_FLAGS =-26
573 _PI_BAD_CHANNEL =-27
574 _PI_BAD_PRIM_CHANNEL=-27
575 _PI_BAD_SOCKET_PORT =-28
576 _PI_BAD_FIFO_COMMAND=-29
577 _PI_BAD_SECO_CHANNEL=-30
578 _PI_NOT_INITIALISED =-31
579 _PI_INITIALISED =-32
580 _PI_BAD_WAVE_MODE =-33
581 _PI_BAD_CFG_INTERNAL=-34
582 PI_BAD_WAVE_BAUD =-35
583 PI_TOO_MANY_PULSES =-36
584 PI_TOO_MANY_CHARS =-37
585 PI_NOT_SERIAL_GPIO =-38
586 _PI_BAD_SERIAL_STRUC=-39
587 _PI_BAD_SERIAL_BUF =-40
588 PI_NOT_PERMITTED =-41
589 PI_SOME_PERMITTED =-42
590 PI_BAD_WVSC_COMMND =-43
591 PI_BAD_WVSM_COMMND =-44
592 PI_BAD_WVSP_COMMND =-45
593 PI_BAD_PULSELEN =-46
594 PI_BAD_SCRIPT =-47
595 PI_BAD_SCRIPT_ID =-48
596 PI_BAD_SER_OFFSET =-49
597 PI_GPIO_IN_USE =-50
598 PI_BAD_SERIAL_COUNT =-51
599 PI_BAD_PARAM_NUM =-52
600 PI_DUP_TAG =-53
601 PI_TOO_MANY_TAGS =-54
602 PI_BAD_SCRIPT_CMD =-55
603 PI_BAD_VAR_NUM =-56
604 PI_NO_SCRIPT_ROOM =-57
605 PI_NO_MEMORY =-58
606 PI_SOCK_READ_FAILED =-59
607 PI_SOCK_WRIT_FAILED =-60
608 PI_TOO_MANY_PARAM =-61
609 PI_SCRIPT_NOT_READY =-62
610 PI_BAD_TAG =-63
611 PI_BAD_MICS_DELAY =-64
612 PI_BAD_MILS_DELAY =-65
613 PI_BAD_WAVE_ID =-66
614 PI_TOO_MANY_CBS =-67
615 PI_TOO_MANY_OOL =-68
616 PI_EMPTY_WAVEFORM =-69
617 PI_NO_WAVEFORM_ID =-70
618 PI_I2C_OPEN_FAILED =-71
619 PI_SER_OPEN_FAILED =-72
620 PI_SPI_OPEN_FAILED =-73
621 PI_BAD_I2C_BUS =-74
622 PI_BAD_I2C_ADDR =-75
623 PI_BAD_SPI_CHANNEL =-76
624 PI_BAD_FLAGS =-77
625 PI_BAD_SPI_SPEED =-78
626 PI_BAD_SER_DEVICE =-79
627 PI_BAD_SER_SPEED =-80
628 PI_BAD_PARAM =-81
629 PI_I2C_WRITE_FAILED =-82
630 PI_I2C_READ_FAILED =-83
631 PI_BAD_SPI_COUNT =-84
632 PI_SER_WRITE_FAILED =-85
633 PI_SER_READ_FAILED =-86
634 PI_SER_READ_NO_DATA =-87
635 PI_UNKNOWN_COMMAND =-88
636 PI_SPI_XFER_FAILED =-89
637 _PI_BAD_POINTER =-90
638 PI_NO_AUX_SPI =-91
639 PI_NOT_PWM_GPIO =-92
640 PI_NOT_SERVO_GPIO =-93
641 PI_NOT_HCLK_GPIO =-94
642 PI_NOT_HPWM_GPIO =-95
643 PI_BAD_HPWM_FREQ =-96
644 PI_BAD_HPWM_DUTY =-97
645 PI_BAD_HCLK_FREQ =-98
646 PI_BAD_HCLK_PASS =-99
647 PI_HPWM_ILLEGAL =-100
648 PI_BAD_DATABITS =-101
649 PI_BAD_STOPBITS =-102
650 PI_MSG_TOOBIG =-103
651 PI_BAD_MALLOC_MODE =-104
652 _PI_TOO_MANY_SEGS =-105
653 _PI_BAD_I2C_SEG =-106
654 PI_BAD_SMBUS_CMD =-107
655 PI_NOT_I2C_GPIO =-108
656 PI_BAD_I2C_WLEN =-109
657 PI_BAD_I2C_RLEN =-110
658 PI_BAD_I2C_CMD =-111
659 PI_BAD_I2C_BAUD =-112
660 PI_CHAIN_LOOP_CNT =-113
661 PI_BAD_CHAIN_LOOP =-114
662 PI_CHAIN_COUNTER =-115
663 PI_BAD_CHAIN_CMD =-116
664 PI_BAD_CHAIN_DELAY =-117
665 PI_CHAIN_NESTING =-118
666 PI_CHAIN_TOO_BIG =-119
667 PI_DEPRECATED =-120
668 PI_BAD_SER_INVERT =-121
669 _PI_BAD_EDGE =-122
670 _PI_BAD_ISR_INIT =-123
671 PI_BAD_FOREVER =-124
672 PI_BAD_FILTER =-125
673 PI_BAD_PAD =-126
674 PI_BAD_STRENGTH =-127
675 PI_FIL_OPEN_FAILED =-128
676 PI_BAD_FILE_MODE =-129
677 PI_BAD_FILE_FLAG =-130
678 PI_BAD_FILE_READ =-131
679 PI_BAD_FILE_WRITE =-132
680 PI_FILE_NOT_ROPEN =-133
681 PI_FILE_NOT_WOPEN =-134
682 PI_BAD_FILE_SEEK =-135
683 PI_NO_FILE_MATCH =-136
684 PI_NO_FILE_ACCESS =-137
685 PI_FILE_IS_A_DIR =-138
686 PI_BAD_SHELL_STATUS =-139
687 PI_BAD_SCRIPT_NAME =-140
688 PI_BAD_SPI_BAUD =-141
689 PI_NOT_SPI_GPIO =-142
690 PI_BAD_EVENT_ID =-143
691 PI_CMD_INTERRUPTED =-144
692 
693 # pigpio error text
694 
695 _errors=[
696  [_PI_INIT_FAILED , "pigpio initialisation failed"],
697  [PI_BAD_USER_GPIO , "GPIO not 0-31"],
698  [PI_BAD_GPIO , "GPIO not 0-53"],
699  [PI_BAD_MODE , "mode not 0-7"],
700  [PI_BAD_LEVEL , "level not 0-1"],
701  [PI_BAD_PUD , "pud not 0-2"],
702  [PI_BAD_PULSEWIDTH , "pulsewidth not 0 or 500-2500"],
703  [PI_BAD_DUTYCYCLE , "dutycycle not 0-range (default 255)"],
704  [_PI_BAD_TIMER , "timer not 0-9"],
705  [_PI_BAD_MS , "ms not 10-60000"],
706  [_PI_BAD_TIMETYPE , "timetype not 0-1"],
707  [_PI_BAD_SECONDS , "seconds < 0"],
708  [_PI_BAD_MICROS , "micros not 0-999999"],
709  [_PI_TIMER_FAILED , "gpioSetTimerFunc failed"],
710  [PI_BAD_WDOG_TIMEOUT , "timeout not 0-60000"],
711  [_PI_NO_ALERT_FUNC , "DEPRECATED"],
712  [_PI_BAD_CLK_PERIPH , "clock peripheral not 0-1"],
713  [_PI_BAD_CLK_SOURCE , "DEPRECATED"],
714  [_PI_BAD_CLK_MICROS , "clock micros not 1, 2, 4, 5, 8, or 10"],
715  [_PI_BAD_BUF_MILLIS , "buf millis not 100-10000"],
716  [PI_BAD_DUTYRANGE , "dutycycle range not 25-40000"],
717  [_PI_BAD_SIGNUM , "signum not 0-63"],
718  [_PI_BAD_PATHNAME , "can't open pathname"],
719  [PI_NO_HANDLE , "no handle available"],
720  [PI_BAD_HANDLE , "unknown handle"],
721  [_PI_BAD_IF_FLAGS , "ifFlags > 4"],
722  [_PI_BAD_CHANNEL , "DMA channel not 0-14"],
723  [_PI_BAD_SOCKET_PORT , "socket port not 1024-30000"],
724  [_PI_BAD_FIFO_COMMAND , "unknown fifo command"],
725  [_PI_BAD_SECO_CHANNEL , "DMA secondary channel not 0-14"],
726  [_PI_NOT_INITIALISED , "function called before gpioInitialise"],
727  [_PI_INITIALISED , "function called after gpioInitialise"],
728  [_PI_BAD_WAVE_MODE , "waveform mode not 0-1"],
729  [_PI_BAD_CFG_INTERNAL , "bad parameter in gpioCfgInternals call"],
730  [PI_BAD_WAVE_BAUD , "baud rate not 50-250000(RX)/1000000(TX)"],
731  [PI_TOO_MANY_PULSES , "waveform has too many pulses"],
732  [PI_TOO_MANY_CHARS , "waveform has too many chars"],
733  [PI_NOT_SERIAL_GPIO , "no bit bang serial read in progress on GPIO"],
734  [PI_NOT_PERMITTED , "no permission to update GPIO"],
735  [PI_SOME_PERMITTED , "no permission to update one or more GPIO"],
736  [PI_BAD_WVSC_COMMND , "bad WVSC subcommand"],
737  [PI_BAD_WVSM_COMMND , "bad WVSM subcommand"],
738  [PI_BAD_WVSP_COMMND , "bad WVSP subcommand"],
739  [PI_BAD_PULSELEN , "trigger pulse length not 1-100"],
740  [PI_BAD_SCRIPT , "invalid script"],
741  [PI_BAD_SCRIPT_ID , "unknown script id"],
742  [PI_BAD_SER_OFFSET , "add serial data offset > 30 minute"],
743  [PI_GPIO_IN_USE , "GPIO already in use"],
744  [PI_BAD_SERIAL_COUNT , "must read at least a byte at a time"],
745  [PI_BAD_PARAM_NUM , "script parameter id not 0-9"],
746  [PI_DUP_TAG , "script has duplicate tag"],
747  [PI_TOO_MANY_TAGS , "script has too many tags"],
748  [PI_BAD_SCRIPT_CMD , "illegal script command"],
749  [PI_BAD_VAR_NUM , "script variable id not 0-149"],
750  [PI_NO_SCRIPT_ROOM , "no more room for scripts"],
751  [PI_NO_MEMORY , "can't allocate temporary memory"],
752  [PI_SOCK_READ_FAILED , "socket read failed"],
753  [PI_SOCK_WRIT_FAILED , "socket write failed"],
754  [PI_TOO_MANY_PARAM , "too many script parameters (> 10)"],
755  [PI_SCRIPT_NOT_READY , "script initialising"],
756  [PI_BAD_TAG , "script has unresolved tag"],
757  [PI_BAD_MICS_DELAY , "bad MICS delay (too large)"],
758  [PI_BAD_MILS_DELAY , "bad MILS delay (too large)"],
759  [PI_BAD_WAVE_ID , "non existent wave id"],
760  [PI_TOO_MANY_CBS , "No more CBs for waveform"],
761  [PI_TOO_MANY_OOL , "No more OOL for waveform"],
762  [PI_EMPTY_WAVEFORM , "attempt to create an empty waveform"],
763  [PI_NO_WAVEFORM_ID , "No more waveform ids"],
764  [PI_I2C_OPEN_FAILED , "can't open I2C device"],
765  [PI_SER_OPEN_FAILED , "can't open serial device"],
766  [PI_SPI_OPEN_FAILED , "can't open SPI device"],
767  [PI_BAD_I2C_BUS , "bad I2C bus"],
768  [PI_BAD_I2C_ADDR , "bad I2C address"],
769  [PI_BAD_SPI_CHANNEL , "bad SPI channel"],
770  [PI_BAD_FLAGS , "bad i2c/spi/ser open flags"],
771  [PI_BAD_SPI_SPEED , "bad SPI speed"],
772  [PI_BAD_SER_DEVICE , "bad serial device name"],
773  [PI_BAD_SER_SPEED , "bad serial baud rate"],
774  [PI_BAD_PARAM , "bad i2c/spi/ser parameter"],
775  [PI_I2C_WRITE_FAILED , "I2C write failed"],
776  [PI_I2C_READ_FAILED , "I2C read failed"],
777  [PI_BAD_SPI_COUNT , "bad SPI count"],
778  [PI_SER_WRITE_FAILED , "ser write failed"],
779  [PI_SER_READ_FAILED , "ser read failed"],
780  [PI_SER_READ_NO_DATA , "ser read no data available"],
781  [PI_UNKNOWN_COMMAND , "unknown command"],
782  [PI_SPI_XFER_FAILED , "SPI xfer/read/write failed"],
783  [_PI_BAD_POINTER , "bad (NULL) pointer"],
784  [PI_NO_AUX_SPI , "no auxiliary SPI on Pi A or B"],
785  [PI_NOT_PWM_GPIO , "GPIO is not in use for PWM"],
786  [PI_NOT_SERVO_GPIO , "GPIO is not in use for servo pulses"],
787  [PI_NOT_HCLK_GPIO , "GPIO has no hardware clock"],
788  [PI_NOT_HPWM_GPIO , "GPIO has no hardware PWM"],
789  [PI_BAD_HPWM_FREQ , "hardware PWM frequency not 1-125M"],
790  [PI_BAD_HPWM_DUTY , "hardware PWM dutycycle not 0-1M"],
791  [PI_BAD_HCLK_FREQ , "hardware clock frequency not 4689-250M"],
792  [PI_BAD_HCLK_PASS , "need password to use hardware clock 1"],
793  [PI_HPWM_ILLEGAL , "illegal, PWM in use for main clock"],
794  [PI_BAD_DATABITS , "serial data bits not 1-32"],
795  [PI_BAD_STOPBITS , "serial (half) stop bits not 2-8"],
796  [PI_MSG_TOOBIG , "socket/pipe message too big"],
797  [PI_BAD_MALLOC_MODE , "bad memory allocation mode"],
798  [_PI_TOO_MANY_SEGS , "too many I2C transaction segments"],
799  [_PI_BAD_I2C_SEG , "an I2C transaction segment failed"],
800  [PI_BAD_SMBUS_CMD , "SMBus command not supported"],
801  [PI_NOT_I2C_GPIO , "no bit bang I2C in progress on GPIO"],
802  [PI_BAD_I2C_WLEN , "bad I2C write length"],
803  [PI_BAD_I2C_RLEN , "bad I2C read length"],
804  [PI_BAD_I2C_CMD , "bad I2C command"],
805  [PI_BAD_I2C_BAUD , "bad I2C baud rate, not 50-500k"],
806  [PI_CHAIN_LOOP_CNT , "bad chain loop count"],
807  [PI_BAD_CHAIN_LOOP , "empty chain loop"],
808  [PI_CHAIN_COUNTER , "too many chain counters"],
809  [PI_BAD_CHAIN_CMD , "bad chain command"],
810  [PI_BAD_CHAIN_DELAY , "bad chain delay micros"],
811  [PI_CHAIN_NESTING , "chain counters nested too deeply"],
812  [PI_CHAIN_TOO_BIG , "chain is too long"],
813  [PI_DEPRECATED , "deprecated function removed"],
814  [PI_BAD_SER_INVERT , "bit bang serial invert not 0 or 1"],
815  [_PI_BAD_EDGE , "bad ISR edge value, not 0-2"],
816  [_PI_BAD_ISR_INIT , "bad ISR initialisation"],
817  [PI_BAD_FOREVER , "loop forever must be last chain command"],
818  [PI_BAD_FILTER , "bad filter parameter"],
819  [PI_BAD_PAD , "bad pad number"],
820  [PI_BAD_STRENGTH , "bad pad drive strength"],
821  [PI_FIL_OPEN_FAILED , "file open failed"],
822  [PI_BAD_FILE_MODE , "bad file mode"],
823  [PI_BAD_FILE_FLAG , "bad file flag"],
824  [PI_BAD_FILE_READ , "bad file read"],
825  [PI_BAD_FILE_WRITE , "bad file write"],
826  [PI_FILE_NOT_ROPEN , "file not open for read"],
827  [PI_FILE_NOT_WOPEN , "file not open for write"],
828  [PI_BAD_FILE_SEEK , "bad file seek"],
829  [PI_NO_FILE_MATCH , "no files match pattern"],
830  [PI_NO_FILE_ACCESS , "no permission to access file"],
831  [PI_FILE_IS_A_DIR , "file is a directory"],
832  [PI_BAD_SHELL_STATUS , "bad shell return status"],
833  [PI_BAD_SCRIPT_NAME , "bad script name"],
834  [PI_BAD_SPI_BAUD , "bad SPI baud rate, not 50-500k"],
835  [PI_NOT_SPI_GPIO , "no bit bang SPI in progress on GPIO"],
836  [PI_BAD_EVENT_ID , "bad event id"],
837  [PI_CMD_INTERRUPTED , "pigpio command interrupted"],
838 ]
839 
840 _except_a = "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n{}"
841 
842 _except_z = "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
843 
844 _except_1 = """
845 Did you start the pigpio daemon? E.g. sudo pigpiod
846 
847 Did you specify the correct Pi host/port in the environment
848 variables PIGPIO_ADDR/PIGPIO_PORT?
849 E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888
850 
851 Did you specify the correct Pi host/port in the
852 pigpio.pi() function? E.g. pigpio.pi('soft', 8888)"""
853 
854 _except_2 = """
855 Do you have permission to access the pigpio daemon?
856 Perhaps it was started with sudo pigpiod -nlocalhost"""
857 
858 _except_3 = """
859 Can't create callback thread.
860 Perhaps too many simultaneous pigpio connections."""
861 
862 class _socklock:
863  """
864  A class to store socket and lock.
865  """
866  def __init__(self):
867  self.s = None
868  self.l = threading.Lock()
869 
870 class error(Exception):
871  """pigpio module exception"""
872  def __init__(self, value):
873  self.value = value
874  def __str__(self):
875  return repr(self.value)
876 
877 class pulse:
878  """
879  A class to store pulse information.
880  """
881 
882  def __init__(self, gpio_on, gpio_off, delay):
883  """
884  Initialises a pulse.
885 
886  gpio_on:= the GPIO to switch on at the start of the pulse.
887  gpio_off:= the GPIO to switch off at the start of the pulse.
888  delay:= the delay in microseconds before the next pulse.
889 
890  """
891  self.gpio_on = gpio_on
892  self.gpio_off = gpio_off
893  self.delay = delay
894 
895 def error_text(errnum):
896  """
897  Returns a text description of a pigpio error.
898 
899  errnum:= <0, the error number
900 
901  ...
902  print(pigpio.error_text(-5))
903  level not 0-1
904  ...
905  """
906  for e in _errors:
907  if e[0] == errnum:
908  return e[1]
909  return "unknown error ({})".format(errnum)
910 
911 def tickDiff(t1, t2):
912  """
913  Returns the microsecond difference between two ticks.
914 
915  t1:= the earlier tick
916  t2:= the later tick
917 
918  ...
919  print(pigpio.tickDiff(4294967272, 12))
920  36
921  ...
922  """
923  tDiff = t2 - t1
924  if tDiff < 0:
925  tDiff += (1 << 32)
926  return tDiff
927 
928 # A couple of hacks to cope with different string handling
929 # between various Python versions
930 # 3 != 2.7.8 != 2.7.3
931 
932 if sys.hexversion < 0x03000000:
933  def _b(x):
934  return x
935 else:
936  def _b(x):
937  return x.encode('latin-1')
938 
939 if sys.hexversion < 0x02070800:
940  def _str(x):
941  return buffer(x)
942 else:
943  def _str(x):
944  return x
945 
946 def u2i(uint32):
947  """
948  Converts a 32 bit unsigned number to signed.
949 
950  uint32:= an unsigned 32 bit number
951 
952  ...
953  print(u2i(4294967272))
954  -24
955  print(u2i(37))
956  37
957  ...
958  """
959  mask = (2 ** 32) - 1
960  if uint32 & (1 << 31):
961  v = uint32 | ~mask
962  else:
963  v = uint32 & mask
964  return v
965 
966 def _u2i(uint32):
967  """
968  Converts a 32 bit unsigned number to signed. If the number
969  is negative it indicates an error. On error a pigpio
970  exception will be raised if exceptions is True.
971  """
972  v = u2i(uint32)
973  if v < 0:
974  if exceptions:
975  raise error(error_text(v))
976  return v
977 
978 def _pigpio_command(sl, cmd, p1, p2):
979  """
980  Runs a pigpio socket command.
981 
982  sl:= command socket and lock.
983  cmd:= the command to be executed.
984  p1:= command parameter 1 (if applicable).
985  p2:= command parameter 2 (if applicable).
986  """
987  res = PI_CMD_INTERRUPTED
988  with sl.l:
989  sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
990  dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
991  return res
992 
993 def _pigpio_command_nolock(sl, cmd, p1, p2):
994  """
995  Runs a pigpio socket command.
996 
997  sl:= command socket and lock.
998  cmd:= the command to be executed.
999  p1:= command parameter 1 (if applicable).
1000  p2:= command parameter 2 (if applicable).
1001  """
1002  res = PI_CMD_INTERRUPTED
1003  sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
1004  dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
1005  return res
1006 
1007 def _pigpio_command_ext(sl, cmd, p1, p2, p3, extents):
1008  """
1009  Runs an extended pigpio socket command.
1010 
1011  sl:= command socket and lock.
1012  cmd:= the command to be executed.
1013  p1:= command parameter 1 (if applicable).
1014  p2:= command parameter 2 (if applicable).
1015  p3:= total size in bytes of following extents
1016  extents:= additional data blocks
1017  """
1018  ext = bytearray(struct.pack('IIII', cmd, p1, p2, p3))
1019  for x in extents:
1020  if type(x) == type(""):
1021  ext.extend(_b(x))
1022  else:
1023  ext.extend(x)
1024  res = PI_CMD_INTERRUPTED
1025  with sl.l:
1026  sl.s.sendall(ext)
1027  dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
1028  return res
1029 
1030 def _pigpio_command_ext_nolock(sl, cmd, p1, p2, p3, extents):
1031  """
1032  Runs an extended pigpio socket command.
1033 
1034  sl:= command socket and lock.
1035  cmd:= the command to be executed.
1036  p1:= command parameter 1 (if applicable).
1037  p2:= command parameter 2 (if applicable).
1038  p3:= total size in bytes of following extents
1039  extents:= additional data blocks
1040  """
1041  res = PI_CMD_INTERRUPTED
1042  ext = bytearray(struct.pack('IIII', cmd, p1, p2, p3))
1043  for x in extents:
1044  if type(x) == type(""):
1045  ext.extend(_b(x))
1046  else:
1047  ext.extend(x)
1048  sl.s.sendall(ext)
1049  dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
1050  return res
1051 
1053  """
1054  An ADT class to hold event callback information.
1055  """
1056 
1057  def __init__(self, event, func):
1058  """
1059  Initialises an event callback ADT.
1060 
1061  event:= the event id.
1062  func:= a user function taking one argument, the event id.
1063  """
1064  self.event = event
1065  self.func = func
1066  self.bit = 1<<event
1067 
1069  """An ADT class to hold callback information."""
1070 
1071  def __init__(self, gpio, edge, func):
1072  """
1073  Initialises a callback ADT.
1074 
1075  gpio:= Broadcom GPIO number.
1076  edge:= EITHER_EDGE, RISING_EDGE, or FALLING_EDGE.
1077  func:= a user function taking three arguments (GPIO, level, tick).
1078  """
1079  self.gpio = gpio
1080  self.edge = edge
1081  self.func = func
1082  self.bit = 1<<gpio
1083 
1084 class _callback_thread(threading.Thread):
1085  """A class to encapsulate pigpio notification callbacks."""
1086  def __init__(self, control, host, port):
1087  """Initialises notifications."""
1088  threading.Thread.__init__(self)
1089  self.control = control
1090  self.sl = _socklock()
1091  self.go = False
1092  self.daemon = True
1093  self.monitor = 0
1094  self.event_bits = 0
1095  self.callbacks = []
1096  self.events = []
1097  self.sl.s = socket.create_connection((host, port), None)
1098  self.lastLevel = _pigpio_command(self.sl, _PI_CMD_BR1, 0, 0)
1099  self.handle = _u2i(_pigpio_command(self.sl, _PI_CMD_NOIB, 0, 0))
1100  self.go = True
1101  self.start()
1102 
1103  def stop(self):
1104  """Stops notifications."""
1105  if self.go:
1106  self.go = False
1107  self.sl.s.send(struct.pack('IIII', _PI_CMD_NC, self.handle, 0, 0))
1108 
1109  def append(self, callb):
1110  """Adds a callback to the notification thread."""
1111  self.callbacks.append(callb)
1112  self.monitor = self.monitor | callb.bit
1113  _pigpio_command(self.control, _PI_CMD_NB, self.handle, self.monitor)
1114 
1115  def remove(self, callb):
1116  """Removes a callback from the notification thread."""
1117  if callb in self.callbacks:
1118  self.callbacks.remove(callb)
1119  newMonitor = 0
1120  for c in self.callbacks:
1121  newMonitor |= c.bit
1122  if newMonitor != self.monitor:
1123  self.monitor = newMonitor
1125  self.control, _PI_CMD_NB, self.handle, self.monitor)
1126 
1127  def append_event(self, callb):
1128  """
1129  Adds an event callback to the notification thread.
1130  """
1131  self.events.append(callb)
1132  self.event_bits = self.event_bits | callb.bit
1133  _pigpio_command(self.control, _PI_CMD_EVM, self.handle, self.event_bits)
1134 
1135  def remove_event(self, callb):
1136  """
1137  Removes an event callback from the notification thread.
1138  """
1139  if callb in self.events:
1140  self.events.remove(callb)
1141  new_event_bits = 0
1142  for c in self.events:
1143  new_event_bits |= c.bit
1144  if new_event_bits != self.event_bits:
1145  self.event_bits = new_event_bits
1147  self.control, _PI_CMD_EVM, self.handle, self.event_bits)
1148 
1149  def run(self):
1150  """Runs the notification thread."""
1151 
1152  lastLevel = self.lastLevel
1153 
1154  RECV_SIZ = 4096
1155  MSG_SIZ = 12
1156 
1157  buf = bytes()
1158  while self.go:
1159 
1160  buf += self.sl.s.recv(RECV_SIZ)
1161  offset = 0
1162 
1163  while self.go and (len(buf) - offset) >= MSG_SIZ:
1164  msgbuf = buf[offset:offset + MSG_SIZ]
1165  offset += MSG_SIZ
1166  seq, flags, tick, level = (struct.unpack('HHII', msgbuf))
1167 
1168  if flags == 0:
1169  changed = level ^ lastLevel
1170  lastLevel = level
1171  for cb in self.callbacks:
1172  if cb.bit & changed:
1173  newLevel = 0
1174  if cb.bit & level:
1175  newLevel = 1
1176  if (cb.edge ^ newLevel):
1177  cb.func(cb.gpio, newLevel, tick)
1178  else:
1179  if flags & NTFY_FLAGS_WDOG:
1180  gpio = flags & NTFY_FLAGS_GPIO
1181  for cb in self.callbacks:
1182  if cb.gpio == gpio:
1183  cb.func(gpio, TIMEOUT, tick)
1184  elif flags & NTFY_FLAGS_EVENT:
1185  event = flags & NTFY_FLAGS_GPIO
1186  for cb in self.events:
1187  if cb.event == event:
1188  cb.func(event, tick)
1189  buf = buf[offset:]
1190 
1191  self.sl.s.close()
1192 
1194  """A class to provide GPIO level change callbacks."""
1195 
1196  def __init__(self, notify, user_gpio, edge=RISING_EDGE, func=None):
1197  """
1198  Initialise a callback and adds it to the notification thread.
1199  """
1200  self._notify = notify
1201  self.count=0
1202  self._reset = False
1203  if func is None:
1204  func=self._tally
1205  self.callb = _callback_ADT(user_gpio, edge, func)
1206  self._notify.append(self.callb)
1207 
1208  def cancel(self):
1209  """Cancels a callback by removing it from the notification thread."""
1210  self._notify.remove(self.callb)
1211 
1212  def _tally(self, user_gpio, level, tick):
1213  """Increment the callback called count."""
1214  if self._reset:
1215  self._reset = False
1216  self.count = 0
1217  self.count += 1
1218 
1219  def tally(self):
1220  """
1221  Provides a count of how many times the default tally
1222  callback has triggered.
1223 
1224  The count will be zero if the user has supplied their own
1225  callback function.
1226  """
1227  return self.count
1228 
1229  def reset_tally(self):
1230  """
1231  Resets the tally count to zero.
1232  """
1233  self._reset = True
1234  self.count = 0
1235 
1236 class _event:
1237  """A class to provide event callbacks."""
1238 
1239  def __init__(self, notify, event, func=None):
1240  """
1241  Initialise an event and adds it to the notification thread.
1242  """
1243  self._notify = notify
1244  self.count=0
1245  self._reset = False
1246  if func is None:
1247  func=self._tally
1248  self.callb = _event_ADT(event, func)
1249  self._notify.append_event(self.callb)
1250 
1251  def cancel(self):
1252  """
1253  Cancels a event callback by removing it from the
1254  notification thread.
1255  """
1256  self._notify.remove_event(self.callb)
1257 
1258  def _tally(self, event, tick):
1259  """Increment the event callback called count."""
1260  if self._reset:
1261  self._reset = False
1262  self.count = 0
1263  self.count += 1
1264 
1265  def tally(self):
1266  """
1267  Provides a count of how many times the default tally
1268  callback has triggered.
1269 
1270  The count will be zero if the user has supplied their own
1271  callback function.
1272  """
1273  return self.count
1274 
1275  def reset_tally(self):
1276  """
1277  Resets the tally count to zero.
1278  """
1279  self._reset = True
1280  self.count = 0
1281 
1283  """Encapsulates waiting for GPIO edges."""
1284 
1285  def __init__(self, notify, gpio, edge, timeout):
1286  """Initialises a wait_for_edge."""
1287  self._notify = notify
1288  self.callb = _callback_ADT(gpio, edge, self.func)
1289  self.trigger = False
1290  self._notify.append(self.callb)
1291  self.start = time.time()
1292  while (self.trigger == False) and ((time.time()-self.start) < timeout):
1293  time.sleep(0.05)
1294  self._notify.remove(self.callb)
1295 
1296  def func(self, gpio, level, tick):
1297  """Sets wait_for_edge triggered."""
1298  self.trigger = True
1299 
1301  """Encapsulates waiting for an event."""
1302 
1303  def __init__(self, notify, event, timeout):
1304  """Initialises wait_for_event."""
1305  self._notify = notify
1306  self.callb = _event_ADT(event, self.func)
1307  self.trigger = False
1308  self._notify.append_event(self.callb)
1309  self.start = time.time()
1310  while (self.trigger == False) and ((time.time()-self.start) < timeout):
1311  time.sleep(0.05)
1312  self._notify.remove_event(self.callb)
1313 
1314  def func(self, event, tick):
1315  """Sets wait_for_event triggered."""
1316  self.trigger = True
1317 
1318 class pi():
1319 
1320  def _rxbuf(self, count):
1321  """Returns count bytes from the command socket."""
1322  ext = bytearray(self.sl.s.recv(count))
1323  while len(ext) < count:
1324  ext.extend(self.sl.s.recv(count - len(ext)))
1325  return ext
1326 
1327  def set_mode(self, gpio, mode):
1328  """
1329  Sets the GPIO mode.
1330 
1331  gpio:= 0-53.
1332  mode:= INPUT, OUTPUT, ALT0, ALT1, ALT2, ALT3, ALT4, ALT5.
1333 
1334  ...
1335  pi.set_mode( 4, pigpio.INPUT) # GPIO 4 as input
1336  pi.set_mode(17, pigpio.OUTPUT) # GPIO 17 as output
1337  pi.set_mode(24, pigpio.ALT2) # GPIO 24 as ALT2
1338  ...
1339  """
1340  return _u2i(_pigpio_command(self.sl, _PI_CMD_MODES, gpio, mode))
1341 
1342  def get_mode(self, gpio):
1343  """
1344  Returns the GPIO mode.
1345 
1346  gpio:= 0-53.
1347 
1348  Returns a value as follows
1349 
1350  . .
1351  0 = INPUT
1352  1 = OUTPUT
1353  2 = ALT5
1354  3 = ALT4
1355  4 = ALT0
1356  5 = ALT1
1357  6 = ALT2
1358  7 = ALT3
1359  . .
1360 
1361  ...
1362  print(pi.get_mode(0))
1363  4
1364  ...
1365  """
1366  return _u2i(_pigpio_command(self.sl, _PI_CMD_MODEG, gpio, 0))
1367 
1368  def set_pull_up_down(self, gpio, pud):
1369  """
1370  Sets or clears the internal GPIO pull-up/down resistor.
1371 
1372  gpio:= 0-53.
1373  pud:= PUD_UP, PUD_DOWN, PUD_OFF.
1374 
1375  ...
1376  pi.set_pull_up_down(17, pigpio.PUD_OFF)
1377  pi.set_pull_up_down(23, pigpio.PUD_UP)
1378  pi.set_pull_up_down(24, pigpio.PUD_DOWN)
1379  ...
1380  """
1381  return _u2i(_pigpio_command(self.sl, _PI_CMD_PUD, gpio, pud))
1382 
1383  def read(self, gpio):
1384  """
1385  Returns the GPIO level.
1386 
1387  gpio:= 0-53.
1388 
1389  ...
1390  pi.set_mode(23, pigpio.INPUT)
1391 
1392  pi.set_pull_up_down(23, pigpio.PUD_DOWN)
1393  print(pi.read(23))
1394  0
1395 
1396  pi.set_pull_up_down(23, pigpio.PUD_UP)
1397  print(pi.read(23))
1398  1
1399  ...
1400  """
1401  return _u2i(_pigpio_command(self.sl, _PI_CMD_READ, gpio, 0))
1402 
1403  def write(self, gpio, level):
1404  """
1405  Sets the GPIO level.
1406 
1407  GPIO:= 0-53.
1408  level:= 0, 1.
1409 
1410  If PWM or servo pulses are active on the GPIO they are
1411  switched off.
1412 
1413  ...
1414  pi.set_mode(17, pigpio.OUTPUT)
1415 
1416  pi.write(17,0)
1417  print(pi.read(17))
1418  0
1419 
1420  pi.write(17,1)
1421  print(pi.read(17))
1422  1
1423  ...
1424  """
1425  return _u2i(_pigpio_command(self.sl, _PI_CMD_WRITE, gpio, level))
1426 
1427  def set_PWM_dutycycle(self, user_gpio, dutycycle):
1428  """
1429  Starts (non-zero dutycycle) or stops (0) PWM pulses on the GPIO.
1430 
1431  user_gpio:= 0-31.
1432  dutycycle:= 0-range (range defaults to 255).
1433 
1434  The [*set_PWM_range*] function can change the default range of 255.
1435 
1436  ...
1437  pi.set_PWM_dutycycle(4, 0) # PWM off
1438  pi.set_PWM_dutycycle(4, 64) # PWM 1/4 on
1439  pi.set_PWM_dutycycle(4, 128) # PWM 1/2 on
1440  pi.set_PWM_dutycycle(4, 192) # PWM 3/4 on
1441  pi.set_PWM_dutycycle(4, 255) # PWM full on
1442  ...
1443  """
1444  return _u2i(_pigpio_command(
1445  self.sl, _PI_CMD_PWM, user_gpio, int(dutycycle)))
1446 
1447  def get_PWM_dutycycle(self, user_gpio):
1448  """
1449  Returns the PWM dutycycle being used on the GPIO.
1450 
1451  user_gpio:= 0-31.
1452 
1453  Returns the PWM dutycycle.
1454 
1455 
1456  For normal PWM the dutycycle will be out of the defined range
1457  for the GPIO (see [*get_PWM_range*]).
1458 
1459  If a hardware clock is active on the GPIO the reported
1460  dutycycle will be 500000 (500k) out of 1000000 (1M).
1461 
1462  If hardware PWM is active on the GPIO the reported dutycycle
1463  will be out of a 1000000 (1M).
1464 
1465  ...
1466  pi.set_PWM_dutycycle(4, 25)
1467  print(pi.get_PWM_dutycycle(4))
1468  25
1469 
1470  pi.set_PWM_dutycycle(4, 203)
1471  print(pi.get_PWM_dutycycle(4))
1472  203
1473  ...
1474  """
1475  return _u2i(_pigpio_command(self.sl, _PI_CMD_GDC, user_gpio, 0))
1476 
1477  def set_PWM_range(self, user_gpio, range_):
1478  """
1479  Sets the range of PWM values to be used on the GPIO.
1480 
1481  user_gpio:= 0-31.
1482  range_:= 25-40000.
1483 
1484  ...
1485  pi.set_PWM_range(9, 100) # now 25 1/4, 50 1/2, 75 3/4 on
1486  pi.set_PWM_range(9, 500) # now 125 1/4, 250 1/2, 375 3/4 on
1487  pi.set_PWM_range(9, 3000) # now 750 1/4, 1500 1/2, 2250 3/4 on
1488  ...
1489  """
1490  return _u2i(_pigpio_command(self.sl, _PI_CMD_PRS, user_gpio, range_))
1491 
1492  def get_PWM_range(self, user_gpio):
1493  """
1494  Returns the range of PWM values being used on the GPIO.
1495 
1496  user_gpio:= 0-31.
1497 
1498  If a hardware clock or hardware PWM is active on the GPIO
1499  the reported range will be 1000000 (1M).
1500 
1501  ...
1502  pi.set_PWM_range(9, 500)
1503  print(pi.get_PWM_range(9))
1504  500
1505  ...
1506  """
1507  return _u2i(_pigpio_command(self.sl, _PI_CMD_PRG, user_gpio, 0))
1508 
1509  def get_PWM_real_range(self, user_gpio):
1510  """
1511  Returns the real (underlying) range of PWM values being
1512  used on the GPIO.
1513 
1514  user_gpio:= 0-31.
1515 
1516  If a hardware clock is active on the GPIO the reported
1517  real range will be 1000000 (1M).
1518 
1519  If hardware PWM is active on the GPIO the reported real range
1520  will be approximately 250M divided by the set PWM frequency.
1521 
1522  ...
1523  pi.set_PWM_frequency(4, 800)
1524  print(pi.get_PWM_real_range(4))
1525  250
1526  ...
1527  """
1528  return _u2i(_pigpio_command(self.sl, _PI_CMD_PRRG, user_gpio, 0))
1529 
1530  def set_PWM_frequency(self, user_gpio, frequency):
1531  """
1532  Sets the frequency (in Hz) of the PWM to be used on the GPIO.
1533 
1534  user_gpio:= 0-31.
1535  frequency:= >=0 Hz
1536 
1537  Returns the numerically closest frequency if OK, otherwise
1538  PI_BAD_USER_GPIO or PI_NOT_PERMITTED.
1539 
1540  If PWM is currently active on the GPIO it will be switched
1541  off and then back on at the new frequency.
1542 
1543  Each GPIO can be independently set to one of 18 different
1544  PWM frequencies.
1545 
1546  The selectable frequencies depend upon the sample rate which
1547  may be 1, 2, 4, 5, 8, or 10 microseconds (default 5). The
1548  sample rate is set when the pigpio daemon is started.
1549 
1550  The frequencies for each sample rate are:
1551 
1552  . .
1553  Hertz
1554 
1555  1: 40000 20000 10000 8000 5000 4000 2500 2000 1600
1556  1250 1000 800 500 400 250 200 100 50
1557 
1558  2: 20000 10000 5000 4000 2500 2000 1250 1000 800
1559  625 500 400 250 200 125 100 50 25
1560 
1561  4: 10000 5000 2500 2000 1250 1000 625 500 400
1562  313 250 200 125 100 63 50 25 13
1563  sample
1564  rate
1565  (us) 5: 8000 4000 2000 1600 1000 800 500 400 320
1566  250 200 160 100 80 50 40 20 10
1567 
1568  8: 5000 2500 1250 1000 625 500 313 250 200
1569  156 125 100 63 50 31 25 13 6
1570 
1571  10: 4000 2000 1000 800 500 400 250 200 160
1572  125 100 80 50 40 25 20 10 5
1573  . .
1574 
1575  ...
1576  pi.set_PWM_frequency(4,0)
1577  print(pi.get_PWM_frequency(4))
1578  10
1579 
1580  pi.set_PWM_frequency(4,100000)
1581  print(pi.get_PWM_frequency(4))
1582  8000
1583  ...
1584  """
1585  return _u2i(
1586  _pigpio_command(self.sl, _PI_CMD_PFS, user_gpio, frequency))
1587 
1588  def get_PWM_frequency(self, user_gpio):
1589  """
1590  Returns the frequency of PWM being used on the GPIO.
1591 
1592  user_gpio:= 0-31.
1593 
1594  Returns the frequency (in Hz) used for the GPIO.
1595 
1596  For normal PWM the frequency will be that defined for the GPIO
1597  by [*set_PWM_frequency*].
1598 
1599  If a hardware clock is active on the GPIO the reported frequency
1600  will be that set by [*hardware_clock*].
1601 
1602  If hardware PWM is active on the GPIO the reported frequency
1603  will be that set by [*hardware_PWM*].
1604 
1605  ...
1606  pi.set_PWM_frequency(4,0)
1607  print(pi.get_PWM_frequency(4))
1608  10
1609 
1610  pi.set_PWM_frequency(4, 800)
1611  print(pi.get_PWM_frequency(4))
1612  800
1613  ...
1614  """
1615  return _u2i(_pigpio_command(self.sl, _PI_CMD_PFG, user_gpio, 0))
1616 
1617  def set_servo_pulsewidth(self, user_gpio, pulsewidth):
1618  """
1619  Starts (500-2500) or stops (0) servo pulses on the GPIO.
1620 
1621  user_gpio:= 0-31.
1622  pulsewidth:= 0 (off),
1623  500 (most anti-clockwise) - 2500 (most clockwise).
1624 
1625  The selected pulsewidth will continue to be transmitted until
1626  changed by a subsequent call to set_servo_pulsewidth.
1627 
1628  The pulsewidths supported by servos varies and should probably
1629  be determined by experiment. A value of 1500 should always be
1630  safe and represents the mid-point of rotation.
1631 
1632  You can DAMAGE a servo if you command it to move beyond its
1633  limits.
1634 
1635  ...
1636  pi.set_servo_pulsewidth(17, 0) # off
1637  pi.set_servo_pulsewidth(17, 1000) # safe anti-clockwise
1638  pi.set_servo_pulsewidth(17, 1500) # centre
1639  pi.set_servo_pulsewidth(17, 2000) # safe clockwise
1640  ...
1641  """
1642  return _u2i(_pigpio_command(
1643  self.sl, _PI_CMD_SERVO, user_gpio, int(pulsewidth)))
1644 
1645  def get_servo_pulsewidth(self, user_gpio):
1646  """
1647  Returns the servo pulsewidth being used on the GPIO.
1648 
1649  user_gpio:= 0-31.
1650 
1651  Returns the servo pulsewidth.
1652 
1653  ...
1654  pi.set_servo_pulsewidth(4, 525)
1655  print(pi.get_servo_pulsewidth(4))
1656  525
1657 
1658  pi.set_servo_pulsewidth(4, 2130)
1659  print(pi.get_servo_pulsewidth(4))
1660  2130
1661  ...
1662  """
1663  return _u2i(_pigpio_command(self.sl, _PI_CMD_GPW, user_gpio, 0))
1664 
1665  def notify_open(self):
1666  """
1667  Returns a notification handle (>=0).
1668 
1669  A notification is a method for being notified of GPIO state
1670  changes via a pipe.
1671 
1672  Pipes are only accessible from the local machine so this
1673  function serves no purpose if you are using Python from a
1674  remote machine. The in-built (socket) notifications
1675  provided by [*callback*] should be used instead.
1676 
1677  Notifications for handle x will be available at the pipe
1678  named /dev/pigpiox (where x is the handle number).
1679 
1680  E.g. if the function returns 15 then the notifications must be
1681  read from /dev/pigpio15.
1682 
1683  Notifications have the following structure:
1684 
1685  . .
1686  H seqno
1687  H flags
1688  I tick
1689  I level
1690  . .
1691 
1692  seqno: starts at 0 each time the handle is opened and then
1693  increments by one for each report.
1694 
1695  flags: three flags are defined, PI_NTFY_FLAGS_WDOG,
1696  PI_NTFY_FLAGS_ALIVE, and PI_NTFY_FLAGS_EVENT.
1697 
1698  If bit 5 is set (PI_NTFY_FLAGS_WDOG) then bits 0-4 of the
1699  flags indicate a GPIO which has had a watchdog timeout.
1700 
1701  If bit 6 is set (PI_NTFY_FLAGS_ALIVE) this indicates a keep
1702  alive signal on the pipe/socket and is sent once a minute
1703  in the absence of other notification activity.
1704 
1705  If bit 7 is set (PI_NTFY_FLAGS_EVENT) then bits 0-4 of the
1706  flags indicate an event which has been triggered.
1707 
1708 
1709  tick: the number of microseconds since system boot. It wraps
1710  around after 1h12m.
1711 
1712  level: indicates the level of each GPIO. If bit 1<<x is set
1713  then GPIO x is high.
1714 
1715  ...
1716  h = pi.notify_open()
1717  if h >= 0:
1718  pi.notify_begin(h, 1234)
1719  ...
1720  """
1721  return _u2i(_pigpio_command(self.sl, _PI_CMD_NO, 0, 0))
1722 
1723  def notify_begin(self, handle, bits):
1724  """
1725  Starts notifications on a handle.
1726 
1727  handle:= >=0 (as returned by a prior call to [*notify_open*])
1728  bits:= a 32 bit mask indicating the GPIO to be notified.
1729 
1730  The notification sends state changes for each GPIO whose
1731  corresponding bit in bits is set.
1732 
1733  The following code starts notifications for GPIO 1, 4,
1734  6, 7, and 10 (1234 = 0x04D2 = 0b0000010011010010).
1735 
1736  ...
1737  h = pi.notify_open()
1738  if h >= 0:
1739  pi.notify_begin(h, 1234)
1740  ...
1741  """
1742  return _u2i(_pigpio_command(self.sl, _PI_CMD_NB, handle, bits))
1743 
1744  def notify_pause(self, handle):
1745  """
1746  Pauses notifications on a handle.
1747 
1748  handle:= >=0 (as returned by a prior call to [*notify_open*])
1749 
1750  Notifications for the handle are suspended until
1751  [*notify_begin*] is called again.
1752 
1753  ...
1754  h = pi.notify_open()
1755  if h >= 0:
1756  pi.notify_begin(h, 1234)
1757  ...
1758  pi.notify_pause(h)
1759  ...
1760  pi.notify_begin(h, 1234)
1761  ...
1762  ...
1763  """
1764  return _u2i(_pigpio_command(self.sl, _PI_CMD_NB, handle, 0))
1765 
1766  def notify_close(self, handle):
1767  """
1768  Stops notifications on a handle and releases the handle for reuse.
1769 
1770  handle:= >=0 (as returned by a prior call to [*notify_open*])
1771 
1772  ...
1773  h = pi.notify_open()
1774  if h >= 0:
1775  pi.notify_begin(h, 1234)
1776  ...
1777  pi.notify_close(h)
1778  ...
1779  ...
1780  """
1781  return _u2i(_pigpio_command(self.sl, _PI_CMD_NC, handle, 0))
1782 
1783  def set_watchdog(self, user_gpio, wdog_timeout):
1784  """
1785  Sets a watchdog timeout for a GPIO.
1786 
1787  user_gpio:= 0-31.
1788  wdog_timeout:= 0-60000.
1789 
1790  The watchdog is nominally in milliseconds.
1791 
1792  Only one watchdog may be registered per GPIO.
1793 
1794  The watchdog may be cancelled by setting timeout to 0.
1795 
1796  Once a watchdog has been started callbacks for the GPIO
1797  will be triggered every timeout interval after the last
1798  GPIO activity.
1799 
1800  The callback will receive the special level TIMEOUT.
1801 
1802  ...
1803  pi.set_watchdog(23, 1000) # 1000 ms watchdog on GPIO 23
1804  pi.set_watchdog(23, 0) # cancel watchdog on GPIO 23
1805  ...
1806  """
1807  return _u2i(_pigpio_command(
1808  self.sl, _PI_CMD_WDOG, user_gpio, int(wdog_timeout)))
1809 
1810  def read_bank_1(self):
1811  """
1812  Returns the levels of the bank 1 GPIO (GPIO 0-31).
1813 
1814  The returned 32 bit integer has a bit set if the corresponding
1815  GPIO is high. GPIO n has bit value (1<<n).
1816 
1817  ...
1818  print(bin(pi.read_bank_1()))
1819  0b10010100000011100100001001111
1820  ...
1821  """
1822  return _pigpio_command(self.sl, _PI_CMD_BR1, 0, 0)
1823 
1824  def read_bank_2(self):
1825  """
1826  Returns the levels of the bank 2 GPIO (GPIO 32-53).
1827 
1828  The returned 32 bit integer has a bit set if the corresponding
1829  GPIO is high. GPIO n has bit value (1<<(n-32)).
1830 
1831  ...
1832  print(bin(pi.read_bank_2()))
1833  0b1111110000000000000000
1834  ...
1835  """
1836  return _pigpio_command(self.sl, _PI_CMD_BR2, 0, 0)
1837 
1838  def clear_bank_1(self, bits):
1839  """
1840  Clears GPIO 0-31 if the corresponding bit in bits is set.
1841 
1842  bits:= a 32 bit mask with 1 set if the corresponding GPIO is
1843  to be cleared.
1844 
1845  A returned status of PI_SOME_PERMITTED indicates that the user
1846  is not allowed to write to one or more of the GPIO.
1847 
1848  ...
1849  pi.clear_bank_1(int("111110010000",2))
1850  ...
1851  """
1852  return _u2i(_pigpio_command(self.sl, _PI_CMD_BC1, bits, 0))
1853 
1854  def clear_bank_2(self, bits):
1855  """
1856  Clears GPIO 32-53 if the corresponding bit (0-21) in bits is set.
1857 
1858  bits:= a 32 bit mask with 1 set if the corresponding GPIO is
1859  to be cleared.
1860 
1861  A returned status of PI_SOME_PERMITTED indicates that the user
1862  is not allowed to write to one or more of the GPIO.
1863 
1864  ...
1865  pi.clear_bank_2(0x1010)
1866  ...
1867  """
1868  return _u2i(_pigpio_command(self.sl, _PI_CMD_BC2, bits, 0))
1869 
1870  def set_bank_1(self, bits):
1871  """
1872  Sets GPIO 0-31 if the corresponding bit in bits is set.
1873 
1874  bits:= a 32 bit mask with 1 set if the corresponding GPIO is
1875  to be set.
1876 
1877  A returned status of PI_SOME_PERMITTED indicates that the user
1878  is not allowed to write to one or more of the GPIO.
1879 
1880  ...
1881  pi.set_bank_1(int("111110010000",2))
1882  ...
1883  """
1884  return _u2i(_pigpio_command(self.sl, _PI_CMD_BS1, bits, 0))
1885 
1886  def set_bank_2(self, bits):
1887  """
1888  Sets GPIO 32-53 if the corresponding bit (0-21) in bits is set.
1889 
1890  bits:= a 32 bit mask with 1 set if the corresponding GPIO is
1891  to be set.
1892 
1893  A returned status of PI_SOME_PERMITTED indicates that the user
1894  is not allowed to write to one or more of the GPIO.
1895 
1896  ...
1897  pi.set_bank_2(0x303)
1898  ...
1899  """
1900  return _u2i(_pigpio_command(self.sl, _PI_CMD_BS2, bits, 0))
1901 
1902  def hardware_clock(self, gpio, clkfreq):
1903  """
1904  Starts a hardware clock on a GPIO at the specified frequency.
1905  Frequencies above 30MHz are unlikely to work.
1906 
1907  gpio:= see description
1908  clkfreq:= 0 (off) or 4689-250000000 (250M)
1909 
1910 
1911  Returns 0 if OK, otherwise PI_NOT_PERMITTED, PI_BAD_GPIO,
1912  PI_NOT_HCLK_GPIO, PI_BAD_HCLK_FREQ,or PI_BAD_HCLK_PASS.
1913 
1914  The same clock is available on multiple GPIO. The latest
1915  frequency setting will be used by all GPIO which share a clock.
1916 
1917  The GPIO must be one of the following:
1918 
1919  . .
1920  4 clock 0 All models
1921  5 clock 1 All models but A and B (reserved for system use)
1922  6 clock 2 All models but A and B
1923  20 clock 0 All models but A and B
1924  21 clock 1 All models but A and Rev.2 B (reserved for system use)
1925 
1926  32 clock 0 Compute module only
1927  34 clock 0 Compute module only
1928  42 clock 1 Compute module only (reserved for system use)
1929  43 clock 2 Compute module only
1930  44 clock 1 Compute module only (reserved for system use)
1931  . .
1932 
1933  Access to clock 1 is protected by a password as its use will
1934  likely crash the Pi. The password is given by or'ing 0x5A000000
1935  with the GPIO number.
1936 
1937  ...
1938  pi.hardware_clock(4, 5000) # 5 KHz clock on GPIO 4
1939 
1940  pi.hardware_clock(4, 40000000) # 40 MHz clock on GPIO 4
1941  ...
1942  """
1943  return _u2i(_pigpio_command(self.sl, _PI_CMD_HC, gpio, clkfreq))
1944 
1945  def hardware_PWM(self, gpio, PWMfreq, PWMduty):
1946  """
1947  Starts hardware PWM on a GPIO at the specified frequency
1948  and dutycycle. Frequencies above 30MHz are unlikely to work.
1949 
1950  NOTE: Any waveform started by [*wave_send_once*],
1951  [*wave_send_repeat*], or [*wave_chain*] will be cancelled.
1952 
1953  This function is only valid if the pigpio main clock is PCM.
1954  The main clock defaults to PCM but may be overridden when the
1955  pigpio daemon is started (option -t).
1956 
1957  gpio:= see descripton
1958  PWMfreq:= 0 (off) or 1-125000000 (125M).
1959  PWMduty:= 0 (off) to 1000000 (1M)(fully on).
1960 
1961  Returns 0 if OK, otherwise PI_NOT_PERMITTED, PI_BAD_GPIO,
1962  PI_NOT_HPWM_GPIO, PI_BAD_HPWM_DUTY, PI_BAD_HPWM_FREQ.
1963 
1964  The same PWM channel is available on multiple GPIO.
1965  The latest frequency and dutycycle setting will be used
1966  by all GPIO which share a PWM channel.
1967 
1968  The GPIO must be one of the following:
1969 
1970  . .
1971  12 PWM channel 0 All models but A and B
1972  13 PWM channel 1 All models but A and B
1973  18 PWM channel 0 All models
1974  19 PWM channel 1 All models but A and B
1975 
1976  40 PWM channel 0 Compute module only
1977  41 PWM channel 1 Compute module only
1978  45 PWM channel 1 Compute module only
1979  52 PWM channel 0 Compute module only
1980  53 PWM channel 1 Compute module only
1981  . .
1982 
1983  The actual number of steps beween off and fully on is the
1984  integral part of 250 million divided by PWMfreq.
1985 
1986  The actual frequency set is 250 million / steps.
1987 
1988  There will only be a million steps for a PWMfreq of 250.
1989  Lower frequencies will have more steps and higher
1990  frequencies will have fewer steps. PWMduty is
1991  automatically scaled to take this into account.
1992 
1993  ...
1994  pi.hardware_PWM(18, 800, 250000) # 800Hz 25% dutycycle
1995 
1996  pi.hardware_PWM(18, 2000, 750000) # 2000Hz 75% dutycycle
1997  ...
1998  """
1999  # pigpio message format
2000 
2001  # I p1 gpio
2002  # I p2 PWMfreq
2003  # I p3 4
2004  ## extension ##
2005  # I PWMdutycycle
2006  extents = [struct.pack("I", PWMduty)]
2007  return _u2i(_pigpio_command_ext(
2008  self.sl, _PI_CMD_HP, gpio, PWMfreq, 4, extents))
2009 
2010 
2011  def get_current_tick(self):
2012  """
2013  Returns the current system tick.
2014 
2015  Tick is the number of microseconds since system boot. As an
2016  unsigned 32 bit quantity tick wraps around approximately
2017  every 71.6 minutes.
2018 
2019  ...
2020  t1 = pi.get_current_tick()
2021  time.sleep(1)
2022  t2 = pi.get_current_tick()
2023  ...
2024  """
2025  return _pigpio_command(self.sl, _PI_CMD_TICK, 0, 0)
2026 
2028  """
2029  Returns the Pi's hardware revision number.
2030 
2031  The hardware revision is the last few characters on the
2032  Revision line of /proc/cpuinfo.
2033 
2034  The revision number can be used to determine the assignment
2035  of GPIO to pins (see [*gpio*]).
2036 
2037  There are at least three types of board.
2038 
2039  Type 1 boards have hardware revision numbers of 2 and 3.
2040 
2041  Type 2 boards have hardware revision numbers of 4, 5, 6, and 15.
2042 
2043  Type 3 boards have hardware revision numbers of 16 or greater.
2044 
2045  If the hardware revision can not be found or is not a valid
2046  hexadecimal number the function returns 0.
2047 
2048  ...
2049  print(pi.get_hardware_revision())
2050  2
2051  ...
2052  """
2053  return _pigpio_command(self.sl, _PI_CMD_HWVER, 0, 0)
2054 
2056  """
2057  Returns the pigpio software version.
2058 
2059  ...
2060  v = pi.get_pigpio_version()
2061  ...
2062  """
2063  return _pigpio_command(self.sl, _PI_CMD_PIGPV, 0, 0)
2064 
2065  def wave_clear(self):
2066  """
2067  Clears all waveforms and any data added by calls to the
2068  [*wave_add_**] functions.
2069 
2070  ...
2071  pi.wave_clear()
2072  ...
2073  """
2074  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVCLR, 0, 0))
2075 
2076  def wave_add_new(self):
2077  """
2078  Starts a new empty waveform.
2079 
2080  You would not normally need to call this function as it is
2081  automatically called after a waveform is created with the
2082  [*wave_create*] function.
2083 
2084  ...
2085  pi.wave_add_new()
2086  ...
2087  """
2088  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVNEW, 0, 0))
2089 
2090  def wave_add_generic(self, pulses):
2091  """
2092  Adds a list of pulses to the current waveform.
2093 
2094  pulses:= list of pulses to add to the waveform.
2095 
2096  Returns the new total number of pulses in the current waveform.
2097 
2098  The pulses are interleaved in time order within the existing
2099  waveform (if any).
2100 
2101  Merging allows the waveform to be built in parts, that is the
2102  settings for GPIO#1 can be added, and then GPIO#2 etc.
2103 
2104  If the added waveform is intended to start after or within
2105  the existing waveform then the first pulse should consist
2106  solely of a delay.
2107 
2108  ...
2109  G1=4
2110  G2=24
2111 
2112  pi.set_mode(G1, pigpio.OUTPUT)
2113  pi.set_mode(G2, pigpio.OUTPUT)
2114 
2115  flash_500=[] # flash every 500 ms
2116  flash_100=[] # flash every 100 ms
2117 
2118  # ON OFF DELAY
2119 
2120  flash_500.append(pigpio.pulse(1<<G1, 1<<G2, 500000))
2121  flash_500.append(pigpio.pulse(1<<G2, 1<<G1, 500000))
2122 
2123  flash_100.append(pigpio.pulse(1<<G1, 1<<G2, 100000))
2124  flash_100.append(pigpio.pulse(1<<G2, 1<<G1, 100000))
2125 
2126  pi.wave_clear() # clear any existing waveforms
2127 
2128  pi.wave_add_generic(flash_500) # 500 ms flashes
2129  f500 = pi.wave_create() # create and save id
2130 
2131  pi.wave_add_generic(flash_100) # 100 ms flashes
2132  f100 = pi.wave_create() # create and save id
2133 
2134  pi.wave_send_repeat(f500)
2135 
2136  time.sleep(4)
2137 
2138  pi.wave_send_repeat(f100)
2139 
2140  time.sleep(4)
2141 
2142  pi.wave_send_repeat(f500)
2143 
2144  time.sleep(4)
2145 
2146  pi.wave_tx_stop() # stop waveform
2147 
2148  pi.wave_clear() # clear all waveforms
2149  ...
2150  """
2151  # pigpio message format
2152 
2153  # I p1 0
2154  # I p2 0
2155  # I p3 pulses * 12
2156  ## extension ##
2157  # III on/off/delay * pulses
2158  if len(pulses):
2159  ext = bytearray()
2160  for p in pulses:
2161  ext.extend(struct.pack("III", p.gpio_on, p.gpio_off, p.delay))
2162  extents = [ext]
2163  return _u2i(_pigpio_command_ext(
2164  self.sl, _PI_CMD_WVAG, 0, 0, len(pulses)*12, extents))
2165  else:
2166  return 0
2167 
2168  def wave_add_serial(
2169  self, user_gpio, baud, data, offset=0, bb_bits=8, bb_stop=2):
2170  """
2171  Adds a waveform representing serial data to the existing
2172  waveform (if any). The serial data starts [*offset*]
2173  microseconds from the start of the waveform.
2174 
2175  user_gpio:= GPIO to transmit data. You must set the GPIO mode
2176  to output.
2177  baud:= 50-1000000 bits per second.
2178  data:= the bytes to write.
2179  offset:= number of microseconds from the start of the
2180  waveform, default 0.
2181  bb_bits:= number of data bits, default 8.
2182  bb_stop:= number of stop half bits, default 2.
2183 
2184  Returns the new total number of pulses in the current waveform.
2185 
2186  The serial data is formatted as one start bit, [*bb_bits*]
2187  data bits, and [*bb_stop*]/2 stop bits.
2188 
2189  It is legal to add serial data streams with different baud
2190  rates to the same waveform.
2191 
2192  The bytes required for each character depend upon [*bb_bits*].
2193 
2194  For [*bb_bits*] 1-8 there will be one byte per character.
2195  For [*bb_bits*] 9-16 there will be two bytes per character.
2196  For [*bb_bits*] 17-32 there will be four bytes per character.
2197 
2198  ...
2199  pi.wave_add_serial(4, 300, 'Hello world')
2200 
2201  pi.wave_add_serial(4, 300, b"Hello world")
2202 
2203  pi.wave_add_serial(4, 300, b'\\x23\\x01\\x00\\x45')
2204 
2205  pi.wave_add_serial(17, 38400, [23, 128, 234], 5000)
2206  ...
2207  """
2208  # pigpio message format
2209 
2210  # I p1 gpio
2211  # I p2 baud
2212  # I p3 len+12
2213  ## extension ##
2214  # I bb_bits
2215  # I bb_stop
2216  # I offset
2217  # s len data bytes
2218  if len(data):
2219  extents = [struct.pack("III", bb_bits, bb_stop, offset), data]
2220  return _u2i(_pigpio_command_ext(
2221  self.sl, _PI_CMD_WVAS, user_gpio, baud, len(data)+12, extents))
2222  else:
2223  return 0
2224 
2225  def wave_create(self):
2226  """
2227  Creates a waveform from the data provided by the prior calls
2228  to the [*wave_add_**] functions.
2229 
2230  Returns a wave id (>=0) if OK, otherwise PI_EMPTY_WAVEFORM,
2231  PI_TOO_MANY_CBS, PI_TOO_MANY_OOL, or PI_NO_WAVEFORM_ID.
2232 
2233  The data provided by the [*wave_add_**] functions is consumed by
2234  this function.
2235 
2236  As many waveforms may be created as there is space available.
2237  The wave id is passed to [*wave_send_**] to specify the waveform
2238  to transmit.
2239 
2240  Normal usage would be
2241 
2242  Step 1. [*wave_clear*] to clear all waveforms and added data.
2243 
2244  Step 2. [*wave_add_**] calls to supply the waveform data.
2245 
2246  Step 3. [*wave_create*] to create the waveform and get a unique id
2247 
2248  Repeat steps 2 and 3 as needed.
2249 
2250  Step 4. [*wave_send_**] with the id of the waveform to transmit.
2251 
2252  A waveform comprises one or more pulses.
2253 
2254  A pulse specifies
2255 
2256  1) the GPIO to be switched on at the start of the pulse.
2257  2) the GPIO to be switched off at the start of the pulse.
2258  3) the delay in microseconds before the next pulse.
2259 
2260  Any or all the fields can be zero. It doesn't make any sense
2261  to set all the fields to zero (the pulse will be ignored).
2262 
2263  When a waveform is started each pulse is executed in order with
2264  the specified delay between the pulse and the next.
2265 
2266  ...
2267  wid = pi.wave_create()
2268  ...
2269  """
2270  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVCRE, 0, 0))
2271 
2272  def wave_delete(self, wave_id):
2273  """
2274  This function deletes the waveform with id wave_id.
2275 
2276  wave_id:= >=0 (as returned by a prior call to [*wave_create*]).
2277 
2278  Wave ids are allocated in order, 0, 1, 2, etc.
2279 
2280  The wave is flagged for deletion. The resources used by the wave
2281  will only be reused when either of the following apply.
2282 
2283  - all waves with higher numbered wave ids have been deleted or have
2284  been flagged for deletion.
2285 
2286  - a new wave is created which uses exactly the same resources as
2287  the current wave (see the C source for gpioWaveCreate for details).
2288 
2289  ...
2290  pi.wave_delete(6) # delete waveform with id 6
2291 
2292  pi.wave_delete(0) # delete waveform with id 0
2293  ...
2294  """
2295  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVDEL, wave_id, 0))
2296 
2297  def wave_tx_start(self): # DEPRECATED
2298  """
2299  This function is deprecated and has been removed.
2300 
2301  Use [*wave_create*]/[*wave_send_**] instead.
2302  """
2303  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVGO, 0, 0))
2304 
2305  def wave_tx_repeat(self): # DEPRECATED
2306  """
2307  This function is deprecated and has beeen removed.
2308 
2309  Use [*wave_create*]/[*wave_send_**] instead.
2310  """
2311  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVGOR, 0, 0))
2312 
2313  def wave_send_once(self, wave_id):
2314  """
2315  Transmits the waveform with id wave_id. The waveform is sent
2316  once.
2317 
2318  NOTE: Any hardware PWM started by [*hardware_PWM*] will
2319  be cancelled.
2320 
2321  wave_id:= >=0 (as returned by a prior call to [*wave_create*]).
2322 
2323  Returns the number of DMA control blocks used in the waveform.
2324 
2325  ...
2326  cbs = pi.wave_send_once(wid)
2327  ...
2328  """
2329  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVTX, wave_id, 0))
2330 
2331  def wave_send_repeat(self, wave_id):
2332  """
2333  Transmits the waveform with id wave_id. The waveform repeats
2334  until wave_tx_stop is called or another call to [*wave_send_**]
2335  is made.
2336 
2337  NOTE: Any hardware PWM started by [*hardware_PWM*] will
2338  be cancelled.
2339 
2340  wave_id:= >=0 (as returned by a prior call to [*wave_create*]).
2341 
2342  Returns the number of DMA control blocks used in the waveform.
2343 
2344  ...
2345  cbs = pi.wave_send_repeat(wid)
2346  ...
2347  """
2348  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVTXR, wave_id, 0))
2349 
2350  def wave_send_using_mode(self, wave_id, mode):
2351  """
2352  Transmits the waveform with id wave_id using mode mode.
2353 
2354  wave_id:= >=0 (as returned by a prior call to [*wave_create*]).
2355  mode:= WAVE_MODE_ONE_SHOT, WAVE_MODE_REPEAT,
2356  WAVE_MODE_ONE_SHOT_SYNC, or WAVE_MODE_REPEAT_SYNC.
2357 
2358  WAVE_MODE_ONE_SHOT: same as [*wave_send_once*].
2359 
2360  WAVE_MODE_REPEAT same as [*wave_send_repeat*].
2361 
2362  WAVE_MODE_ONE_SHOT_SYNC same as [*wave_send_once*] but tries
2363  to sync with the previous waveform.
2364 
2365  WAVE_MODE_REPEAT_SYNC same as [*wave_send_repeat*] but tries
2366  to sync with the previous waveform.
2367 
2368  WARNING: bad things may happen if you delete the previous
2369  waveform before it has been synced to the new waveform.
2370 
2371  NOTE: Any hardware PWM started by [*hardware_PWM*] will
2372  be cancelled.
2373 
2374  wave_id:= >=0 (as returned by a prior call to [*wave_create*]).
2375 
2376  Returns the number of DMA control blocks used in the waveform.
2377 
2378  ...
2379  cbs = pi.wave_send_using_mode(wid, WAVE_MODE_REPEAT_SYNC)
2380  ...
2381  """
2382  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVTXM, wave_id, mode))
2383 
2384  def wave_tx_at(self):
2385  """
2386  Returns the id of the waveform currently being
2387  transmitted.
2388 
2389  Returns the waveform id or one of the following special
2390  values:
2391 
2392  WAVE_NOT_FOUND (9998) - transmitted wave not found.
2393  NO_TX_WAVE (9999) - no wave being transmitted.
2394 
2395  ...
2396  wid = pi.wave_tx_at()
2397  ...
2398  """
2399  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVTAT, 0, 0))
2400 
2401  def wave_tx_busy(self):
2402  """
2403  Returns 1 if a waveform is currently being transmitted,
2404  otherwise 0.
2405 
2406  ...
2407  pi.wave_send_once(0) # send first waveform
2408 
2409  while pi.wave_tx_busy(): # wait for waveform to be sent
2410  time.sleep(0.1)
2411 
2412  pi.wave_send_once(1) # send next waveform
2413  ...
2414  """
2415  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVBSY, 0, 0))
2416 
2417  def wave_tx_stop(self):
2418  """
2419  Stops the transmission of the current waveform.
2420 
2421  This function is intended to stop a waveform started with
2422  wave_send_repeat.
2423 
2424  ...
2425  pi.wave_send_repeat(3)
2426 
2427  time.sleep(5)
2428 
2429  pi.wave_tx_stop()
2430  ...
2431  """
2432  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVHLT, 0, 0))
2433 
2434  def wave_chain(self, data):
2435  """
2436  This function transmits a chain of waveforms.
2437 
2438  NOTE: Any hardware PWM started by [*hardware_PWM*]
2439  will be cancelled.
2440 
2441  The waves to be transmitted are specified by the contents
2442  of data which contains an ordered list of [*wave_id*]s
2443  and optional command codes and related data.
2444 
2445  Returns 0 if OK, otherwise PI_CHAIN_NESTING,
2446  PI_CHAIN_LOOP_CNT, PI_BAD_CHAIN_LOOP, PI_BAD_CHAIN_CMD,
2447  PI_CHAIN_COUNTER, PI_BAD_CHAIN_DELAY, PI_CHAIN_TOO_BIG,
2448  or PI_BAD_WAVE_ID.
2449 
2450  Each wave is transmitted in the order specified. A wave
2451  may occur multiple times per chain.
2452 
2453  A blocks of waves may be transmitted multiple times by
2454  using the loop commands. The block is bracketed by loop
2455  start and end commands. Loops may be nested.
2456 
2457  Delays between waves may be added with the delay command.
2458 
2459  The following command codes are supported:
2460 
2461  Name @ Cmd & Data @ Meaning
2462  Loop Start @ 255 0 @ Identify start of a wave block
2463  Loop Repeat @ 255 1 x y @ loop x + y*256 times
2464  Delay @ 255 2 x y @ delay x + y*256 microseconds
2465  Loop Forever @ 255 3 @ loop forever
2466 
2467  If present Loop Forever must be the last entry in the chain.
2468 
2469  The code is currently dimensioned to support a chain with
2470  roughly 600 entries and 20 loop counters.
2471 
2472  ...
2473  #!/usr/bin/env python
2474 
2475  import time
2476  import pigpio
2477 
2478  WAVES=5
2479  GPIO=4
2480 
2481  wid=[0]*WAVES
2482 
2483  pi = pigpio.pi() # Connect to local Pi.
2484 
2485  pi.set_mode(GPIO, pigpio.OUTPUT);
2486 
2487  for i in range(WAVES):
2488  pi.wave_add_generic([
2489  pigpio.pulse(1<<GPIO, 0, 20),
2490  pigpio.pulse(0, 1<<GPIO, (i+1)*200)]);
2491 
2492  wid[i] = pi.wave_create();
2493 
2494  pi.wave_chain([
2495  wid[4], wid[3], wid[2], # transmit waves 4+3+2
2496  255, 0, # loop start
2497  wid[0], wid[0], wid[0], # transmit waves 0+0+0
2498  255, 0, # loop start
2499  wid[0], wid[1], # transmit waves 0+1
2500  255, 2, 0x88, 0x13, # delay 5000us
2501  255, 1, 30, 0, # loop end (repeat 30 times)
2502  255, 0, # loop start
2503  wid[2], wid[3], wid[0], # transmit waves 2+3+0
2504  wid[3], wid[1], wid[2], # transmit waves 3+1+2
2505  255, 1, 10, 0, # loop end (repeat 10 times)
2506  255, 1, 5, 0, # loop end (repeat 5 times)
2507  wid[4], wid[4], wid[4], # transmit waves 4+4+4
2508  255, 2, 0x20, 0x4E, # delay 20000us
2509  wid[0], wid[0], wid[0], # transmit waves 0+0+0
2510  ])
2511 
2512  while pi.wave_tx_busy():
2513  time.sleep(0.1);
2514 
2515  for i in range(WAVES):
2516  pi.wave_delete(wid[i])
2517 
2518  pi.stop()
2519  ...
2520  """
2521  # I p1 0
2522  # I p2 0
2523  # I p3 len
2524  ## extension ##
2525  # s len data bytes
2526 
2527  return _u2i(_pigpio_command_ext(
2528  self.sl, _PI_CMD_WVCHA, 0, 0, len(data), [data]))
2529 
2530 
2531  def wave_get_micros(self):
2532  """
2533  Returns the length in microseconds of the current waveform.
2534 
2535  ...
2536  micros = pi.wave_get_micros()
2537  ...
2538  """
2539  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVSM, 0, 0))
2540 
2542  """
2543  Returns the maximum possible size of a waveform in microseconds.
2544 
2545  ...
2546  micros = pi.wave_get_max_micros()
2547  ...
2548  """
2549  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVSM, 2, 0))
2550 
2551  def wave_get_pulses(self):
2552  """
2553  Returns the length in pulses of the current waveform.
2554 
2555  ...
2556  pulses = pi.wave_get_pulses()
2557  ...
2558  """
2559  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVSP, 0, 0))
2560 
2562  """
2563  Returns the maximum possible size of a waveform in pulses.
2564 
2565  ...
2566  pulses = pi.wave_get_max_pulses()
2567  ...
2568  """
2569  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVSP, 2, 0))
2570 
2571  def wave_get_cbs(self):
2572  """
2573  Returns the length in DMA control blocks of the current
2574  waveform.
2575 
2576  ...
2577  cbs = pi.wave_get_cbs()
2578  ...
2579  """
2580  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVSC, 0, 0))
2581 
2582  def wave_get_max_cbs(self):
2583  """
2584  Returns the maximum possible size of a waveform in DMA
2585  control blocks.
2586 
2587  ...
2588  cbs = pi.wave_get_max_cbs()
2589  ...
2590  """
2591  return _u2i(_pigpio_command(self.sl, _PI_CMD_WVSC, 2, 0))
2592 
2593  def i2c_open(self, i2c_bus, i2c_address, i2c_flags=0):
2594  """
2595  Returns a handle (>=0) for the device at the I2C bus address.
2596 
2597  i2c_bus:= >=0.
2598  i2c_address:= 0-0x7F.
2599  i2c_flags:= 0, no flags are currently defined.
2600 
2601  Normally you would only use the [*i2c_**] functions if
2602  you are or will be connecting to the Pi over a network. If
2603  you will always run on the local Pi use the standard SMBus
2604  module instead.
2605 
2606  Physically buses 0 and 1 are available on the Pi. Higher
2607  numbered buses will be available if a kernel supported bus
2608  multiplexor is being used.
2609 
2610  For the SMBus commands the low level transactions are shown
2611  at the end of the function description. The following
2612  abbreviations are used:
2613 
2614  . .
2615  S (1 bit) : Start bit
2616  P (1 bit) : Stop bit
2617  Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0.
2618  A, NA (1 bit) : Accept and not accept bit.
2619  Addr (7 bits): I2C 7 bit address.
2620  reg (8 bits): Command byte, which often selects a register.
2621  Data (8 bits): A data byte.
2622  Count (8 bits): A byte defining the length of a block operation.
2623 
2624  [..]: Data sent by the device.
2625  . .
2626 
2627  ...
2628  h = pi.i2c_open(1, 0x53) # open device at address 0x53 on bus 1
2629  ...
2630  """
2631  # I p1 i2c_bus
2632  # I p2 i2c_addr
2633  # I p3 4
2634  ## extension ##
2635  # I i2c_flags
2636  extents = [struct.pack("I", i2c_flags)]
2637  return _u2i(_pigpio_command_ext(
2638  self.sl, _PI_CMD_I2CO, i2c_bus, i2c_address, 4, extents))
2639 
2640  def i2c_close(self, handle):
2641  """
2642  Closes the I2C device associated with handle.
2643 
2644  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2645 
2646  ...
2647  pi.i2c_close(h)
2648  ...
2649  """
2650  return _u2i(_pigpio_command(self.sl, _PI_CMD_I2CC, handle, 0))
2651 
2652  def i2c_write_quick(self, handle, bit):
2653  """
2654  Sends a single bit to the device associated with handle.
2655 
2656  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2657  bit:= 0 or 1, the value to write.
2658 
2659  SMBus 2.0 5.5.1 - Quick command.
2660  . .
2661  S Addr bit [A] P
2662  . .
2663 
2664  ...
2665  pi.i2c_write_quick(0, 1) # send 1 to device 0
2666  pi.i2c_write_quick(3, 0) # send 0 to device 3
2667  ...
2668  """
2669  return _u2i(_pigpio_command(self.sl, _PI_CMD_I2CWQ, handle, bit))
2670 
2671  def i2c_write_byte(self, handle, byte_val):
2672  """
2673  Sends a single byte to the device associated with handle.
2674 
2675  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2676  byte_val:= 0-255, the value to write.
2677 
2678  SMBus 2.0 5.5.2 - Send byte.
2679  . .
2680  S Addr Wr [A] byte_val [A] P
2681  . .
2682 
2683  ...
2684  pi.i2c_write_byte(1, 17) # send byte 17 to device 1
2685  pi.i2c_write_byte(2, 0x23) # send byte 0x23 to device 2
2686  ...
2687  """
2688  return _u2i(
2689  _pigpio_command(self.sl, _PI_CMD_I2CWS, handle, byte_val))
2690 
2691  def i2c_read_byte(self, handle):
2692  """
2693  Reads a single byte from the device associated with handle.
2694 
2695  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2696 
2697  SMBus 2.0 5.5.3 - Receive byte.
2698  . .
2699  S Addr Rd [A] [Data] NA P
2700  . .
2701 
2702  ...
2703  b = pi.i2c_read_byte(2) # read a byte from device 2
2704  ...
2705  """
2706  return _u2i(_pigpio_command(self.sl, _PI_CMD_I2CRS, handle, 0))
2707 
2708  def i2c_write_byte_data(self, handle, reg, byte_val):
2709  """
2710  Writes a single byte to the specified register of the device
2711  associated with handle.
2712 
2713  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2714  reg:= >=0, the device register.
2715  byte_val:= 0-255, the value to write.
2716 
2717  SMBus 2.0 5.5.4 - Write byte.
2718  . .
2719  S Addr Wr [A] reg [A] byte_val [A] P
2720  . .
2721 
2722  ...
2723  # send byte 0xC5 to reg 2 of device 1
2724  pi.i2c_write_byte_data(1, 2, 0xC5)
2725 
2726  # send byte 9 to reg 4 of device 2
2727  pi.i2c_write_byte_data(2, 4, 9)
2728  ...
2729  """
2730  # I p1 handle
2731  # I p2 reg
2732  # I p3 4
2733  ## extension ##
2734  # I byte_val
2735  extents = [struct.pack("I", byte_val)]
2736  return _u2i(_pigpio_command_ext(
2737  self.sl, _PI_CMD_I2CWB, handle, reg, 4, extents))
2738 
2739  def i2c_write_word_data(self, handle, reg, word_val):
2740  """
2741  Writes a single 16 bit word to the specified register of the
2742  device associated with handle.
2743 
2744  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2745  reg:= >=0, the device register.
2746  word_val:= 0-65535, the value to write.
2747 
2748  SMBus 2.0 5.5.4 - Write word.
2749  . .
2750  S Addr Wr [A] reg [A] word_val_Low [A] word_val_High [A] P
2751  . .
2752 
2753  ...
2754  # send word 0xA0C5 to reg 5 of device 4
2755  pi.i2c_write_word_data(4, 5, 0xA0C5)
2756 
2757  # send word 2 to reg 2 of device 5
2758  pi.i2c_write_word_data(5, 2, 23)
2759  ...
2760  """
2761  # I p1 handle
2762  # I p2 reg
2763  # I p3 4
2764  ## extension ##
2765  # I word_val
2766  extents = [struct.pack("I", word_val)]
2767  return _u2i(_pigpio_command_ext(
2768  self.sl, _PI_CMD_I2CWW, handle, reg, 4, extents))
2769 
2770  def i2c_read_byte_data(self, handle, reg):
2771  """
2772  Reads a single byte from the specified register of the device
2773  associated with handle.
2774 
2775  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2776  reg:= >=0, the device register.
2777 
2778  SMBus 2.0 5.5.5 - Read byte.
2779  . .
2780  S Addr Wr [A] reg [A] S Addr Rd [A] [Data] NA P
2781  . .
2782 
2783  ...
2784  # read byte from reg 17 of device 2
2785  b = pi.i2c_read_byte_data(2, 17)
2786 
2787  # read byte from reg 1 of device 0
2788  b = pi.i2c_read_byte_data(0, 1)
2789  ...
2790  """
2791  return _u2i(_pigpio_command(self.sl, _PI_CMD_I2CRB, handle, reg))
2792 
2793  def i2c_read_word_data(self, handle, reg):
2794  """
2795  Reads a single 16 bit word from the specified register of the
2796  device associated with handle.
2797 
2798  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2799  reg:= >=0, the device register.
2800 
2801  SMBus 2.0 5.5.5 - Read word.
2802  . .
2803  S Addr Wr [A] reg [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
2804  . .
2805 
2806  ...
2807  # read word from reg 2 of device 3
2808  w = pi.i2c_read_word_data(3, 2)
2809 
2810  # read word from reg 7 of device 2
2811  w = pi.i2c_read_word_data(2, 7)
2812  ...
2813  """
2814  return _u2i(_pigpio_command(self.sl, _PI_CMD_I2CRW, handle, reg))
2815 
2816  def i2c_process_call(self, handle, reg, word_val):
2817  """
2818  Writes 16 bits of data to the specified register of the device
2819  associated with handle and reads 16 bits of data in return.
2820 
2821  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2822  reg:= >=0, the device register.
2823  word_val:= 0-65535, the value to write.
2824 
2825  SMBus 2.0 5.5.6 - Process call.
2826  . .
2827  S Addr Wr [A] reg [A] word_val_Low [A] word_val_High [A]
2828  S Addr Rd [A] [DataLow] A [DataHigh] NA P
2829  . .
2830 
2831  ...
2832  r = pi.i2c_process_call(h, 4, 0x1231)
2833  r = pi.i2c_process_call(h, 6, 0)
2834  ...
2835  """
2836  # I p1 handle
2837  # I p2 reg
2838  # I p3 4
2839  ## extension ##
2840  # I word_val
2841  extents = [struct.pack("I", word_val)]
2842  return _u2i(_pigpio_command_ext(
2843  self.sl, _PI_CMD_I2CPC, handle, reg, 4, extents))
2844 
2845  def i2c_write_block_data(self, handle, reg, data):
2846  """
2847  Writes up to 32 bytes to the specified register of the device
2848  associated with handle.
2849 
2850  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2851  reg:= >=0, the device register.
2852  data:= the bytes to write.
2853 
2854  SMBus 2.0 5.5.7 - Block write.
2855  . .
2856  S Addr Wr [A] reg [A] len(data) [A] data0 [A] data1 [A] ... [A]
2857  datan [A] P
2858  . .
2859 
2860  ...
2861  pi.i2c_write_block_data(4, 5, b'hello')
2862 
2863  pi.i2c_write_block_data(4, 5, "data bytes")
2864 
2865  pi.i2c_write_block_data(5, 0, b'\\x00\\x01\\x22')
2866 
2867  pi.i2c_write_block_data(6, 2, [0, 1, 0x22])
2868  ...
2869  """
2870  # I p1 handle
2871  # I p2 reg
2872  # I p3 len
2873  ## extension ##
2874  # s len data bytes
2875  if len(data):
2876  return _u2i(_pigpio_command_ext(
2877  self.sl, _PI_CMD_I2CWK, handle, reg, len(data), [data]))
2878  else:
2879  return 0
2880 
2881  def i2c_read_block_data(self, handle, reg):
2882  """
2883  Reads a block of up to 32 bytes from the specified register of
2884  the device associated with handle.
2885 
2886  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2887  reg:= >=0, the device register.
2888 
2889  SMBus 2.0 5.5.7 - Block read.
2890  . .
2891  S Addr Wr [A] reg [A]
2892  S Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P
2893  . .
2894 
2895  The amount of returned data is set by the device.
2896 
2897  The returned value is a tuple of the number of bytes read and a
2898  bytearray containing the bytes. If there was an error the
2899  number of bytes read will be less than zero (and will contain
2900  the error code).
2901 
2902  ...
2903  (b, d) = pi.i2c_read_block_data(h, 10)
2904  if b >= 0:
2905  # process data
2906  else:
2907  # process read failure
2908  ...
2909  """
2910  bytes = PI_CMD_INTERRUPTED
2911  rdata = ""
2912  with self.sl.l:
2913  bytes = u2i(_pigpio_command_nolock(
2914  self.sl, _PI_CMD_I2CRK, handle, reg))
2915  if bytes > 0:
2916  rdata = self._rxbuf(bytes)
2917  return bytes, rdata
2918 
2919  def i2c_block_process_call(self, handle, reg, data):
2920  """
2921  Writes data bytes to the specified register of the device
2922  associated with handle and reads a device specified number
2923  of bytes of data in return.
2924 
2925  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2926  reg:= >=0, the device register.
2927  data:= the bytes to write.
2928 
2929  The SMBus 2.0 documentation states that a minimum of 1 byte may
2930  be sent and a minimum of 1 byte may be received. The total
2931  number of bytes sent/received must be 32 or less.
2932 
2933  SMBus 2.0 5.5.8 - Block write-block read.
2934  . .
2935  S Addr Wr [A] reg [A] len(data) [A] data0 [A] ... datan [A]
2936  S Addr Rd [A] [Count] A [Data] ... A P
2937  . .
2938 
2939  The returned value is a tuple of the number of bytes read and a
2940  bytearray containing the bytes. If there was an error the
2941  number of bytes read will be less than zero (and will contain
2942  the error code).
2943 
2944  ...
2945  (b, d) = pi.i2c_block_process_call(h, 10, b'\\x02\\x05\\x00')
2946 
2947  (b, d) = pi.i2c_block_process_call(h, 10, b'abcdr')
2948 
2949  (b, d) = pi.i2c_block_process_call(h, 10, "abracad")
2950 
2951  (b, d) = pi.i2c_block_process_call(h, 10, [2, 5, 16])
2952  ...
2953  """
2954  # I p1 handle
2955  # I p2 reg
2956  # I p3 len
2957  ## extension ##
2958  # s len data bytes
2959 
2960  bytes = PI_CMD_INTERRUPTED
2961  rdata = ""
2962  with self.sl.l:
2964  self.sl, _PI_CMD_I2CPK, handle, reg, len(data), [data]))
2965  if bytes > 0:
2966  rdata = self._rxbuf(bytes)
2967  return bytes, rdata
2968 
2969  def i2c_write_i2c_block_data(self, handle, reg, data):
2970  """
2971  Writes data bytes to the specified register of the device
2972  associated with handle . 1-32 bytes may be written.
2973 
2974  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
2975  reg:= >=0, the device register.
2976  data:= the bytes to write.
2977 
2978  . .
2979  S Addr Wr [A] reg [A] data0 [A] data1 [A] ... [A] datan [NA] P
2980  . .
2981 
2982  ...
2983  pi.i2c_write_i2c_block_data(4, 5, 'hello')
2984 
2985  pi.i2c_write_i2c_block_data(4, 5, b'hello')
2986 
2987  pi.i2c_write_i2c_block_data(5, 0, b'\\x00\\x01\\x22')
2988 
2989  pi.i2c_write_i2c_block_data(6, 2, [0, 1, 0x22])
2990  ...
2991  """
2992  # I p1 handle
2993  # I p2 reg
2994  # I p3 len
2995  ## extension ##
2996  # s len data bytes
2997  if len(data):
2998  return _u2i(_pigpio_command_ext(
2999  self.sl, _PI_CMD_I2CWI, handle, reg, len(data), [data]))
3000  else:
3001  return 0
3002 
3003  def i2c_read_i2c_block_data(self, handle, reg, count):
3004  """
3005  Reads count bytes from the specified register of the device
3006  associated with handle . The count may be 1-32.
3007 
3008  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
3009  reg:= >=0, the device register.
3010  count:= >0, the number of bytes to read.
3011 
3012  . .
3013  S Addr Wr [A] reg [A]
3014  S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P
3015  . .
3016 
3017  The returned value is a tuple of the number of bytes read and a
3018  bytearray containing the bytes. If there was an error the
3019  number of bytes read will be less than zero (and will contain
3020  the error code).
3021 
3022  ...
3023  (b, d) = pi.i2c_read_i2c_block_data(h, 4, 32)
3024  if b >= 0:
3025  # process data
3026  else:
3027  # process read failure
3028  ...
3029  """
3030  # I p1 handle
3031  # I p2 reg
3032  # I p3 4
3033  ## extension ##
3034  # I count
3035  extents = [struct.pack("I", count)]
3036 
3037  bytes = PI_CMD_INTERRUPTED
3038  rdata = ""
3039  with self.sl.l:
3041  self.sl, _PI_CMD_I2CRI, handle, reg, 4, extents))
3042  if bytes > 0:
3043  rdata = self._rxbuf(bytes)
3044  return bytes, rdata
3045 
3046  def i2c_read_device(self, handle, count):
3047  """
3048  Returns count bytes read from the raw device associated
3049  with handle.
3050 
3051  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
3052  count:= >0, the number of bytes to read.
3053 
3054  . .
3055  S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P
3056  . .
3057 
3058  The returned value is a tuple of the number of bytes read and a
3059  bytearray containing the bytes. If there was an error the
3060  number of bytes read will be less than zero (and will contain
3061  the error code).
3062 
3063  ...
3064  (count, data) = pi.i2c_read_device(h, 12)
3065  ...
3066  """
3067  bytes = PI_CMD_INTERRUPTED
3068  rdata = ""
3069  with self.sl.l:
3070  bytes = u2i(
3071  _pigpio_command_nolock(self.sl, _PI_CMD_I2CRD, handle, count))
3072  if bytes > 0:
3073  rdata = self._rxbuf(bytes)
3074  return bytes, rdata
3075 
3076  def i2c_write_device(self, handle, data):
3077  """
3078  Writes the data bytes to the raw device associated with handle.
3079 
3080  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
3081  data:= the bytes to write.
3082 
3083  . .
3084  S Addr Wr [A] data0 [A] data1 [A] ... [A] datan [A] P
3085  . .
3086 
3087  ...
3088  pi.i2c_write_device(h, b"\\x12\\x34\\xA8")
3089 
3090  pi.i2c_write_device(h, b"help")
3091 
3092  pi.i2c_write_device(h, 'help')
3093 
3094  pi.i2c_write_device(h, [23, 56, 231])
3095  ...
3096  """
3097  # I p1 handle
3098  # I p2 0
3099  # I p3 len
3100  ## extension ##
3101  # s len data bytes
3102  if len(data):
3103  return _u2i(_pigpio_command_ext(
3104  self.sl, _PI_CMD_I2CWD, handle, 0, len(data), [data]))
3105  else:
3106  return 0
3107 
3108 
3109  def i2c_zip(self, handle, data):
3110  """
3111  This function executes a sequence of I2C operations. The
3112  operations to be performed are specified by the contents of data
3113  which contains the concatenated command codes and associated data.
3114 
3115  handle:= >=0 (as returned by a prior call to [*i2c_open*]).
3116  data:= the concatenated I2C commands, see below
3117 
3118  The returned value is a tuple of the number of bytes read and a
3119  bytearray containing the bytes. If there was an error the
3120  number of bytes read will be less than zero (and will contain
3121  the error code).
3122 
3123  ...
3124  (count, data) = pi.i2c_zip(h, [4, 0x53, 7, 1, 0x32, 6, 6, 0])
3125  ...
3126 
3127  The following command codes are supported:
3128 
3129  Name @ Cmd & Data @ Meaning
3130  End @ 0 @ No more commands
3131  Escape @ 1 @ Next P is two bytes
3132  On @ 2 @ Switch combined flag on
3133  Off @ 3 @ Switch combined flag off
3134  Address @ 4 P @ Set I2C address to P
3135  Flags @ 5 lsb msb @ Set I2C flags to lsb + (msb << 8)
3136  Read @ 6 P @ Read P bytes of data
3137  Write @ 7 P ... @ Write P bytes of data
3138 
3139  The address, read, and write commands take a parameter P.
3140  Normally P is one byte (0-255). If the command is preceded by
3141  the Escape command then P is two bytes (0-65535, least significant
3142  byte first).
3143 
3144  The address defaults to that associated with the handle.
3145  The flags default to 0. The address and flags maintain their
3146  previous value until updated.
3147 
3148  Any read I2C data is concatenated in the returned bytearray.
3149 
3150  ...
3151  Set address 0x53, write 0x32, read 6 bytes
3152  Set address 0x1E, write 0x03, read 6 bytes
3153  Set address 0x68, write 0x1B, read 8 bytes
3154  End
3155 
3156  0x04 0x53 0x07 0x01 0x32 0x06 0x06
3157  0x04 0x1E 0x07 0x01 0x03 0x06 0x06
3158  0x04 0x68 0x07 0x01 0x1B 0x06 0x08
3159  0x00
3160  ...
3161  """
3162  # I p1 handle
3163  # I p2 0
3164  # I p3 len
3165  ## extension ##
3166  # s len data bytes
3167 
3168  bytes = PI_CMD_INTERRUPTED
3169  rdata = ""
3170  with self.sl.l:
3172  self.sl, _PI_CMD_I2CZ, handle, 0, len(data), [data]))
3173  if bytes > 0:
3174  rdata = self._rxbuf(bytes)
3175  return bytes, rdata
3176 
3177 
3178  def bb_spi_open(self, CS, MISO, MOSI, SCLK, baud=100000, spi_flags=0):
3179  """
3180  This function selects a set of GPIO for bit banging SPI at a
3181  specified baud rate.
3182 
3183  CS := 0-31
3184  MISO := 0-31
3185  MOSI := 0-31
3186  SCLK := 0-31
3187  baud := 50-250000
3188  spiFlags := see below
3189 
3190  spiFlags consists of the least significant 22 bits.
3191 
3192  . .
3193  21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3194  0 0 0 0 0 0 R T 0 0 0 0 0 0 0 0 0 0 0 p m m
3195  . .
3196 
3197  mm defines the SPI mode, defaults to 0
3198 
3199  . .
3200  Mode CPOL CPHA
3201  0 0 0
3202  1 0 1
3203  2 1 0
3204  3 1 1
3205  . .
3206 
3207  The following constants may be used to set the mode:
3208 
3209  . .
3210  pigpio.SPI_MODE_0
3211  pigpio.SPI_MODE_1
3212  pigpio.SPI_MODE_2
3213  pigpio.SPI_MODE_3
3214  . .
3215 
3216  Alternatively pigpio.SPI_CPOL and/or pigpio.SPI_CPHA
3217  may be used.
3218 
3219  p is 0 if CS is active low (default) and 1 for active high.
3220  pigpio.SPI_CS_HIGH_ACTIVE may be used to set this flag.
3221 
3222  T is 1 if the least significant bit is transmitted on MOSI first,
3223  the default (0) shifts the most significant bit out first.
3224  pigpio.SPI_TX_LSBFIRST may be used to set this flag.
3225 
3226  R is 1 if the least significant bit is received on MISO first,
3227  the default (0) receives the most significant bit first.
3228  pigpio.SPI_RX_LSBFIRST may be used to set this flag.
3229 
3230  The other bits in spiFlags should be set to zero.
3231 
3232  Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_SPI_BAUD, or
3233  PI_GPIO_IN_USE.
3234 
3235  If more than one device is connected to the SPI bus (defined by
3236  SCLK, MOSI, and MISO) each must have its own CS.
3237 
3238  ...
3239  bb_spi_open(10, MISO, MOSI, SCLK, 10000, 0); // device 1
3240  bb_spi_open(11, MISO, MOSI, SCLK, 20000, 3); // device 2
3241  ...
3242  """
3243  # I p1 CS
3244  # I p2 0
3245  # I p3 20
3246  ## extension ##
3247  # I MISO
3248  # I MOSI
3249  # I SCLK
3250  # I baud
3251  # I spi_flags
3252 
3253  extents = [struct.pack("IIIII", MISO, MOSI, SCLK, baud, spi_flags)]
3254  return _u2i(_pigpio_command_ext(
3255  self.sl, _PI_CMD_BSPIO, CS, 0, 20, extents))
3256 
3257 
3258  def bb_spi_close(self, CS):
3259  """
3260  This function stops bit banging SPI on a set of GPIO
3261  opened with [*bb_spi_open*].
3262 
3263  CS:= 0-31, the CS GPIO used in a prior call to [*bb_spi_open*]
3264 
3265  Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_SPI_GPIO.
3266 
3267  ...
3268  pi.bb_spi_close(CS)
3269  ...
3270  """
3271  return _u2i(_pigpio_command(self.sl, _PI_CMD_BSPIC, CS, 0))
3272 
3273 
3274  def bb_spi_xfer(self, CS, data):
3275  """
3276  This function executes a bit banged SPI transfer.
3277 
3278  CS:= 0-31 (as used in a prior call to [*bb_spi_open*])
3279  data:= data to be sent
3280 
3281  The returned value is a tuple of the number of bytes read and a
3282  bytearray containing the bytes. If there was an error the
3283  number of bytes read will be less than zero (and will contain
3284  the error code).
3285 
3286  ...
3287  #!/usr/bin/env python
3288 
3289  import pigpio
3290 
3291  CE0=5
3292  CE1=6
3293  MISO=13
3294  MOSI=19
3295  SCLK=12
3296 
3297  pi = pigpio.pi()
3298  if not pi.connected:
3299  exit()
3300 
3301  pi.bb_spi_open(CE0, MISO, MOSI, SCLK, 10000, 0) # MCP4251 DAC
3302  pi.bb_spi_open(CE1, MISO, MOSI, SCLK, 20000, 3) # MCP3008 ADC
3303 
3304  for i in range(256):
3305 
3306  count, data = pi.bb_spi_xfer(CE0, [0, i]) # Set DAC value
3307 
3308  if count == 2:
3309 
3310  count, data = pi.bb_spi_xfer(CE0, [12, 0]) # Read back DAC
3311 
3312  if count == 2:
3313 
3314  set_val = data[1]
3315 
3316  count, data = pi.bb_spi_xfer(CE1, [1, 128, 0]) # Read ADC
3317 
3318  if count == 3:
3319 
3320  read_val = ((data[1]&3)<<8) | data[2]
3321 
3322  print("{} {}".format(set_val, read_val))
3323 
3324  pi.bb_spi_close(CE0)
3325  pi.bb_spi_close(CE1)
3326 
3327  pi.stop()
3328  ...
3329  """
3330  # I p1 CS
3331  # I p2 0
3332  # I p3 len
3333  ## extension ##
3334  # s len data bytes
3335 
3336  bytes = PI_CMD_INTERRUPTED
3337  rdata = ""
3338  with self.sl.l:
3340  self.sl, _PI_CMD_BSPIX, CS, 0, len(data), [data]))
3341  if bytes > 0:
3342  rdata = self._rxbuf(bytes)
3343  return bytes, rdata
3344 
3345 
3346  def bb_i2c_open(self, SDA, SCL, baud=100000):
3347  """
3348  This function selects a pair of GPIO for bit banging I2C at a
3349  specified baud rate.
3350 
3351  Bit banging I2C allows for certain operations which are not possible
3352  with the standard I2C driver.
3353 
3354  o baud rates as low as 50
3355  o repeated starts
3356  o clock stretching
3357  o I2C on any pair of spare GPIO
3358 
3359  SDA:= 0-31
3360  SCL:= 0-31
3361  baud:= 50-500000
3362 
3363  Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_I2C_BAUD, or
3364  PI_GPIO_IN_USE.
3365 
3366  NOTE:
3367 
3368  The GPIO used for SDA and SCL must have pull-ups to 3V3 connected.
3369  As a guide the hardware pull-ups on pins 3 and 5 are 1k8 in value.
3370 
3371  ...
3372  h = pi.bb_i2c_open(4, 5, 50000) # bit bang on GPIO 4/5 at 50kbps
3373  ...
3374  """
3375  # I p1 SDA
3376  # I p2 SCL
3377  # I p3 4
3378  ## extension ##
3379  # I baud
3380  extents = [struct.pack("I", baud)]
3381  return _u2i(_pigpio_command_ext(
3382  self.sl, _PI_CMD_BI2CO, SDA, SCL, 4, extents))
3383 
3384 
3385  def bb_i2c_close(self, SDA):
3386  """
3387  This function stops bit banging I2C on a pair of GPIO
3388  previously opened with [*bb_i2c_open*].
3389 
3390  SDA:= 0-31, the SDA GPIO used in a prior call to [*bb_i2c_open*]
3391 
3392  Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_I2C_GPIO.
3393 
3394  ...
3395  pi.bb_i2c_close(SDA)
3396  ...
3397  """
3398  return _u2i(_pigpio_command(self.sl, _PI_CMD_BI2CC, SDA, 0))
3399 
3400 
3401  def bb_i2c_zip(self, SDA, data):
3402  """
3403  This function executes a sequence of bit banged I2C operations.
3404  The operations to be performed are specified by the contents
3405  of data which contains the concatenated command codes and
3406  associated data.
3407 
3408  SDA:= 0-31 (as used in a prior call to [*bb_i2c_open*])
3409  data:= the concatenated I2C commands, see below
3410 
3411  The returned value is a tuple of the number of bytes read and a
3412  bytearray containing the bytes. If there was an error the
3413  number of bytes read will be less than zero (and will contain
3414  the error code).
3415 
3416  ...
3417  (count, data) = pi.bb_i2c_zip(
3418  SDA, [4, 0x53, 2, 7, 1, 0x32, 2, 6, 6, 3, 0])
3419  ...
3420 
3421  The following command codes are supported:
3422 
3423  Name @ Cmd & Data @ Meaning
3424  End @ 0 @ No more commands
3425  Escape @ 1 @ Next P is two bytes
3426  Start @ 2 @ Start condition
3427  Stop @ 3 @ Stop condition
3428  Address @ 4 P @ Set I2C address to P
3429  Flags @ 5 lsb msb @ Set I2C flags to lsb + (msb << 8)
3430  Read @ 6 P @ Read P bytes of data
3431  Write @ 7 P ... @ Write P bytes of data
3432 
3433  The address, read, and write commands take a parameter P.
3434  Normally P is one byte (0-255). If the command is preceded by
3435  the Escape command then P is two bytes (0-65535, least significant
3436  byte first).
3437 
3438  The address and flags default to 0. The address and flags maintain
3439  their previous value until updated.
3440 
3441  No flags are currently defined.
3442 
3443  Any read I2C data is concatenated in the returned bytearray.
3444 
3445  ...
3446  Set address 0x53
3447  start, write 0x32, (re)start, read 6 bytes, stop
3448  Set address 0x1E
3449  start, write 0x03, (re)start, read 6 bytes, stop
3450  Set address 0x68
3451  start, write 0x1B, (re)start, read 8 bytes, stop
3452  End
3453 
3454  0x04 0x53
3455  0x02 0x07 0x01 0x32 0x02 0x06 0x06 0x03
3456 
3457  0x04 0x1E
3458  0x02 0x07 0x01 0x03 0x02 0x06 0x06 0x03
3459 
3460  0x04 0x68
3461  0x02 0x07 0x01 0x1B 0x02 0x06 0x08 0x03
3462 
3463  0x00
3464  ...
3465  """
3466  # I p1 SDA
3467  # I p2 0
3468  # I p3 len
3469  ## extension ##
3470  # s len data bytes
3471 
3472  bytes = PI_CMD_INTERRUPTED
3473  rdata = ""
3474  with self.sl.l:
3476  self.sl, _PI_CMD_BI2CZ, SDA, 0, len(data), [data]))
3477  if bytes > 0:
3478  rdata = self._rxbuf(bytes)
3479  return bytes, rdata
3480 
3481  def event_trigger(self, event):
3482  """
3483  This function signals the occurrence of an event.
3484 
3485  event:= 0-31, the event
3486 
3487  Returns 0 if OK, otherwise PI_BAD_EVENT_ID.
3488 
3489  An event is a signal used to inform one or more consumers
3490  to start an action. Each consumer which has registered an
3491  interest in the event (e.g. by calling [*event_callback*]) will
3492  be informed by a callback.
3493 
3494  One event, EVENT_BSC (31) is predefined. This event is
3495  auto generated on BSC slave activity.
3496 
3497  The meaning of other events is arbitrary.
3498 
3499  Note that other than its id and its tick there is no data associated
3500  with an event.
3501 
3502  ...
3503  pi.event_trigger(23)
3504  ...
3505  """
3506  return _u2i(_pigpio_command(self.sl, _PI_CMD_EVT, event, 0))
3507 
3508 
3509  def bsc_xfer(self, bsc_control, data):
3510  """
3511  This function provides a low-level interface to the
3512  SPI/I2C Slave peripheral. This peripheral allows the
3513  Pi to act as a slave device on an I2C or SPI bus.
3514 
3515  I can't get SPI to work properly. I tried with a
3516  control word of 0x303 and swapped MISO and MOSI.
3517 
3518 
3519  The function sets the BSC mode, writes any data in
3520  the transmit buffer to the BSC transmit FIFO, and
3521  copies any data in the BSC receive FIFO to the
3522  receive buffer.
3523 
3524  bsc_control:= see below
3525  data:= the data bytes to place in the transmit FIFO.
3526 
3527  The returned value is a tuple of the status (see below),
3528  the number of bytes read, and a bytearray containing the
3529  read bytes. If there was an error the status will be less
3530  than zero (and will contain the error code).
3531 
3532  Note that the control word sets the BSC mode. The BSC will
3533  stay in that mode until a different control word is sent.
3534 
3535  The BSC peripheral uses GPIO 18 (SDA) and 19 (SCL)
3536  in I2C mode and GPIO 18 (MOSI), 19 (SCLK), 20 (MISO),
3537  and 21 (CE) in SPI mode. You need to swap MISO/MOSI
3538  between master and slave.
3539 
3540  When a zero control word is received GPIO 18-21 will be reset
3541  to INPUT mode.
3542 
3543  bsc_control consists of the following bits:
3544 
3545  . .
3546  22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3547  a a a a a a a - - IT HC TF IR RE TE BK EC ES PL PH I2 SP EN
3548  . .
3549 
3550  Bits 0-13 are copied unchanged to the BSC CR register. See
3551  pages 163-165 of the Broadcom peripherals document for full
3552  details.
3553 
3554  aaaaaaa @ defines the I2C slave address (only relevant in I2C mode)
3555  IT @ invert transmit status flags
3556  HC @ enable host control
3557  TF @ enable test FIFO
3558  IR @ invert receive status flags
3559  RE @ enable receive
3560  TE @ enable transmit
3561  BK @ abort operation and clear FIFOs
3562  EC @ send control register as first I2C byte
3563  ES @ send status register as first I2C byte
3564  PL @ set SPI polarity high
3565  PH @ set SPI phase high
3566  I2 @ enable I2C mode
3567  SP @ enable SPI mode
3568  EN @ enable BSC peripheral
3569 
3570  The status has the following format:
3571 
3572  . .
3573  20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3574  S S S S S R R R R R T T T T T RB TE RF TF RE TB
3575  . .
3576 
3577  Bits 0-15 are copied unchanged from the BSC FR register. See
3578  pages 165-166 of the Broadcom peripherals document for full
3579  details.
3580 
3581  SSSSS @ number of bytes successfully copied to transmit FIFO
3582  RRRRR @ number of bytes in receieve FIFO
3583  TTTTT @ number of bytes in transmit FIFO
3584  RB @ receive busy
3585  TE @ transmit FIFO empty
3586  RF @ receive FIFO full
3587  TF @ transmit FIFO full
3588  RE @ receive FIFO empty
3589  TB @ transmit busy
3590 
3591  ...
3592  (status, count, data) = pi.bsc_xfer(0x330305, "Hello!")
3593  ...
3594  """
3595  # I p1 control
3596  # I p2 0
3597  # I p3 len
3598  ## extension ##
3599  # s len data bytes
3600 
3601  status = PI_CMD_INTERRUPTED
3602  bytes = 0
3603  rdata = bytearray(b'')
3604  with self.sl.l:
3606  self.sl, _PI_CMD_BSCX, bsc_control, 0, len(data), [data]))
3607  if bytes > 0:
3608  rx = self._rxbuf(bytes)
3609  status = struct.unpack('I', rx[0:4])[0]
3610  bytes -= 4
3611  rdata = rx[4:]
3612  else:
3613  status = bytes
3614  bytes = 0
3615  return status, bytes, rdata
3616 
3617  def bsc_i2c(self, i2c_address, data=[]):
3618  """
3619  This function allows the Pi to act as a slave I2C device.
3620 
3621  The data bytes (if any) are written to the BSC transmit
3622  FIFO and the bytes in the BSC receive FIFO are returned.
3623 
3624  i2c_address:= the I2C slave address.
3625  data:= the data bytes to transmit.
3626 
3627  The returned value is a tuple of the status, the number
3628  of bytes read, and a bytearray containing the read bytes.
3629 
3630  See [*bsc_xfer*] for details of the status value.
3631 
3632  If there was an error the status will be less than zero
3633  (and will contain the error code).
3634 
3635  Note that an i2c_address of 0 may be used to close
3636  the BSC device and reassign the used GPIO (18/19)
3637  as inputs.
3638 
3639  This example assumes GPIO 2/3 are connected to GPIO 18/19.
3640 
3641  ...
3642  #!/usr/bin/env python
3643  import time
3644  import pigpio
3645 
3646  I2C_ADDR=0x13
3647 
3648  def i2c(id, tick):
3649  global pi
3650 
3651  s, b, d = pi.bsc_i2c(I2C_ADDR)
3652  if b:
3653  if d[0] == ord('t'): # 116 send 'HH:MM:SS*'
3654 
3655  print("sent={} FR={} received={} [{}]".
3656  format(s>>16, s&0xfff,b,d))
3657 
3658  s, b, d = pi.bsc_i2c(I2C_ADDR,
3659  "{}*".format(time.asctime()[11:19]))
3660 
3661  elif d[0] == ord('d'): # 100 send 'Sun Oct 30*'
3662 
3663  print("sent={} FR={} received={} [{}]".
3664  format(s>>16, s&0xfff,b,d))
3665 
3666  s, b, d = pi.bsc_i2c(I2C_ADDR,
3667  "{}*".format(time.asctime()[:10]))
3668 
3669  pi = pigpio.pi()
3670 
3671  if not pi.connected:
3672  exit()
3673 
3674  # Respond to BSC slave activity
3675 
3676  e = pi.event_callback(pigpio.EVENT_BSC, i2c)
3677 
3678  pi.bsc_i2c(I2C_ADDR) # Configure BSC as I2C slave
3679 
3680  time.sleep(600)
3681 
3682  e.cancel()
3683 
3684  pi.bsc_i2c(0) # Disable BSC peripheral
3685 
3686  pi.stop()
3687  ...
3688 
3689  While running the above.
3690 
3691  . .
3692  $ i2cdetect -y 1
3693  0 1 2 3 4 5 6 7 8 9 a b c d e f
3694  00: -- -- -- -- -- -- -- -- -- -- -- -- --
3695  10: -- -- -- 13 -- -- -- -- -- -- -- -- -- -- -- --
3696  20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
3697  30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
3698  40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
3699  50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
3700  60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
3701  70: -- -- -- -- -- -- -- --
3702 
3703  $ pigs i2co 1 0x13 0
3704  0
3705 
3706  $ pigs i2cwd 0 116
3707  $ pigs i2crd 0 9 -a
3708  9 10:13:58*
3709 
3710  $ pigs i2cwd 0 116
3711  $ pigs i2crd 0 9 -a
3712  9 10:14:29*
3713 
3714  $ pigs i2cwd 0 100
3715  $ pigs i2crd 0 11 -a
3716  11 Sun Oct 30*
3717 
3718  $ pigs i2cwd 0 100
3719  $ pigs i2crd 0 11 -a
3720  11 Sun Oct 30*
3721 
3722  $ pigs i2cwd 0 116
3723  $ pigs i2crd 0 9 -a
3724  9 10:23:16*
3725 
3726  $ pigs i2cwd 0 100
3727  $ pigs i2crd 0 11 -a
3728  11 Sun Oct 30*
3729  . .
3730  """
3731  if i2c_address:
3732  control = (i2c_address<<16)|0x305
3733  else:
3734  control = 0
3735  return self.bsc_xfer(control, data)
3736 
3737  def spi_open(self, spi_channel, baud, spi_flags=0):
3738  """
3739  Returns a handle for the SPI device on channel. Data will be
3740  transferred at baud bits per second. The flags may be used to
3741  modify the default behaviour of 4-wire operation, mode 0,
3742  active low chip select.
3743 
3744  An auxiliary SPI device is available on all models but the
3745  A and B and may be selected by setting the A bit in the
3746  flags. The auxiliary device has 3 chip selects and a
3747  selectable word size in bits.
3748 
3749  spi_channel:= 0-1 (0-2 for the auxiliary SPI device).
3750  baud:= 32K-125M (values above 30M are unlikely to work).
3751  spi_flags:= see below.
3752 
3753  Normally you would only use the [*spi_**] functions if
3754  you are or will be connecting to the Pi over a network. If
3755  you will always run on the local Pi use the standard SPI
3756  module instead.
3757 
3758  spi_flags consists of the least significant 22 bits.
3759 
3760  . .
3761  21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3762  b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m
3763  . .
3764 
3765  mm defines the SPI mode.
3766 
3767  WARNING: modes 1 and 3 do not appear to work on
3768  the auxiliary device.
3769 
3770  . .
3771  Mode POL PHA
3772  0 0 0
3773  1 0 1
3774  2 1 0
3775  3 1 1
3776  . .
3777 
3778  px is 0 if CEx is active low (default) and 1 for active high.
3779 
3780  ux is 0 if the CEx GPIO is reserved for SPI (default)
3781  and 1 otherwise.
3782 
3783  A is 0 for the standard SPI device, 1 for the auxiliary SPI.
3784 
3785  W is 0 if the device is not 3-wire, 1 if the device is 3-wire.
3786  Standard SPI device only.
3787 
3788  nnnn defines the number of bytes (0-15) to write before
3789  switching the MOSI line to MISO to read data. This field
3790  is ignored if W is not set. Standard SPI device only.
3791 
3792  T is 1 if the least significant bit is transmitted on MOSI
3793  first, the default (0) shifts the most significant bit out
3794  first. Auxiliary SPI device only.
3795 
3796  R is 1 if the least significant bit is received on MISO
3797  first, the default (0) receives the most significant bit
3798  first. Auxiliary SPI device only.
3799 
3800  bbbbbb defines the word size in bits (0-32). The default (0)
3801  sets 8 bits per word. Auxiliary SPI device only.
3802 
3803  The [*spi_read*], [*spi_write*], and [*spi_xfer*] functions
3804  transfer data packed into 1, 2, or 4 bytes according to
3805  the word size in bits.
3806 
3807  For bits 1-8 there will be one byte per character.
3808  For bits 9-16 there will be two bytes per character.
3809  For bits 17-32 there will be four bytes per character.
3810 
3811  Multi-byte transfers are made in least significant byte
3812  first order.
3813 
3814  E.g. to transfer 32 11-bit words data should
3815  contain 64 bytes.
3816 
3817  E.g. to transfer the 14 bit value 0x1ABC send the
3818  bytes 0xBC followed by 0x1A.
3819 
3820  The other bits in flags should be set to zero.
3821 
3822  ...
3823  # open SPI device on channel 1 in mode 3 at 50000 bits per second
3824 
3825  h = pi.spi_open(1, 50000, 3)
3826  ...
3827  """
3828  # I p1 spi_channel
3829  # I p2 baud
3830  # I p3 4
3831  ## extension ##
3832  # I spi_flags
3833  extents = [struct.pack("I", spi_flags)]
3834  return _u2i(_pigpio_command_ext(
3835  self.sl, _PI_CMD_SPIO, spi_channel, baud, 4, extents))
3836 
3837  def spi_close(self, handle):
3838  """
3839  Closes the SPI device associated with handle.
3840 
3841  handle:= >=0 (as returned by a prior call to [*spi_open*]).
3842 
3843  ...
3844  pi.spi_close(h)
3845  ...
3846  """
3847  return _u2i(_pigpio_command(self.sl, _PI_CMD_SPIC, handle, 0))
3848 
3849  def spi_read(self, handle, count):
3850  """
3851  Reads count bytes from the SPI device associated with handle.
3852 
3853  handle:= >=0 (as returned by a prior call to [*spi_open*]).
3854  count:= >0, the number of bytes to read.
3855 
3856  The returned value is a tuple of the number of bytes read and a
3857  bytearray containing the bytes. If there was an error the
3858  number of bytes read will be less than zero (and will contain
3859  the error code).
3860 
3861  ...
3862  (b, d) = pi.spi_read(h, 60) # read 60 bytes from device h
3863  if b == 60:
3864  # process read data
3865  else:
3866  # error path
3867  ...
3868  """
3869  bytes = PI_CMD_INTERRUPTED
3870  rdata = ""
3871  with self.sl.l:
3872  bytes = u2i(_pigpio_command_nolock(
3873  self.sl, _PI_CMD_SPIR, handle, count))
3874  if bytes > 0:
3875  rdata = self._rxbuf(bytes)
3876  return bytes, rdata
3877 
3878  def spi_write(self, handle, data):
3879  """
3880  Writes the data bytes to the SPI device associated with handle.
3881 
3882  handle:= >=0 (as returned by a prior call to [*spi_open*]).
3883  data:= the bytes to write.
3884 
3885  ...
3886  pi.spi_write(0, b'\\x02\\xc0\\x80') # write 3 bytes to device 0
3887 
3888  pi.spi_write(0, b'defgh') # write 5 bytes to device 0
3889 
3890  pi.spi_write(0, "def") # write 3 bytes to device 0
3891 
3892  pi.spi_write(1, [2, 192, 128]) # write 3 bytes to device 1
3893  ...
3894  """
3895  # I p1 handle
3896  # I p2 0
3897  # I p3 len
3898  ## extension ##
3899  # s len data bytes
3900  return _u2i(_pigpio_command_ext(
3901  self.sl, _PI_CMD_SPIW, handle, 0, len(data), [data]))
3902 
3903  def spi_xfer(self, handle, data):
3904  """
3905  Writes the data bytes to the SPI device associated with handle,
3906  returning the data bytes read from the device.
3907 
3908  handle:= >=0 (as returned by a prior call to [*spi_open*]).
3909  data:= the bytes to write.
3910 
3911  The returned value is a tuple of the number of bytes read and a
3912  bytearray containing the bytes. If there was an error the
3913  number of bytes read will be less than zero (and will contain
3914  the error code).
3915 
3916  ...
3917  (count, rx_data) = pi.spi_xfer(h, b'\\x01\\x80\\x00')
3918 
3919  (count, rx_data) = pi.spi_xfer(h, [1, 128, 0])
3920 
3921  (count, rx_data) = pi.spi_xfer(h, b"hello")
3922 
3923  (count, rx_data) = pi.spi_xfer(h, "hello")
3924  ...
3925  """
3926  # I p1 handle
3927  # I p2 0
3928  # I p3 len
3929  ## extension ##
3930  # s len data bytes
3931 
3932  bytes = PI_CMD_INTERRUPTED
3933  rdata = ""
3934  with self.sl.l:
3936  self.sl, _PI_CMD_SPIX, handle, 0, len(data), [data]))
3937  if bytes > 0:
3938  rdata = self._rxbuf(bytes)
3939  return bytes, rdata
3940 
3941  def serial_open(self, tty, baud, ser_flags=0):
3942  """
3943  Returns a handle for the serial tty device opened
3944  at baud bits per second. The device name must start
3945  with /dev/tty or /dev/serial.
3946 
3947  tty:= the serial device to open.
3948  baud:= baud rate in bits per second, see below.
3949  ser_flags:= 0, no flags are currently defined.
3950 
3951  Normally you would only use the [*serial_**] functions if
3952  you are or will be connecting to the Pi over a network. If
3953  you will always run on the local Pi use the standard serial
3954  module instead.
3955 
3956  The baud rate must be one of 50, 75, 110, 134, 150,
3957  200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200,
3958  38400, 57600, 115200, or 230400.
3959 
3960  ...
3961  h1 = pi.serial_open("/dev/ttyAMA0", 300)
3962 
3963  h2 = pi.serial_open("/dev/ttyUSB1", 19200, 0)
3964 
3965  h3 = pi.serial_open("/dev/serial0", 9600)
3966  ...
3967  """
3968  # I p1 baud
3969  # I p2 ser_flags
3970  # I p3 len
3971  ## extension ##
3972  # s len data bytes
3973  return _u2i(_pigpio_command_ext(
3974  self.sl, _PI_CMD_SERO, baud, ser_flags, len(tty), [tty]))
3975 
3976  def serial_close(self, handle):
3977  """
3978  Closes the serial device associated with handle.
3979 
3980  handle:= >=0 (as returned by a prior call to [*serial_open*]).
3981 
3982  ...
3983  pi.serial_close(h1)
3984  ...
3985  """
3986  return _u2i(_pigpio_command(self.sl, _PI_CMD_SERC, handle, 0))
3987 
3988  def serial_read_byte(self, handle):
3989  """
3990  Returns a single byte from the device associated with handle.
3991 
3992  handle:= >=0 (as returned by a prior call to [*serial_open*]).
3993 
3994  If no data is ready a negative error code will be returned.
3995 
3996  ...
3997  b = pi.serial_read_byte(h1)
3998  ...
3999  """
4000  return _u2i(_pigpio_command(self.sl, _PI_CMD_SERRB, handle, 0))
4001 
4002  def serial_write_byte(self, handle, byte_val):
4003  """
4004  Writes a single byte to the device associated with handle.
4005 
4006  handle:= >=0 (as returned by a prior call to [*serial_open*]).
4007  byte_val:= 0-255, the value to write.
4008 
4009  ...
4010  pi.serial_write_byte(h1, 23)
4011 
4012  pi.serial_write_byte(h1, ord('Z'))
4013  ...
4014  """
4015  return _u2i(
4016  _pigpio_command(self.sl, _PI_CMD_SERWB, handle, byte_val))
4017 
4018  def serial_read(self, handle, count=1000):
4019  """
4020  Reads up to count bytes from the device associated with handle.
4021 
4022  handle:= >=0 (as returned by a prior call to [*serial_open*]).
4023  count:= >0, the number of bytes to read (defaults to 1000).
4024 
4025  The returned value is a tuple of the number of bytes read and
4026  a bytearray containing the bytes. If there was an error the
4027  number of bytes read will be less than zero (and will contain
4028  the error code).
4029 
4030  If no data is ready a bytes read of zero is returned.
4031  ...
4032  (b, d) = pi.serial_read(h2, 100)
4033  if b > 0:
4034  # process read data
4035  ...
4036  """
4037  bytes = PI_CMD_INTERRUPTED
4038  rdata = ""
4039  with self.sl.l:
4040  bytes = u2i(
4041  _pigpio_command_nolock(self.sl, _PI_CMD_SERR, handle, count))
4042  if bytes > 0:
4043  rdata = self._rxbuf(bytes)
4044  return bytes, rdata
4045 
4046  def serial_write(self, handle, data):
4047  """
4048  Writes the data bytes to the device associated with handle.
4049 
4050  handle:= >=0 (as returned by a prior call to [*serial_open*]).
4051  data:= the bytes to write.
4052 
4053  ...
4054  pi.serial_write(h1, b'\\x02\\x03\\x04')
4055 
4056  pi.serial_write(h2, b'help')
4057 
4058  pi.serial_write(h2, "hello")
4059 
4060  pi.serial_write(h1, [2, 3, 4])
4061  ...
4062  """
4063  # I p1 handle
4064  # I p2 0
4065  # I p3 len
4066  ## extension ##
4067  # s len data bytes
4068 
4069  return _u2i(_pigpio_command_ext(
4070  self.sl, _PI_CMD_SERW, handle, 0, len(data), [data]))
4071 
4072  def serial_data_available(self, handle):
4073  """
4074  Returns the number of bytes available to be read from the
4075  device associated with handle.
4076 
4077  handle:= >=0 (as returned by a prior call to [*serial_open*]).
4078 
4079  ...
4080  rdy = pi.serial_data_available(h1)
4081 
4082  if rdy > 0:
4083  (b, d) = pi.serial_read(h1, rdy)
4084  ...
4085  """
4086  return _u2i(_pigpio_command(self.sl, _PI_CMD_SERDA, handle, 0))
4087 
4088  def gpio_trigger(self, user_gpio, pulse_len=10, level=1):
4089  """
4090  Send a trigger pulse to a GPIO. The GPIO is set to
4091  level for pulse_len microseconds and then reset to not level.
4092 
4093  user_gpio:= 0-31
4094  pulse_len:= 1-100
4095  level:= 0-1
4096 
4097  ...
4098  pi.gpio_trigger(23, 10, 1)
4099  ...
4100  """
4101  # pigpio message format
4102 
4103  # I p1 user_gpio
4104  # I p2 pulse_len
4105  # I p3 4
4106  ## extension ##
4107  # I level
4108  extents = [struct.pack("I", level)]
4109  return _u2i(_pigpio_command_ext(
4110  self.sl, _PI_CMD_TRIG, user_gpio, pulse_len, 4, extents))
4111 
4112  def set_glitch_filter(self, user_gpio, steady):
4113  """
4114  Sets a glitch filter on a GPIO.
4115 
4116  Level changes on the GPIO are not reported unless the level
4117  has been stable for at least [*steady*] microseconds. The
4118  level is then reported. Level changes of less than [*steady*]
4119  microseconds are ignored.
4120 
4121  user_gpio:= 0-31
4122  steady:= 0-300000
4123 
4124  Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER.
4125 
4126  This filter affects the GPIO samples returned to callbacks set up
4127  with [*callback*] and [*wait_for_edge*].
4128 
4129  It does not affect levels read by [*read*],
4130  [*read_bank_1*], or [*read_bank_2*].
4131 
4132  Each (stable) edge will be timestamped [*steady*]
4133  microseconds after it was first detected.
4134 
4135  ...
4136  pi.set_glitch_filter(23, 100)
4137  ...
4138  """
4139  return _u2i(_pigpio_command(self.sl, _PI_CMD_FG, user_gpio, steady))
4140 
4141  def set_noise_filter(self, user_gpio, steady, active):
4142  """
4143  Sets a noise filter on a GPIO.
4144 
4145  Level changes on the GPIO are ignored until a level which has
4146  been stable for [*steady*] microseconds is detected. Level
4147  changes on the GPIO are then reported for [*active*]
4148  microseconds after which the process repeats.
4149 
4150  user_gpio:= 0-31
4151  steady:= 0-300000
4152  active:= 0-1000000
4153 
4154  Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER.
4155 
4156  This filter affects the GPIO samples returned to callbacks set up
4157  with [*callback*] and [*wait_for_edge*].
4158 
4159  It does not affect levels read by [*read*],
4160  [*read_bank_1*], or [*read_bank_2*].
4161 
4162  Level changes before and after the active period may
4163  be reported. Your software must be designed to cope with
4164  such reports.
4165 
4166  ...
4167  pi.set_noise_filter(23, 1000, 5000)
4168  ...
4169  """
4170  # pigpio message format
4171 
4172  # I p1 user_gpio
4173  # I p2 steady
4174  # I p3 4
4175  ## extension ##
4176  # I active
4177  extents = [struct.pack("I", active)]
4178  return _u2i(_pigpio_command_ext(
4179  self.sl, _PI_CMD_FN, user_gpio, steady, 4, extents))
4180 
4181  def store_script(self, script):
4182  """
4183  Store a script for later execution.
4184 
4185  See [[http://abyz.me.uk/rpi/pigpio/pigs.html#Scripts]] for
4186  details.
4187 
4188  script:= the script text as a series of bytes.
4189 
4190  Returns a >=0 script id if OK.
4191 
4192  ...
4193  sid = pi.store_script(
4194  b'tag 0 w 22 1 mils 100 w 22 0 mils 100 dcr p0 jp 0')
4195  ...
4196  """
4197  # I p1 0
4198  # I p2 0
4199  # I p3 len
4200  ## extension ##
4201  # s len data bytes
4202  if len(script):
4203  return _u2i(_pigpio_command_ext(
4204  self.sl, _PI_CMD_PROC, 0, 0, len(script), [script]))
4205  else:
4206  return 0
4207 
4208  def run_script(self, script_id, params=None):
4209  """
4210  Runs a stored script.
4211 
4212  script_id:= id of stored script.
4213  params:= up to 10 parameters required by the script.
4214 
4215  ...
4216  s = pi.run_script(sid, [par1, par2])
4217 
4218  s = pi.run_script(sid)
4219 
4220  s = pi.run_script(sid, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
4221  ...
4222  """
4223  # I p1 script id
4224  # I p2 0
4225  # I p3 params * 4 (0-10 params)
4226  ## (optional) extension ##
4227  # I[] params
4228  if params is not None:
4229  ext = bytearray()
4230  for p in params:
4231  ext.extend(struct.pack("I", p))
4232  nump = len(params)
4233  extents = [ext]
4234  else:
4235  nump = 0
4236  extents = []
4237  return _u2i(_pigpio_command_ext(
4238  self.sl, _PI_CMD_PROCR, script_id, 0, nump*4, extents))
4239 
4240  def update_script(self, script_id, params=None):
4241  """
4242  Sets the parameters of a script. The script may or
4243  may not be running. The first parameters of the script are
4244  overwritten with the new values.
4245 
4246  script_id:= id of stored script.
4247  params:= up to 10 parameters required by the script.
4248 
4249  ...
4250  s = pi.update_script(sid, [par1, par2])
4251 
4252  s = pi.update_script(sid, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
4253  ...
4254  """
4255  # I p1 script id
4256  # I p2 0
4257  # I p3 params * 4 (0-10 params)
4258  ## (optional) extension ##
4259  # I[] params
4260  if params is not None:
4261  ext = bytearray()
4262  for p in params:
4263  ext.extend(struct.pack("I", p))
4264  nump = len(params)
4265  extents = [ext]
4266  else:
4267  nump = 0
4268  extents = []
4269  return _u2i(_pigpio_command_ext(
4270  self.sl, _PI_CMD_PROCU, script_id, 0, nump*4, extents))
4271 
4272  def script_status(self, script_id):
4273  """
4274  Returns the run status of a stored script as well as the
4275  current values of parameters 0 to 9.
4276 
4277  script_id:= id of stored script.
4278 
4279  The run status may be
4280 
4281  . .
4282  PI_SCRIPT_INITING
4283  PI_SCRIPT_HALTED
4284  PI_SCRIPT_RUNNING
4285  PI_SCRIPT_WAITING
4286  PI_SCRIPT_FAILED
4287  . .
4288 
4289  The return value is a tuple of run status and a list of
4290  the 10 parameters. On error the run status will be negative
4291  and the parameter list will be empty.
4292 
4293  ...
4294  (s, pars) = pi.script_status(sid)
4295  ...
4296  """
4297  status = PI_CMD_INTERRUPTED
4298  params = ()
4299  with self.sl.l:
4300  bytes = u2i(
4301  _pigpio_command_nolock(self.sl, _PI_CMD_PROCP, script_id, 0))
4302  if bytes > 0:
4303  data = self._rxbuf(bytes)
4304  pars = struct.unpack('11i', _str(data))
4305  status = pars[0]
4306  params = pars[1:]
4307  else:
4308  status = bytes
4309  return status, params
4310 
4311  def stop_script(self, script_id):
4312  """
4313  Stops a running script.
4314 
4315  script_id:= id of stored script.
4316 
4317  ...
4318  status = pi.stop_script(sid)
4319  ...
4320  """
4321  return _u2i(_pigpio_command(self.sl, _PI_CMD_PROCS, script_id, 0))
4322 
4323  def delete_script(self, script_id):
4324  """
4325  Deletes a stored script.
4326 
4327  script_id:= id of stored script.
4328 
4329  ...
4330  status = pi.delete_script(sid)
4331  ...
4332  """
4333  return _u2i(_pigpio_command(self.sl, _PI_CMD_PROCD, script_id, 0))
4334 
4335  def bb_serial_read_open(self, user_gpio, baud, bb_bits=8):
4336  """
4337  Opens a GPIO for bit bang reading of serial data.
4338 
4339  user_gpio:= 0-31, the GPIO to use.
4340  baud:= 50-250000, the baud rate.
4341  bb_bits:= 1-32, the number of bits per word, default 8.
4342 
4343  The serial data is held in a cyclic buffer and is read using
4344  [*bb_serial_read*].
4345 
4346  It is the caller's responsibility to read data from the cyclic
4347  buffer in a timely fashion.
4348 
4349  ...
4350  status = pi.bb_serial_read_open(4, 19200)
4351  status = pi.bb_serial_read_open(17, 9600)
4352  ...
4353  """
4354  # pigpio message format
4355 
4356  # I p1 user_gpio
4357  # I p2 baud
4358  # I p3 4
4359  ## extension ##
4360  # I bb_bits
4361  extents = [struct.pack("I", bb_bits)]
4362  return _u2i(_pigpio_command_ext(
4363  self.sl, _PI_CMD_SLRO, user_gpio, baud, 4, extents))
4364 
4365  def bb_serial_read(self, user_gpio):
4366  """
4367  Returns data from the bit bang serial cyclic buffer.
4368 
4369  user_gpio:= 0-31 (opened in a prior call to [*bb_serial_read_open*])
4370 
4371  The returned value is a tuple of the number of bytes read and a
4372  bytearray containing the bytes. If there was an error the
4373  number of bytes read will be less than zero (and will contain
4374  the error code).
4375 
4376  The bytes returned for each character depend upon the number of
4377  data bits [*bb_bits*] specified in the [*bb_serial_read_open*]
4378  command.
4379 
4380  For [*bb_bits*] 1-8 there will be one byte per character.
4381  For [*bb_bits*] 9-16 there will be two bytes per character.
4382  For [*bb_bits*] 17-32 there will be four bytes per character.
4383 
4384  ...
4385  (count, data) = pi.bb_serial_read(4)
4386  ...
4387  """
4388  bytes = PI_CMD_INTERRUPTED
4389  rdata = ""
4390  with self.sl.l:
4391  bytes = u2i(
4392  _pigpio_command_nolock(self.sl, _PI_CMD_SLR, user_gpio, 10000))
4393  if bytes > 0:
4394  rdata = self._rxbuf(bytes)
4395  return bytes, rdata
4396 
4397 
4398  def bb_serial_read_close(self, user_gpio):
4399  """
4400  Closes a GPIO for bit bang reading of serial data.
4401 
4402  user_gpio:= 0-31 (opened in a prior call to [*bb_serial_read_open*])
4403 
4404  ...
4405  status = pi.bb_serial_read_close(17)
4406  ...
4407  """
4408  return _u2i(_pigpio_command(self.sl, _PI_CMD_SLRC, user_gpio, 0))
4409 
4410  def bb_serial_invert(self, user_gpio, invert):
4411  """
4412  Invert serial logic.
4413 
4414  user_gpio:= 0-31 (opened in a prior call to [*bb_serial_read_open*])
4415  invert:= 0-1 (1 invert, 0 normal)
4416 
4417  ...
4418  status = pi.bb_serial_invert(17, 1)
4419  ...
4420  """
4421  return _u2i(_pigpio_command(self.sl, _PI_CMD_SLRI, user_gpio, invert))
4422 
4423 
4424  def custom_1(self, arg1=0, arg2=0, argx=[]):
4425  """
4426  Calls a pigpio function customised by the user.
4427 
4428  arg1:= >=0, default 0.
4429  arg2:= >=0, default 0.
4430  argx:= extra arguments (each 0-255), default empty.
4431 
4432  The returned value is an integer which by convention
4433  should be >=0 for OK and <0 for error.
4434 
4435  ...
4436  value = pi.custom_1()
4437 
4438  value = pi.custom_1(23)
4439 
4440  value = pi.custom_1(0, 55)
4441 
4442  value = pi.custom_1(23, 56, [1, 5, 7])
4443 
4444  value = pi.custom_1(23, 56, b"hello")
4445 
4446  value = pi.custom_1(23, 56, "hello")
4447  ...
4448  """
4449  # I p1 arg1
4450  # I p2 arg2
4451  # I p3 len
4452  ## extension ##
4453  # s len argx bytes
4454 
4455  return u2i(_pigpio_command_ext(
4456  self.sl, _PI_CMD_CF1, arg1, arg2, len(argx), [argx]))
4457 
4458  def custom_2(self, arg1=0, argx=[], retMax=8192):
4459  """
4460  Calls a pigpio function customised by the user.
4461 
4462  arg1:= >=0, default 0.
4463  argx:= extra arguments (each 0-255), default empty.
4464  retMax:= >=0, maximum number of bytes to return, default 8192.
4465 
4466  The returned value is a tuple of the number of bytes
4467  returned and a bytearray containing the bytes. If
4468  there was an error the number of bytes read will be
4469  less than zero (and will contain the error code).
4470 
4471  ...
4472  (count, data) = pi.custom_2()
4473 
4474  (count, data) = pi.custom_2(23)
4475 
4476  (count, data) = pi.custom_2(23, [1, 5, 7])
4477 
4478  (count, data) = pi.custom_2(23, b"hello")
4479 
4480  (count, data) = pi.custom_2(23, "hello", 128)
4481  ...
4482  """
4483  # I p1 arg1
4484  # I p2 retMax
4485  # I p3 len
4486  ## extension ##
4487  # s len argx bytes
4488 
4489  bytes = PI_CMD_INTERRUPTED
4490  rdata = ""
4491  with self.sl.l:
4493  self.sl, _PI_CMD_CF2, arg1, retMax, len(argx), [argx]))
4494  if bytes > 0:
4495  rdata = self._rxbuf(bytes)
4496  return bytes, rdata
4497 
4498  def get_pad_strength(self, pad):
4499  """
4500  This function returns the pad drive strength in mA.
4501 
4502  pad:= 0-2, the pad to get.
4503 
4504  Returns the pad drive strength if OK, otherwise PI_BAD_PAD.
4505 
4506  Pad @ GPIO
4507  0 @ 0-27
4508  1 @ 28-45
4509  2 @ 46-53
4510 
4511  ...
4512  strength = pi.get_pad_strength(0) # Get pad 0 strength.
4513  ...
4514  """
4515  return _u2i(_pigpio_command(self.sl, _PI_CMD_PADG, pad, 0))
4516 
4517  def set_pad_strength(self, pad, pad_strength):
4518  """
4519  This function sets the pad drive strength in mA.
4520 
4521 
4522  pad:= 0-2, the pad to set.
4523  pad_strength:= 1-16 mA.
4524 
4525  Returns 0 if OK, otherwise PI_BAD_PAD, or PI_BAD_STRENGTH.
4526 
4527  Pad @ GPIO
4528  0 @ 0-27
4529  1 @ 28-45
4530  2 @ 46-53
4531 
4532  ...
4533  pi.set_pad_strength(2, 14) # Set pad 2 to 14 mA.
4534  ...
4535  """
4536  return _u2i(_pigpio_command(self.sl, _PI_CMD_PADS, pad, pad_strength))
4537 
4538 
4539  def file_open(self, file_name, file_mode):
4540  """
4541  This function returns a handle to a file opened in a specified mode.
4542 
4543  file_name:= the file to open.
4544  file_mode:= the file open mode.
4545 
4546  Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE,
4547  PI_NO_FILE_ACCESS, PI_BAD_FILE_MODE,
4548  PI_FILE_OPEN_FAILED, or PI_FILE_IS_A_DIR.
4549 
4550  ...
4551  h = pi.file_open("/home/pi/shared/dir_3/file.txt",
4552  pigpio.FILE_WRITE | pigpio.FILE_CREATE)
4553 
4554  pi.file_write(h, "Hello world")
4555 
4556  pi.file_close(h)
4557  ...
4558 
4559  File
4560 
4561  A file may only be opened if permission is granted by an entry
4562  in /opt/pigpio/access. This is intended to allow remote access
4563  to files in a more or less controlled manner.
4564 
4565  Each entry in /opt/pigpio/access takes the form of a file path
4566  which may contain wildcards followed by a single letter permission.
4567  The permission may be R for read, W for write, U for read/write,
4568  and N for no access.
4569 
4570  Where more than one entry matches a file the most specific rule
4571  applies. If no entry matches a file then access is denied.
4572 
4573  Suppose /opt/pigpio/access contains the following entries:
4574 
4575  . .
4576  /home/* n
4577  /home/pi/shared/dir_1/* w
4578  /home/pi/shared/dir_2/* r
4579  /home/pi/shared/dir_3/* u
4580  /home/pi/shared/dir_1/file.txt n
4581  . .
4582 
4583  Files may be written in directory dir_1 with the exception
4584  of file.txt.
4585 
4586  Files may be read in directory dir_2.
4587 
4588  Files may be read and written in directory dir_3.
4589 
4590  If a directory allows read, write, or read/write access then files
4591  may be created in that directory.
4592 
4593  In an attempt to prevent risky permissions the following paths are
4594  ignored in /opt/pigpio/access:
4595 
4596  . .
4597  a path containing ..
4598  a path containing only wildcards (*?)
4599  a path containing less than two non-wildcard parts
4600  . .
4601 
4602  Mode
4603 
4604  The mode may have the following values:
4605 
4606  Constant @ Value @ Meaning
4607  FILE_READ @ 1 @ open file for reading
4608  FILE_WRITE @ 2 @ open file for writing
4609  FILE_RW @ 3 @ open file for reading and writing
4610 
4611  The following values may be or'd into the mode:
4612 
4613  Name @ Value @ Meaning
4614  FILE_APPEND @ 4 @ All writes append data to the end of the file
4615  FILE_CREATE @ 8 @ The file is created if it doesn't exist
4616  FILE_TRUNC @ 16 @ The file is truncated
4617 
4618  Newly created files are owned by root with permissions owner
4619  read and write.
4620 
4621  ...
4622  #!/usr/bin/env python
4623 
4624  import pigpio
4625 
4626  pi = pigpio.pi()
4627 
4628  if not pi.connected:
4629  exit()
4630 
4631  # Assumes /opt/pigpio/access contains the following line:
4632  # /ram/*.c r
4633 
4634  handle = pi.file_open("/ram/pigpio.c", pigpio.FILE_READ)
4635 
4636  done = False
4637 
4638  while not done:
4639  c, d = pi.file_read(handle, 60000)
4640  if c > 0:
4641  print(d)
4642  else:
4643  done = True
4644 
4645  pi.file_close(handle)
4646 
4647  pi.stop()
4648  ...
4649  """
4650  # I p1 file_mode
4651  # I p2 0
4652  # I p3 len
4653  ## extension ##
4654  # s len data bytes
4655  return _u2i(_pigpio_command_ext(
4656  self.sl, _PI_CMD_FO, file_mode, 0, len(file_name), [file_name]))
4657 
4658  def file_close(self, handle):
4659  """
4660  Closes the file associated with handle.
4661 
4662  handle:= >=0 (as returned by a prior call to [*file_open*]).
4663 
4664  ...
4665  pi.file_close(handle)
4666  ...
4667  """
4668  return _u2i(_pigpio_command(self.sl, _PI_CMD_FC, handle, 0))
4669 
4670  def file_read(self, handle, count):
4671  """
4672  Reads up to count bytes from the file associated with handle.
4673 
4674  handle:= >=0 (as returned by a prior call to [*file_open*]).
4675  count:= >0, the number of bytes to read.
4676 
4677  The returned value is a tuple of the number of bytes read and a
4678  bytearray containing the bytes. If there was an error the
4679  number of bytes read will be less than zero (and will contain
4680  the error code).
4681 
4682  ...
4683  (b, d) = pi.file_read(h2, 100)
4684  if b > 0:
4685  # process read data
4686  ...
4687  """
4688  bytes = PI_CMD_INTERRUPTED
4689  rdata = ""
4690  with self.sl.l:
4691  bytes = u2i(
4692  _pigpio_command_nolock(self.sl, _PI_CMD_FR, handle, count))
4693  if bytes > 0:
4694  rdata = self._rxbuf(bytes)
4695  return bytes, rdata
4696 
4697  def file_write(self, handle, data):
4698  """
4699  Writes the data bytes to the file associated with handle.
4700 
4701  handle:= >=0 (as returned by a prior call to [*file_open*]).
4702  data:= the bytes to write.
4703 
4704  ...
4705  pi.file_write(h1, b'\\x02\\x03\\x04')
4706 
4707  pi.file_write(h2, b'help')
4708 
4709  pi.file_write(h2, "hello")
4710 
4711  pi.file_write(h1, [2, 3, 4])
4712  ...
4713  """
4714  # I p1 handle
4715  # I p2 0
4716  # I p3 len
4717  ## extension ##
4718  # s len data bytes
4719 
4720  return _u2i(_pigpio_command_ext(
4721  self.sl, _PI_CMD_FW, handle, 0, len(data), [data]))
4722 
4723  def file_seek(self, handle, seek_offset, seek_from):
4724  """
4725  Seeks to a position relative to the start, current position,
4726  or end of the file. Returns the new position.
4727 
4728  handle:= >=0 (as returned by a prior call to [*file_open*]).
4729  seek_offset:= byte offset.
4730  seek_from:= FROM_START, FROM_CURRENT, or FROM_END.
4731 
4732  ...
4733  new_pos = pi.file_seek(h, 100, pigpio.FROM_START)
4734 
4735  cur_pos = pi.file_seek(h, 0, pigpio.FROM_CURRENT)
4736 
4737  file_size = pi.file_seek(h, 0, pigpio.FROM_END)
4738  ...
4739  """
4740  # I p1 handle
4741  # I p2 seek_offset
4742  # I p3 4
4743  ## extension ##
4744  # I seek_from
4745  extents = [struct.pack("I", seek_from)]
4746  return _u2i(_pigpio_command_ext(
4747  self.sl, _PI_CMD_FS, handle, seek_offset, 4, extents))
4748 
4749  def file_list(self, fpattern):
4750  """
4751  Returns a list of files which match a pattern.
4752 
4753  fpattern:= file pattern to match.
4754 
4755  Returns the number of returned bytes if OK, otherwise
4756  PI_NO_FILE_ACCESS, or PI_NO_FILE_MATCH.
4757 
4758  The pattern must match an entry in /opt/pigpio/access. The
4759  pattern may contain wildcards. See [*file_open*].
4760 
4761  NOTE
4762 
4763  The returned value is not the number of files, it is the number
4764  of bytes in the buffer. The file names are separated by newline
4765  characters.
4766 
4767  ...
4768  #!/usr/bin/env python
4769 
4770  import pigpio
4771 
4772  pi = pigpio.pi()
4773 
4774  if not pi.connected:
4775  exit()
4776 
4777  # Assumes /opt/pigpio/access contains the following line:
4778  # /ram/*.c r
4779 
4780  c, d = pi.file_list("/ram/p*.c")
4781  if c > 0:
4782  print(d)
4783 
4784  pi.stop()
4785  ...
4786  """
4787  # I p1 60000
4788  # I p2 0
4789  # I p3 len
4790  ## extension ##
4791  # s len data bytes
4792 
4793  bytes = PI_CMD_INTERRUPTED
4794  rdata = ""
4795  with self.sl.l:
4797  self.sl, _PI_CMD_FL, 60000, 0, len(fpattern), [fpattern]))
4798  if bytes > 0:
4799  rdata = self._rxbuf(bytes)
4800  return bytes, rdata
4801 
4802  def shell(self, shellscr, pstring=""):
4803  """
4804  This function uses the system call to execute a shell script
4805  with the given string as its parameter.
4806 
4807  shellscr:= the name of the script, only alphanumerics,
4808  '-' and '_' are allowed in the name
4809  pstring := the parameter string to pass to the script
4810 
4811  The exit status of the system call is returned if OK,
4812  otherwise PI_BAD_SHELL_STATUS.
4813 
4814  [*shellscr*] must exist in /opt/pigpio/cgi and must be executable.
4815 
4816  The returned exit status is normally 256 times that set by
4817  the shell script exit function. If the script can't be
4818  found 32512 will be returned.
4819 
4820  The following table gives some example returned statuses:
4821 
4822  Script exit status @ Returned system call status
4823  1 @ 256
4824  5 @ 1280
4825  10 @ 2560
4826  200 @ 51200
4827  script not found @ 32512
4828 
4829  ...
4830  // pass two parameters, hello and world
4831  status = pi.shell("scr1", "hello world");
4832 
4833  // pass three parameters, hello, string with spaces, and world
4834  status = pi.shell("scr1", "hello 'string with spaces' world");
4835 
4836  // pass one parameter, hello string with spaces world
4837  status = pi.shell("scr1", "\\"hello string with spaces world\\"");
4838  ...
4839  """
4840  # I p1 len(shellscr)
4841  # I p2 0
4842  # I p3 len(shellscr)+len(pstring)+1
4843  ## extension ##
4844  # s len data bytes
4845 
4846  ls = len(shellscr)
4847  lp = len(pstring)
4848  return _u2i(_pigpio_command_ext(
4849  self.sl, _PI_CMD_SHELL, ls, 0, ls+lp+1, [shellscr+'\x00'+pstring]))
4850 
4851 
4852  def callback(self, user_gpio, edge=RISING_EDGE, func=None):
4853  """
4854  Calls a user supplied function (a callback) whenever the
4855  specified GPIO edge is detected.
4856 
4857  user_gpio:= 0-31.
4858  edge:= EITHER_EDGE, RISING_EDGE (default), or FALLING_EDGE.
4859  func:= user supplied callback function.
4860 
4861  The user supplied callback receives three parameters, the GPIO,
4862  the level, and the tick.
4863 
4864  . .
4865  Parameter Value Meaning
4866 
4867  GPIO 0-31 The GPIO which has changed state
4868 
4869  level 0-2 0 = change to low (a falling edge)
4870  1 = change to high (a rising edge)
4871  2 = no level change (a watchdog timeout)
4872 
4873  tick 32 bit The number of microseconds since boot
4874  WARNING: this wraps around from
4875  4294967295 to 0 roughly every 72 minutes
4876  . .
4877 
4878  If a user callback is not specified a default tally callback is
4879  provided which simply counts edges. The count may be retrieved
4880  by calling the tally function. The count may be reset to zero
4881  by calling the reset_tally function.
4882 
4883  The callback may be cancelled by calling the cancel function.
4884 
4885  A GPIO may have multiple callbacks (although I can't think of
4886  a reason to do so).
4887 
4888  ...
4889  def cbf(gpio, level, tick):
4890  print(gpio, level, tick)
4891 
4892  cb1 = pi.callback(22, pigpio.EITHER_EDGE, cbf)
4893 
4894  cb2 = pi.callback(4, pigpio.EITHER_EDGE)
4895 
4896  cb3 = pi.callback(17)
4897 
4898  print(cb3.tally())
4899 
4900  cb3.reset_tally()
4901 
4902  cb1.cancel() # To cancel callback cb1.
4903  ...
4904  """
4905  return _callback(self._notify, user_gpio, edge, func)
4906 
4907  def event_callback(self, event, func=None):
4908  """
4909  Calls a user supplied function (a callback) whenever the
4910  specified event is signalled.
4911 
4912  event:= 0-31.
4913  func:= user supplied callback function.
4914 
4915  The user supplied callback receives two parameters, the event id,
4916  and the tick.
4917 
4918  If a user callback is not specified a default tally callback is
4919  provided which simply counts events. The count may be retrieved
4920  by calling the tally function. The count may be reset to zero
4921  by calling the reset_tally function.
4922 
4923  The callback may be cancelled by calling the event_cancel function.
4924 
4925  An event may have multiple callbacks (although I can't think of
4926  a reason to do so).
4927 
4928  ...
4929  def cbf(event, tick):
4930  print(event, tick)
4931 
4932  cb1 = pi.event_callback(22, cbf)
4933 
4934  cb2 = pi.event_callback(4)
4935 
4936  print(cb2.tally())
4937 
4938  cb2.reset_tally()
4939 
4940  cb1.event_cancel() # To cancel callback cb1.
4941  ...
4942  """
4943 
4944  return _event(self._notify, event, func)
4945 
4946  def wait_for_edge(self, user_gpio, edge=RISING_EDGE, wait_timeout=60.0):
4947  """
4948  Wait for an edge event on a GPIO.
4949 
4950  user_gpio:= 0-31.
4951  edge:= EITHER_EDGE, RISING_EDGE (default), or
4952  FALLING_EDGE.
4953  wait_timeout:= >=0.0 (default 60.0).
4954 
4955  The function returns when the edge is detected or after
4956  the number of seconds specified by timeout has expired.
4957 
4958  Do not use this function for precise timing purposes,
4959  the edge is only checked 20 times a second. Whenever
4960  you need to know the accurate time of GPIO events use
4961  a [*callback*] function.
4962 
4963  The function returns True if the edge is detected,
4964  otherwise False.
4965 
4966  ...
4967  if pi.wait_for_edge(23):
4968  print("Rising edge detected")
4969  else:
4970  print("wait for edge timed out")
4971 
4972  if pi.wait_for_edge(23, pigpio.FALLING_EDGE, 5.0):
4973  print("Falling edge detected")
4974  else:
4975  print("wait for falling edge timed out")
4976  ...
4977  """
4978  a = _wait_for_edge(self._notify, user_gpio, edge, wait_timeout)
4979  return a.trigger
4980 
4981  def wait_for_event(self, event, wait_timeout=60.0):
4982  """
4983  Wait for an event.
4984 
4985  event:= 0-31.
4986  wait_timeout:= >=0.0 (default 60.0).
4987 
4988  The function returns when the event is signalled or after
4989  the number of seconds specified by timeout has expired.
4990 
4991  The function returns True if the event is detected,
4992  otherwise False.
4993 
4994  ...
4995  if pi.wait_for_event(23):
4996  print("event detected")
4997  else:
4998  print("wait for event timed out")
4999  ...
5000  """
5001  a = _wait_for_event(self._notify, event, wait_timeout)
5002  return a.trigger
5003 
5004  def __init__(self,
5005  host = os.getenv("PIGPIO_ADDR", 'localhost'),
5006  port = os.getenv("PIGPIO_PORT", 8888),
5007  show_errors = True):
5008  """
5009  Grants access to a Pi's GPIO.
5010 
5011  host:= the host name of the Pi on which the pigpio daemon is
5012  running. The default is localhost unless overridden by
5013  the PIGPIO_ADDR environment variable.
5014 
5015  port:= the port number on which the pigpio daemon is listening.
5016  The default is 8888 unless overridden by the PIGPIO_PORT
5017  environment variable. The pigpio daemon must have been
5018  started with the same port number.
5019 
5020  This connects to the pigpio daemon and reserves resources
5021  to be used for sending commands and receiving notifications.
5022 
5023  An instance attribute [*connected*] may be used to check the
5024  success of the connection. If the connection is established
5025  successfully [*connected*] will be True, otherwise False.
5026 
5027  ...
5028  pi = pigio.pi() # use defaults
5029  pi = pigpio.pi('mypi') # specify host, default port
5030  pi = pigpio.pi('mypi', 7777) # specify host and port
5031 
5032  pi = pigpio.pi() # exit script if no connection
5033  if not pi.connected:
5034  exit()
5035  ...
5036  """
5037  self.connected = True
5038 
5039  self.sl = _socklock()
5040  self._notify = None
5041 
5042  port = int(port)
5043 
5044  if host == '':
5045  host = "localhost"
5046 
5047  self._host = host
5048  self._port = port
5049 
5050  try:
5051  self.sl.s = socket.create_connection((host, port), None)
5052 
5053  # Disable the Nagle algorithm.
5054  self.sl.s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
5055 
5056  self._notify = _callback_thread(self.sl, host, port)
5057 
5058  except socket.error:
5059  exception = 1
5060 
5061  except struct.error:
5062  exception = 2
5063 
5064  except error:
5065  # assumed to be no handle available
5066  exception = 3
5067 
5068  else:
5069  exception = 0
5070  atexit.register(self.stop)
5071 
5072  if exception != 0:
5073 
5074  self.connected = False
5075 
5076  if self.sl.s is not None:
5077  self.sl.s = None
5078 
5079  if show_errors:
5080 
5081  s = "Can't connect to pigpio at {}({})".format(host, str(port))
5082 
5083 
5084  print(_except_a.format(s))
5085  if exception == 1:
5086  print(_except_1)
5087  elif exception == 2:
5088  print(_except_2)
5089  else:
5090  print(_except_3)
5091  print(_except_z)
5092 
5093  def stop(self):
5094  """Release pigpio resources.
5095 
5096  ...
5097  pi.stop()
5098  ...
5099  """
5100 
5101  self.connected = False
5102 
5103  if self._notify is not None:
5104  self._notify.stop()
5105  self._notify = None
5106 
5107  if self.sl.s is not None:
5108  self.sl.s.close()
5109  self.sl.s = None
5110 
5111 def xref():
5112  """
5113  active: 0-1000000
5114  The number of microseconds level changes are reported for once
5115  a noise filter has been triggered (by [*steady*] microseconds of
5116  a stable level).
5117 
5118 
5119  arg1:
5120  An unsigned argument passed to a user customised function. Its
5121  meaning is defined by the customiser.
5122 
5123  arg2:
5124  An unsigned argument passed to a user customised function. Its
5125  meaning is defined by the customiser.
5126 
5127  argx:
5128  An array of bytes passed to a user customised function.
5129  Its meaning and content is defined by the customiser.
5130 
5131  baud:
5132  The speed of serial communication (I2C, SPI, serial link, waves)
5133  in bits per second.
5134 
5135  bb_bits: 1-32
5136  The number of data bits to be used when adding serial data to a
5137  waveform.
5138 
5139  bb_stop: 2-8
5140  The number of (half) stop bits to be used when adding serial data
5141  to a waveform.
5142 
5143  bit: 0-1
5144  A value of 0 or 1.
5145 
5146  bits: 32 bit number
5147  A mask used to select GPIO to be operated on. If bit n is set
5148  then GPIO n is selected. A convenient way of setting bit n is to
5149  bit or in the value (1<<n).
5150 
5151  To select GPIO 1, 7, 23
5152 
5153  bits = (1<<1) | (1<<7) | (1<<23)
5154 
5155  bsc_control:
5156 
5157  . .
5158  22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
5159  a a a a a a a - - IT HC TF IR RE TE BK EC ES PL PH I2 SP EN
5160  . .
5161 
5162  aaaaaaa defines the I2C slave address (only relevant in I2C mode)
5163 
5164  Bits 0-13 are copied unchanged to the BSC CR register. See
5165  pages 163-165 of the Broadcom peripherals document.
5166 
5167  byte_val: 0-255
5168  A whole number.
5169 
5170  clkfreq: 4689-250M
5171  The hardware clock frequency.
5172 
5173  connected:
5174  True if a connection was established, False otherwise.
5175 
5176  count:
5177  The number of bytes of data to be transferred.
5178 
5179  CS:
5180  The GPIO used for the slave select signal when bit banging SPI.
5181 
5182  data:
5183  Data to be transmitted, a series of bytes.
5184 
5185  delay: >=1
5186  The length of a pulse in microseconds.
5187 
5188  dutycycle: 0-range_
5189  A number between 0 and range_.
5190 
5191  The dutycycle sets the proportion of time on versus time off during each
5192  PWM cycle.
5193 
5194  Dutycycle @ On time
5195  0 @ Off
5196  range_ * 0.25 @ 25% On
5197  range_ * 0.50 @ 50% On
5198  range_ * 0.75 @ 75% On
5199  range_ @ Fully On
5200 
5201  edge: 0-2
5202 
5203  . .
5204  EITHER_EDGE = 2
5205  FALLING_EDGE = 1
5206  RISING_EDGE = 0
5207  . .
5208 
5209  errnum: <0
5210 
5211  . .
5212  PI_BAD_USER_GPIO = -2
5213  PI_BAD_GPIO = -3
5214  PI_BAD_MODE = -4
5215  PI_BAD_LEVEL = -5
5216  PI_BAD_PUD = -6
5217  PI_BAD_PULSEWIDTH = -7
5218  PI_BAD_DUTYCYCLE = -8
5219  PI_BAD_WDOG_TIMEOUT = -15
5220  PI_BAD_DUTYRANGE = -21
5221  PI_NO_HANDLE = -24
5222  PI_BAD_HANDLE = -25
5223  PI_BAD_WAVE_BAUD = -35
5224  PI_TOO_MANY_PULSES = -36
5225  PI_TOO_MANY_CHARS = -37
5226  PI_NOT_SERIAL_GPIO = -38
5227  PI_NOT_PERMITTED = -41
5228  PI_SOME_PERMITTED = -42
5229  PI_BAD_WVSC_COMMND = -43
5230  PI_BAD_WVSM_COMMND = -44
5231  PI_BAD_WVSP_COMMND = -45
5232  PI_BAD_PULSELEN = -46
5233  PI_BAD_SCRIPT = -47
5234  PI_BAD_SCRIPT_ID = -48
5235  PI_BAD_SER_OFFSET = -49
5236  PI_GPIO_IN_USE = -50
5237  PI_BAD_SERIAL_COUNT = -51
5238  PI_BAD_PARAM_NUM = -52
5239  PI_DUP_TAG = -53
5240  PI_TOO_MANY_TAGS = -54
5241  PI_BAD_SCRIPT_CMD = -55
5242  PI_BAD_VAR_NUM = -56
5243  PI_NO_SCRIPT_ROOM = -57
5244  PI_NO_MEMORY = -58
5245  PI_SOCK_READ_FAILED = -59
5246  PI_SOCK_WRIT_FAILED = -60
5247  PI_TOO_MANY_PARAM = -61
5248  PI_SCRIPT_NOT_READY = -62
5249  PI_BAD_TAG = -63
5250  PI_BAD_MICS_DELAY = -64
5251  PI_BAD_MILS_DELAY = -65
5252  PI_BAD_WAVE_ID = -66
5253  PI_TOO_MANY_CBS = -67
5254  PI_TOO_MANY_OOL = -68
5255  PI_EMPTY_WAVEFORM = -69
5256  PI_NO_WAVEFORM_ID = -70
5257  PI_I2C_OPEN_FAILED = -71
5258  PI_SER_OPEN_FAILED = -72
5259  PI_SPI_OPEN_FAILED = -73
5260  PI_BAD_I2C_BUS = -74
5261  PI_BAD_I2C_ADDR = -75
5262  PI_BAD_SPI_CHANNEL = -76
5263  PI_BAD_FLAGS = -77
5264  PI_BAD_SPI_SPEED = -78
5265  PI_BAD_SER_DEVICE = -79
5266  PI_BAD_SER_SPEED = -80
5267  PI_BAD_PARAM = -81
5268  PI_I2C_WRITE_FAILED = -82
5269  PI_I2C_READ_FAILED = -83
5270  PI_BAD_SPI_COUNT = -84
5271  PI_SER_WRITE_FAILED = -85
5272  PI_SER_READ_FAILED = -86
5273  PI_SER_READ_NO_DATA = -87
5274  PI_UNKNOWN_COMMAND = -88
5275  PI_SPI_XFER_FAILED = -89
5276  PI_NO_AUX_SPI = -91
5277  PI_NOT_PWM_GPIO = -92
5278  PI_NOT_SERVO_GPIO = -93
5279  PI_NOT_HCLK_GPIO = -94
5280  PI_NOT_HPWM_GPIO = -95
5281  PI_BAD_HPWM_FREQ = -96
5282  PI_BAD_HPWM_DUTY = -97
5283  PI_BAD_HCLK_FREQ = -98
5284  PI_BAD_HCLK_PASS = -99
5285  PI_HPWM_ILLEGAL = -100
5286  PI_BAD_DATABITS = -101
5287  PI_BAD_STOPBITS = -102
5288  PI_MSG_TOOBIG = -103
5289  PI_BAD_MALLOC_MODE = -104
5290  PI_BAD_SMBUS_CMD = -107
5291  PI_NOT_I2C_GPIO = -108
5292  PI_BAD_I2C_WLEN = -109
5293  PI_BAD_I2C_RLEN = -110
5294  PI_BAD_I2C_CMD = -111
5295  PI_BAD_I2C_BAUD = -112
5296  PI_CHAIN_LOOP_CNT = -113
5297  PI_BAD_CHAIN_LOOP = -114
5298  PI_CHAIN_COUNTER = -115
5299  PI_BAD_CHAIN_CMD = -116
5300  PI_BAD_CHAIN_DELAY = -117
5301  PI_CHAIN_NESTING = -118
5302  PI_CHAIN_TOO_BIG = -119
5303  PI_DEPRECATED = -120
5304  PI_BAD_SER_INVERT = -121
5305  PI_BAD_FOREVER = -124
5306  PI_BAD_FILTER = -125
5307  PI_BAD_PAD = -126
5308  PI_BAD_STRENGTH = -127
5309  PI_FIL_OPEN_FAILED = -128
5310  PI_BAD_FILE_MODE = -129
5311  PI_BAD_FILE_FLAG = -130
5312  PI_BAD_FILE_READ = -131
5313  PI_BAD_FILE_WRITE = -132
5314  PI_FILE_NOT_ROPEN = -133
5315  PI_FILE_NOT_WOPEN = -134
5316  PI_BAD_FILE_SEEK = -135
5317  PI_NO_FILE_MATCH = -136
5318  PI_NO_FILE_ACCESS = -137
5319  PI_FILE_IS_A_DIR = -138
5320  PI_BAD_SHELL_STATUS = -139
5321  PI_BAD_SCRIPT_NAME = -140
5322  PI_BAD_SPI_BAUD = -141
5323  PI_NOT_SPI_GPIO = -142
5324  PI_BAD_EVENT_ID = -143
5325  PI_CMD_INTERRUPTED = -144
5326  . .
5327 
5328  event:0-31
5329  An event is a signal used to inform one or more consumers
5330  to start an action.
5331 
5332  file_mode:
5333  The mode may have the following values
5334 
5335  . .
5336  FILE_READ 1
5337  FILE_WRITE 2
5338  FILE_RW 3
5339  . .
5340 
5341  The following values can be or'd into the file open mode
5342 
5343  . .
5344  FILE_APPEND 4
5345  FILE_CREATE 8
5346  FILE_TRUNC 16
5347  . .
5348 
5349  file_name:
5350  A full file path. To be accessible the path must match
5351  an entry in /opt/pigpio/access.
5352 
5353  fpattern:
5354  A file path which may contain wildcards. To be accessible the path
5355  must match an entry in /opt/pigpio/access.
5356 
5357  frequency: 0-40000
5358  Defines the frequency to be used for PWM on a GPIO.
5359  The closest permitted frequency will be used.
5360 
5361  func:
5362  A user supplied callback function.
5363 
5364  gpio: 0-53
5365  A Broadcom numbered GPIO. All the user GPIO are in the range 0-31.
5366 
5367  There are 54 General Purpose Input Outputs (GPIO) named GPIO0
5368  through GPIO53.
5369 
5370  They are split into two banks. Bank 1 consists of GPIO0
5371  through GPIO31. Bank 2 consists of GPIO32 through GPIO53.
5372 
5373  All the GPIO which are safe for the user to read and write are in
5374  bank 1. Not all GPIO in bank 1 are safe though. Type 1 boards
5375  have 17 safe GPIO. Type 2 boards have 21. Type 3 boards have 26.
5376 
5377  See [*get_hardware_revision*].
5378 
5379  The user GPIO are marked with an X in the following table
5380 
5381  . .
5382  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
5383  Type 1 X X - - X - - X X X X X - - X X
5384  Type 2 - - X X X - - X X X X X - - X X
5385  Type 3 X X X X X X X X X X X X X X
5386 
5387  16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
5388  Type 1 - X X - - X X X X X - - - - - -
5389  Type 2 - X X - - - X X X X - X X X X X
5390  Type 3 X X X X X X X X X X X X - - - -
5391  . .
5392 
5393  gpio_off:
5394  A mask used to select GPIO to be operated on. See [*bits*].
5395 
5396  This mask selects the GPIO to be switched off at the start
5397  of a pulse.
5398 
5399  gpio_on:
5400  A mask used to select GPIO to be operated on. See [*bits*].
5401 
5402  This mask selects the GPIO to be switched on at the start
5403  of a pulse.
5404 
5405  handle: >=0
5406  A number referencing an object opened by one of the following
5407 
5408  [*file_open*]
5409  [*i2c_open*]
5410  [*notify_open*]
5411  [*serial_open*]
5412  [*spi_open*]
5413 
5414  host:
5415  The name or IP address of the Pi running the pigpio daemon.
5416 
5417  i2c_*:
5418  One of the i2c_ functions.
5419 
5420  i2c_address: 0-0x7F
5421  The address of a device on the I2C bus.
5422 
5423  i2c_bus: >=0
5424  An I2C bus number.
5425 
5426  i2c_flags: 0
5427  No I2C flags are currently defined.
5428 
5429  invert: 0-1
5430  A flag used to set normal or inverted bit bang serial data
5431  level logic.
5432 
5433  level: 0-1 (2)
5434 
5435  . .
5436  CLEAR = 0
5437  HIGH = 1
5438  LOW = 0
5439  OFF = 0
5440  ON = 1
5441  SET = 1
5442  TIMEOUT = 2 # only returned for a watchdog timeout
5443  . .
5444 
5445  MISO:
5446  The GPIO used for the MISO signal when bit banging SPI.
5447 
5448  mode:
5449 
5450  1.The operational mode of a GPIO, normally INPUT or OUTPUT.
5451 
5452  . .
5453  ALT0 = 4
5454  ALT1 = 5
5455  ALT2 = 6
5456  ALT3 = 7
5457  ALT4 = 3
5458  ALT5 = 2
5459  INPUT = 0
5460  OUTPUT = 1
5461  . .
5462 
5463  2. The mode of waveform transmission.
5464 
5465  . .
5466  WAVE_MODE_ONE_SHOT = 0
5467  WAVE_MODE_REPEAT = 1
5468  WAVE_MODE_ONE_SHOT_SYNC = 2
5469  WAVE_MODE_REPEAT_SYNC = 3
5470  . .
5471 
5472  MOSI:
5473  The GPIO used for the MOSI signal when bit banging SPI.
5474 
5475  offset: >=0
5476  The offset wave data starts from the beginning of the waveform
5477  being currently defined.
5478 
5479  pad: 0-2
5480  A set of GPIO which share common drivers.
5481 
5482  Pad @ GPIO
5483  0 @ 0-27
5484  1 @ 28-45
5485  2 @ 46-53
5486 
5487  pad_strength: 1-16
5488  The mA which may be drawn from each GPIO whilst still guaranteeing the
5489  high and low levels.
5490 
5491  params: 32 bit number
5492  When scripts are started they can receive up to 10 parameters
5493  to define their operation.
5494 
5495  port:
5496  The port used by the pigpio daemon, defaults to 8888.
5497 
5498  pstring:
5499  The string to be passed to a [*shell*] script to be executed.
5500 
5501  pud: 0-2
5502  . .
5503  PUD_DOWN = 1
5504  PUD_OFF = 0
5505  PUD_UP = 2
5506  . .
5507 
5508  pulse_len: 1-100
5509  The length of the trigger pulse in microseconds.
5510 
5511  pulses:
5512  A list of class pulse objects defining the characteristics of a
5513  waveform.
5514 
5515  pulsewidth:
5516  The servo pulsewidth in microseconds. 0 switches pulses off.
5517 
5518  PWMduty: 0-1000000 (1M)
5519  The hardware PWM dutycycle.
5520 
5521  PWMfreq: 1-125000000 (125M)
5522  The hardware PWM frequency.
5523 
5524  range_: 25-40000
5525  Defines the limits for the [*dutycycle*] parameter.
5526 
5527  range_ defaults to 255.
5528 
5529  reg: 0-255
5530  An I2C device register. The usable registers depend on the
5531  actual device.
5532 
5533  retMax: >=0
5534  The maximum number of bytes a user customised function
5535  should return, default 8192.
5536 
5537  SCL:
5538  The user GPIO to use for the clock when bit banging I2C.
5539 
5540  SCLK::
5541  The GPIO used for the SCLK signal when bit banging SPI.
5542 
5543  script:
5544  The text of a script to store on the pigpio daemon.
5545 
5546  script_id: >=0
5547  A number referencing a script created by [*store_script*].
5548 
5549  SDA:
5550  The user GPIO to use for data when bit banging I2C.
5551 
5552  seek_from: 0-2
5553  Direction to seek for [*file_seek*].
5554 
5555  . .
5556  FROM_START=0
5557  FROM_CURRENT=1
5558  FROM_END=2
5559  . .
5560 
5561  seek_offset:
5562  The number of bytes to move forward (positive) or backwards
5563  (negative) from the seek position (start, current, or end of file).
5564 
5565  ser_flags: 32 bit
5566  No serial flags are currently defined.
5567 
5568  serial_*:
5569  One of the serial_ functions.
5570 
5571  shellscr:
5572  The name of a shell script. The script must exist
5573  in /opt/pigpio/cgi and must be executable.
5574 
5575  show_errors:
5576  Controls the display of pigpio daemon connection failures.
5577  The default of True prints the probable failure reasons to
5578  standard output.
5579 
5580  spi_*:
5581  One of the spi_ functions.
5582 
5583  spi_channel: 0-2
5584  A SPI channel.
5585 
5586  spi_flags: 32 bit
5587  See [*spi_open*].
5588 
5589  steady: 0-300000
5590 
5591  The number of microseconds level changes must be stable for
5592  before reporting the level changed ([*set_glitch_filter*])
5593  or triggering the active part of a noise filter
5594  ([*set_noise_filter*]).
5595 
5596  t1:
5597  A tick (earlier).
5598 
5599  t2:
5600  A tick (later).
5601 
5602  tty:
5603  A Pi serial tty device, e.g. /dev/ttyAMA0, /dev/ttyUSB0
5604 
5605  uint32:
5606  An unsigned 32 bit number.
5607 
5608  user_gpio: 0-31
5609  A Broadcom numbered GPIO.
5610 
5611  All the user GPIO are in the range 0-31.
5612 
5613  Not all the GPIO within this range are usable, some are reserved
5614  for system use.
5615 
5616  See [*gpio*].
5617 
5618  wait_timeout: 0.0 -
5619  The number of seconds to wait in [*wait_for_edge*] before timing out.
5620 
5621  wave_add_*:
5622  One of the following
5623 
5624  [*wave_add_new*]
5625  [*wave_add_generic*]
5626  [*wave_add_serial*]
5627 
5628  wave_id: >=0
5629  A number referencing a wave created by [*wave_create*].
5630 
5631  wave_send_*:
5632  One of the following
5633 
5634  [*wave_send_once*]
5635  [*wave_send_repeat*]
5636 
5637  wdog_timeout: 0-60000
5638  Defines a GPIO watchdog timeout in milliseconds. If no level
5639  change is detected on the GPIO for timeout millisecond a watchdog
5640  timeout report is issued (with level TIMEOUT).
5641 
5642  word_val: 0-65535
5643  A whole number.
5644  """
5645  pass
5646 
def serial_open(self, tty, baud, ser_flags=0)
Definition: pigpio.py:3941
def bb_serial_invert(self, user_gpio, invert)
Definition: pigpio.py:4410
def serial_read(self, handle, count=1000)
Definition: pigpio.py:4018
def __init__(self, notify, user_gpio, edge=RISING_EDGE, func=None)
Definition: pigpio.py:1196
def wave_delete(self, wave_id)
Definition: pigpio.py:2272
def __init__(self, notify, event, timeout)
Definition: pigpio.py:1303
def __init__(self, host=os.getenv("PIGPIO_ADDR", 'localhost'), port=os.getenv("PIGPIO_PORT", 8888), show_errors=True)
Definition: pigpio.py:5007
def bsc_i2c(self, i2c_address, data=[])
Definition: pigpio.py:3617
def wave_clear(self)
Definition: pigpio.py:2065
def i2c_write_device(self, handle, data)
Definition: pigpio.py:3076
def wave_get_max_micros(self)
Definition: pigpio.py:2541
def wait_for_event(self, event, wait_timeout=60.0)
Definition: pigpio.py:4981
def wave_tx_repeat(self)
Definition: pigpio.py:2305
def custom_2(self, arg1=0, argx=[], retMax=8192)
Definition: pigpio.py:4458
def _pigpio_command_ext_nolock(sl, cmd, p1, p2, p3, extents)
Definition: pigpio.py:1030
def get_pad_strength(self, pad)
Definition: pigpio.py:4498
def _tally(self, event, tick)
Definition: pigpio.py:1258
def spi_open(self, spi_channel, baud, spi_flags=0)
Definition: pigpio.py:3737
def __init__(self, notify, gpio, edge, timeout)
Definition: pigpio.py:1285
def bb_i2c_close(self, SDA)
Definition: pigpio.py:3385
def _pigpio_command_nolock(sl, cmd, p1, p2)
Definition: pigpio.py:993
def cancel(self)
Definition: pigpio.py:1208
def func(self, event, tick)
Definition: pigpio.py:1314
def wave_send_once(self, wave_id)
Definition: pigpio.py:2313
def notify_begin(self, handle, bits)
Definition: pigpio.py:1723
def remove_event(self, callb)
Definition: pigpio.py:1135
def notify_pause(self, handle)
Definition: pigpio.py:1744
def bb_spi_open(self, CS, MISO, MOSI, SCLK, baud=100000, spi_flags=0)
Definition: pigpio.py:3178
def i2c_process_call(self, handle, reg, word_val)
Definition: pigpio.py:2816
def xref()
Definition: pigpio.py:5111
def wave_chain(self, data)
Definition: pigpio.py:2434
def wait_for_edge(self, user_gpio, edge=RISING_EDGE, wait_timeout=60.0)
Definition: pigpio.py:4946
def i2c_write_byte(self, handle, byte_val)
Definition: pigpio.py:2671
def tally(self)
Definition: pigpio.py:1219
def hardware_PWM(self, gpio, PWMfreq, PWMduty)
Definition: pigpio.py:1945
def func(self, gpio, level, tick)
Definition: pigpio.py:1296
def bb_serial_read_close(self, user_gpio)
Definition: pigpio.py:4398
def append_event(self, callb)
Definition: pigpio.py:1127
def i2c_write_quick(self, handle, bit)
Definition: pigpio.py:2652
def get_mode(self, gpio)
Definition: pigpio.py:1342
def set_noise_filter(self, user_gpio, steady, active)
Definition: pigpio.py:4141
def serial_close(self, handle)
Definition: pigpio.py:3976
def __init__(self, notify, event, func=None)
Definition: pigpio.py:1239
def hardware_clock(self, gpio, clkfreq)
Definition: pigpio.py:1902
def write(self, gpio, level)
Definition: pigpio.py:1403
def get_current_tick(self)
Definition: pigpio.py:2011
def set_PWM_frequency(self, user_gpio, frequency)
Definition: pigpio.py:1530
def shell(self, shellscr, pstring="")
Definition: pigpio.py:4802
def i2c_zip(self, handle, data)
Definition: pigpio.py:3109
def set_pad_strength(self, pad, pad_strength)
Definition: pigpio.py:4517
def spi_close(self, handle)
Definition: pigpio.py:3837
def _str(x)
Definition: pigpio.py:940
def read(self, gpio)
Definition: pigpio.py:1383
def spi_write(self, handle, data)
Definition: pigpio.py:3878
def wave_get_max_pulses(self)
Definition: pigpio.py:2561
def wave_tx_stop(self)
Definition: pigpio.py:2417
def remove(self, callb)
Definition: pigpio.py:1115
def __init__(self, control, host, port)
Definition: pigpio.py:1086
def i2c_write_word_data(self, handle, reg, word_val)
Definition: pigpio.py:2739
def i2c_read_i2c_block_data(self, handle, reg, count)
Definition: pigpio.py:3003
def file_list(self, fpattern)
Definition: pigpio.py:4749
def set_bank_2(self, bits)
Definition: pigpio.py:1886
def _rxbuf(self, count)
Definition: pigpio.py:1320
def i2c_write_byte_data(self, handle, reg, byte_val)
Definition: pigpio.py:2708
def wave_send_repeat(self, wave_id)
Definition: pigpio.py:2331
def i2c_block_process_call(self, handle, reg, data)
Definition: pigpio.py:2919
def wave_send_using_mode(self, wave_id, mode)
Definition: pigpio.py:2350
def __init__(self, value)
Definition: pigpio.py:872
def _pigpio_command(sl, cmd, p1, p2)
Definition: pigpio.py:978
def serial_write_byte(self, handle, byte_val)
Definition: pigpio.py:4002
def set_servo_pulsewidth(self, user_gpio, pulsewidth)
Definition: pigpio.py:1617
def append(self, callb)
Definition: pigpio.py:1109
def wave_add_new(self)
Definition: pigpio.py:2076
def notify_open(self)
Definition: pigpio.py:1665
def _u2i(uint32)
Definition: pigpio.py:966
def wave_get_max_cbs(self)
Definition: pigpio.py:2582
def bb_serial_read(self, user_gpio)
Definition: pigpio.py:4365
def _tally(self, user_gpio, level, tick)
Definition: pigpio.py:1212
def run_script(self, script_id, params=None)
Definition: pigpio.py:4208
def file_write(self, handle, data)
Definition: pigpio.py:4697
def u2i(uint32)
Definition: pigpio.py:946
def file_read(self, handle, count)
Definition: pigpio.py:4670
def i2c_read_byte(self, handle)
Definition: pigpio.py:2691
def wave_create(self)
Definition: pigpio.py:2225
def wave_tx_at(self)
Definition: pigpio.py:2384
def event_trigger(self, event)
Definition: pigpio.py:3481
def wave_add_serial(self, user_gpio, baud, data, offset=0, bb_bits=8, bb_stop=2)
Definition: pigpio.py:2169
def __init__(self)
Definition: pigpio.py:866
def clear_bank_1(self, bits)
Definition: pigpio.py:1838
def file_open(self, file_name, file_mode)
Definition: pigpio.py:4539
def _b(x)
Definition: pigpio.py:933
def get_PWM_dutycycle(self, user_gpio)
Definition: pigpio.py:1447
def notify_close(self, handle)
Definition: pigpio.py:1766
def gpio_trigger(self, user_gpio, pulse_len=10, level=1)
Definition: pigpio.py:4088
def set_mode(self, gpio, mode)
Definition: pigpio.py:1327
def reset_tally(self)
Definition: pigpio.py:1229
def get_servo_pulsewidth(self, user_gpio)
Definition: pigpio.py:1645
def tickDiff(t1, t2)
Definition: pigpio.py:911
def reset_tally(self)
Definition: pigpio.py:1275
def set_glitch_filter(self, user_gpio, steady)
Definition: pigpio.py:4112
def __init__(self, gpio, edge, func)
Definition: pigpio.py:1071
def wave_add_generic(self, pulses)
Definition: pigpio.py:2090
def read_bank_1(self)
Definition: pigpio.py:1810
def i2c_read_word_data(self, handle, reg)
Definition: pigpio.py:2793
def stop(self)
Definition: pigpio.py:5093
def __init__(self, gpio_on, gpio_off, delay)
Definition: pigpio.py:882
def get_PWM_range(self, user_gpio)
Definition: pigpio.py:1492
def get_PWM_real_range(self, user_gpio)
Definition: pigpio.py:1509
def __init__(self, event, func)
Definition: pigpio.py:1057
def set_pull_up_down(self, gpio, pud)
Definition: pigpio.py:1368
def bb_i2c_open(self, SDA, SCL, baud=100000)
Definition: pigpio.py:3346
def spi_xfer(self, handle, data)
Definition: pigpio.py:3903
def read_bank_2(self)
Definition: pigpio.py:1824
def wave_get_cbs(self)
Definition: pigpio.py:2571
def cancel(self)
Definition: pigpio.py:1251
def i2c_write_i2c_block_data(self, handle, reg, data)
Definition: pigpio.py:2969
def get_PWM_frequency(self, user_gpio)
Definition: pigpio.py:1588
def set_PWM_dutycycle(self, user_gpio, dutycycle)
Definition: pigpio.py:1427
def i2c_write_block_data(self, handle, reg, data)
Definition: pigpio.py:2845
def i2c_read_byte_data(self, handle, reg)
Definition: pigpio.py:2770
def i2c_read_block_data(self, handle, reg)
Definition: pigpio.py:2881
def callback(self, user_gpio, edge=RISING_EDGE, func=None)
Definition: pigpio.py:4852
def bb_spi_xfer(self, CS, data)
Definition: pigpio.py:3274
def clear_bank_2(self, bits)
Definition: pigpio.py:1854
def file_close(self, handle)
Definition: pigpio.py:4658
def update_script(self, script_id, params=None)
Definition: pigpio.py:4240
def bsc_xfer(self, bsc_control, data)
Definition: pigpio.py:3509
def wave_get_micros(self)
Definition: pigpio.py:2531
def script_status(self, script_id)
Definition: pigpio.py:4272
def i2c_read_device(self, handle, count)
Definition: pigpio.py:3046
def serial_write(self, handle, data)
Definition: pigpio.py:4046
def error_text(errnum)
Definition: pigpio.py:895
def wave_tx_busy(self)
Definition: pigpio.py:2401
def delete_script(self, script_id)
Definition: pigpio.py:4323
def set_watchdog(self, user_gpio, wdog_timeout)
Definition: pigpio.py:1783
def store_script(self, script)
Definition: pigpio.py:4181
def spi_read(self, handle, count)
Definition: pigpio.py:3849
def bb_spi_close(self, CS)
Definition: pigpio.py:3258
def set_PWM_range(self, user_gpio, range_)
Definition: pigpio.py:1477
def wave_get_pulses(self)
Definition: pigpio.py:2551
def tally(self)
Definition: pigpio.py:1265
def serial_read_byte(self, handle)
Definition: pigpio.py:3988
def i2c_close(self, handle)
Definition: pigpio.py:2640
def __str__(self)
Definition: pigpio.py:874
def stop_script(self, script_id)
Definition: pigpio.py:4311
def _pigpio_command_ext(sl, cmd, p1, p2, p3, extents)
Definition: pigpio.py:1007
def serial_data_available(self, handle)
Definition: pigpio.py:4072
def event_callback(self, event, func=None)
Definition: pigpio.py:4907
def custom_1(self, arg1=0, arg2=0, argx=[])
Definition: pigpio.py:4424
def i2c_open(self, i2c_bus, i2c_address, i2c_flags=0)
Definition: pigpio.py:2593
def get_pigpio_version(self)
Definition: pigpio.py:2055
def file_seek(self, handle, seek_offset, seek_from)
Definition: pigpio.py:4723
def bb_serial_read_open(self, user_gpio, baud, bb_bits=8)
Definition: pigpio.py:4335
def wave_tx_start(self)
Definition: pigpio.py:2297
def bb_i2c_zip(self, SDA, data)
Definition: pigpio.py:3401
def get_hardware_revision(self)
Definition: pigpio.py:2027
def set_bank_1(self, bits)
Definition: pigpio.py:1870


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57