/ CPB_Magic_Light_Mixer / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 John Edgar Park for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # Magic Light Bulb remote color mixer 6 # Sends RGB color values, read from three faders on CPB to the bulb 7 # https://www.magiclightbulbs.com/collections/bluetooth-bulbs 8 9 import adafruit_ble 10 from adafruit_ble.advertising.standard import ProvideServicesAdvertisement 11 from adafruit_ble_magic_light import MagicLightService 12 import _bleio 13 import board 14 from analogio import AnalogIn 15 from adafruit_circuitplayground import cp 16 17 18 def find_connection(): 19 for connection in radio.connections: 20 if MagicLightService not in connection: # Filter services 21 continue 22 return connection, connection[MagicLightService] 23 return None, None 24 25 radio = adafruit_ble.BLERadio() 26 27 28 def scale(value): 29 # Scale a value from 0-65535 (AnalogIn range) to 0-255 (RGB range) 30 return int(value / 65535 * 255) 31 a4 = AnalogIn(board.A4) # red slider 32 a5 = AnalogIn(board.A5) # green slider 33 a6 = AnalogIn(board.A6) # blue slider 34 35 cp.pixels.brightness = 0.1 36 dimmer = 1.0 37 38 active_connection, bulb = find_connection() # In case already connected 39 40 while True: 41 if not active_connection: # There's no connection, so let's scan for one 42 cp.pixels[0] = (60, 40, 0) # set CPB NeoPixel 0 to yellow while searching 43 print("Scanning for Magic Light...") 44 # Scan and filter for advertisements with ProvideServicesAdvertiesment type 45 for advertisement in radio.start_scan(ProvideServicesAdvertisement): 46 # Filter further for advertisements with MagicLightService 47 if MagicLightService in advertisement.services: 48 active_connection = radio.connect(advertisement) 49 print("Connected to Magic Light") 50 cp.pixels[0] = (0, 0, 255) # Set NeoPixel 0 to blue when connected 51 # Play a happy tone 52 cp.play_tone(440, 0.1) 53 cp.play_tone(880, 0.1) 54 print("Adjust slide potentiometers to mix RGB colors") 55 try: 56 bulb = active_connection[MagicLightService] 57 except _bleio.ConnectionError: 58 print("disconnected") 59 continue 60 break 61 radio.stop_scan() # Now that we're connected, stop scanning 62 63 while active_connection.connected: # Connected, now we can set attrs to change colors 64 # Toggle slide switch to go to half or full brightness 65 if cp.switch: 66 cp.red_led = True 67 dimmer = 0.5 68 else: 69 cp.red_led = False 70 dimmer = 1.0 71 72 # Press the 'A' button to momentarily black the bulb 73 if cp.button_a: 74 dimmer = 0.0 75 76 r = scale(a4.value * dimmer) 77 g = scale(a5.value * dimmer) 78 b = scale(a6.value * dimmer) 79 80 # Press the 'B' button to momentarily white the bulb 81 if cp.button_b: 82 r, g, b = (255, 255, 255) 83 84 color = (r, g, b) 85 86 try: 87 bulb[0] = color # Send color to bulb's color characteristic 88 except _bleio.ConnectionError: 89 print("disconnected") 90 continue 91 cp.pixels[2] = (r, 0, 0) 92 cp.pixels[3] = (0, g, 0) 93 cp.pixels[4] = (0, 0, b) 94 cp.pixels[7] = (color) 95 96 active_connection = None # Not connected, start scanning again 97 cp.pixels[0] = (60, 40, 0)