/ BLE_Colorific / flux.py
flux.py
 1  # SPDX-FileCopyrightText: Jeremy Plichta
 2  # SPDX-FileCopyrightText: 2019 Tony DiCola for Adafruit Industries
 3  #
 4  # SPDX-License-Identifier: MIT
 5  
 6  # Smart Bulb Flux Bulb Control With Bluez
 7  # Author: Tony DiCola
 8  # Author: (Flux Bulb modifications) Jeremy Plichta
 9  #
10  # This script will cycle a Flux Bulb Bluetooth Low Energy light bulb
11  # through a rainbow of different hues.
12  #
13  # Dependencies:
14  # - You must install the pexpect library, typically with 'sudo pip install pexpect'.
15  # - You must have bluez installed and gatttool in your path (copy it from the
16  #   attrib directory after building bluez into the /usr/bin/ location).
17  #
18  # License: Released under an MIT license: http://opensource.org/licenses/MIT
19  import colorsys
20  import math
21  import sys
22  import time
23  
24  import pexpect
25  
26  
27  # Configuration values.
28  HUE_RANGE  = (0.0, 1.0)  # Tuple with the minimum and maximum hue values for a 
29                           # cycle. Stick with 0 to 1 to cover all hues.
30  SATURATION = 1.0         # Color saturation for hues (1 is full color).
31  VALUE      = 1.0         # Color value for hues (1 is full value).
32  CYCLE_SEC  = 5.0         # Amount of time for a full cycle of hues to complete.
33  SLEEP_SEC  = 0.01        # Amount of time to sleep between loop iterations.
34  
35  
36  # Get bulb address from command parameters.
37  if len(sys.argv) != 2:
38      print ('Error must specify bulb address as parameter!')
39      print ('Usage: sudo python colorific.py <bulb address>')
40      print ('Example: sudo python colorific.py 5C:31:3E:F2:16:13')
41      sys.exit(1)
42  bulb = sys.argv[1]
43  
44  # Run gatttool interactively.
45  gatt = pexpect.spawn('gatttool -I')
46  
47  # Connect to the device.
48  gatt.sendline('connect {0}'.format(bulb))
49  gatt.expect('Connection successful')
50  
51  # Setup range of hue value and start at minimum hue.
52  hue_min, hue_max = HUE_RANGE
53  hue = hue_min
54  
55  # Enter main loop.
56  print ('Press Ctrl-C to quit.')
57  last = time.time()
58  while True:
59      # Get amount of time elapsed since last update, then compute hue delta.
60      now = time.time()
61      hue_delta = (now-last)/CYCLE_SEC*(hue_max-hue_min)
62      hue += hue_delta
63      # If hue exceeds the maximum wrap back around to start from the minimum.
64      if hue > hue_max:
65          hue = hue_min+math.modf(hue)[0]
66      # Compute 24-bit RGB color based on HSV values.
67      r, g, b = map(lambda x: int(x*255.0), colorsys.hsv_to_rgb(hue, SATURATION, 
68                                                                VALUE))
69      # Set light color by sending color change packet over BLE.
70      # 56RRGGBB00f0aa
71      line = 'char-write-cmd 0x002e 56{0:02X}{1:02X}{2:02X}00f0aa'.format(r, g, b)
72      print (line)
73      gatt.sendline(line)
74      # Wait a short period of time and setup for the next loop iteration.
75      time.sleep(SLEEP_SEC)
76      last = now