Skip to content

Commit a15271c

Browse files
authored
Merge pull request #1830 from dherrada/uart-fix
Updated UART code
2 parents 33e90f1 + d8fbe17 commit a15271c

1 file changed

Lines changed: 91 additions & 38 deletions

File tree

UART-Between-Boards/code.py

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,109 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2021 Dylan Herrada for Adafruit Industries
2+
# SPDX-FileCopyrightText: Copyright (c) 2021 Dan Halbert for Adafruit Industries
23
#
34
# SPDX-License-Identifier: Unlicense
45
"""
5-
Code for communicating between two CircuitPython boards using UART.
6-
Increments built-in neopixel values of the other board starting from blue, then green and red.
6+
Code for communicating between two CircuitPlayground Express boards using UART.
7+
Sends value from the onboard light sensor to the other board and the other board sets its
8+
NeoPixels accordingly.
79
"""
10+
811
import time
912
import board
1013
import busio
14+
import digitalio
1115
import neopixel
16+
import analogio
17+
18+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1, auto_write=False)
19+
20+
light_sensor = analogio.AnalogIn(board.LIGHT)
21+
22+
btn_A = digitalio.DigitalInOut(board.BUTTON_A)
23+
btn_A.switch_to_input(pull=digitalio.Pull.DOWN)
1224

13-
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1.0, auto_write=True)
25+
btn_B = digitalio.DigitalInOut(board.BUTTON_B)
26+
btn_B.switch_to_input(pull=digitalio.Pull.DOWN)
1427

15-
uart = busio.UART(board.TX, board.RX, baudrate=9600)
28+
# Use a timeout of zero so we don't delay while waiting for a message.
29+
uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=0)
30+
31+
# Messages are of the form:
32+
# "<TYPE,value,value,value,...>"
33+
# We send and receive two types of messages:
34+
#
35+
# Message contains a light sensor value (float):
36+
# <L,light>
37+
#
38+
# Message contains statuses of two buttons. Increment NeoPixel brightness by 0.1 if the second
39+
# button is pressed, and reduce brightness by 0.1 if the first button is pressed.
40+
# <B,btn_A,btn_B>
1641

42+
UPDATE_INTERVAL = 3.0
43+
last_time_sent = 0
1744

18-
array = [0x55, 0, 0, 0, 0xAA]
19-
print(array)
20-
barray = bytearray(array)
21-
print(barray)
45+
# Wait for the beginning of a message.
46+
message_started = False
2247

23-
uart.write(barray)
2448
while True:
25-
data = uart.read(32) # read up to 32 bytes
26-
if data is not None:
27-
msg = []
28-
print(data)
29-
if data[0] == 85 and data[-1] == 170:
30-
for byte in data[1:-1]:
31-
if len(msg) == 3:
32-
break
33-
print(byte)
34-
msg.append(byte)
35-
36-
print(msg)
37-
print(type(msg))
38-
39-
pixel.fill(msg)
49+
# Send light sensor value only every UPDATE_INTERVAL seconds.
50+
now = time.monotonic()
51+
if now - last_time_sent >= UPDATE_INTERVAL:
52+
light = light_sensor.value
53+
uart.write(bytes(f"<L,{light}>", "ascii"))
54+
print("sending light value", light)
55+
last_time_sent = now
4056

57+
if any((btn_A.value, btn_B.value)):
58+
# Send values of built-in buttons if any are pressed
59+
uart.write(bytes(f"<B,{btn_A.value},{btn_B.value}>", "ascii"))
60+
print(f"Sent ({btn_A.value}, {btn_B.value})")
61+
62+
# Don't do anything else until both buttons are released
4163
time.sleep(0.1)
64+
while any((btn_A.value, btn_B.value)):
65+
pass
66+
67+
byte_read = uart.read(1) # Read one byte over UART lines
68+
if byte_read is None:
69+
# Nothing read.
70+
continue
71+
72+
if byte_read == b"<":
73+
# Start of message. Start accumulating bytes, but don't record the "<".
74+
message = []
75+
message_started = True
76+
continue
77+
78+
if message_started:
79+
if byte_read == b">":
80+
# End of message. Don't record the ">".
81+
# Now we have a complete message. Convert it to a string, and split it up.
82+
message_parts = "".join(message).split(",")
83+
message_type = message_parts[0]
84+
message_started = False
85+
86+
if message_parts[0] == "L":
87+
# Received a message telling us a light sensor value
88+
peak = int(((int(message_parts[1]) - 2000) / 62000) * 10)
89+
for i in range(0, 10):
90+
if i <= peak:
91+
pixels[i] = (0, 255, 0)
92+
else:
93+
pixels[i] = (0, 0, 0)
94+
pixels.show()
95+
print(f"Received light value of {message_parts[1]}")
96+
print(f"Lighting up {peak + 1} NeoPixels")
97+
98+
elif message_parts[0] == "B":
99+
# Received a message asking us to change our brightness.
100+
if message_parts[1] == "True":
101+
pixels.brightness = max(0.0, pixels.brightness - 0.1)
102+
print(f"Brightness set to: {pixels.brightness}")
103+
if message_parts[2] == "True":
104+
pixels.brightness = min(1.0, pixels.brightness + 0.1)
105+
print(f"Brightness set to: {pixels.brightness}")
42106

43-
if msg[2] < 255:
44-
data = [msg[0], msg[1], msg[2] + 5]
45-
data.insert(0, 0x55)
46-
data.append(0xAA)
47-
elif msg[1] < 255:
48-
data = [msg[0], msg[1] + 5, msg[2]]
49-
data.insert(0, 0x55)
50-
data.append(0xAA)
51-
elif msg[0] < 255:
52-
data = [msg[0] + 5, msg[1], msg[2]]
53-
data.insert(0, 0x55)
54-
data.append(0xAA)
55-
print(data)
56-
uart.write(bytearray(data))
107+
else:
108+
# Accumulate message byte.
109+
message.append(chr(byte_read[0]))

0 commit comments

Comments
 (0)