From 8434390115a451491225f2f3c69c0f65801b22ba Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Thu, 24 Feb 2022 08:33:59 -0500 Subject: [PATCH] new usb uart examples --- CANBus/README.md | 4 ++ CANBus/example.py | 5 +++ basics/README.md | 9 +++++ bluetooth/esp32/arni/robot.py | 3 -- uart/usb_echo/README.md | 9 +++++ uart/usb_echo/main.py | 10 +++++ uart/usb_echo/serial_echo.py | 38 ++++++++++++++++++ uart/usb_serial_led/main.py | 42 ++++++++++++++++++++ uart/usb_serial_led/serial_color_switcher.py | 41 +++++++++++++++++++ 9 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 CANBus/README.md create mode 100644 CANBus/example.py create mode 100644 uart/usb_echo/README.md create mode 100644 uart/usb_echo/main.py create mode 100644 uart/usb_echo/serial_echo.py create mode 100644 uart/usb_serial_led/main.py create mode 100644 uart/usb_serial_led/serial_color_switcher.py diff --git a/CANBus/README.md b/CANBus/README.md new file mode 100644 index 0000000..4195518 --- /dev/null +++ b/CANBus/README.md @@ -0,0 +1,4 @@ +# CANBus + +I don't know if I believe it but this https://docs.pycom.io/firmwareapi/pycom/machine/can/ article claims that we can +use the `machine.CAN` with the ESP32. \ No newline at end of file diff --git a/CANBus/example.py b/CANBus/example.py new file mode 100644 index 0000000..06618ae --- /dev/null +++ b/CANBus/example.py @@ -0,0 +1,5 @@ +from machine import CAN + +can = CAN(mode=CAN.NORMAL, baudrate=500000, pins=('P22', 'P23')) +can.send(id=12, data=bytes([1, 2, 3, 4, 5, 6, 7, 8])) +can.recv() \ No newline at end of file diff --git a/basics/README.md b/basics/README.md index 45ced08..f26089c 100644 --- a/basics/README.md +++ b/basics/README.md @@ -87,7 +87,16 @@ def free(full=False): esptool.py --port /dev/cu.SLAB_USBtoUART erase_flash ``` +For an ESP32-S3 +``` +esptool.py --chip esp32-s3 --port /dev/cu.SLAB_USBtoUART erase_flash +``` 2. If you have external spi ram (4MB) you want to use the spiram bin. ``` esptool.py --chip esp32 --port /dev/cu.SLAB_USBtoUART --baud 460800 write_flash -z 0x1000 ./binaries/esp32-idf4-20210202-v1.14.bin ``` + +For an ESP32-S3 +``` +esptool.py --chip esp32-s3 --port /dev/cu.SLAB_USBtoUART --baud 460800 write_flash -z 0x0000 ./binaries/GENERIC_S3_SPIRAM-20220117-v1.18.bin +``` diff --git a/bluetooth/esp32/arni/robot.py b/bluetooth/esp32/arni/robot.py index 41c94f3..90c348b 100644 --- a/bluetooth/esp32/arni/robot.py +++ b/bluetooth/esp32/arni/robot.py @@ -179,6 +179,3 @@ class ArniRobot: """Set motor velocity.""" self._vel = velocity return self._vel - -# arni = ArniRobot(uart) -# controller = RobotController(arni) \ No newline at end of file diff --git a/uart/usb_echo/README.md b/uart/usb_echo/README.md new file mode 100644 index 0000000..6a4e144 --- /dev/null +++ b/uart/usb_echo/README.md @@ -0,0 +1,9 @@ +# USB Echo + +This is a simple program that runs in a loop, looking for character data over the +UART0 line which happens to be hooked up to the MicroUSB to UART circuit on these +dev kits. + +## Usage + +You should use the \ No newline at end of file diff --git a/uart/usb_echo/main.py b/uart/usb_echo/main.py new file mode 100644 index 0000000..8a1e039 --- /dev/null +++ b/uart/usb_echo/main.py @@ -0,0 +1,10 @@ +import select +import sys + +print("Starting echo server over USB UART") +print("...") + +while True: + if select.select([sys.stdin], [], [], 0)[0]: + characters = sys.stdin.readline() + print(f"Recieved: {characters.strip()}") \ No newline at end of file diff --git a/uart/usb_echo/serial_echo.py b/uart/usb_echo/serial_echo.py new file mode 100644 index 0000000..064b9d1 --- /dev/null +++ b/uart/usb_echo/serial_echo.py @@ -0,0 +1,38 @@ +""" +This is a simple program that runs in a loop, looking for character data over the +UART0 line which happens to be hooked up to the MicroUSB to UART circuit on these +dev kits. +""" +import os +import random +import serial +import time + +ESP32_PORT= '/dev/cu.SLAB_USBtoUART' if os.uname().sysname == "Darwin" else "/dev/ttyUSB0" +SENTENCES = [ + "The little green frog hopped to the pond.\n", + "The cheese was late to the party.\n", + "Don't let your dreams just be dreams.\n" +] + + +def main(): + print(f"Connecting to {ESP32_PORT}....") + try: + with serial.Serial(ESP32_PORT, 115200, timeout=1) as serial_conn: + while True: + if serial_conn.is_open: + serial_conn.write(random.choice(SENTENCES).encode('ascii')) + time.sleep(0.5) + if serial_conn.in_waiting: + print(f"Found {serial_conn.in_waiting} bytes waiting in input buffer!") + print(serial_conn.readlines()) + else: + print("No data received.") + except KeyboardInterrupt: + print("Exiting program") + + exit(0) + +if __name__ == "__main__": + main() diff --git a/uart/usb_serial_led/main.py b/uart/usb_serial_led/main.py new file mode 100644 index 0000000..eb63a54 --- /dev/null +++ b/uart/usb_serial_led/main.py @@ -0,0 +1,42 @@ +import select +import sys +import time +from machine import Pin +from neopixel import NeoPixel + +print("Starting USB UART") +print("...") + +NEO_PIXELS = NeoPixel(Pin(48), 1) + + +def parse_cmd(raw_bytes): + # Drop the last element + raw_cmds = raw_bytes.strip().split(';') + cmds = [] + for cmd in raw_cmds: + try: + led, red, green, blue = cmd.split(',') + # Still strings so zero is true here + if all([led, red, green, blue]) and len(cmd.split(',')) == 4: + cmds.append(tuple([int(led), tuple([int(red), int(green), int(blue)])])) + except ValueError: + continue + return cmds + + +def main(): + print("Waiting for commands...") + + while True: + if select.select([sys.stdin], [], [], 0)[0]: + raw_cmds = sys.stdin.readline() + cmds = parse_cmd(raw_cmds) + # print(f"Parsed: {cmds}") + if cmds: + for led_cmd in cmds: + led_id, led_value = led_cmd + NEO_PIXELS[led_id] = led_value + NEO_PIXELS.write() + +main() \ No newline at end of file diff --git a/uart/usb_serial_led/serial_color_switcher.py b/uart/usb_serial_led/serial_color_switcher.py new file mode 100644 index 0000000..15f7b7c --- /dev/null +++ b/uart/usb_serial_led/serial_color_switcher.py @@ -0,0 +1,41 @@ +""" +This is a simple program that runs in a loop, looking for character data over the +UART0 line which happens to be hooked up to the MicroUSB to UART circuit on these +dev kits. +""" +import os +import random +import serial +import time + +ESP32_PORT= '/dev/cu.SLAB_USBtoUART' if os.uname().sysname == "Darwin" else "/dev/ttyUSB0" + +COLORS = { + "red": "0,255,0,0;\n", + "blue": "0,125,204,223;\n", + "yellow": "0,120,153,23;\n", + "purple": "0,255,0,153;\n" +} + +def main(): + print(f"Connecting to {ESP32_PORT}....") + print("Sending color commands") + try: + with serial.Serial(ESP32_PORT, 115200, timeout=1) as serial_conn: + while True: + if serial_conn.is_open: + color = random.choice(list(COLORS.keys())) + print(f"Setting color: {color.upper()}") + serial_conn.write(COLORS[color].encode('ascii')) + time.sleep(0.1) + if serial_conn.in_waiting: + # print(f"Found {serial_conn.in_waiting} bytes waiting in input buffer!") + print(serial_conn.readlines()) + time.sleep(2) + except KeyboardInterrupt: + print("Exiting program") + + exit(0) + +if __name__ == "__main__": + main()