mirror of
https://github.com/pommi/python-itho-wpu.git
synced 2024-11-21 13:52:15 +01:00
feat(itho-wpu): cli arguments to run master/slave only + set slave timeout
--master-only allows to only run the I2C master functionality. --slave-only allows to only run the I2C slave functionality. The I2C slave functionality via the "set_callback" mechanism (--type callback) just executes the pigpio "event_callback()" function and waits for 2 seconds for a response. The amount of seconds can now be configured via --slave-timeout.
This commit is contained in:
parent
ff3c705608
commit
f989b61789
33
itho-wpu.py
33
itho-wpu.py
@ -34,6 +34,10 @@ def parse_args():
|
|||||||
parser.add_argument('--loglevel', nargs='?',
|
parser.add_argument('--loglevel', nargs='?',
|
||||||
choices=["debug", "info", "warning", "error", "critical"],
|
choices=["debug", "info", "warning", "error", "critical"],
|
||||||
help="Loglevel")
|
help="Loglevel")
|
||||||
|
parser.add_argument('--master-only', action='store_true', help="Only run I2C master")
|
||||||
|
parser.add_argument('--slave-only', action='store_true', help="Only run I2C slave")
|
||||||
|
parser.add_argument('--slave-timeout', nargs='?', type=int, default=2,
|
||||||
|
help="Slave timeout in seconds.")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
return args
|
return args
|
||||||
@ -47,11 +51,12 @@ class I2CSlave():
|
|||||||
logger.error("not pi.connected")
|
logger.error("not pi.connected")
|
||||||
return
|
return
|
||||||
|
|
||||||
def set_callback(self, q):
|
def set_callback(self, q, timeout):
|
||||||
logger.debug("set_callback()")
|
logger.debug("set_callback()")
|
||||||
e = self.pi.event_callback(pigpio.EVENT_BSC, self.callback)
|
e = self.pi.event_callback(pigpio.EVENT_BSC, self.callback)
|
||||||
self.pi.bsc_i2c(self.address)
|
self.pi.bsc_i2c(self.address)
|
||||||
time.sleep(2)
|
logger.debug(f"Waiting {timeout}s for activity")
|
||||||
|
time.sleep(timeout)
|
||||||
e.cancel()
|
e.cancel()
|
||||||
self.pi.bsc_i2c(0) # Disable BSC peripheral
|
self.pi.bsc_i2c(0) # Disable BSC peripheral
|
||||||
self.pi.stop()
|
self.pi.stop()
|
||||||
@ -117,18 +122,20 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
q = queue.Queue()
|
q = queue.Queue()
|
||||||
|
|
||||||
slave = I2CSlave(address=0x40)
|
if not args.master_only:
|
||||||
if args.type == 'callback':
|
slave = I2CSlave(address=0x40)
|
||||||
slave_thread = threading.Thread(target=slave.set_callback, args=[q])
|
if args.type == 'callback':
|
||||||
elif args.type == 'wait':
|
slave_thread = threading.Thread(target=slave.set_callback, args=[q, args.slave_timeout])
|
||||||
slave_thread = threading.Thread(target=slave.wait, args=[q])
|
elif args.type == 'wait':
|
||||||
slave_thread.start()
|
slave_thread = threading.Thread(target=slave.wait, args=[q])
|
||||||
|
slave_thread.start()
|
||||||
|
|
||||||
master = I2CMaster(address=0x41, bus=1)
|
if not args.slave_only:
|
||||||
if args.action:
|
master = I2CMaster(address=0x41, bus=1)
|
||||||
master.execute_action(args.action)
|
if args.action:
|
||||||
master.close()
|
master.execute_action(args.action)
|
||||||
|
master.close()
|
||||||
|
|
||||||
if args.type == 'wait':
|
if args.type == 'wait' and not args.master_only:
|
||||||
result = q.get()
|
result = q.get()
|
||||||
logger.info(f"Response: {result}")
|
logger.info(f"Response: {result}")
|
||||||
|
Loading…
Reference in New Issue
Block a user