new usb uart examples
							parent
							
								
									1ad857a8b5
								
							
						
					
					
						commit
						8434390115
					
				| @ -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. | ||||
| @ -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() | ||||
| @ -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  | ||||
| @ -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()}") | ||||
| @ -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() | ||||
| @ -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() | ||||
| @ -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() | ||||
					Loading…
					
					
				
		Reference in New Issue
	
	 Drew Bednar
						Drew Bednar