|
| 1 | +# SPDX-FileCopyrightText: 2019 John Edgar Park for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +# BLE Crickit Light Switch |
| 6 | +# Use with the Adafruit BlueFruit LE Connect app |
| 7 | +# Works with CircuitPython 5.0.0-beta.0 and later |
| 8 | +# running on an nRF52840 Feather board and Crickit FeatherWing |
| 9 | +# micro servo, 3D printed switch actuator |
| 10 | + |
| 11 | +import time |
| 12 | + |
| 13 | +import board |
| 14 | +import digitalio |
| 15 | + |
| 16 | +from adafruit_crickit import crickit |
| 17 | + |
| 18 | +from adafruit_ble import BLERadio |
| 19 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 20 | +from adafruit_ble.services.nordic import UARTService |
| 21 | + |
| 22 | +from adafruit_bluefruit_connect.packet import Packet |
| 23 | +# Only the packet classes that are imported will be known to Packet. |
| 24 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 25 | + |
| 26 | +# Prep the status LEDs on the Feather |
| 27 | +blue_led = digitalio.DigitalInOut(board.BLUE_LED) |
| 28 | +red_led = digitalio.DigitalInOut(board.RED_LED) |
| 29 | +blue_led.direction = digitalio.Direction.OUTPUT |
| 30 | +red_led.direction = digitalio.Direction.OUTPUT |
| 31 | + |
| 32 | +ble = BLERadio() |
| 33 | +uart_service = UARTService() |
| 34 | +advertisement = ProvideServicesAdvertisement(uart_service) |
| 35 | + |
| 36 | +UP_ANGLE = 180 |
| 37 | +NEUTRAL_ANGLE = 120 |
| 38 | +DOWN_ANGLE = 60 |
| 39 | +crickit.servo_1.angle = NEUTRAL_ANGLE |
| 40 | + |
| 41 | +angle = NEUTRAL_ANGLE # use to track state |
| 42 | + |
| 43 | +print("BLE Light Switch") |
| 44 | +print("Use Adafruit Bluefruit app to connect") |
| 45 | +while True: |
| 46 | + blue_led.value = False |
| 47 | + ble.start_advertising(advertisement) |
| 48 | + |
| 49 | + while not ble.connected: |
| 50 | + # Wait for a connection. |
| 51 | + pass |
| 52 | + blue_led.value = True # turn on blue LED when connected |
| 53 | + while ble.connected: |
| 54 | + if uart_service.in_waiting: |
| 55 | + # Packet is arriving. |
| 56 | + red_led.value = False # turn off red LED |
| 57 | + packet = Packet.from_stream(uart_service) |
| 58 | + if isinstance(packet, ButtonPacket) and packet.pressed: |
| 59 | + red_led.value = True # blink to show a packet has been received |
| 60 | + if packet.button == ButtonPacket.UP and angle != UP_ANGLE: # UP button pressed |
| 61 | + angle = NEUTRAL_ANGLE - 45 # set anticipation angle, opposite of goal angle |
| 62 | + for a in range(angle, UP_ANGLE+1, 1): # anticipation angle, ramp to goal angle |
| 63 | + crickit.servo_1.angle = a |
| 64 | + time.sleep(0.1) # wait a moment |
| 65 | + crickit.servo_1.angle = NEUTRAL_ANGLE # then return to neutral angle |
| 66 | + angle = UP_ANGLE # set state to prevent redundant hits |
| 67 | + elif packet.button == ButtonPacket.DOWN and angle != DOWN_ANGLE: # DOWN button |
| 68 | + angle = NEUTRAL_ANGLE + 45 |
| 69 | + for a in range(angle, DOWN_ANGLE-1, -1): |
| 70 | + crickit.servo_1.angle = a |
| 71 | + time.sleep(0.1) # wait a moment |
| 72 | + crickit.servo_1.angle = NEUTRAL_ANGLE # then return to neutral angle |
| 73 | + angle = DOWN_ANGLE # set state to prevent redundant hits |
0 commit comments