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

Move esp command pin control to esp_config

parent ebcf96c8
No related branches found
No related tags found
1 merge request!7Merge old hardware code with new hardware code
......@@ -45,11 +45,12 @@ class Esp:
MODE_WIFI = const(1)
MODE_CMD = const(2)
def __init__(self, baud_rate=115200):
def __init__(self, pcf_buttons, baud_rate=115200):
self.uart = UART(INTERNAL_UART_HW_ID, baudrate=baud_rate, bits=8, parity=None, stop=1,
tx=Pin(INTERNAL_UART_TX_PIN), rx=Pin(INTERNAL_UART_RX_PIN), flow=0, invert=0)
self.uart_timer = Timer(-1)
self.uart_timer.init(mode=Timer.PERIODIC, period=10, callback=self.UART_RX_handler)
self.pcf_buttons = pcf_buttons
# CONFIG
self.header = 0
self.length = 0
......@@ -221,28 +222,32 @@ class Esp:
response = self.repeat_cmd(Esp.COM_BT_PIN, 50)
self.cmd_stop()
return response
utime.sleep(0.6)
else:
return None
def wifi(self):
response = None
if self.esp_mode == Esp.MODE_BT:
self.esp_mode = Esp.MODE_WIFI
self.cmd_start()
response = self.repeat_cmd(Esp.COM_WIFI, 50)
self.cmd_stop()
else:
return None
utime.sleep(0.6)
return response
def set_name(self, name: str, timeout=50):
self.cmd_start()
response = self.repeat_str(Esp.COM_NAME, name, timeout)
self.cmd_stop()
utime.sleep(0.6)
return response
def set_password(self, password: str, timeout=50):
self.cmd_start()
response = self.repeat_str(Esp.COM_PASSWORD, password, timeout)
self.cmd_stop()
utime.sleep(0.6)
return response
# ********** INTERNAL FUNCTIONS **********
......@@ -252,12 +257,19 @@ class Esp:
def cmd_start(self):
if self.esp_mode != Esp.MODE_CMD:
self.esp_mode_prev = self.esp_mode
self.esp_mode_prev = self.esp_mode
self.esp_mode = Esp.MODE_CMD
self.pcf_buttons.set_pin(8, False)
utime.sleep(0.01)
self.flush()
def cmd_stop(self):
if self.esp_mode == Esp.MODE_CMD:
utime.sleep(0.01)
self.flush()
self.esp_mode = self.esp_mode_prev
self.pcf_buttons.set_pin(8, True)
def timeout(self):
self.not_responding += 1
......@@ -321,15 +333,18 @@ class Esp:
return array[0]
def req_bt_pin(self, timeout=100):
self.cmd_start()
time = utime.ticks_ms()
self.send_message(self.i2b(Esp.COM_BT_PIN)+self.i2b(0))
while utime.ticks_diff(utime.ticks_ms(), time) < timeout:
utime.sleep_ms(1)
if self.ack_received() == Esp.NACK:
self.cmd_stop()
return None
if self.new_cmd and self.header == Esp.COM_BT_PIN:
self.new_cmd = False
return struct.unpack('<I', self.payload)[0]
self.cmd_stop()
return -1
def send_reset(self):
......@@ -341,6 +356,18 @@ class Esp:
def send_nack(self):
self.send_message(self.i2b(Esp.COM_NACK)+self.i2b(0))
def send_pair(self):
self.cmd_start()
response = self.repeat_cmd(Esp.COM_BT_PAIR, 50)
self.cmd_stop()
return response
def send_cancel(self):
self.cmd_start()
response = self.repeat_cmd(Esp.COM_BT_CANCEL, 50)
self.cmd_stop()
return response
def repeat_cmd(self, command: int, timeout=50):
for i in range(Esp.NACK_ATTEMPTS):
self.send_message(self.i2b(command)+self.i2b(0))
......
......@@ -63,20 +63,7 @@ def main():
robot.display.fill(0)
robot.display.text('Program', 35, 20, 1)
robot.display.text('running', 40, 30, 1)
robot.display.show()
def esp_command_on(robot):
robot.esp_command = True
robot.buttons.set_pin(8, False)
utime.sleep(0.01)
robot.esp.cmd_start()
def esp_command_off(robot):
robot.esp.cmd_stop()
robot.buttons.set_pin(8, True)
utime.sleep(0.01)
robot.esp_command = False
robot.display.show()
# Analyze pressed buttons for menu movement
def buttons(robot, button_values, robotState, menu_move, menu_debounce):
......@@ -102,10 +89,8 @@ def main():
return menu_move, menu_debounce, ok_pressed
def send_esp_req(robot):
esp_command_on(robot)
pin = robot.esp.req_bt_pin()
robot.esp.flush()
esp_command_off(robot)
print("Pin: ", pin)
if pin is not None and pin != -1:
bt_pair = True
......@@ -153,15 +138,11 @@ def main():
while(True):
if not robot.esp.running() and counter > 0 and counter % 20 == 0:
robot.buttons.set_pin(14, False)
esp_command_on(robot)
reset = robot.esp.reset(100)
print("Reset: ", reset)
if reset:
utime.sleep(0.5)
robot.esp.flush()
print("Name: ", robot.esp.set_name("Open-Cube5",300))
robot.buttons.set_pin(14, True)
esp_command_off(robot)
bat_voltage = robot.battery.voltage() # Get current battery voltage
button_values = robot.buttons.pressed() # Get buttons state
......@@ -182,18 +163,12 @@ def main():
if bt_pair:
display_show_bt_pin(robot, pin, bt_connecting)
if button_values[Button.RIGHT]:
esp_command_on(robot)
print("pair", robot.esp.repeat_cmd(robot.esp.COM_BT_PAIR, 300)==robot.esp.ACK)
robot.esp.flush()
esp_command_off(robot)
print("pair", robot.esp.send_pair() == robot.esp.ACK)
bt_connecting = True
elif button_values[Button.LEFT]:
rejected_pin = pin
esp_command_on(robot)
print("cancel", robot.esp.repeat_cmd(robot.esp.COM_BT_CANCEL, 300)==robot.esp.ACK)
robot.esp.flush()
esp_command_off(robot)
print("cancel", robot.esp.send_cancel()==robot.esp.ACK)
robot.display.show()
counter += 1
......
......@@ -5,28 +5,34 @@ from lib.cube.esp_config import Esp
def main():
global robot
counter = 0
indicators = [True, False, True, False, False]
labels = ["1", "test1", "test2", "", "123"]
btn_labels = ["", "up", "", "left", "ok", "right", "", "down", "" ]
run = True
while run:
time = utime.ticks_us()
robot.esp_command = True
robot.buttons.set_pin(8, False)
utime.sleep(0.01)
wifi = (robot.esp.repeat_cmd(Esp.COM_WIFI, 50) == Esp.ACK)
wifi = robot.esp.wifi()
print("Wifi: ", wifi)
utime.sleep(0.5)
if wifi:
print("Name: ", robot.esp.set_name("Open-Cube-Wifi",300))
utime.sleep(0.5)
robot.esp.flush()
print("Password: ", robot.esp.set_password("01234567",1000))
utime.sleep(0.5)
robot.esp.flush()
break
print("Name: ", robot.esp.set_name("Open-Cube-Wifi"))
print("Password: ", robot.esp.set_password("01234567"))
robot.buttons.set_pin(8, True)
utime.sleep(0.01)
robot.esp_command = False
robot.esp.wifi_set_indicators_labels(("1", "", "test2", "test3", "123"))
robot.esp.wifi_set_buttons_labels(btn_labels)
robot.esp.wifi_set_switches_labels(labels)
while run:
utime.sleep(0.1)
if counter % 10 == 0:
robot.esp.wifi_set_indicators(indicators)
indicators = indicators[-1:]+indicators[:-1]
print("Buttons: ", robot.esp.wifi_get_buttons())
print("Switches: ", robot.esp.wifi_get_switches())
counter +=1
buttons = robot.buttons.pressed()
if buttons[Button.LEFT]:
run = False
......
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