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

Working I2C master slave menu programs

parent e34e57c5
No related branches found
No related tags found
1 merge request!12Major update of menu visuals and example programs
Pipeline #108625 passed
from machine import I2C, Pin
from lib.hw_defs.pins import UTZ_I2C_SDA_PIN, UTZ_I2C_SCK_PIN, I2C_STRONG_PULL_RPIN
from lib.hw_defs.pins import UTZ_I2C_SDA_PIN, UTZ_I2C_SCK_PIN, I2C_STRONG_PULL_RPIN_BIT
from lib.robot_consts import I2C_MULTICUBE_FREQ
class I2C_master:
......@@ -12,7 +12,7 @@ class I2C_master:
self.start()
def start(self):
self.pcf_buttons.set_pin(I2C_STRONG_PULL_RPIN, False)
self.pcf_buttons.set_pin(I2C_STRONG_PULL_RPIN_BIT, False)
self.i2c = I2C(id=0,scl=Pin(UTZ_I2C_SCK_PIN),
sda=Pin(UTZ_I2C_SDA_PIN), freq=I2C_MULTICUBE_FREQ)
......@@ -43,4 +43,4 @@ class I2C_master:
def deinit(self):
self.i2c = None
self.pcf_buttons.set_pin(I2C_STRONG_PULL_RPIN, True)
\ No newline at end of file
self.pcf_buttons.set_pin(I2C_STRONG_PULL_RPIN_BIT, True)
\ No newline at end of file
......@@ -66,15 +66,24 @@ class I2C_slave():
self.received_data = None
self.send_data = None
self.read_timer = Timer(-1)
self.write_timer = Timer(-1)
self.read_timer.init(mode=Timer.PERIODIC, period=I2C_SLAVE_READ_PERIOD,
callback=self.read_message)
def read_message(self, timer):
if self.anyRead():
if self.any():
self.received_data = self.get()
if self.send_data:
#self.write_timer.init(mode=Timer.ONE_SHOT, period=0.0002,
# callback=self.write_message)
self.write_message(timer)
def write_message(self, timer):
if self.anyRead():
if self.send_data is not None:
self.put(self.send_data)
self.send_data = None
else:
self.put(0)
def read(self):
message = None
......@@ -84,7 +93,7 @@ class I2C_slave():
return message
def write(self, data):
self.put(data)
self.send_data = data
def anyRead(self):
......
......@@ -79,7 +79,7 @@ BAT_VOLTAGE_MIN = 6.2
BAT_VOLTAGE_TURNOFF = (BAT_VOLTAGE_MIN+0.1) # Volts
PCF_CHECK_PERIOD = const(50) # ms
I2C_SLAVE_READ_PERIOD = const(10) # ms
I2C_SLAVE_READ_PERIOD = const(1) # ms
I2C_FREQ = const(400000) # Hz
I2C_MULTICUBE_FREQ = const(100000)
......@@ -87,4 +87,4 @@ I2C_MULTICUBE_FREQ = const(100000)
ESP_BAUDRATE = const(115200) # bps
ESP32_COMMAND_PIN = const(8)
FW_VERSION = "17.10.2024" # Firmware version
\ No newline at end of file
FW_VERSION = "25.10.2024" # Firmware version
\ No newline at end of file
......@@ -2,9 +2,10 @@ import utime
from machine import I2C, Pin
from lib.robot_consts import Button
from lib.hw_defs.pins import UTZ_I2C_SDA_PIN, UTZ_I2C_SCK_PIN, I2C_STRONG_PULL_RPIN_BIT
ASCII_a = 97
ADDRESS = 0x45
ADDRESS = 0x44
def i2c_master_run(robot):
robot.init_i2c_master()
......@@ -19,39 +20,37 @@ def i2c_master_run(robot):
robot.display.show()
utime.sleep(0.2)
while ADDRESS not in robot.i2c_master.i2c.scan():
robot.display.centered_text(f"I2C master", 0, 1)
robot.display.centered_text(f"Slave", 18, 1)
robot.display.centered_text(f"not found", 28, 1)
robot.display.text('<', 0, 54, 1)
robot.display.show()
while True:
robot.display.fill(0)
robot.display.centered_text(f"I2C master", 0, 1)
scan = robot.i2c_master.i2c.scan()
print(scan)
if ADDRESS in scan:
connected = True
robot.display.text(f"Sending: {chr(data_sending+ASCII_a)}", 0, 16, 1)
robot.display.text(f"Received: {chr(data_received_parsed)}", 0, 24, 1)
robot.display.text('<', 0, 54, 1)
else:
connected = False
robot.display.centered_text(f"Slave", 16, 1)
robot.display.centered_text(f"not found", 24, 1)
robot.display.text('<', 0, 54, 1)
robot.display.text(f"Sending: {chr(data_sending+ASCII_a)}", 0, 18, 1)
robot.display.text(f"Received: {chr(data_received_parsed)}", 0, 28, 1)
robot.display.text('<', 0, 54, 1)
robot.display.show()
print("writing")
robot.i2c_master.write(ADDRESS, (data_sending + ASCII_a).to_bytes(1, 'big'))
#data_received = robot.i2c_master.read(ADDRESS, 1)
#if data_received:
# data_received_parsed = int.from_bytes(data_received, 'big')
data_received = robot.i2c_master.read(ADDRESS, 1)
if data_received and data_received != b'\x00':
data_received_parsed = int.from_bytes(data_received, 'big')
buttons = robot.buttons.pressed()
if buttons[Button.LEFT]:
break
if not debounce:
if buttons[Button.UP]:
robot.buttons.set_pin(I2C_STRONG_PULL_RPIN_BIT, True)
data_sending -= 1
data_sending = data_sending % 26
debounce = True
if buttons[Button.DOWN]:
robot.buttons.set_pin(I2C_STRONG_PULL_RPIN_BIT, False)
data_sending += 1
data_sending = data_sending % 26
debounce = True
......
import utime
from machine import mem32,mem8,Pin
from lib.robot_consts import Button
from lib.hw_defs.pins import UTZ_I2C_SDA_PIN, UTZ_I2C_SCK_PIN, I2C_STRONG_PULL_RPIN
ASCII_a = 97
ADDRESS = 0x45
ADDRESS = 0x44
def i2c_slave_run(robot):
robot.init_i2c_slave(ADDRESS)
data_received = 0x5F
data_received = None
data_show = 0x5F
data_sending = 0
debounce = False
......@@ -20,16 +23,17 @@ def i2c_slave_run(robot):
robot.display.fill(0)
robot.display.centered_text(f"I2C slave", 0, 1)
robot.display.text(f"Sending: {chr(data_sending+ASCII_a)}", 0, 16, 1)
robot.display.text(f"Received: {chr(data_received)}", 0, 24, 1)
robot.display.text(f"Received: {chr(data_show)}", 0, 24, 1)
robot.display.text('<', 0, 54, 1)
robot.display.show()
data_received = robot.i2c_slave.read()
if data_received:
print(data_received)
data_show = data_received
robot.i2c_slave.write(data_sending + ASCII_a)
else:
data_received = 0x5F
buttons = robot.buttons.pressed()
if buttons[Button.LEFT]:
break
......
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