V67: use with for Python locks, show_errors flag

This commit is contained in:
joan 2018-03-20 18:39:50 +00:00
parent 424ce5af6a
commit 934874be2f
5 changed files with 120 additions and 150 deletions

View File

@ -26,7 +26,7 @@ For more information, please refer to <http://unlicense.org/>
*/ */
/* /*
This version is for pigpio version 66+ This version is for pigpio version 67+
*/ */
#include <stdio.h> #include <stdio.h>
@ -562,6 +562,7 @@ static errInfo_t errInfo[]=
{PI_BAD_SPI_BAUD , "bad SPI baud rate, not 50-500k"}, {PI_BAD_SPI_BAUD , "bad SPI baud rate, not 50-500k"},
{PI_NOT_SPI_GPIO , "no bit bang SPI in progress on GPIO"}, {PI_NOT_SPI_GPIO , "no bit bang SPI in progress on GPIO"},
{PI_BAD_EVENT_ID , "bad event id"}, {PI_BAD_EVENT_ID , "bad event id"},
{PI_CMD_INTERRUPTED , "command interrupted, Python"},
}; };

View File

@ -7051,7 +7051,7 @@ If the board revision is not recognised then GPIO 2-27 are allowed.
.br .br
.br .br
Unknown board PI_DEFAULT_UPDATE_MASK_UNKNOWN 0xFFFFFFFF Unknown board PI_DEFAULT_UPDATE_MASK_UNKNOWN 0x0FFFFFFC
.br .br
.br .br
Type 1 board PI_DEFAULT_UPDATE_MASK_B1 0x03E6CF93 Type 1 board PI_DEFAULT_UPDATE_MASK_B1 0x03E6CF93
@ -10303,6 +10303,8 @@ A 16-bit word value.
.br .br
#define PI_BAD_EVENT_ID -143 // bad event id #define PI_BAD_EVENT_ID -143 // bad event id
.br .br
#define PI_CMD_INTERRUPTED -144 // Used by Python
.br
.br .br
#define PI_PIGIF_ERR_0 -2000 #define PI_PIGIF_ERR_0 -2000

View File

@ -31,7 +31,7 @@ For more information, please refer to <http://unlicense.org/>
#include <stdint.h> #include <stdint.h>
#include <pthread.h> #include <pthread.h>
#define PIGPIO_VERSION 6703 #define PIGPIO_VERSION 67
/*TEXT /*TEXT
@ -4779,7 +4779,7 @@ added to the mask.
If the board revision is not recognised then GPIO 2-27 are allowed. If the board revision is not recognised then GPIO 2-27 are allowed.
Unknown board @ PI_DEFAULT_UPDATE_MASK_UNKNOWN @ 0xFFFFFFFF Unknown board @ PI_DEFAULT_UPDATE_MASK_UNKNOWN @ 0x0FFFFFFC
Type 1 board @ PI_DEFAULT_UPDATE_MASK_B1 @ 0x03E6CF93 Type 1 board @ PI_DEFAULT_UPDATE_MASK_B1 @ 0x03E6CF93
Type 2 board @ PI_DEFAULT_UPDATE_MASK_A_B2 @ 0xFBC6CF9C Type 2 board @ PI_DEFAULT_UPDATE_MASK_A_B2 @ 0xFBC6CF9C
Type 3 board @ PI_DEFAULT_UPDATE_MASK_R3 @ 0x0FFFFFFC Type 3 board @ PI_DEFAULT_UPDATE_MASK_R3 @ 0x0FFFFFFC
@ -6406,6 +6406,7 @@ after this command is issued.
#define PI_BAD_SPI_BAUD -141 // bad SPI baud rate, not 50-500k #define PI_BAD_SPI_BAUD -141 // bad SPI baud rate, not 50-500k
#define PI_NOT_SPI_GPIO -142 // no bit bang SPI in progress on GPIO #define PI_NOT_SPI_GPIO -142 // no bit bang SPI in progress on GPIO
#define PI_BAD_EVENT_ID -143 // bad event id #define PI_BAD_EVENT_ID -143 // bad event id
#define PI_CMD_INTERRUPTED -144 // Used by Python
#define PI_PIGIF_ERR_0 -2000 #define PI_PIGIF_ERR_0 -2000
#define PI_PIGIF_ERR_99 -2099 #define PI_PIGIF_ERR_99 -2099

254
pigpio.py
View File

@ -300,7 +300,7 @@ import threading
import os import os
import atexit import atexit
VERSION = "1.40" VERSION = "1.41"
exceptions = True exceptions = True
@ -688,6 +688,7 @@ PI_BAD_SCRIPT_NAME =-140
PI_BAD_SPI_BAUD =-141 PI_BAD_SPI_BAUD =-141
PI_NOT_SPI_GPIO =-142 PI_NOT_SPI_GPIO =-142
PI_BAD_EVENT_ID =-143 PI_BAD_EVENT_ID =-143
PI_CMD_INTERRUPTED =-144
# pigpio error text # pigpio error text
@ -833,6 +834,7 @@ _errors=[
[PI_BAD_SPI_BAUD , "bad SPI baud rate, not 50-500k"], [PI_BAD_SPI_BAUD , "bad SPI baud rate, not 50-500k"],
[PI_NOT_SPI_GPIO , "no bit bang SPI in progress on GPIO"], [PI_NOT_SPI_GPIO , "no bit bang SPI in progress on GPIO"],
[PI_BAD_EVENT_ID , "bad event id"], [PI_BAD_EVENT_ID , "bad event id"],
[PI_CMD_INTERRUPTED , "pigpio command interrupted"],
] ]
_except_a = "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n{}" _except_a = "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n{}"
@ -982,12 +984,10 @@ def _pigpio_command(sl, cmd, p1, p2):
p1:= command parameter 1 (if applicable). p1:= command parameter 1 (if applicable).
p2:= command parameter 2 (if applicable). p2:= command parameter 2 (if applicable).
""" """
sl.l.acquire() res = PI_CMD_INTERRUPTED
try: with sl.l:
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0)) sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN)) dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
finally:
sl.l.release()
return res return res
def _pigpio_command_nolock(sl, cmd, p1, p2): def _pigpio_command_nolock(sl, cmd, p1, p2):
@ -999,6 +999,7 @@ def _pigpio_command_nolock(sl, cmd, p1, p2):
p1:= command parameter 1 (if applicable). p1:= command parameter 1 (if applicable).
p2:= command parameter 2 (if applicable). p2:= command parameter 2 (if applicable).
""" """
res = PI_CMD_INTERRUPTED
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0)) sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN)) dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
return res return res
@ -1020,12 +1021,10 @@ def _pigpio_command_ext(sl, cmd, p1, p2, p3, extents):
ext.extend(_b(x)) ext.extend(_b(x))
else: else:
ext.extend(x) ext.extend(x)
sl.l.acquire() res = PI_CMD_INTERRUPTED
try: with sl.l:
sl.s.sendall(ext) sl.s.sendall(ext)
dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN)) dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
finally:
sl.l.release()
return res return res
def _pigpio_command_ext_nolock(sl, cmd, p1, p2, p3, extents): def _pigpio_command_ext_nolock(sl, cmd, p1, p2, p3, extents):
@ -1039,6 +1038,7 @@ def _pigpio_command_ext_nolock(sl, cmd, p1, p2, p3, extents):
p3:= total size in bytes of following extents p3:= total size in bytes of following extents
extents:= additional data blocks extents:= additional data blocks
""" """
res = PI_CMD_INTERRUPTED
ext = bytearray(struct.pack('IIII', cmd, p1, p2, p3)) ext = bytearray(struct.pack('IIII', cmd, p1, p2, p3))
for x in extents: for x in extents:
if type(x) == type(""): if type(x) == type(""):
@ -2907,17 +2907,14 @@ class pi():
# process read failure # process read failure
... ...
""" """
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_nolock( bytes = u2i(_pigpio_command_nolock(
self.sl, _PI_CMD_I2CRK, handle, reg)) self.sl, _PI_CMD_I2CRK, handle, reg))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def i2c_block_process_call(self, handle, reg, data): def i2c_block_process_call(self, handle, reg, data):
""" """
@ -2960,17 +2957,14 @@ class pi():
## extension ## ## extension ##
# s len data bytes # s len data bytes
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_I2CPK, handle, reg, len(data), [data])) self.sl, _PI_CMD_I2CPK, handle, reg, len(data), [data]))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def i2c_write_i2c_block_data(self, handle, reg, data): def i2c_write_i2c_block_data(self, handle, reg, data):
""" """
@ -3040,17 +3034,14 @@ class pi():
# I count # I count
extents = [struct.pack("I", count)] extents = [struct.pack("I", count)]
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_I2CRI, handle, reg, 4, extentse)) self.sl, _PI_CMD_I2CRI, handle, reg, 4, extents))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def i2c_read_device(self, handle, count): def i2c_read_device(self, handle, count):
""" """
@ -3073,17 +3064,14 @@ class pi():
(count, data) = pi.i2c_read_device(h, 12) (count, data) = pi.i2c_read_device(h, 12)
... ...
""" """
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i( bytes = u2i(
_pigpio_command_nolock(self.sl, _PI_CMD_I2CRD, handle, count)) _pigpio_command_nolock(self.sl, _PI_CMD_I2CRD, handle, count))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def i2c_write_device(self, handle, data): def i2c_write_device(self, handle, data):
""" """
@ -3177,17 +3165,14 @@ class pi():
## extension ## ## extension ##
# s len data bytes # s len data bytes
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_I2CZ, handle, 0, len(data), [data])) self.sl, _PI_CMD_I2CZ, handle, 0, len(data), [data]))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def bb_spi_open(self, CS, MISO, MOSI, SCLK, baud=100000, spi_flags=0): def bb_spi_open(self, CS, MISO, MOSI, SCLK, baud=100000, spi_flags=0):
@ -3348,17 +3333,14 @@ class pi():
## extension ## ## extension ##
# s len data bytes # s len data bytes
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_BSPIX, CS, 0, len(data), [data])) self.sl, _PI_CMD_BSPIX, CS, 0, len(data), [data]))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def bb_i2c_open(self, SDA, SCL, baud=100000): def bb_i2c_open(self, SDA, SCL, baud=100000):
@ -3487,17 +3469,14 @@ class pi():
## extension ## ## extension ##
# s len data bytes # s len data bytes
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_BI2CZ, SDA, 0, len(data), [data])) self.sl, _PI_CMD_BI2CZ, SDA, 0, len(data), [data]))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def event_trigger(self, event): def event_trigger(self, event):
""" """
@ -3619,22 +3598,21 @@ class pi():
## extension ## ## extension ##
# s len data bytes # s len data bytes
self.sl.l.acquire() status = PI_CMD_INTERRUPTED
try: bytes = 0
rdata = bytearray(b'')
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_BSCX, bsc_control, 0, len(data), [data])) self.sl, _PI_CMD_BSCX, bsc_control, 0, len(data), [data]))
if bytes > 0: if bytes > 0:
rx = self._rxbuf(bytes) rx = self._rxbuf(bytes)
status = struct.unpack('I', rx[0:4])[0] status = struct.unpack('I', rx[0:4])[0]
bytes -= 4 bytes -= 4
data = rx[4:] rdata = rx[4:]
else: else:
status = bytes status = bytes
bytes = 0 bytes = 0
data = bytearray(b'') return status, bytes, rdata
finally:
self.sl.l.release()
return status, bytes, data
def bsc_i2c(self, i2c_address, data=[]): def bsc_i2c(self, i2c_address, data=[]):
""" """
@ -3888,17 +3866,14 @@ class pi():
# error path # error path
... ...
""" """
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_nolock( bytes = u2i(_pigpio_command_nolock(
self.sl, _PI_CMD_SPIR, handle, count)) self.sl, _PI_CMD_SPIR, handle, count))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def spi_write(self, handle, data): def spi_write(self, handle, data):
""" """
@ -3954,17 +3929,14 @@ class pi():
## extension ## ## extension ##
# s len data bytes # s len data bytes
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_SPIX, handle, 0, len(data), [data])) self.sl, _PI_CMD_SPIX, handle, 0, len(data), [data]))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def serial_open(self, tty, baud, ser_flags=0): def serial_open(self, tty, baud, ser_flags=0):
""" """
@ -4062,17 +4034,14 @@ class pi():
# process read data # process read data
... ...
""" """
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i( bytes = u2i(
_pigpio_command_nolock(self.sl, _PI_CMD_SERR, handle, count)) _pigpio_command_nolock(self.sl, _PI_CMD_SERR, handle, count))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def serial_write(self, handle, data): def serial_write(self, handle, data):
""" """
@ -4325,8 +4294,9 @@ class pi():
(s, pars) = pi.script_status(sid) (s, pars) = pi.script_status(sid)
... ...
""" """
self.sl.l.acquire() status = PI_CMD_INTERRUPTED
try: params = ()
with self.sl.l:
bytes = u2i( bytes = u2i(
_pigpio_command_nolock(self.sl, _PI_CMD_PROCP, script_id, 0)) _pigpio_command_nolock(self.sl, _PI_CMD_PROCP, script_id, 0))
if bytes > 0: if bytes > 0:
@ -4336,9 +4306,6 @@ class pi():
params = pars[1:] params = pars[1:]
else: else:
status = bytes status = bytes
params = ()
finally:
self.sl.l.release()
return status, params return status, params
def stop_script(self, script_id): def stop_script(self, script_id):
@ -4418,17 +4385,14 @@ class pi():
(count, data) = pi.bb_serial_read(4) (count, data) = pi.bb_serial_read(4)
... ...
""" """
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i( bytes = u2i(
_pigpio_command_nolock(self.sl, _PI_CMD_SLR, user_gpio, 10000)) _pigpio_command_nolock(self.sl, _PI_CMD_SLR, user_gpio, 10000))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def bb_serial_read_close(self, user_gpio): def bb_serial_read_close(self, user_gpio):
@ -4522,17 +4486,14 @@ class pi():
## extension ## ## extension ##
# s len argx bytes # s len argx bytes
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_CF2, arg1, retMax, len(argx), [argx])) self.sl, _PI_CMD_CF2, arg1, retMax, len(argx), [argx]))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def get_pad_strength(self, pad): def get_pad_strength(self, pad):
""" """
@ -4724,17 +4685,14 @@ class pi():
# process read data # process read data
... ...
""" """
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i( bytes = u2i(
_pigpio_command_nolock(self.sl, _PI_CMD_FR, handle, count)) _pigpio_command_nolock(self.sl, _PI_CMD_FR, handle, count))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def file_write(self, handle, data): def file_write(self, handle, data):
""" """
@ -4832,17 +4790,14 @@ class pi():
## extension ## ## extension ##
# s len data bytes # s len data bytes
self.sl.l.acquire() bytes = PI_CMD_INTERRUPTED
try: rdata = ""
with self.sl.l:
bytes = u2i(_pigpio_command_ext_nolock( bytes = u2i(_pigpio_command_ext_nolock(
self.sl, _PI_CMD_FL, 60000, 0, len(fpattern), [fpattern])) self.sl, _PI_CMD_FL, 60000, 0, len(fpattern), [fpattern]))
if bytes > 0: if bytes > 0:
data = self._rxbuf(bytes) rdata = self._rxbuf(bytes)
else: return bytes, rdata
data = ""
finally:
self.sl.l.release()
return bytes, data
def shell(self, shellscr, pstring=""): def shell(self, shellscr, pstring=""):
""" """
@ -5048,7 +5003,8 @@ class pi():
def __init__(self, def __init__(self,
host = os.getenv("PIGPIO_ADDR", 'localhost'), host = os.getenv("PIGPIO_ADDR", 'localhost'),
port = os.getenv("PIGPIO_PORT", 8888)): port = os.getenv("PIGPIO_PORT", 8888),
show_errors = True):
""" """
Grants access to a Pi's GPIO. Grants access to a Pi's GPIO.
@ -5106,6 +5062,7 @@ class pi():
exception = 2 exception = 2
except error: except error:
# assumed to be no handle available
exception = 3 exception = 3
else: else:
@ -5119,16 +5076,19 @@ class pi():
if self.sl.s is not None: if self.sl.s is not None:
self.sl.s = None self.sl.s = None
s = "Can't connect to pigpio at {}({})".format(host, str(port)) if show_errors:
print(_except_a.format(s)) s = "Can't connect to pigpio at {}({})".format(host, str(port))
if exception == 1:
print(_except_1)
elif exception == 2: print(_except_a.format(s))
print(_except_2) if exception == 1:
else: print(_except_1)
print(_except_3) elif exception == 2:
print(_except_z) print(_except_2)
else:
print(_except_3)
print(_except_z)
def stop(self): def stop(self):
"""Release pigpio resources. """Release pigpio resources.
@ -5362,6 +5322,7 @@ def xref():
PI_BAD_SPI_BAUD = -141 PI_BAD_SPI_BAUD = -141
PI_NOT_SPI_GPIO = -142 PI_NOT_SPI_GPIO = -142
PI_BAD_EVENT_ID = -143 PI_BAD_EVENT_ID = -143
PI_CMD_INTERRUPTED = -144
. . . .
event:0-31 event:0-31
@ -5611,6 +5572,11 @@ def xref():
The name of a shell script. The script must exist The name of a shell script. The script must exist
in /opt/pigpio/cgi and must be executable. in /opt/pigpio/cgi and must be executable.
show_errors:
Controls the display of pigpio daemon connection failures.
The default of True prints the probable failure reasons to
standard output.
spi_*: spi_*:
One of the spi_ functions. One of the spi_ functions.

View File

@ -3,7 +3,7 @@
from distutils.core import setup from distutils.core import setup
setup(name='pigpio', setup(name='pigpio',
version='1.40', version='1.41',
author='joan', author='joan',
author_email='joan@abyz.me.uk', author_email='joan@abyz.me.uk',
maintainer='joan', maintainer='joan',