Skip to content

Commit f69211b

Browse files
committed
Added UART code
1 parent dfd77c4 commit f69211b

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

UART-Between-Boards/code.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Dylan Herrada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
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.
7+
"""
8+
import time
9+
import board
10+
import busio
11+
import neopixel
12+
13+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1.0, auto_write=True)
14+
15+
uart = busio.UART(board.TX, board.RX, baudrate=9600)
16+
17+
18+
array = [0x55, 0, 0, 0, 0xAA]
19+
print(array)
20+
barray = bytearray(array)
21+
print(barray)
22+
23+
uart.write(barray)
24+
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)
40+
41+
time.sleep(0.1)
42+
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))

0 commit comments

Comments
 (0)