/ examples / bluefruitspi_neocolorpicker.py
bluefruitspi_neocolorpicker.py
 1  # NeoPixel Color Picker demo - wire up some NeoPixels and set their color
 2  # using Adafruit Bluefruit Connect App on your phone
 3  
 4  import time
 5  import busio
 6  import board
 7  from digitalio import DigitalInOut
 8  from adafruit_bluefruitspi import BluefruitSPI
 9  import neopixel
10  
11  ADVERT_NAME = b"BlinkaNeoLamp"
12  
13  # 16 neopixels on a digital pin, adjust as necessary!
14  pixels = neopixel.NeoPixel(board.D5, 16)
15  pixels.fill(0)
16  
17  spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
18  cs = DigitalInOut(board.D8)
19  irq = DigitalInOut(board.D7)
20  rst = DigitalInOut(board.D4)
21  bluefruit = BluefruitSPI(spi_bus, cs, irq, rst, debug=False)
22  
23  
24  def init_bluefruit():
25      # Initialize the device and perform a factory reset
26      print("Initializing the Bluefruit LE SPI Friend module")
27      bluefruit.init()
28      bluefruit.command_check_OK(b"AT+FACTORYRESET", delay=1)
29      # Print the response to 'ATI' (info request) as a string
30      print(str(bluefruit.command_check_OK(b"ATI"), "utf-8"))
31      # Change advertised name
32      bluefruit.command_check_OK(b"AT+GAPDEVNAME=" + ADVERT_NAME)
33  
34  
35  def wait_for_connection():
36      print("Waiting for a connection to Bluefruit LE Connect ...")
37      # Wait for a connection ...
38      dotcount = 0
39      while not bluefruit.connected:
40          print(".", end="")
41          dotcount = (dotcount + 1) % 80
42          if dotcount == 79:
43              print("")
44          time.sleep(0.5)
45  
46  
47  # This code will check the connection but only query the module if it has been
48  # at least 'n_sec' seconds. Otherwise it 'caches' the response, to keep from
49  # hogging the Bluefruit connection with constant queries
50  connection_timestamp = None
51  is_connected = None
52  
53  
54  def check_connection(n_sec):
55      # pylint: disable=global-statement
56      global connection_timestamp, is_connected
57      if (not connection_timestamp) or (time.monotonic() - connection_timestamp > n_sec):
58          connection_timestamp = time.monotonic()
59          is_connected = bluefruit.connected
60      return is_connected
61  
62  
63  # Unlike most circuitpython code, this runs in two loops
64  # one outer loop manages reconnecting bluetooth if we lose connection
65  # then one inner loop for doing what we want when connected!
66  while True:
67      # Initialize the module
68      try:  # Wireless connections can have corrupt data or other runtime failures
69          # This try block will reset the module if that happens
70          init_bluefruit()
71          wait_for_connection()
72          print("\n *Connected!*")
73  
74          # Once connected, check for incoming BLE UART data
75          while check_connection(3):  # Check our connection status every 3 seconds
76              # OK we're still connected, see if we have any data waiting
77              resp = bluefruit.read_packet()
78              if not resp:
79                  continue  # nothin'
80              print("Read packet", resp)
81              # Look for a 'C'olor packet
82              if resp[0] != "C":
83                  continue
84              # Set the neopixels to the three bytes in the packet
85              pixels.fill(resp[1:4])
86          print("Connection lost.")
87  
88      except RuntimeError as e:
89          print(e)  # Print what happened
90          continue  # retry!