2 pigpio is a Python module for the Raspberry which talks to 3 the pigpio daemon to allow control of the general purpose 6 [http://abyz.me.uk/rpi/pigpio/python.html] 10 o the pigpio Python module can run on Windows, Macs, or Linux 12 o controls one or more Pi's 14 o hardware timed PWM on any of GPIO 0-31 16 o hardware timed servo pulses on any of GPIO 0-31 18 o callbacks when any of GPIO 0-31 change state 20 o creating and transmitting precisely timed waveforms 22 o reading/writing GPIO and setting their modes 24 o wrappers for I2C, SPI, and serial links 26 o creating and running scripts on the pigpio daemon 30 ALL GPIO are identified by their Broadcom number. 34 Transmitted waveforms are accurate to a microsecond. 36 Callback level changes are time-stamped and will be 37 accurate to within a few microseconds. 41 A number of settings are determined when the pigpio daemon is started. 43 o the sample rate (1, 2, 4, 5, 8, or 10 us, default 5 us). 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. 48 o the available PWM frequencies (see [*set_PWM_frequency*]). 52 By default a fatal exception is raised if you pass an invalid 53 argument to a pigpio function. 55 If you wish to handle the returned status yourself you should set 56 pigpio.exceptions to False. 58 You may prefer to check the returned status in only a few parts 59 of your code. In that case do the following: 62 pigpio.exceptions = False 64 # Code where you want to test the error status. 66 pigpio.exceptions = True 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. 74 The normal way to start pigpio is as a daemon (during system 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. 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 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 93 The later example code snippets assume that pi is an instance of 100 pigpio.pi Initialise Pi connection 101 stop Stop a Pi connection 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 112 set_PWM_dutycycle Start/stop PWM pulses on a GPIO 113 get_PWM_dutycycle Get PWM dutycycle set on a GPIO 115 set_servo_pulsewidth Start/Stop servo pulses on a GPIO 116 get_servo_pulsewidth Get servo pulsewidth set on a GPIO 118 callback Create GPIO level change callback 119 wait_for_edge Wait for GPIO level change 123 gpio_trigger Send a trigger pulse to a GPIO 125 set_watchdog Set a watchdog on a GPIO 127 set_PWM_range Configure PWM range of a GPIO 128 get_PWM_range Get configured PWM range of a GPIO 130 set_PWM_frequency Set PWM frequency of a GPIO 131 get_PWM_frequency Get PWM frequency of a GPIO 133 read_bank_1 Read all bank 1 GPIO 134 read_bank_2 Read all bank 2 GPIO 136 clear_bank_1 Clear selected GPIO in bank 1 137 clear_bank_2 Clear selected GPIO in bank 2 139 set_bank_1 Set selected GPIO in bank 1 140 set_bank_2 Set selected GPIO in bank 2 144 get_PWM_real_range Get underlying PWM range for a GPIO 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 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) 156 hardware_clock Start hardware clock on supported GPIO 157 hardware_PWM Start hardware PWM on supported GPIO 159 set_glitch_filter Set a glitch filter on a GPIO 160 set_noise_filter Set a noise filter on a GPIO 162 get_pad_strength Gets a pads drive strength 163 set_pad_strength Sets a pads drive strength 165 shell Executes a shell command 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 178 wave_clear Deletes all waveforms 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 184 wave_create Creates a waveform from added data 185 wave_delete Deletes a waveform 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 191 wave_chain Transmits a chain of waveforms 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 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 206 i2c_open Opens an I2C device 207 i2c_close Closes an I2C device 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 221 i2c_read_i2c_block_data SMBus read I2C block data 222 i2c_write_i2c_block_data SMBus write I2C block data 224 i2c_read_device Reads the raw I2C device 225 i2c_write_device Writes the raw I2C device 227 i2c_zip Performs multiple I2C transactions 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 235 spi_open Opens a SPI device 236 spi_close Closes a SPI device 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 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 248 bsc_xfer I2C/SPI as slave transfer 249 bsc_i2c I2C as slave transfer 253 serial_open Opens a serial device 254 serial_close Closes a serial device 256 serial_read Reads bytes from a serial device 257 serial_read_byte Reads a byte from a serial device 259 serial_write Writes bytes to a serial device 260 serial_write_byte Writes a byte to a serial device 262 serial_data_available Returns number of bytes ready to be read 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 275 event_callback Sets a callback for an event 276 event_trigger Triggers an event 277 wait_for_event Wait for an event 281 custom_1 User custom function 1 282 custom_2 User custom function 2 286 get_current_tick Get current tick (microseconds) 288 get_hardware_revision Get hardware revision 289 get_pigpio_version Get the pigpio version 291 pigpio.error_text Gets error text from error number 292 pigpio.tickDiff Returns difference between two ticks 352 NTFY_FLAGS_EVENT = (1 << 7)
353 NTFY_FLAGS_ALIVE = (1 << 6)
354 NTFY_FLAGS_WDOG = (1 << 5)
359 WAVE_MODE_ONE_SHOT =0
361 WAVE_MODE_ONE_SHOT_SYNC=2
362 WAVE_MODE_REPEAT_SYNC =3
364 WAVE_NOT_FOUND = 9998
387 SPI_CS_HIGH_ACTIVE = 1 << 2
389 SPI_TX_LSBFIRST = 1 << 14
390 SPI_RX_LSBFIRST = 1 << 15
553 PI_BAD_PULSEWIDTH =-7
557 _PI_BAD_TIMETYPE =-11
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
569 _PI_BAD_PATHNAME =-23
572 _PI_BAD_IF_FLAGS =-26
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
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
595 PI_BAD_SCRIPT_ID =-48
596 PI_BAD_SER_OFFSET =-49
598 PI_BAD_SERIAL_COUNT =-51
599 PI_BAD_PARAM_NUM =-52
601 PI_TOO_MANY_TAGS =-54
602 PI_BAD_SCRIPT_CMD =-55
604 PI_NO_SCRIPT_ROOM =-57
606 PI_SOCK_READ_FAILED =-59
607 PI_SOCK_WRIT_FAILED =-60
608 PI_TOO_MANY_PARAM =-61
609 PI_SCRIPT_NOT_READY =-62
611 PI_BAD_MICS_DELAY =-64
612 PI_BAD_MILS_DELAY =-65
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
623 PI_BAD_SPI_CHANNEL =-76
625 PI_BAD_SPI_SPEED =-78
626 PI_BAD_SER_DEVICE =-79
627 PI_BAD_SER_SPEED =-80
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
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
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
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
668 PI_BAD_SER_INVERT =-121
670 _PI_BAD_ISR_INIT =-123
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
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"],
840 _except_a =
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n{}" 842 _except_z =
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" 845 Did you start the pigpio daemon? E.g. sudo pigpiod 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 851 Did you specify the correct Pi host/port in the 852 pigpio.pi() function? E.g. pigpio.pi('soft', 8888)""" 855 Do you have permission to access the pigpio daemon? 856 Perhaps it was started with sudo pigpiod -nlocalhost""" 859 Can't create callback thread. 860 Perhaps too many simultaneous pigpio connections.""" 864 A class to store socket and lock. 868 self.
l = threading.Lock()
871 """pigpio module exception""" 875 return repr(self.
value)
879 A class to store pulse information. 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. 897 Returns a text description of a pigpio error. 899 errnum:= <0, the error number 902 print(pigpio.error_text(-5)) 909 return "unknown error ({})".format(errnum)
913 Returns the microsecond difference between two ticks. 915 t1:= the earlier tick 919 print(pigpio.tickDiff(4294967272, 12)) 932 if sys.hexversion < 0x03000000:
937 return x.encode(
'latin-1')
939 if sys.hexversion < 0x02070800:
948 Converts a 32 bit unsigned number to signed. 950 uint32:= an unsigned 32 bit number 953 print(u2i(4294967272)) 960 if uint32 & (1 << 31):
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. 980 Runs a pigpio socket command. 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). 987 res = PI_CMD_INTERRUPTED
989 sl.s.send(struct.pack(
'IIII', cmd, p1, p2, 0))
990 dummy, res = struct.unpack(
'12sI', sl.s.recv(_SOCK_CMD_LEN))
995 Runs a pigpio socket command. 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). 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))
1009 Runs an extended pigpio socket command. 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 1018 ext = bytearray(struct.pack(
'IIII', cmd, p1, p2, p3))
1020 if type(x) == type(
""):
1024 res = PI_CMD_INTERRUPTED
1027 dummy, res = struct.unpack(
'12sI', sl.s.recv(_SOCK_CMD_LEN))
1032 Runs an extended pigpio socket command. 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 1041 res = PI_CMD_INTERRUPTED
1042 ext = bytearray(struct.pack(
'IIII', cmd, p1, p2, p3))
1044 if type(x) == type(
""):
1049 dummy, res = struct.unpack(
'12sI', sl.s.recv(_SOCK_CMD_LEN))
1054 An ADT class to hold event callback information. 1059 Initialises an event callback ADT. 1061 event:= the event id. 1062 func:= a user function taking one argument, the event id. 1069 """An ADT class to hold callback information.""" 1073 Initialises a callback ADT. 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). 1085 """A class to encapsulate pigpio notification callbacks.""" 1087 """Initialises notifications.""" 1088 threading.Thread.__init__(self)
1097 self.sl.s = socket.create_connection((host, port),
None)
1104 """Stops notifications.""" 1107 self.sl.s.send(struct.pack(
'IIII', _PI_CMD_NC, self.
handle, 0, 0))
1110 """Adds a callback to the notification thread.""" 1111 self.callbacks.append(callb)
1116 """Removes a callback from the notification thread.""" 1118 self.callbacks.remove(callb)
1122 if newMonitor != self.
monitor:
1129 Adds an event callback to the notification thread. 1131 self.events.append(callb)
1137 Removes an event callback from the notification thread. 1140 self.events.remove(callb)
1143 new_event_bits |= c.bit
1150 """Runs the notification thread.""" 1160 buf += self.sl.s.recv(RECV_SIZ)
1163 while self.
go and (len(buf) - offset) >= MSG_SIZ:
1164 msgbuf = buf[offset:offset + MSG_SIZ]
1166 seq, flags, tick, level = (struct.unpack(
'HHII', msgbuf))
1169 changed = level ^ lastLevel
1172 if cb.bit & changed:
1176 if (cb.edge ^ newLevel):
1177 cb.func(cb.gpio, newLevel, tick)
1179 if flags & NTFY_FLAGS_WDOG:
1180 gpio = flags & NTFY_FLAGS_GPIO
1183 cb.func(gpio, TIMEOUT, tick)
1184 elif flags & NTFY_FLAGS_EVENT:
1185 event = flags & NTFY_FLAGS_GPIO
1187 if cb.event == event:
1188 cb.func(event, tick)
1194 """A class to provide GPIO level change callbacks.""" 1196 def __init__(self, notify, user_gpio, edge=RISING_EDGE, func=None):
1198 Initialise a callback and adds it to the notification thread. 1206 self._notify.append(self.
callb)
1209 """Cancels a callback by removing it from the notification thread.""" 1210 self._notify.remove(self.
callb)
1213 """Increment the callback called count.""" 1221 Provides a count of how many times the default tally 1222 callback has triggered. 1224 The count will be zero if the user has supplied their own 1231 Resets the tally count to zero. 1237 """A class to provide event callbacks.""" 1241 Initialise an event and adds it to the notification thread. 1249 self._notify.append_event(self.
callb)
1253 Cancels a event callback by removing it from the 1254 notification thread. 1256 self._notify.remove_event(self.
callb)
1259 """Increment the event callback called count.""" 1267 Provides a count of how many times the default tally 1268 callback has triggered. 1270 The count will be zero if the user has supplied their own 1277 Resets the tally count to zero. 1283 """Encapsulates waiting for GPIO edges.""" 1286 """Initialises a wait_for_edge.""" 1290 self._notify.append(self.
callb)
1292 while (self.
trigger ==
False)
and ((time.time()-self.
start) < timeout):
1294 self._notify.remove(self.
callb)
1297 """Sets wait_for_edge triggered.""" 1301 """Encapsulates waiting for an event.""" 1304 """Initialises wait_for_event.""" 1308 self._notify.append_event(self.
callb)
1310 while (self.
trigger ==
False)
and ((time.time()-self.
start) < timeout):
1312 self._notify.remove_event(self.
callb)
1315 """Sets wait_for_event triggered.""" 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)))
1332 mode:= INPUT, OUTPUT, ALT0, ALT1, ALT2, ALT3, ALT4, ALT5. 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 1344 Returns the GPIO mode. 1348 Returns a value as follows 1362 print(pi.get_mode(0)) 1370 Sets or clears the internal GPIO pull-up/down resistor. 1373 pud:= PUD_UP, PUD_DOWN, PUD_OFF. 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) 1385 Returns the GPIO level. 1390 pi.set_mode(23, pigpio.INPUT) 1392 pi.set_pull_up_down(23, pigpio.PUD_DOWN) 1396 pi.set_pull_up_down(23, pigpio.PUD_UP) 1405 Sets the GPIO level. 1410 If PWM or servo pulses are active on the GPIO they are 1414 pi.set_mode(17, pigpio.OUTPUT) 1429 Starts (non-zero dutycycle) or stops (0) PWM pulses on the GPIO. 1432 dutycycle:= 0-range (range defaults to 255). 1434 The [*set_PWM_range*] function can change the default range of 255. 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 1445 self.
sl, _PI_CMD_PWM, user_gpio, int(dutycycle)))
1449 Returns the PWM dutycycle being used on the GPIO. 1453 Returns the PWM dutycycle. 1456 For normal PWM the dutycycle will be out of the defined range 1457 for the GPIO (see [*get_PWM_range*]). 1459 If a hardware clock is active on the GPIO the reported 1460 dutycycle will be 500000 (500k) out of 1000000 (1M). 1462 If hardware PWM is active on the GPIO the reported dutycycle 1463 will be out of a 1000000 (1M). 1466 pi.set_PWM_dutycycle(4, 25) 1467 print(pi.get_PWM_dutycycle(4)) 1470 pi.set_PWM_dutycycle(4, 203) 1471 print(pi.get_PWM_dutycycle(4)) 1479 Sets the range of PWM values to be used on the GPIO. 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 1494 Returns the range of PWM values being used on the GPIO. 1498 If a hardware clock or hardware PWM is active on the GPIO 1499 the reported range will be 1000000 (1M). 1502 pi.set_PWM_range(9, 500) 1503 print(pi.get_PWM_range(9)) 1511 Returns the real (underlying) range of PWM values being 1516 If a hardware clock is active on the GPIO the reported 1517 real range will be 1000000 (1M). 1519 If hardware PWM is active on the GPIO the reported real range 1520 will be approximately 250M divided by the set PWM frequency. 1523 pi.set_PWM_frequency(4, 800) 1524 print(pi.get_PWM_real_range(4)) 1532 Sets the frequency (in Hz) of the PWM to be used on the GPIO. 1537 Returns the numerically closest frequency if OK, otherwise 1538 PI_BAD_USER_GPIO or PI_NOT_PERMITTED. 1540 If PWM is currently active on the GPIO it will be switched 1541 off and then back on at the new frequency. 1543 Each GPIO can be independently set to one of 18 different 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. 1550 The frequencies for each sample rate are: 1555 1: 40000 20000 10000 8000 5000 4000 2500 2000 1600 1556 1250 1000 800 500 400 250 200 100 50 1558 2: 20000 10000 5000 4000 2500 2000 1250 1000 800 1559 625 500 400 250 200 125 100 50 25 1561 4: 10000 5000 2500 2000 1250 1000 625 500 400 1562 313 250 200 125 100 63 50 25 13 1565 (us) 5: 8000 4000 2000 1600 1000 800 500 400 320 1566 250 200 160 100 80 50 40 20 10 1568 8: 5000 2500 1250 1000 625 500 313 250 200 1569 156 125 100 63 50 31 25 13 6 1571 10: 4000 2000 1000 800 500 400 250 200 160 1572 125 100 80 50 40 25 20 10 5 1576 pi.set_PWM_frequency(4,0) 1577 print(pi.get_PWM_frequency(4)) 1580 pi.set_PWM_frequency(4,100000) 1581 print(pi.get_PWM_frequency(4)) 1590 Returns the frequency of PWM being used on the GPIO. 1594 Returns the frequency (in Hz) used for the GPIO. 1596 For normal PWM the frequency will be that defined for the GPIO 1597 by [*set_PWM_frequency*]. 1599 If a hardware clock is active on the GPIO the reported frequency 1600 will be that set by [*hardware_clock*]. 1602 If hardware PWM is active on the GPIO the reported frequency 1603 will be that set by [*hardware_PWM*]. 1606 pi.set_PWM_frequency(4,0) 1607 print(pi.get_PWM_frequency(4)) 1610 pi.set_PWM_frequency(4, 800) 1611 print(pi.get_PWM_frequency(4)) 1619 Starts (500-2500) or stops (0) servo pulses on the GPIO. 1622 pulsewidth:= 0 (off), 1623 500 (most anti-clockwise) - 2500 (most clockwise). 1625 The selected pulsewidth will continue to be transmitted until 1626 changed by a subsequent call to set_servo_pulsewidth. 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. 1632 You can DAMAGE a servo if you command it to move beyond its 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 1643 self.
sl, _PI_CMD_SERVO, user_gpio, int(pulsewidth)))
1647 Returns the servo pulsewidth being used on the GPIO. 1651 Returns the servo pulsewidth. 1654 pi.set_servo_pulsewidth(4, 525) 1655 print(pi.get_servo_pulsewidth(4)) 1658 pi.set_servo_pulsewidth(4, 2130) 1659 print(pi.get_servo_pulsewidth(4)) 1667 Returns a notification handle (>=0). 1669 A notification is a method for being notified of GPIO state 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. 1677 Notifications for handle x will be available at the pipe 1678 named /dev/pigpiox (where x is the handle number). 1680 E.g. if the function returns 15 then the notifications must be 1681 read from /dev/pigpio15. 1683 Notifications have the following structure: 1692 seqno: starts at 0 each time the handle is opened and then 1693 increments by one for each report. 1695 flags: three flags are defined, PI_NTFY_FLAGS_WDOG, 1696 PI_NTFY_FLAGS_ALIVE, and PI_NTFY_FLAGS_EVENT. 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. 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. 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. 1709 tick: the number of microseconds since system boot. It wraps 1712 level: indicates the level of each GPIO. If bit 1<<x is set 1713 then GPIO x is high. 1716 h = pi.notify_open() 1718 pi.notify_begin(h, 1234) 1725 Starts notifications on a handle. 1727 handle:= >=0 (as returned by a prior call to [*notify_open*]) 1728 bits:= a 32 bit mask indicating the GPIO to be notified. 1730 The notification sends state changes for each GPIO whose 1731 corresponding bit in bits is set. 1733 The following code starts notifications for GPIO 1, 4, 1734 6, 7, and 10 (1234 = 0x04D2 = 0b0000010011010010). 1737 h = pi.notify_open() 1739 pi.notify_begin(h, 1234) 1746 Pauses notifications on a handle. 1748 handle:= >=0 (as returned by a prior call to [*notify_open*]) 1750 Notifications for the handle are suspended until 1751 [*notify_begin*] is called again. 1754 h = pi.notify_open() 1756 pi.notify_begin(h, 1234) 1760 pi.notify_begin(h, 1234) 1768 Stops notifications on a handle and releases the handle for reuse. 1770 handle:= >=0 (as returned by a prior call to [*notify_open*]) 1773 h = pi.notify_open() 1775 pi.notify_begin(h, 1234) 1785 Sets a watchdog timeout for a GPIO. 1788 wdog_timeout:= 0-60000. 1790 The watchdog is nominally in milliseconds. 1792 Only one watchdog may be registered per GPIO. 1794 The watchdog may be cancelled by setting timeout to 0. 1796 Once a watchdog has been started callbacks for the GPIO 1797 will be triggered every timeout interval after the last 1800 The callback will receive the special level TIMEOUT. 1803 pi.set_watchdog(23, 1000) # 1000 ms watchdog on GPIO 23 1804 pi.set_watchdog(23, 0) # cancel watchdog on GPIO 23 1808 self.
sl, _PI_CMD_WDOG, user_gpio, int(wdog_timeout)))
1812 Returns the levels of the bank 1 GPIO (GPIO 0-31). 1814 The returned 32 bit integer has a bit set if the corresponding 1815 GPIO is high. GPIO n has bit value (1<<n). 1818 print(bin(pi.read_bank_1())) 1819 0b10010100000011100100001001111 1826 Returns the levels of the bank 2 GPIO (GPIO 32-53). 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)). 1832 print(bin(pi.read_bank_2())) 1833 0b1111110000000000000000 1840 Clears GPIO 0-31 if the corresponding bit in bits is set. 1842 bits:= a 32 bit mask with 1 set if the corresponding GPIO is 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. 1849 pi.clear_bank_1(int("111110010000",2)) 1856 Clears GPIO 32-53 if the corresponding bit (0-21) in bits is set. 1858 bits:= a 32 bit mask with 1 set if the corresponding GPIO is 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. 1865 pi.clear_bank_2(0x1010) 1872 Sets GPIO 0-31 if the corresponding bit in bits is set. 1874 bits:= a 32 bit mask with 1 set if the corresponding GPIO is 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. 1881 pi.set_bank_1(int("111110010000",2)) 1888 Sets GPIO 32-53 if the corresponding bit (0-21) in bits is set. 1890 bits:= a 32 bit mask with 1 set if the corresponding GPIO is 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. 1897 pi.set_bank_2(0x303) 1904 Starts a hardware clock on a GPIO at the specified frequency. 1905 Frequencies above 30MHz are unlikely to work. 1907 gpio:= see description 1908 clkfreq:= 0 (off) or 4689-250000000 (250M) 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. 1914 The same clock is available on multiple GPIO. The latest 1915 frequency setting will be used by all GPIO which share a clock. 1917 The GPIO must be one of the following: 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) 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) 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. 1938 pi.hardware_clock(4, 5000) # 5 KHz clock on GPIO 4 1940 pi.hardware_clock(4, 40000000) # 40 MHz clock on GPIO 4 1947 Starts hardware PWM on a GPIO at the specified frequency 1948 and dutycycle. Frequencies above 30MHz are unlikely to work. 1950 NOTE: Any waveform started by [*wave_send_once*], 1951 [*wave_send_repeat*], or [*wave_chain*] will be cancelled. 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). 1957 gpio:= see descripton 1958 PWMfreq:= 0 (off) or 1-125000000 (125M). 1959 PWMduty:= 0 (off) to 1000000 (1M)(fully on). 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. 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. 1968 The GPIO must be one of the following: 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 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 1983 The actual number of steps beween off and fully on is the 1984 integral part of 250 million divided by PWMfreq. 1986 The actual frequency set is 250 million / steps. 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. 1994 pi.hardware_PWM(18, 800, 250000) # 800Hz 25% dutycycle 1996 pi.hardware_PWM(18, 2000, 750000) # 2000Hz 75% dutycycle 2006 extents = [struct.pack(
"I", PWMduty)]
2008 self.
sl, _PI_CMD_HP, gpio, PWMfreq, 4, extents))
2013 Returns the current system tick. 2015 Tick is the number of microseconds since system boot. As an 2016 unsigned 32 bit quantity tick wraps around approximately 2020 t1 = pi.get_current_tick() 2022 t2 = pi.get_current_tick() 2029 Returns the Pi's hardware revision number. 2031 The hardware revision is the last few characters on the 2032 Revision line of /proc/cpuinfo. 2034 The revision number can be used to determine the assignment 2035 of GPIO to pins (see [*gpio*]). 2037 There are at least three types of board. 2039 Type 1 boards have hardware revision numbers of 2 and 3. 2041 Type 2 boards have hardware revision numbers of 4, 5, 6, and 15. 2043 Type 3 boards have hardware revision numbers of 16 or greater. 2045 If the hardware revision can not be found or is not a valid 2046 hexadecimal number the function returns 0. 2049 print(pi.get_hardware_revision()) 2057 Returns the pigpio software version. 2060 v = pi.get_pigpio_version() 2067 Clears all waveforms and any data added by calls to the 2068 [*wave_add_**] functions. 2078 Starts a new empty waveform. 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. 2092 Adds a list of pulses to the current waveform. 2094 pulses:= list of pulses to add to the waveform. 2096 Returns the new total number of pulses in the current waveform. 2098 The pulses are interleaved in time order within the existing 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. 2104 If the added waveform is intended to start after or within 2105 the existing waveform then the first pulse should consist 2112 pi.set_mode(G1, pigpio.OUTPUT) 2113 pi.set_mode(G2, pigpio.OUTPUT) 2115 flash_500=[] # flash every 500 ms 2116 flash_100=[] # flash every 100 ms 2120 flash_500.append(pigpio.pulse(1<<G1, 1<<G2, 500000)) 2121 flash_500.append(pigpio.pulse(1<<G2, 1<<G1, 500000)) 2123 flash_100.append(pigpio.pulse(1<<G1, 1<<G2, 100000)) 2124 flash_100.append(pigpio.pulse(1<<G2, 1<<G1, 100000)) 2126 pi.wave_clear() # clear any existing waveforms 2128 pi.wave_add_generic(flash_500) # 500 ms flashes 2129 f500 = pi.wave_create() # create and save id 2131 pi.wave_add_generic(flash_100) # 100 ms flashes 2132 f100 = pi.wave_create() # create and save id 2134 pi.wave_send_repeat(f500) 2138 pi.wave_send_repeat(f100) 2142 pi.wave_send_repeat(f500) 2146 pi.wave_tx_stop() # stop waveform 2148 pi.wave_clear() # clear all waveforms 2161 ext.extend(struct.pack(
"III", p.gpio_on, p.gpio_off, p.delay))
2164 self.
sl, _PI_CMD_WVAG, 0, 0, len(pulses)*12, extents))
2169 self, user_gpio, baud, data, offset=0, bb_bits=8, bb_stop=2):
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. 2175 user_gpio:= GPIO to transmit data. You must set the GPIO mode 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. 2184 Returns the new total number of pulses in the current waveform. 2186 The serial data is formatted as one start bit, [*bb_bits*] 2187 data bits, and [*bb_stop*]/2 stop bits. 2189 It is legal to add serial data streams with different baud 2190 rates to the same waveform. 2192 The bytes required for each character depend upon [*bb_bits*]. 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. 2199 pi.wave_add_serial(4, 300, 'Hello world') 2201 pi.wave_add_serial(4, 300, b"Hello world") 2203 pi.wave_add_serial(4, 300, b'\\x23\\x01\\x00\\x45') 2205 pi.wave_add_serial(17, 38400, [23, 128, 234], 5000) 2219 extents = [struct.pack(
"III", bb_bits, bb_stop, offset), data]
2221 self.
sl, _PI_CMD_WVAS, user_gpio, baud, len(data)+12, extents))
2227 Creates a waveform from the data provided by the prior calls 2228 to the [*wave_add_**] functions. 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. 2233 The data provided by the [*wave_add_**] functions is consumed by 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 2240 Normal usage would be 2242 Step 1. [*wave_clear*] to clear all waveforms and added data. 2244 Step 2. [*wave_add_**] calls to supply the waveform data. 2246 Step 3. [*wave_create*] to create the waveform and get a unique id 2248 Repeat steps 2 and 3 as needed. 2250 Step 4. [*wave_send_**] with the id of the waveform to transmit. 2252 A waveform comprises one or more pulses. 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. 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). 2263 When a waveform is started each pulse is executed in order with 2264 the specified delay between the pulse and the next. 2267 wid = pi.wave_create() 2274 This function deletes the waveform with id wave_id. 2276 wave_id:= >=0 (as returned by a prior call to [*wave_create*]). 2278 Wave ids are allocated in order, 0, 1, 2, etc. 2280 The wave is flagged for deletion. The resources used by the wave 2281 will only be reused when either of the following apply. 2283 - all waves with higher numbered wave ids have been deleted or have 2284 been flagged for deletion. 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). 2290 pi.wave_delete(6) # delete waveform with id 6 2292 pi.wave_delete(0) # delete waveform with id 0 2299 This function is deprecated and has been removed. 2301 Use [*wave_create*]/[*wave_send_**] instead. 2307 This function is deprecated and has beeen removed. 2309 Use [*wave_create*]/[*wave_send_**] instead. 2315 Transmits the waveform with id wave_id. The waveform is sent 2318 NOTE: Any hardware PWM started by [*hardware_PWM*] will 2321 wave_id:= >=0 (as returned by a prior call to [*wave_create*]). 2323 Returns the number of DMA control blocks used in the waveform. 2326 cbs = pi.wave_send_once(wid) 2333 Transmits the waveform with id wave_id. The waveform repeats 2334 until wave_tx_stop is called or another call to [*wave_send_**] 2337 NOTE: Any hardware PWM started by [*hardware_PWM*] will 2340 wave_id:= >=0 (as returned by a prior call to [*wave_create*]). 2342 Returns the number of DMA control blocks used in the waveform. 2345 cbs = pi.wave_send_repeat(wid) 2352 Transmits the waveform with id wave_id using mode mode. 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. 2358 WAVE_MODE_ONE_SHOT: same as [*wave_send_once*]. 2360 WAVE_MODE_REPEAT same as [*wave_send_repeat*]. 2362 WAVE_MODE_ONE_SHOT_SYNC same as [*wave_send_once*] but tries 2363 to sync with the previous waveform. 2365 WAVE_MODE_REPEAT_SYNC same as [*wave_send_repeat*] but tries 2366 to sync with the previous waveform. 2368 WARNING: bad things may happen if you delete the previous 2369 waveform before it has been synced to the new waveform. 2371 NOTE: Any hardware PWM started by [*hardware_PWM*] will 2374 wave_id:= >=0 (as returned by a prior call to [*wave_create*]). 2376 Returns the number of DMA control blocks used in the waveform. 2379 cbs = pi.wave_send_using_mode(wid, WAVE_MODE_REPEAT_SYNC) 2386 Returns the id of the waveform currently being 2389 Returns the waveform id or one of the following special 2392 WAVE_NOT_FOUND (9998) - transmitted wave not found. 2393 NO_TX_WAVE (9999) - no wave being transmitted. 2396 wid = pi.wave_tx_at() 2403 Returns 1 if a waveform is currently being transmitted, 2407 pi.wave_send_once(0) # send first waveform 2409 while pi.wave_tx_busy(): # wait for waveform to be sent 2412 pi.wave_send_once(1) # send next waveform 2419 Stops the transmission of the current waveform. 2421 This function is intended to stop a waveform started with 2425 pi.wave_send_repeat(3) 2436 This function transmits a chain of waveforms. 2438 NOTE: Any hardware PWM started by [*hardware_PWM*] 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. 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, 2450 Each wave is transmitted in the order specified. A wave 2451 may occur multiple times per chain. 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. 2457 Delays between waves may be added with the delay command. 2459 The following command codes are supported: 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 2467 If present Loop Forever must be the last entry in the chain. 2469 The code is currently dimensioned to support a chain with 2470 roughly 600 entries and 20 loop counters. 2473 #!/usr/bin/env python 2483 pi = pigpio.pi() # Connect to local Pi. 2485 pi.set_mode(GPIO, pigpio.OUTPUT); 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)]); 2492 wid[i] = pi.wave_create(); 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 2512 while pi.wave_tx_busy(): 2515 for i in range(WAVES): 2516 pi.wave_delete(wid[i]) 2528 self.
sl, _PI_CMD_WVCHA, 0, 0, len(data), [data]))
2533 Returns the length in microseconds of the current waveform. 2536 micros = pi.wave_get_micros() 2543 Returns the maximum possible size of a waveform in microseconds. 2546 micros = pi.wave_get_max_micros() 2553 Returns the length in pulses of the current waveform. 2556 pulses = pi.wave_get_pulses() 2563 Returns the maximum possible size of a waveform in pulses. 2566 pulses = pi.wave_get_max_pulses() 2573 Returns the length in DMA control blocks of the current 2577 cbs = pi.wave_get_cbs() 2584 Returns the maximum possible size of a waveform in DMA 2588 cbs = pi.wave_get_max_cbs() 2595 Returns a handle (>=0) for the device at the I2C bus address. 2598 i2c_address:= 0-0x7F. 2599 i2c_flags:= 0, no flags are currently defined. 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 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. 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: 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. 2624 [..]: Data sent by the device. 2628 h = pi.i2c_open(1, 0x53) # open device at address 0x53 on bus 1 2636 extents = [struct.pack(
"I", i2c_flags)]
2638 self.
sl, _PI_CMD_I2CO, i2c_bus, i2c_address, 4, extents))
2642 Closes the I2C device associated with handle. 2644 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2654 Sends a single bit to the device associated with handle. 2656 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2657 bit:= 0 or 1, the value to write. 2659 SMBus 2.0 5.5.1 - Quick command. 2665 pi.i2c_write_quick(0, 1) # send 1 to device 0 2666 pi.i2c_write_quick(3, 0) # send 0 to device 3 2673 Sends a single byte to the device associated with handle. 2675 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2676 byte_val:= 0-255, the value to write. 2678 SMBus 2.0 5.5.2 - Send byte. 2680 S Addr Wr [A] byte_val [A] P 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 2693 Reads a single byte from the device associated with handle. 2695 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2697 SMBus 2.0 5.5.3 - Receive byte. 2699 S Addr Rd [A] [Data] NA P 2703 b = pi.i2c_read_byte(2) # read a byte from device 2 2710 Writes a single byte to the specified register of the device 2711 associated with handle. 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. 2717 SMBus 2.0 5.5.4 - Write byte. 2719 S Addr Wr [A] reg [A] byte_val [A] P 2723 # send byte 0xC5 to reg 2 of device 1 2724 pi.i2c_write_byte_data(1, 2, 0xC5) 2726 # send byte 9 to reg 4 of device 2 2727 pi.i2c_write_byte_data(2, 4, 9) 2735 extents = [struct.pack(
"I", byte_val)]
2737 self.
sl, _PI_CMD_I2CWB, handle, reg, 4, extents))
2741 Writes a single 16 bit word to the specified register of the 2742 device associated with handle. 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. 2748 SMBus 2.0 5.5.4 - Write word. 2750 S Addr Wr [A] reg [A] word_val_Low [A] word_val_High [A] P 2754 # send word 0xA0C5 to reg 5 of device 4 2755 pi.i2c_write_word_data(4, 5, 0xA0C5) 2757 # send word 2 to reg 2 of device 5 2758 pi.i2c_write_word_data(5, 2, 23) 2766 extents = [struct.pack(
"I", word_val)]
2768 self.
sl, _PI_CMD_I2CWW, handle, reg, 4, extents))
2772 Reads a single byte from the specified register of the device 2773 associated with handle. 2775 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2776 reg:= >=0, the device register. 2778 SMBus 2.0 5.5.5 - Read byte. 2780 S Addr Wr [A] reg [A] S Addr Rd [A] [Data] NA P 2784 # read byte from reg 17 of device 2 2785 b = pi.i2c_read_byte_data(2, 17) 2787 # read byte from reg 1 of device 0 2788 b = pi.i2c_read_byte_data(0, 1) 2795 Reads a single 16 bit word from the specified register of the 2796 device associated with handle. 2798 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2799 reg:= >=0, the device register. 2801 SMBus 2.0 5.5.5 - Read word. 2803 S Addr Wr [A] reg [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P 2807 # read word from reg 2 of device 3 2808 w = pi.i2c_read_word_data(3, 2) 2810 # read word from reg 7 of device 2 2811 w = pi.i2c_read_word_data(2, 7) 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. 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. 2825 SMBus 2.0 5.5.6 - Process call. 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 2832 r = pi.i2c_process_call(h, 4, 0x1231) 2833 r = pi.i2c_process_call(h, 6, 0) 2841 extents = [struct.pack(
"I", word_val)]
2843 self.
sl, _PI_CMD_I2CPC, handle, reg, 4, extents))
2847 Writes up to 32 bytes to the specified register of the device 2848 associated with handle. 2850 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2851 reg:= >=0, the device register. 2852 data:= the bytes to write. 2854 SMBus 2.0 5.5.7 - Block write. 2856 S Addr Wr [A] reg [A] len(data) [A] data0 [A] data1 [A] ... [A] 2861 pi.i2c_write_block_data(4, 5, b'hello') 2863 pi.i2c_write_block_data(4, 5, "data bytes") 2865 pi.i2c_write_block_data(5, 0, b'\\x00\\x01\\x22') 2867 pi.i2c_write_block_data(6, 2, [0, 1, 0x22]) 2877 self.
sl, _PI_CMD_I2CWK, handle, reg, len(data), [data]))
2883 Reads a block of up to 32 bytes from the specified register of 2884 the device associated with handle. 2886 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2887 reg:= >=0, the device register. 2889 SMBus 2.0 5.5.7 - Block read. 2891 S Addr Wr [A] reg [A] 2892 S Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P 2895 The amount of returned data is set by the device. 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 2903 (b, d) = pi.i2c_read_block_data(h, 10) 2907 # process read failure 2910 bytes = PI_CMD_INTERRUPTED
2914 self.
sl, _PI_CMD_I2CRK, handle, reg))
2916 rdata = self.
_rxbuf(bytes)
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. 2925 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2926 reg:= >=0, the device register. 2927 data:= the bytes to write. 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. 2933 SMBus 2.0 5.5.8 - Block write-block read. 2935 S Addr Wr [A] reg [A] len(data) [A] data0 [A] ... datan [A] 2936 S Addr Rd [A] [Count] A [Data] ... A P 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 2945 (b, d) = pi.i2c_block_process_call(h, 10, b'\\x02\\x05\\x00') 2947 (b, d) = pi.i2c_block_process_call(h, 10, b'abcdr') 2949 (b, d) = pi.i2c_block_process_call(h, 10, "abracad") 2951 (b, d) = pi.i2c_block_process_call(h, 10, [2, 5, 16]) 2960 bytes = PI_CMD_INTERRUPTED
2964 self.
sl, _PI_CMD_I2CPK, handle, reg, len(data), [data]))
2966 rdata = self.
_rxbuf(bytes)
2971 Writes data bytes to the specified register of the device 2972 associated with handle . 1-32 bytes may be written. 2974 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 2975 reg:= >=0, the device register. 2976 data:= the bytes to write. 2979 S Addr Wr [A] reg [A] data0 [A] data1 [A] ... [A] datan [NA] P 2983 pi.i2c_write_i2c_block_data(4, 5, 'hello') 2985 pi.i2c_write_i2c_block_data(4, 5, b'hello') 2987 pi.i2c_write_i2c_block_data(5, 0, b'\\x00\\x01\\x22') 2989 pi.i2c_write_i2c_block_data(6, 2, [0, 1, 0x22]) 2999 self.
sl, _PI_CMD_I2CWI, handle, reg, len(data), [data]))
3005 Reads count bytes from the specified register of the device 3006 associated with handle . The count may be 1-32. 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. 3013 S Addr Wr [A] reg [A] 3014 S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P 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 3023 (b, d) = pi.i2c_read_i2c_block_data(h, 4, 32) 3027 # process read failure 3035 extents = [struct.pack(
"I", count)]
3037 bytes = PI_CMD_INTERRUPTED
3041 self.
sl, _PI_CMD_I2CRI, handle, reg, 4, extents))
3043 rdata = self.
_rxbuf(bytes)
3048 Returns count bytes read from the raw device associated 3051 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 3052 count:= >0, the number of bytes to read. 3055 S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P 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 3064 (count, data) = pi.i2c_read_device(h, 12) 3067 bytes = PI_CMD_INTERRUPTED
3073 rdata = self.
_rxbuf(bytes)
3078 Writes the data bytes to the raw device associated with handle. 3080 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 3081 data:= the bytes to write. 3084 S Addr Wr [A] data0 [A] data1 [A] ... [A] datan [A] P 3088 pi.i2c_write_device(h, b"\\x12\\x34\\xA8") 3090 pi.i2c_write_device(h, b"help") 3092 pi.i2c_write_device(h, 'help') 3094 pi.i2c_write_device(h, [23, 56, 231]) 3104 self.
sl, _PI_CMD_I2CWD, handle, 0, len(data), [data]))
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. 3115 handle:= >=0 (as returned by a prior call to [*i2c_open*]). 3116 data:= the concatenated I2C commands, see below 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 3124 (count, data) = pi.i2c_zip(h, [4, 0x53, 7, 1, 0x32, 6, 6, 0]) 3127 The following command codes are supported: 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 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 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. 3148 Any read I2C data is concatenated in the returned bytearray. 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 3156 0x04 0x53 0x07 0x01 0x32 0x06 0x06 3157 0x04 0x1E 0x07 0x01 0x03 0x06 0x06 3158 0x04 0x68 0x07 0x01 0x1B 0x06 0x08 3168 bytes = PI_CMD_INTERRUPTED
3172 self.
sl, _PI_CMD_I2CZ, handle, 0, len(data), [data]))
3174 rdata = self.
_rxbuf(bytes)
3178 def bb_spi_open(self, CS, MISO, MOSI, SCLK, baud=100000, spi_flags=0):
3180 This function selects a set of GPIO for bit banging SPI at a 3181 specified baud rate. 3188 spiFlags := see below 3190 spiFlags consists of the least significant 22 bits. 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 3197 mm defines the SPI mode, defaults to 0 3207 The following constants may be used to set the mode: 3216 Alternatively pigpio.SPI_CPOL and/or pigpio.SPI_CPHA 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. 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. 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. 3230 The other bits in spiFlags should be set to zero. 3232 Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_SPI_BAUD, or 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. 3239 bb_spi_open(10, MISO, MOSI, SCLK, 10000, 0); // device 1 3240 bb_spi_open(11, MISO, MOSI, SCLK, 20000, 3); // device 2 3253 extents = [struct.pack(
"IIIII", MISO, MOSI, SCLK, baud, spi_flags)]
3255 self.
sl, _PI_CMD_BSPIO, CS, 0, 20, extents))
3260 This function stops bit banging SPI on a set of GPIO 3261 opened with [*bb_spi_open*]. 3263 CS:= 0-31, the CS GPIO used in a prior call to [*bb_spi_open*] 3265 Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_SPI_GPIO. 3276 This function executes a bit banged SPI transfer. 3278 CS:= 0-31 (as used in a prior call to [*bb_spi_open*]) 3279 data:= data to be sent 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 3287 #!/usr/bin/env python 3298 if not pi.connected: 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 3304 for i in range(256): 3306 count, data = pi.bb_spi_xfer(CE0, [0, i]) # Set DAC value 3310 count, data = pi.bb_spi_xfer(CE0, [12, 0]) # Read back DAC 3316 count, data = pi.bb_spi_xfer(CE1, [1, 128, 0]) # Read ADC 3320 read_val = ((data[1]&3)<<8) | data[2] 3322 print("{} {}".format(set_val, read_val)) 3324 pi.bb_spi_close(CE0) 3325 pi.bb_spi_close(CE1) 3336 bytes = PI_CMD_INTERRUPTED
3340 self.
sl, _PI_CMD_BSPIX, CS, 0, len(data), [data]))
3342 rdata = self.
_rxbuf(bytes)
3348 This function selects a pair of GPIO for bit banging I2C at a 3349 specified baud rate. 3351 Bit banging I2C allows for certain operations which are not possible 3352 with the standard I2C driver. 3354 o baud rates as low as 50 3357 o I2C on any pair of spare GPIO 3363 Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_I2C_BAUD, or 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. 3372 h = pi.bb_i2c_open(4, 5, 50000) # bit bang on GPIO 4/5 at 50kbps 3380 extents = [struct.pack(
"I", baud)]
3382 self.
sl, _PI_CMD_BI2CO, SDA, SCL, 4, extents))
3387 This function stops bit banging I2C on a pair of GPIO 3388 previously opened with [*bb_i2c_open*]. 3390 SDA:= 0-31, the SDA GPIO used in a prior call to [*bb_i2c_open*] 3392 Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_I2C_GPIO. 3395 pi.bb_i2c_close(SDA) 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 3408 SDA:= 0-31 (as used in a prior call to [*bb_i2c_open*]) 3409 data:= the concatenated I2C commands, see below 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 3417 (count, data) = pi.bb_i2c_zip( 3418 SDA, [4, 0x53, 2, 7, 1, 0x32, 2, 6, 6, 3, 0]) 3421 The following command codes are supported: 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 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 3438 The address and flags default to 0. The address and flags maintain 3439 their previous value until updated. 3441 No flags are currently defined. 3443 Any read I2C data is concatenated in the returned bytearray. 3447 start, write 0x32, (re)start, read 6 bytes, stop 3449 start, write 0x03, (re)start, read 6 bytes, stop 3451 start, write 0x1B, (re)start, read 8 bytes, stop 3455 0x02 0x07 0x01 0x32 0x02 0x06 0x06 0x03 3458 0x02 0x07 0x01 0x03 0x02 0x06 0x06 0x03 3461 0x02 0x07 0x01 0x1B 0x02 0x06 0x08 0x03 3472 bytes = PI_CMD_INTERRUPTED
3476 self.
sl, _PI_CMD_BI2CZ, SDA, 0, len(data), [data]))
3478 rdata = self.
_rxbuf(bytes)
3483 This function signals the occurrence of an event. 3485 event:= 0-31, the event 3487 Returns 0 if OK, otherwise PI_BAD_EVENT_ID. 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. 3494 One event, EVENT_BSC (31) is predefined. This event is 3495 auto generated on BSC slave activity. 3497 The meaning of other events is arbitrary. 3499 Note that other than its id and its tick there is no data associated 3503 pi.event_trigger(23) 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. 3515 I can't get SPI to work properly. I tried with a 3516 control word of 0x303 and swapped MISO and MOSI. 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 3524 bsc_control:= see below 3525 data:= the data bytes to place in the transmit FIFO. 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). 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. 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. 3540 When a zero control word is received GPIO 18-21 will be reset 3543 bsc_control consists of the following bits: 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 3550 Bits 0-13 are copied unchanged to the BSC CR register. See 3551 pages 163-165 of the Broadcom peripherals document for full 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 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 3570 The status has the following format: 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 3577 Bits 0-15 are copied unchanged from the BSC FR register. See 3578 pages 165-166 of the Broadcom peripherals document for full 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 3585 TE @ transmit FIFO empty 3586 RF @ receive FIFO full 3587 TF @ transmit FIFO full 3588 RE @ receive FIFO empty 3592 (status, count, data) = pi.bsc_xfer(0x330305, "Hello!") 3601 status = PI_CMD_INTERRUPTED
3603 rdata = bytearray(b
'')
3606 self.
sl, _PI_CMD_BSCX, bsc_control, 0, len(data), [data]))
3609 status = struct.unpack(
'I', rx[0:4])[0]
3615 return status, bytes, rdata
3619 This function allows the Pi to act as a slave I2C device. 3621 The data bytes (if any) are written to the BSC transmit 3622 FIFO and the bytes in the BSC receive FIFO are returned. 3624 i2c_address:= the I2C slave address. 3625 data:= the data bytes to transmit. 3627 The returned value is a tuple of the status, the number 3628 of bytes read, and a bytearray containing the read bytes. 3630 See [*bsc_xfer*] for details of the status value. 3632 If there was an error the status will be less than zero 3633 (and will contain the error code). 3635 Note that an i2c_address of 0 may be used to close 3636 the BSC device and reassign the used GPIO (18/19) 3639 This example assumes GPIO 2/3 are connected to GPIO 18/19. 3642 #!/usr/bin/env python 3651 s, b, d = pi.bsc_i2c(I2C_ADDR) 3653 if d[0] == ord('t'): # 116 send 'HH:MM:SS*' 3655 print("sent={} FR={} received={} [{}]". 3656 format(s>>16, s&0xfff,b,d)) 3658 s, b, d = pi.bsc_i2c(I2C_ADDR, 3659 "{}*".format(time.asctime()[11:19])) 3661 elif d[0] == ord('d'): # 100 send 'Sun Oct 30*' 3663 print("sent={} FR={} received={} [{}]". 3664 format(s>>16, s&0xfff,b,d)) 3666 s, b, d = pi.bsc_i2c(I2C_ADDR, 3667 "{}*".format(time.asctime()[:10])) 3671 if not pi.connected: 3674 # Respond to BSC slave activity 3676 e = pi.event_callback(pigpio.EVENT_BSC, i2c) 3678 pi.bsc_i2c(I2C_ADDR) # Configure BSC as I2C slave 3684 pi.bsc_i2c(0) # Disable BSC peripheral 3689 While running the above. 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: -- -- -- -- -- -- -- -- 3703 $ pigs i2co 1 0x13 0 3715 $ pigs i2crd 0 11 -a 3719 $ pigs i2crd 0 11 -a 3727 $ pigs i2crd 0 11 -a 3732 control = (i2c_address<<16)|0x305
3735 return self.
bsc_xfer(control, data)
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. 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. 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. 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 3758 spi_flags consists of the least significant 22 bits. 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 3765 mm defines the SPI mode. 3767 WARNING: modes 1 and 3 do not appear to work on 3768 the auxiliary device. 3778 px is 0 if CEx is active low (default) and 1 for active high. 3780 ux is 0 if the CEx GPIO is reserved for SPI (default) 3783 A is 0 for the standard SPI device, 1 for the auxiliary SPI. 3785 W is 0 if the device is not 3-wire, 1 if the device is 3-wire. 3786 Standard SPI device only. 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. 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. 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. 3800 bbbbbb defines the word size in bits (0-32). The default (0) 3801 sets 8 bits per word. Auxiliary SPI device only. 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. 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. 3811 Multi-byte transfers are made in least significant byte 3814 E.g. to transfer 32 11-bit words data should 3817 E.g. to transfer the 14 bit value 0x1ABC send the 3818 bytes 0xBC followed by 0x1A. 3820 The other bits in flags should be set to zero. 3823 # open SPI device on channel 1 in mode 3 at 50000 bits per second 3825 h = pi.spi_open(1, 50000, 3) 3833 extents = [struct.pack(
"I", spi_flags)]
3835 self.
sl, _PI_CMD_SPIO, spi_channel, baud, 4, extents))
3839 Closes the SPI device associated with handle. 3841 handle:= >=0 (as returned by a prior call to [*spi_open*]). 3851 Reads count bytes from the SPI device associated with handle. 3853 handle:= >=0 (as returned by a prior call to [*spi_open*]). 3854 count:= >0, the number of bytes to read. 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 3862 (b, d) = pi.spi_read(h, 60) # read 60 bytes from device h 3869 bytes = PI_CMD_INTERRUPTED
3873 self.
sl, _PI_CMD_SPIR, handle, count))
3875 rdata = self.
_rxbuf(bytes)
3880 Writes the data bytes to the SPI device associated with handle. 3882 handle:= >=0 (as returned by a prior call to [*spi_open*]). 3883 data:= the bytes to write. 3886 pi.spi_write(0, b'\\x02\\xc0\\x80') # write 3 bytes to device 0 3888 pi.spi_write(0, b'defgh') # write 5 bytes to device 0 3890 pi.spi_write(0, "def") # write 3 bytes to device 0 3892 pi.spi_write(1, [2, 192, 128]) # write 3 bytes to device 1 3901 self.
sl, _PI_CMD_SPIW, handle, 0, len(data), [data]))
3905 Writes the data bytes to the SPI device associated with handle, 3906 returning the data bytes read from the device. 3908 handle:= >=0 (as returned by a prior call to [*spi_open*]). 3909 data:= the bytes to write. 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 3917 (count, rx_data) = pi.spi_xfer(h, b'\\x01\\x80\\x00') 3919 (count, rx_data) = pi.spi_xfer(h, [1, 128, 0]) 3921 (count, rx_data) = pi.spi_xfer(h, b"hello") 3923 (count, rx_data) = pi.spi_xfer(h, "hello") 3932 bytes = PI_CMD_INTERRUPTED
3936 self.
sl, _PI_CMD_SPIX, handle, 0, len(data), [data]))
3938 rdata = self.
_rxbuf(bytes)
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. 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. 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 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. 3961 h1 = pi.serial_open("/dev/ttyAMA0", 300) 3963 h2 = pi.serial_open("/dev/ttyUSB1", 19200, 0) 3965 h3 = pi.serial_open("/dev/serial0", 9600) 3974 self.
sl, _PI_CMD_SERO, baud, ser_flags, len(tty), [tty]))
3978 Closes the serial device associated with handle. 3980 handle:= >=0 (as returned by a prior call to [*serial_open*]). 3990 Returns a single byte from the device associated with handle. 3992 handle:= >=0 (as returned by a prior call to [*serial_open*]). 3994 If no data is ready a negative error code will be returned. 3997 b = pi.serial_read_byte(h1) 4004 Writes a single byte to the device associated with handle. 4006 handle:= >=0 (as returned by a prior call to [*serial_open*]). 4007 byte_val:= 0-255, the value to write. 4010 pi.serial_write_byte(h1, 23) 4012 pi.serial_write_byte(h1, ord('Z')) 4020 Reads up to count bytes from the device associated with handle. 4022 handle:= >=0 (as returned by a prior call to [*serial_open*]). 4023 count:= >0, the number of bytes to read (defaults to 1000). 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 4030 If no data is ready a bytes read of zero is returned. 4032 (b, d) = pi.serial_read(h2, 100) 4037 bytes = PI_CMD_INTERRUPTED
4043 rdata = self.
_rxbuf(bytes)
4048 Writes the data bytes to the device associated with handle. 4050 handle:= >=0 (as returned by a prior call to [*serial_open*]). 4051 data:= the bytes to write. 4054 pi.serial_write(h1, b'\\x02\\x03\\x04') 4056 pi.serial_write(h2, b'help') 4058 pi.serial_write(h2, "hello") 4060 pi.serial_write(h1, [2, 3, 4]) 4070 self.
sl, _PI_CMD_SERW, handle, 0, len(data), [data]))
4074 Returns the number of bytes available to be read from the 4075 device associated with handle. 4077 handle:= >=0 (as returned by a prior call to [*serial_open*]). 4080 rdy = pi.serial_data_available(h1) 4083 (b, d) = pi.serial_read(h1, rdy) 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. 4098 pi.gpio_trigger(23, 10, 1) 4108 extents = [struct.pack(
"I", level)]
4110 self.
sl, _PI_CMD_TRIG, user_gpio, pulse_len, 4, extents))
4114 Sets a glitch filter on a GPIO. 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. 4124 Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER. 4126 This filter affects the GPIO samples returned to callbacks set up 4127 with [*callback*] and [*wait_for_edge*]. 4129 It does not affect levels read by [*read*], 4130 [*read_bank_1*], or [*read_bank_2*]. 4132 Each (stable) edge will be timestamped [*steady*] 4133 microseconds after it was first detected. 4136 pi.set_glitch_filter(23, 100) 4143 Sets a noise filter on a GPIO. 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. 4154 Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER. 4156 This filter affects the GPIO samples returned to callbacks set up 4157 with [*callback*] and [*wait_for_edge*]. 4159 It does not affect levels read by [*read*], 4160 [*read_bank_1*], or [*read_bank_2*]. 4162 Level changes before and after the active period may 4163 be reported. Your software must be designed to cope with 4167 pi.set_noise_filter(23, 1000, 5000) 4177 extents = [struct.pack(
"I", active)]
4179 self.
sl, _PI_CMD_FN, user_gpio, steady, 4, extents))
4183 Store a script for later execution. 4185 See [[http://abyz.me.uk/rpi/pigpio/pigs.html#Scripts]] for 4188 script:= the script text as a series of bytes. 4190 Returns a >=0 script id if OK. 4193 sid = pi.store_script( 4194 b'tag 0 w 22 1 mils 100 w 22 0 mils 100 dcr p0 jp 0') 4204 self.
sl, _PI_CMD_PROC, 0, 0, len(script), [script]))
4210 Runs a stored script. 4212 script_id:= id of stored script. 4213 params:= up to 10 parameters required by the script. 4216 s = pi.run_script(sid, [par1, par2]) 4218 s = pi.run_script(sid) 4220 s = pi.run_script(sid, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 4228 if params
is not None:
4231 ext.extend(struct.pack(
"I", p))
4238 self.
sl, _PI_CMD_PROCR, script_id, 0, nump*4, extents))
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. 4246 script_id:= id of stored script. 4247 params:= up to 10 parameters required by the script. 4250 s = pi.update_script(sid, [par1, par2]) 4252 s = pi.update_script(sid, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 4260 if params
is not None:
4263 ext.extend(struct.pack(
"I", p))
4270 self.
sl, _PI_CMD_PROCU, script_id, 0, nump*4, extents))
4274 Returns the run status of a stored script as well as the 4275 current values of parameters 0 to 9. 4277 script_id:= id of stored script. 4279 The run status may be 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. 4294 (s, pars) = pi.script_status(sid) 4297 status = PI_CMD_INTERRUPTED
4303 data = self.
_rxbuf(bytes)
4304 pars = struct.unpack(
'11i',
_str(data))
4309 return status, params
4313 Stops a running script. 4315 script_id:= id of stored script. 4318 status = pi.stop_script(sid) 4325 Deletes a stored script. 4327 script_id:= id of stored script. 4330 status = pi.delete_script(sid) 4337 Opens a GPIO for bit bang reading of serial data. 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. 4343 The serial data is held in a cyclic buffer and is read using 4346 It is the caller's responsibility to read data from the cyclic 4347 buffer in a timely fashion. 4350 status = pi.bb_serial_read_open(4, 19200) 4351 status = pi.bb_serial_read_open(17, 9600) 4361 extents = [struct.pack(
"I", bb_bits)]
4363 self.
sl, _PI_CMD_SLRO, user_gpio, baud, 4, extents))
4367 Returns data from the bit bang serial cyclic buffer. 4369 user_gpio:= 0-31 (opened in a prior call to [*bb_serial_read_open*]) 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 4376 The bytes returned for each character depend upon the number of 4377 data bits [*bb_bits*] specified in the [*bb_serial_read_open*] 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. 4385 (count, data) = pi.bb_serial_read(4) 4388 bytes = PI_CMD_INTERRUPTED
4394 rdata = self.
_rxbuf(bytes)
4400 Closes a GPIO for bit bang reading of serial data. 4402 user_gpio:= 0-31 (opened in a prior call to [*bb_serial_read_open*]) 4405 status = pi.bb_serial_read_close(17) 4412 Invert serial logic. 4414 user_gpio:= 0-31 (opened in a prior call to [*bb_serial_read_open*]) 4415 invert:= 0-1 (1 invert, 0 normal) 4418 status = pi.bb_serial_invert(17, 1) 4426 Calls a pigpio function customised by the user. 4428 arg1:= >=0, default 0. 4429 arg2:= >=0, default 0. 4430 argx:= extra arguments (each 0-255), default empty. 4432 The returned value is an integer which by convention 4433 should be >=0 for OK and <0 for error. 4436 value = pi.custom_1() 4438 value = pi.custom_1(23) 4440 value = pi.custom_1(0, 55) 4442 value = pi.custom_1(23, 56, [1, 5, 7]) 4444 value = pi.custom_1(23, 56, b"hello") 4446 value = pi.custom_1(23, 56, "hello") 4456 self.
sl, _PI_CMD_CF1, arg1, arg2, len(argx), [argx]))
4460 Calls a pigpio function customised by the user. 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. 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). 4472 (count, data) = pi.custom_2() 4474 (count, data) = pi.custom_2(23) 4476 (count, data) = pi.custom_2(23, [1, 5, 7]) 4478 (count, data) = pi.custom_2(23, b"hello") 4480 (count, data) = pi.custom_2(23, "hello", 128) 4489 bytes = PI_CMD_INTERRUPTED
4493 self.
sl, _PI_CMD_CF2, arg1, retMax, len(argx), [argx]))
4495 rdata = self.
_rxbuf(bytes)
4500 This function returns the pad drive strength in mA. 4502 pad:= 0-2, the pad to get. 4504 Returns the pad drive strength if OK, otherwise PI_BAD_PAD. 4512 strength = pi.get_pad_strength(0) # Get pad 0 strength. 4519 This function sets the pad drive strength in mA. 4522 pad:= 0-2, the pad to set. 4523 pad_strength:= 1-16 mA. 4525 Returns 0 if OK, otherwise PI_BAD_PAD, or PI_BAD_STRENGTH. 4533 pi.set_pad_strength(2, 14) # Set pad 2 to 14 mA. 4541 This function returns a handle to a file opened in a specified mode. 4543 file_name:= the file to open. 4544 file_mode:= the file open mode. 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. 4551 h = pi.file_open("/home/pi/shared/dir_3/file.txt", 4552 pigpio.FILE_WRITE | pigpio.FILE_CREATE) 4554 pi.file_write(h, "Hello world") 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. 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. 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. 4573 Suppose /opt/pigpio/access contains the following entries: 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 4583 Files may be written in directory dir_1 with the exception 4586 Files may be read in directory dir_2. 4588 Files may be read and written in directory dir_3. 4590 If a directory allows read, write, or read/write access then files 4591 may be created in that directory. 4593 In an attempt to prevent risky permissions the following paths are 4594 ignored in /opt/pigpio/access: 4597 a path containing .. 4598 a path containing only wildcards (*?) 4599 a path containing less than two non-wildcard parts 4604 The mode may have the following values: 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 4611 The following values may be or'd into the mode: 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 4618 Newly created files are owned by root with permissions owner 4622 #!/usr/bin/env python 4628 if not pi.connected: 4631 # Assumes /opt/pigpio/access contains the following line: 4634 handle = pi.file_open("/ram/pigpio.c", pigpio.FILE_READ) 4639 c, d = pi.file_read(handle, 60000) 4645 pi.file_close(handle) 4656 self.
sl, _PI_CMD_FO, file_mode, 0, len(file_name), [file_name]))
4660 Closes the file associated with handle. 4662 handle:= >=0 (as returned by a prior call to [*file_open*]). 4665 pi.file_close(handle) 4672 Reads up to count bytes from the file associated with handle. 4674 handle:= >=0 (as returned by a prior call to [*file_open*]). 4675 count:= >0, the number of bytes to read. 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 4683 (b, d) = pi.file_read(h2, 100) 4688 bytes = PI_CMD_INTERRUPTED
4694 rdata = self.
_rxbuf(bytes)
4699 Writes the data bytes to the file associated with handle. 4701 handle:= >=0 (as returned by a prior call to [*file_open*]). 4702 data:= the bytes to write. 4705 pi.file_write(h1, b'\\x02\\x03\\x04') 4707 pi.file_write(h2, b'help') 4709 pi.file_write(h2, "hello") 4711 pi.file_write(h1, [2, 3, 4]) 4721 self.
sl, _PI_CMD_FW, handle, 0, len(data), [data]))
4725 Seeks to a position relative to the start, current position, 4726 or end of the file. Returns the new position. 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. 4733 new_pos = pi.file_seek(h, 100, pigpio.FROM_START) 4735 cur_pos = pi.file_seek(h, 0, pigpio.FROM_CURRENT) 4737 file_size = pi.file_seek(h, 0, pigpio.FROM_END) 4745 extents = [struct.pack(
"I", seek_from)]
4747 self.
sl, _PI_CMD_FS, handle, seek_offset, 4, extents))
4751 Returns a list of files which match a pattern. 4753 fpattern:= file pattern to match. 4755 Returns the number of returned bytes if OK, otherwise 4756 PI_NO_FILE_ACCESS, or PI_NO_FILE_MATCH. 4758 The pattern must match an entry in /opt/pigpio/access. The 4759 pattern may contain wildcards. See [*file_open*]. 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 4768 #!/usr/bin/env python 4774 if not pi.connected: 4777 # Assumes /opt/pigpio/access contains the following line: 4780 c, d = pi.file_list("/ram/p*.c") 4793 bytes = PI_CMD_INTERRUPTED
4797 self.
sl, _PI_CMD_FL, 60000, 0, len(fpattern), [fpattern]))
4799 rdata = self.
_rxbuf(bytes)
4804 This function uses the system call to execute a shell script 4805 with the given string as its parameter. 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 4811 The exit status of the system call is returned if OK, 4812 otherwise PI_BAD_SHELL_STATUS. 4814 [*shellscr*] must exist in /opt/pigpio/cgi and must be executable. 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. 4820 The following table gives some example returned statuses: 4822 Script exit status @ Returned system call status 4827 script not found @ 32512 4830 // pass two parameters, hello and world 4831 status = pi.shell("scr1", "hello world"); 4833 // pass three parameters, hello, string with spaces, and world 4834 status = pi.shell("scr1", "hello 'string with spaces' world"); 4836 // pass one parameter, hello string with spaces world 4837 status = pi.shell("scr1", "\\"hello string with spaces world\\""); 4849 self.
sl, _PI_CMD_SHELL, ls, 0, ls+lp+1, [shellscr+
'\x00'+pstring]))
4852 def callback(self, user_gpio, edge=RISING_EDGE, func=None):
4854 Calls a user supplied function (a callback) whenever the 4855 specified GPIO edge is detected. 4858 edge:= EITHER_EDGE, RISING_EDGE (default), or FALLING_EDGE. 4859 func:= user supplied callback function. 4861 The user supplied callback receives three parameters, the GPIO, 4862 the level, and the tick. 4865 Parameter Value Meaning 4867 GPIO 0-31 The GPIO which has changed state 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) 4873 tick 32 bit The number of microseconds since boot 4874 WARNING: this wraps around from 4875 4294967295 to 0 roughly every 72 minutes 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. 4883 The callback may be cancelled by calling the cancel function. 4885 A GPIO may have multiple callbacks (although I can't think of 4889 def cbf(gpio, level, tick): 4890 print(gpio, level, tick) 4892 cb1 = pi.callback(22, pigpio.EITHER_EDGE, cbf) 4894 cb2 = pi.callback(4, pigpio.EITHER_EDGE) 4896 cb3 = pi.callback(17) 4902 cb1.cancel() # To cancel callback cb1. 4909 Calls a user supplied function (a callback) whenever the 4910 specified event is signalled. 4913 func:= user supplied callback function. 4915 The user supplied callback receives two parameters, the event id, 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. 4923 The callback may be cancelled by calling the event_cancel function. 4925 An event may have multiple callbacks (although I can't think of 4929 def cbf(event, tick): 4932 cb1 = pi.event_callback(22, cbf) 4934 cb2 = pi.event_callback(4) 4940 cb1.event_cancel() # To cancel callback cb1. 4948 Wait for an edge event on a GPIO. 4951 edge:= EITHER_EDGE, RISING_EDGE (default), or 4953 wait_timeout:= >=0.0 (default 60.0). 4955 The function returns when the edge is detected or after 4956 the number of seconds specified by timeout has expired. 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. 4963 The function returns True if the edge is detected, 4967 if pi.wait_for_edge(23): 4968 print("Rising edge detected") 4970 print("wait for edge timed out") 4972 if pi.wait_for_edge(23, pigpio.FALLING_EDGE, 5.0): 4973 print("Falling edge detected") 4975 print("wait for falling edge timed out") 4986 wait_timeout:= >=0.0 (default 60.0). 4988 The function returns when the event is signalled or after 4989 the number of seconds specified by timeout has expired. 4991 The function returns True if the event is detected, 4995 if pi.wait_for_event(23): 4996 print("event detected") 4998 print("wait for event timed out") 5005 host = os.getenv(
"PIGPIO_ADDR",
'localhost'),
5006 port = os.getenv(
"PIGPIO_PORT", 8888),
5007 show_errors =
True):
5009 Grants access to a Pi's GPIO. 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. 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. 5020 This connects to the pigpio daemon and reserves resources 5021 to be used for sending commands and receiving notifications. 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. 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 5032 pi = pigpio.pi() # exit script if no connection 5033 if not pi.connected: 5051 self.sl.s = socket.create_connection((host, port),
None)
5054 self.sl.s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
5058 except socket.error:
5061 except struct.error:
5070 atexit.register(self.
stop)
5076 if self.sl.s
is not None:
5081 s =
"Can't connect to pigpio at {}({})".format(host, str(port))
5084 print(_except_a.format(s))
5087 elif exception == 2:
5094 """Release pigpio resources. 5107 if self.sl.s
is not None:
5114 The number of microseconds level changes are reported for once 5115 a noise filter has been triggered (by [*steady*] microseconds of 5120 An unsigned argument passed to a user customised function. Its 5121 meaning is defined by the customiser. 5124 An unsigned argument passed to a user customised function. Its 5125 meaning is defined by the customiser. 5128 An array of bytes passed to a user customised function. 5129 Its meaning and content is defined by the customiser. 5132 The speed of serial communication (I2C, SPI, serial link, waves) 5136 The number of data bits to be used when adding serial data to a 5140 The number of (half) stop bits to be used when adding serial data 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). 5151 To select GPIO 1, 7, 23 5153 bits = (1<<1) | (1<<7) | (1<<23) 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 5162 aaaaaaa defines the I2C slave address (only relevant in I2C mode) 5164 Bits 0-13 are copied unchanged to the BSC CR register. See 5165 pages 163-165 of the Broadcom peripherals document. 5171 The hardware clock frequency. 5174 True if a connection was established, False otherwise. 5177 The number of bytes of data to be transferred. 5180 The GPIO used for the slave select signal when bit banging SPI. 5183 Data to be transmitted, a series of bytes. 5186 The length of a pulse in microseconds. 5189 A number between 0 and range_. 5191 The dutycycle sets the proportion of time on versus time off during each 5196 range_ * 0.25 @ 25% On 5197 range_ * 0.50 @ 50% On 5198 range_ * 0.75 @ 75% On 5212 PI_BAD_USER_GPIO = -2 5217 PI_BAD_PULSEWIDTH = -7 5218 PI_BAD_DUTYCYCLE = -8 5219 PI_BAD_WDOG_TIMEOUT = -15 5220 PI_BAD_DUTYRANGE = -21 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 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 5240 PI_TOO_MANY_TAGS = -54 5241 PI_BAD_SCRIPT_CMD = -55 5242 PI_BAD_VAR_NUM = -56 5243 PI_NO_SCRIPT_ROOM = -57 5245 PI_SOCK_READ_FAILED = -59 5246 PI_SOCK_WRIT_FAILED = -60 5247 PI_TOO_MANY_PARAM = -61 5248 PI_SCRIPT_NOT_READY = -62 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 5264 PI_BAD_SPI_SPEED = -78 5265 PI_BAD_SER_DEVICE = -79 5266 PI_BAD_SER_SPEED = -80 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 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 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 5329 An event is a signal used to inform one or more consumers 5333 The mode may have the following values 5341 The following values can be or'd into the file open mode 5350 A full file path. To be accessible the path must match 5351 an entry in /opt/pigpio/access. 5354 A file path which may contain wildcards. To be accessible the path 5355 must match an entry in /opt/pigpio/access. 5358 Defines the frequency to be used for PWM on a GPIO. 5359 The closest permitted frequency will be used. 5362 A user supplied callback function. 5365 A Broadcom numbered GPIO. All the user GPIO are in the range 0-31. 5367 There are 54 General Purpose Input Outputs (GPIO) named GPIO0 5370 They are split into two banks. Bank 1 consists of GPIO0 5371 through GPIO31. Bank 2 consists of GPIO32 through GPIO53. 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. 5377 See [*get_hardware_revision*]. 5379 The user GPIO are marked with an X in the following table 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 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 - - - - 5394 A mask used to select GPIO to be operated on. See [*bits*]. 5396 This mask selects the GPIO to be switched off at the start 5400 A mask used to select GPIO to be operated on. See [*bits*]. 5402 This mask selects the GPIO to be switched on at the start 5406 A number referencing an object opened by one of the following 5415 The name or IP address of the Pi running the pigpio daemon. 5418 One of the i2c_ functions. 5421 The address of a device on the I2C bus. 5427 No I2C flags are currently defined. 5430 A flag used to set normal or inverted bit bang serial data 5442 TIMEOUT = 2 # only returned for a watchdog timeout 5446 The GPIO used for the MISO signal when bit banging SPI. 5450 1.The operational mode of a GPIO, normally INPUT or OUTPUT. 5463 2. The mode of waveform transmission. 5466 WAVE_MODE_ONE_SHOT = 0 5467 WAVE_MODE_REPEAT = 1 5468 WAVE_MODE_ONE_SHOT_SYNC = 2 5469 WAVE_MODE_REPEAT_SYNC = 3 5473 The GPIO used for the MOSI signal when bit banging SPI. 5476 The offset wave data starts from the beginning of the waveform 5477 being currently defined. 5480 A set of GPIO which share common drivers. 5488 The mA which may be drawn from each GPIO whilst still guaranteeing the 5489 high and low levels. 5491 params: 32 bit number 5492 When scripts are started they can receive up to 10 parameters 5493 to define their operation. 5496 The port used by the pigpio daemon, defaults to 8888. 5499 The string to be passed to a [*shell*] script to be executed. 5509 The length of the trigger pulse in microseconds. 5512 A list of class pulse objects defining the characteristics of a 5516 The servo pulsewidth in microseconds. 0 switches pulses off. 5518 PWMduty: 0-1000000 (1M) 5519 The hardware PWM dutycycle. 5521 PWMfreq: 1-125000000 (125M) 5522 The hardware PWM frequency. 5525 Defines the limits for the [*dutycycle*] parameter. 5527 range_ defaults to 255. 5530 An I2C device register. The usable registers depend on the 5534 The maximum number of bytes a user customised function 5535 should return, default 8192. 5538 The user GPIO to use for the clock when bit banging I2C. 5541 The GPIO used for the SCLK signal when bit banging SPI. 5544 The text of a script to store on the pigpio daemon. 5547 A number referencing a script created by [*store_script*]. 5550 The user GPIO to use for data when bit banging I2C. 5553 Direction to seek for [*file_seek*]. 5562 The number of bytes to move forward (positive) or backwards 5563 (negative) from the seek position (start, current, or end of file). 5566 No serial flags are currently defined. 5569 One of the serial_ functions. 5572 The name of a shell script. The script must exist 5573 in /opt/pigpio/cgi and must be executable. 5576 Controls the display of pigpio daemon connection failures. 5577 The default of True prints the probable failure reasons to 5581 One of the spi_ functions. 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*]). 5603 A Pi serial tty device, e.g. /dev/ttyAMA0, /dev/ttyUSB0 5606 An unsigned 32 bit number. 5609 A Broadcom numbered GPIO. 5611 All the user GPIO are in the range 0-31. 5613 Not all the GPIO within this range are usable, some are reserved 5619 The number of seconds to wait in [*wait_for_edge*] before timing out. 5622 One of the following 5625 [*wave_add_generic*] 5629 A number referencing a wave created by [*wave_create*]. 5632 One of the following 5635 [*wave_send_repeat*] 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). def serial_open(self, tty, baud, ser_flags=0)
def bb_serial_invert(self, user_gpio, invert)
def serial_read(self, handle, count=1000)
def __init__(self, notify, user_gpio, edge=RISING_EDGE, func=None)
def wave_delete(self, wave_id)
def __init__(self, notify, event, timeout)
def __init__(self, host=os.getenv("PIGPIO_ADDR", 'localhost'), port=os.getenv("PIGPIO_PORT", 8888), show_errors=True)
def bsc_i2c(self, i2c_address, data=[])
def i2c_write_device(self, handle, data)
def wave_get_max_micros(self)
def wait_for_event(self, event, wait_timeout=60.0)
def custom_2(self, arg1=0, argx=[], retMax=8192)
def _pigpio_command_ext_nolock(sl, cmd, p1, p2, p3, extents)
def get_pad_strength(self, pad)
def _tally(self, event, tick)
def spi_open(self, spi_channel, baud, spi_flags=0)
def __init__(self, notify, gpio, edge, timeout)
def bb_i2c_close(self, SDA)
def _pigpio_command_nolock(sl, cmd, p1, p2)
def func(self, event, tick)
def wave_send_once(self, wave_id)
def notify_begin(self, handle, bits)
def remove_event(self, callb)
def notify_pause(self, handle)
def bb_spi_open(self, CS, MISO, MOSI, SCLK, baud=100000, spi_flags=0)
def i2c_process_call(self, handle, reg, word_val)
def wave_chain(self, data)
def wait_for_edge(self, user_gpio, edge=RISING_EDGE, wait_timeout=60.0)
def i2c_write_byte(self, handle, byte_val)
def hardware_PWM(self, gpio, PWMfreq, PWMduty)
def func(self, gpio, level, tick)
def bb_serial_read_close(self, user_gpio)
def append_event(self, callb)
def i2c_write_quick(self, handle, bit)
def set_noise_filter(self, user_gpio, steady, active)
def serial_close(self, handle)
def __init__(self, notify, event, func=None)
def hardware_clock(self, gpio, clkfreq)
def write(self, gpio, level)
def get_current_tick(self)
def set_PWM_frequency(self, user_gpio, frequency)
def shell(self, shellscr, pstring="")
def i2c_zip(self, handle, data)
def set_pad_strength(self, pad, pad_strength)
def spi_close(self, handle)
def spi_write(self, handle, data)
def wave_get_max_pulses(self)
def __init__(self, control, host, port)
def i2c_write_word_data(self, handle, reg, word_val)
def i2c_read_i2c_block_data(self, handle, reg, count)
def file_list(self, fpattern)
def set_bank_2(self, bits)
def i2c_write_byte_data(self, handle, reg, byte_val)
def wave_send_repeat(self, wave_id)
def i2c_block_process_call(self, handle, reg, data)
def wave_send_using_mode(self, wave_id, mode)
def __init__(self, value)
def _pigpio_command(sl, cmd, p1, p2)
def serial_write_byte(self, handle, byte_val)
def set_servo_pulsewidth(self, user_gpio, pulsewidth)
def wave_get_max_cbs(self)
def bb_serial_read(self, user_gpio)
def _tally(self, user_gpio, level, tick)
def run_script(self, script_id, params=None)
def file_write(self, handle, data)
def file_read(self, handle, count)
def i2c_read_byte(self, handle)
def event_trigger(self, event)
def wave_add_serial(self, user_gpio, baud, data, offset=0, bb_bits=8, bb_stop=2)
def clear_bank_1(self, bits)
def file_open(self, file_name, file_mode)
def get_PWM_dutycycle(self, user_gpio)
def notify_close(self, handle)
def gpio_trigger(self, user_gpio, pulse_len=10, level=1)
def set_mode(self, gpio, mode)
def get_servo_pulsewidth(self, user_gpio)
def set_glitch_filter(self, user_gpio, steady)
def __init__(self, gpio, edge, func)
def wave_add_generic(self, pulses)
def i2c_read_word_data(self, handle, reg)
def __init__(self, gpio_on, gpio_off, delay)
def get_PWM_range(self, user_gpio)
def get_PWM_real_range(self, user_gpio)
def __init__(self, event, func)
def set_pull_up_down(self, gpio, pud)
def bb_i2c_open(self, SDA, SCL, baud=100000)
def spi_xfer(self, handle, data)
def i2c_write_i2c_block_data(self, handle, reg, data)
def get_PWM_frequency(self, user_gpio)
def set_PWM_dutycycle(self, user_gpio, dutycycle)
def i2c_write_block_data(self, handle, reg, data)
def i2c_read_byte_data(self, handle, reg)
def i2c_read_block_data(self, handle, reg)
def callback(self, user_gpio, edge=RISING_EDGE, func=None)
def bb_spi_xfer(self, CS, data)
def clear_bank_2(self, bits)
def file_close(self, handle)
def update_script(self, script_id, params=None)
def bsc_xfer(self, bsc_control, data)
def wave_get_micros(self)
def script_status(self, script_id)
def i2c_read_device(self, handle, count)
def serial_write(self, handle, data)
def delete_script(self, script_id)
def set_watchdog(self, user_gpio, wdog_timeout)
def store_script(self, script)
def spi_read(self, handle, count)
def bb_spi_close(self, CS)
def set_PWM_range(self, user_gpio, range_)
def wave_get_pulses(self)
def serial_read_byte(self, handle)
def i2c_close(self, handle)
def stop_script(self, script_id)
def _pigpio_command_ext(sl, cmd, p1, p2, p3, extents)
def serial_data_available(self, handle)
def event_callback(self, event, func=None)
def custom_1(self, arg1=0, arg2=0, argx=[])
def i2c_open(self, i2c_bus, i2c_address, i2c_flags=0)
def get_pigpio_version(self)
def file_seek(self, handle, seek_offset, seek_from)
def bb_serial_read_open(self, user_gpio, baud, bb_bits=8)
def bb_i2c_zip(self, SDA, data)
def get_hardware_revision(self)
def set_bank_1(self, bits)