Skip to content
Snippets Groups Projects
Commit b18a6e69 authored by Václav Jelínek's avatar Václav Jelínek
Browse files

Add function to trigger all I2C slaves

parent e976f1d9
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,7 @@ from lib.hw_defs.pins import UTZ_I2C_SDA_PIN, UTZ_I2C_SCK_PIN, I2C_STRONG_PULL_R
from lib.robot_consts import I2C_MULTICUBE_FREQ, I2C_NXT_UTZ_BUS
I2C_TIME_TRIGGER_REG = const(256-4)
I2C_TIME_TRIGGER_WRITE_TIME = const(900) # us
class I2C_master:
def __init__(self, pcf_buttons):
self.pcf_buttons = pcf_buttons
......@@ -38,11 +38,11 @@ class I2C_master:
except Exception as e:
return False
return True
def trigger(self, add, time_us, callback=None):
def trigger(self, time_us, address, callback=None):
try:
time_start = time.ticks_us()
self.i2c.writeto_mem(add, I2C_TIME_TRIGGER_REG, time_us.to_bytes(4, 'little'))
self.i2c.writeto_mem(address, I2C_TIME_TRIGGER_REG, time_us.to_bytes(4, 'little'))
time_diff = time_us-time.ticks_diff(time.ticks_us(), time_start)
print("Time diff: ", time_diff)
if callback:
......@@ -55,6 +55,31 @@ class I2C_master:
return False
return True
def trigger_all(self, time_us, addresses=None, callback=None):
try:
time_start = time.ticks_us()
if not addresses:
addresses = self.i2c.scan()
time_scan = time_start-time.ticks_us()
time_reserve = time_scan - len(addresses)*I2C_TIME_TRIGGER_WRITE_TIME
if time_reserve < 0:
return False
time_next = time_scan
for i in range(len(addresses)):
time_slave = time.ticks_us()
self.i2c.writeto_mem(addresses[0], I2C_TIME_TRIGGER_REG, time_next.to_bytes(4, 'little'))
time_next = time_next - time.ticks_diff(time.ticks_us(), time_slave)
print("Time diff: ", time_next)
if callback:
if time_next > 0:
self.trigger_timer.init(freq=1000000/time_next, mode=Timer.ONE_SHOT, callback=callback)
else:
callback(None)
except Exception as e:
return False
return True
def deinit(self):
self.trigger_timer.deinit()
self.i2c = None
......
......@@ -80,7 +80,9 @@ def i2c_master_run(robot):
data_sending = data_sending % 26
debounce = True
if buttons[Button.OK]:
robot.i2c_master.trigger(slave_add, SLAVE_TRIGGER_TIME, callback=master_trigger_callback)
trigger = robot.i2c_master.trigger_all(SLAVE_TRIGGER_TIME, adressed=all_add, callback=master_trigger_callback)
if not trigger:
break
debounce = True
if buttons[Button.RIGHT]:
#robot.buttons.set_pin(I2C_STRONG_PULL_RPIN_BIT, True)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment