|
1 | 1 | # SPDX-FileCopyrightText: Copyright (c) 2021 Dylan Herrada for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2021 Dan Halbert for Adafruit Industries |
2 | 3 | # |
3 | 4 | # SPDX-License-Identifier: Unlicense |
4 | 5 | """ |
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. |
7 | 9 | """ |
| 10 | + |
8 | 11 | import time |
9 | 12 | import board |
10 | 13 | import busio |
| 14 | +import digitalio |
11 | 15 | 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) |
12 | 24 |
|
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) |
14 | 27 |
|
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> |
16 | 41 |
|
| 42 | +UPDATE_INTERVAL = 3.0 |
| 43 | +last_time_sent = 0 |
17 | 44 |
|
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 |
22 | 47 |
|
23 | | -uart.write(barray) |
24 | 48 | 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 |
40 | 56 |
|
| 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 |
41 | 63 | 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}") |
42 | 106 |
|
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