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

Add i2c master slave menu programs

parent 6641f497
No related branches found
No related tags found
1 merge request!12Major update of menu visuals and example programs
......@@ -55,6 +55,7 @@ class Esp:
self.uart_timer.init(mode=Timer.PERIODIC, period=10, callback=self.UART_RX_handler)
self.pcf_buttons = pcf_buttons
# CONFIG
self.name = ""
self.header = 0
self.length = 0
self.message_idx = 0
......@@ -263,6 +264,7 @@ class Esp:
return response
def set_name(self, name: str, timeout=50):
self.name = name
self.cmd_start()
response = self.repeat_str(Esp.COM_NAME, name, timeout)
self.cmd_stop()
......@@ -270,11 +272,15 @@ class Esp:
return response
def set_name_nonblocking(self, name: str, timeout=50):
self.name = name
self.cmd_start()
response = self.repeat_str(Esp.COM_NAME, name, timeout)
self.cmd_stop()
return response
def get_name(self):
return self.name
def set_password(self, password: str, timeout=50):
self.cmd_start()
response = self.repeat_str(Esp.COM_PASSWORD, password, timeout)
......
......@@ -85,16 +85,10 @@ def display_fill_sen_test(robot, current_sen_test, sensor_test_menu, sensor_test
display_fill_arrows(robot)
# Fill framebuffer with cube test menu
def display_fill_cube_test(robot, current_oth_test):
def display_fill_cube_test(robot, current_cube_test, cube_test_menu_name):
robot.display.fill(0)
robot.display.centered_text('< Cube test >', DISPLAY_TITLE_POS, 1)
if current_oth_test == 0:
test_name = "Gyro Acc"
elif current_oth_test == 1:
test_name = "Utility"
elif current_oth_test == 2:
test_name = "Wifi webserver"
robot.display.centered_text(test_name, DISPLAY_PROG_POS, 1)
robot.display.centered_text(cube_test_menu_name, DISPLAY_PROG_POS, 1)
display_fill_arrows(robot)
def display_fill_arrows(robot):
......@@ -103,7 +97,7 @@ def display_fill_arrows(robot):
def display_fill_fw_version(robot, fw_version):
robot.display.fill(0)
robot.display.centered_text("Firmware", DISPLAY_TITLE_POS, 1)
robot.display.centered_text("< Firmware >", DISPLAY_TITLE_POS, 1)
robot.display.centered_text(fw_version, DISPLAY_PROG_POS, 1)
# Show program error on display
......
......@@ -35,9 +35,14 @@ def main():
('OC Laser', menu_programs.oc_laser_run), ('OC Ultrasonic', menu_programs.oc_ultra_run))
MENU_PROGRAM_NXT_UTZ_IDX = 3
menu_programs_functions_size = len(menu_programs_functions)
menu_cube_programs_functions = (('Gyro Acc', menu_programs.cube_gyro_acc_run), ('Utility', menu_programs.cube_utility_run),
('Wifi webserver', menu_programs.esp_wifi_run), ('I2C Master', menu_programs.i2c_master_run),
('I2C Slave', menu_programs.i2c_slave_run))
menu_cube_programs_functions_size = len(menu_cube_programs_functions)
menu_list = [0, 0, 0, 0, 0, 0, 0]
MENU_CUBE_TESTS = 3
menu_list_size = [0, Port.M_COUNT, menu_programs_functions_size, MENU_CUBE_TESTS, 1, 1, Port.S_COUNT]
menu_list_size = [0, Port.M_COUNT, menu_programs_functions_size, menu_cube_programs_functions_size, 1, 1, Port.S_COUNT]
SENSOR_TEST_MENU_PORT_IDX = 6
def log_error(e):
......@@ -286,20 +291,14 @@ def main():
continue
elif current_menu == Menu.CUBE_TEST:
display_fill_cube_test(robot, menu_list[current_menu])
cube_test_menu_program = menu_list[current_menu]
cube_test_menu_name = menu_cube_programs_functions[cube_test_menu_program][0]
display_fill_cube_test(robot, menu_list[current_menu], cube_test_menu_name)
if ok_pressed:
robot_state = 1
try:
if menu_list[current_menu] == 0:
print("Gyro Acc test")
menu_programs.cube_gyro_acc_run(robot)
elif menu_list[current_menu] == 1:
print("Utility tets")
menu_programs.cube_utility_run(robot)
elif menu_list[current_menu] == 2:
print("Wifi webserver test")
menu_programs.esp_wifi_run(robot, esp_default_name)
robot.esp.esp_running = False
print("Running cube test: ", cube_test_menu_name)
menu_cube_programs_functions[cube_test_menu_program][1](robot)
except Exception as e:
sys.print_exception(e)
log_error(e)
......
......@@ -2,6 +2,8 @@
from .cube_gyro_acc import cube_gyro_acc_run
from .cube_utility import cube_utility_run
from .esp_wifi import esp_wifi_run
from .i2c_master import i2c_master_run
from .i2c_slave import i2c_slave_run
from .motor import motor_run
from .nxt_light import nxt_light_run
from .nxt_sound import nxt_sound_run
......
......@@ -7,8 +7,9 @@ import random
from lib.robot_consts import Button
def esp_wifi_run(robot, wifi_name):
def esp_wifi_run(robot):
wifi_password = "12345678"
wifi_name = robot.esp.get_name()
counter = 0
indicators = [True, False, True, False, False]
labels = ["1", "test1", "test2", "", "123"]
......@@ -33,7 +34,7 @@ def esp_wifi_run(robot, wifi_name):
robot.display.show()
utime.sleep(2)
return
print("Name: ", robot.esp.set_name(wifi_name))
print("Name: ", wifi_name)
print("Pass: ", robot.esp.set_password(wifi_password))
robot.esp.wifi_set_indicators_labels(("1", "", "test2", "test3", "123"))
......@@ -67,4 +68,5 @@ def esp_wifi_run(robot, wifi_name):
counter +=1
buttons = robot.buttons.pressed()
if buttons[Button.LEFT]:
run = False
\ No newline at end of file
run = False
robot.esp.esp_running = False
\ No newline at end of file
import utime
from machine import I2C, Pin
from lib.robot_consts import Button
ASCII_a = 97
def i2c_master_run(robot):
i2c = I2C(id=0,scl=Pin(17), sda=Pin(16), freq=100000)
data_received = 0x5F
data_sending = 0
robot.display.fill(0)
robot.display.centered_text(f"I2C master", 0, 1)
robot.display.text('<', 0, 54, 1)
robot.display.show()
while True:
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('<', 0, 54, 1)
robot.display.show()
i2c.writeto(0x41, data_sending + ASCII_a)
data_received = int.from_bytes(i2c.readfrom(0x41, 1), 'big')
buttons = robot.buttons.pressed()
if buttons[Button.LEFT]:
break
if not debounce:
if buttons[Button.UP]:
data_sending += 1
data_sending = data_sending % 26
debounce = True
if buttons[Button.DOWN]:
data_sending -= 1
data_sending = data_sending % 26
debounce = True
if not buttons[Button.UP] and not buttons[Button.DOWN]:
debounce = False
utime.sleep(0.1)
\ No newline at end of file
import utime
from machine import mem32,mem8,Pin
from lib.robot_consts import Button
ASCII_a = 97
class i2c_slave():
I2C0_BASE = 0x40044000
I2C1_BASE = 0x40048000
IO_BANK0_BASE = 0x40014000
mem_rw = 0x0000
mem_xor = 0x1000
mem_set = 0x2000
mem_clr = 0x3000
IC_CON = 0
IC_TAR = 4
IC_SAR = 8
IC_DATA_CMD = 0x10
IC_RAW_INTR_STAT = 0x34
IC_RX_TL = 0x38
IC_TX_TL = 0x3C
IC_CLR_INTR = 0x40
IC_CLR_RD_REQ = 0x50
IC_CLR_TX_ABRT = 0x54
IC_ENABLE = 0x6c
IC_STATUS = 0x70
def write_reg(self, reg, data, method=0):
mem32[ self.i2c_base | method | reg] = data
def set_reg(self, reg, data):
self.write_reg(reg, data, method=self.mem_set)
def clr_reg(self, reg, data):
self.write_reg(reg, data, method=self.mem_clr)
def __init__(self, i2cID = 0, sda=16, scl=17, slaveAddress=0x41):
self.scl = scl
self.sda = sda
self.slaveAddress = slaveAddress
self.i2c_ID = i2cID
if self.i2c_ID == 0:
self.i2c_base = self.I2C0_BASE
else:
self.i2c_base = self.I2C1_BASE
# 1 Disable DW_apb_i2c
self.clr_reg(self.IC_ENABLE, 1)
# 2 set slave address
# clr bit 0 to 9
# set slave address
self.clr_reg(self.IC_SAR, 0x1ff)
self.set_reg(self.IC_SAR, self.slaveAddress &0x1ff)
# 3 write IC_CON 7 bit, enable in slave-only
self.clr_reg(self.IC_CON, 0b01001001)
# set SDA PIN
mem32[ self.IO_BANK0_BASE | self.mem_clr | ( 4 + 8 * self.sda) ] = 0x1f
mem32[ self.IO_BANK0_BASE | self.mem_set | ( 4 + 8 * self.sda) ] = 3
# set SLA PIN
mem32[ self.IO_BANK0_BASE | self.mem_clr | ( 4 + 8 * self.scl) ] = 0x1f
mem32[ self.IO_BANK0_BASE | self.mem_set | ( 4 + 8 * self.scl) ] = 3
# 4 enable i2c
self.set_reg(self.IC_ENABLE, 1)
def anyRead(self):
status = mem32[ self.i2c_base | self.IC_RAW_INTR_STAT] & 0x20
if status :
return True
return False
def put(self, data):
# reset flag
self.clr_reg(self.IC_CLR_TX_ABRT,1)
status = mem32[ self.i2c_base | self.IC_CLR_RD_REQ]
mem32[ self.i2c_base | self.IC_DATA_CMD] = data & 0xff
def any(self):
# get IC_STATUS
status = mem32[ self.i2c_base | self.IC_STATUS]
# check RFNE receive fifio not empty
if (status & 8) :
return True
return False
def get(self):
while not self.any():
pass
return mem32[ self.i2c_base | self.IC_DATA_CMD] & 0xff
def i2c_slave_run(robot):
i2c_slave = i2c_slave()
data_received = 0x5F
data_sending = 0
robot.display.fill(0)
robot.display.centered_text(f"I2C slave", 0, 1)
robot.display.text('<', 0, 54, 1)
robot.display.show()
while True:
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('<', 0, 54, 1)
robot.display.show()
if i2c_slave.anyRead():
data_received = i2c_slave.get()
i2c_slave.put(data_sending + ASCII_a)
buttons = robot.buttons.pressed()
if buttons[Button.LEFT]:
break
if not debounce:
if buttons[Button.UP]:
data_sending += 1
data_sending = data_sending % 26
debounce = True
if buttons[Button.DOWN]:
data_sending -= 1
data_sending = data_sending % 26
debounce = True
if not buttons[Button.UP] and not buttons[Button.DOWN]:
debounce = False
utime.sleep(0.1)
\ No newline at end of file
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