|
| 1 | +""" |
| 2 | +Control code for Circuit Playground Bluefruit NeoPixel Animation and Color controller. To be used |
| 3 | +with receiver code. |
| 4 | +""" |
| 5 | + |
| 6 | +import time |
| 7 | + |
| 8 | +from adafruit_circuitplayground.bluefruit import cpb |
| 9 | + |
| 10 | +from adafruit_ble import BLERadio |
| 11 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 12 | +from adafruit_ble.services.nordic import UARTService |
| 13 | +from adafruit_bluefruit_connect.color_packet import ColorPacket |
| 14 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 15 | + |
| 16 | + |
| 17 | +def scale(value): |
| 18 | + """Scale a value from acceleration value range to 0-255 (RGB range)""" |
| 19 | + value = abs(value) |
| 20 | + value = max(min(19.6, value), 0) |
| 21 | + return int(value / 19.6 * 255) |
| 22 | + |
| 23 | + |
| 24 | +def send_packet(uart_connection_name, packet): |
| 25 | + """Returns False if no longer connected.""" |
| 26 | + try: |
| 27 | + uart_connection_name[UARTService].write(packet.to_bytes()) |
| 28 | + except (OSError, KeyError): |
| 29 | + try: |
| 30 | + uart_connection_name.disconnect() |
| 31 | + except: # pylint: disable=bare-except |
| 32 | + pass |
| 33 | + return False |
| 34 | + return True |
| 35 | + |
| 36 | + |
| 37 | +ble = BLERadio() |
| 38 | + |
| 39 | +button_a_pressed = False |
| 40 | +button_b_pressed = False |
| 41 | +last_switch_state = None |
| 42 | + |
| 43 | +uart_connection = None |
| 44 | +# See if any existing connections are providing UARTService. |
| 45 | +if ble.connected: |
| 46 | + for connection in ble.connections: |
| 47 | + if UARTService in connection: |
| 48 | + uart_connection = connection |
| 49 | + break |
| 50 | + |
| 51 | +while True: |
| 52 | + last_switch_state = None |
| 53 | + if not uart_connection or not uart_connection.connected: |
| 54 | + print("Scanning...") |
| 55 | + for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): |
| 56 | + if UARTService in adv.services: |
| 57 | + print("Found a UARTService advertisement.") |
| 58 | + uart_connection = ble.connect(adv) |
| 59 | + break |
| 60 | + # Stop scanning whether or not we are connected. |
| 61 | + ble.stop_scan() |
| 62 | + while uart_connection and uart_connection.connected: |
| 63 | + if cpb.button_a and not button_a_pressed: |
| 64 | + print("Button A pressed.") |
| 65 | + if not send_packet(uart_connection, |
| 66 | + ButtonPacket(ButtonPacket.LEFT, pressed=True)): |
| 67 | + uart_connection = None |
| 68 | + continue |
| 69 | + button_a_pressed = True |
| 70 | + time.sleep(0.05) |
| 71 | + if not cpb.button_a and button_a_pressed: |
| 72 | + button_a_pressed = False |
| 73 | + time.sleep(0.05) |
| 74 | + if cpb.button_b and not button_b_pressed: |
| 75 | + print("Button B pressed.") |
| 76 | + if not send_packet(uart_connection, |
| 77 | + ButtonPacket(ButtonPacket.RIGHT, pressed=True)): |
| 78 | + uart_connection = None |
| 79 | + continue |
| 80 | + button_b_pressed = True |
| 81 | + time.sleep(0.05) |
| 82 | + if not cpb.button_b and button_b_pressed: |
| 83 | + button_b_pressed = False |
| 84 | + time.sleep(0.05) |
| 85 | + if cpb.switch is not last_switch_state: |
| 86 | + last_switch_state = cpb.switch |
| 87 | + print("Switch is to the", "left: LEDs off!" if cpb.switch else "right: LEDs on!") |
| 88 | + if not send_packet(uart_connection, |
| 89 | + ButtonPacket(ButtonPacket.BUTTON_1, pressed=cpb.switch)): |
| 90 | + uart_connection = None |
| 91 | + continue |
| 92 | + if cpb.switch: |
| 93 | + cpb.pixels.fill(0) |
| 94 | + else: |
| 95 | + r, g, b = map(scale, cpb.acceleration) |
| 96 | + color = (r, g, b) |
| 97 | + print(color) |
| 98 | + cpb.pixels.fill(color) |
| 99 | + if not send_packet(uart_connection, ColorPacket(color)): |
| 100 | + uart_connection = None |
| 101 | + continue |
| 102 | + time.sleep(0.1) |
0 commit comments