updated BLE vibration bracelet

Updated code for the BLE Vibration Bracelet Learn Guide

-Uses adafruit_led_animation library now for NeoPixel colors
This commit is contained in:
BlitzCityDIY 2020-05-18 15:40:53 -04:00
parent 211f8ebcf2
commit 40a9e6340e

View file

@ -3,11 +3,12 @@ import board
import busio import busio
import neopixel import neopixel
import adafruit_drv2605 import adafruit_drv2605
from digitalio import DigitalInOut, Direction import adafruit_led_animation.color as color
import adafruit_ble import adafruit_ble
from adafruit_ble.advertising.standard import SolicitServicesAdvertisement from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
from adafruit_ble.services.standard import CurrentTimeService from adafruit_ble.services.standard import CurrentTimeService
from adafruit_ble_apple_notification_center import AppleNotificationCenterService from adafruit_ble_apple_notification_center import AppleNotificationCenterService
from digitalio import DigitalInOut, Direction
# setup for onboard NeoPixel # setup for onboard NeoPixel
pixel_pin = board.NEOPIXEL pixel_pin = board.NEOPIXEL
@ -44,26 +45,16 @@ all_ids = [] # array to hold all of the ids from ANCS
hour = 0 # used to track when it is on the hour for the mindfulness reminder hour = 0 # used to track when it is on the hour for the mindfulness reminder
mindful = False # state used to track if it is time for mindfulness mindful = False # state used to track if it is time for mindfulness
vibration = 16 # vibration effect being used for the haptic motor vibration = 16 # vibration effect being used for the haptic motor
BLUE = (0, 0, 255) # color blue for the NeoPixel
PURPLE = (255, 0, 255) # color purple for the NeoPixel
RED = (255, 0, 0) # color red for the NeoPixel
ORANGE = (255, 255, 0) # color orange
YELLOW = (255, 150, 0) # color yellow
GREEN = (0, 255, 0) # color green
CYAN = (0, 255, 255) # color cyan
PINK = (255, 0, 127) # color pink
WHITE = (255, 255, 255) # color white
clear = (0, 0, 0) # allows for NeoPixel to be turned 'off'
APP_COLORS = { APP_COLORS = {
"com.basecamp.bc3-ios": YELLOW, # Basecamp "com.basecamp.bc3-ios": color.YELLOW, # Basecamp
"com.apple.MobileSMS": GREEN, # Texts "com.apple.MobileSMS": color.GREEN, # Texts
"com.hammerandchisel.discord": PURPLE, # Discord "com.hammerandchisel.discord": color.PURPLE, # Discord
"com.apple.mobilecal": CYAN, # Calendar "com.apple.mobilecal": color.CYAN, # Calendar
"com.apple.mobilephone": GREEN, # Phone "com.apple.mobilephone": color.GREEN, # Phone
"com.google.ios.youtube": ORANGE, # YouTube "com.google.ios.youtube": color.ORANGE, # YouTube
"com.burbn.instagram": PINK, # Instagram "com.burbn.instagram": color.MAGENTA, # Instagram
"com.apple.mobilemail": CYAN # Apple Email "com.apple.mobilemail": color.CYAN # Apple Email
} }
# function for blinking NeoPixel # function for blinking NeoPixel
@ -102,7 +93,7 @@ while True:
# NeoPixel is red when not connected to BLE # NeoPixel is red when not connected to BLE
while not ble.connected: while not ble.connected:
blue_led.value = False blue_led.value = False
pixel.fill(RED) pixel.fill(color.RED)
pixel.show() pixel.show()
print("Connected") print("Connected")
@ -130,14 +121,14 @@ while True:
if current_notification and current_notification.removed: if current_notification and current_notification.removed:
# Stop showing the latest and show that there are no new notifications. # Stop showing the latest and show that there are no new notifications.
current_notification = None current_notification = None
pixel.fill(clear) pixel.fill(color.BLACK)
pixel.show() pixel.show()
if not current_notification and not all_ids and not cleared: if not current_notification and not all_ids and not cleared:
# updates cleared state for notification # updates cleared state for notification
cleared = True cleared = True
# turns off NeoPixel when notifications are clear # turns off NeoPixel when notifications are clear
pixel.fill(clear) pixel.fill(color.BLACK)
pixel.show() pixel.show()
elif all_ids: elif all_ids:
@ -153,7 +144,7 @@ while True:
# if the notification is from an app that is not # if the notification is from an app that is not
# defined in APP_COLORS then the NeoPixel will be white # defined in APP_COLORS then the NeoPixel will be white
if current_notification.app_id not in APP_COLORS: if current_notification.app_id not in APP_COLORS:
notif_color = WHITE notif_color = color.WHITE
# if the notification is from an app defined in # if the notification is from an app defined in
# APP_COLORS then the assigned color will show # APP_COLORS then the assigned color will show
else: else:
@ -173,7 +164,7 @@ while True:
if notification.message: if notification.message:
print("Message:", notification.message) print("Message:", notification.message)
# NeoPixel blinks and then stays on until cleared # NeoPixel blinks and then stays on until cleared
blink_pixel(2, 0.5, notif_color, clear) blink_pixel(2, 0.5, notif_color, color.BLACK)
pixel.fill(notif_color) pixel.fill(notif_color)
pixel.show() pixel.show()
# if it's on the hour: # if it's on the hour:
@ -183,16 +174,16 @@ while True:
# haptic motor vibrates # haptic motor vibrates
vibe(5, vibration, 1) vibe(5, vibration, 1)
# NeoPixel blinks and then stays on # NeoPixel blinks and then stays on
blink_pixel(5, 1, BLUE, clear) blink_pixel(5, 1, color.BLUE, color.BLACK)
mindful = True mindful = True
pixel.fill(BLUE) pixel.fill(color.BLUE)
pixel.show() pixel.show()
print("hour = ", hour) print("hour = ", hour)
# if it's no longer on the hour: # if it's no longer on the hour:
if cts.current_time[4] == (hour + 1) and mindful: if cts.current_time[4] == (hour + 1) and mindful:
# NeoPixel turns off # NeoPixel turns off
mindful = False mindful = False
pixel.fill(clear) pixel.fill(color.BLACK)
pixel.show() pixel.show()
print("mindful time over") print("mindful time over")