|
| 1 | +""" |
| 2 | +Circuit Playground Bluefruit Ornament Proximity |
| 3 | +This demo uses advertising to set the color of scanning devices depending on the strongest broadcast |
| 4 | +signal received. Circuit Playgrounds can be switched between advertising and scanning using the |
| 5 | +slide switch. The buttons change the color when advertising. |
| 6 | +""" |
| 7 | + |
| 8 | +import time |
| 9 | +from adafruit_circuitplayground.bluefruit import cpb |
| 10 | + |
| 11 | +from adafruit_ble import BLERadio |
| 12 | +from adafruit_ble.advertising.adafruit import AdafruitColor |
| 13 | + |
| 14 | +# The color pickers will cycle through this list with buttons A and B. |
| 15 | +color_options = [0x110000, |
| 16 | + 0x111100, |
| 17 | + 0x001100, |
| 18 | + 0x001111, |
| 19 | + 0x000011, |
| 20 | + 0x110011, |
| 21 | + 0x111111, |
| 22 | + 0x221111, |
| 23 | + 0x112211, |
| 24 | + 0x111122] |
| 25 | + |
| 26 | +ble = BLERadio() |
| 27 | + |
| 28 | +i = 0 |
| 29 | +advertisement = AdafruitColor() |
| 30 | +advertisement.color = color_options[i] |
| 31 | +cpb.pixels.auto_write = False |
| 32 | +cpb.pixels.fill(color_options[i]) |
| 33 | +while True: |
| 34 | + # The first mode is the color selector which broadcasts it's current color to other devices. |
| 35 | + if cpb.switch: |
| 36 | + print("Broadcasting color") |
| 37 | + ble.start_advertising(advertisement) |
| 38 | + while cpb.switch: |
| 39 | + last_i = i |
| 40 | + if cpb.button_a: |
| 41 | + i += 1 |
| 42 | + if cpb.button_b: |
| 43 | + i -= 1 |
| 44 | + i %= len(color_options) |
| 45 | + if last_i != i: |
| 46 | + color = color_options[i] |
| 47 | + cpb.pixels.fill(color) |
| 48 | + cpb.pixels.show() |
| 49 | + print("New color {:06x}".format(color)) |
| 50 | + advertisement.color = color |
| 51 | + ble.stop_advertising() |
| 52 | + ble.start_advertising(advertisement) |
| 53 | + time.sleep(0.5) |
| 54 | + ble.stop_advertising() |
| 55 | + # The second mode listens for color broadcasts and shows the color of the strongest signal. |
| 56 | + else: |
| 57 | + closest = None |
| 58 | + closest_rssi = -80 |
| 59 | + closest_last_time = 0 |
| 60 | + print("Scanning for colors") |
| 61 | + while not cpb.switch: |
| 62 | + for entry in ble.start_scan(AdafruitColor, minimum_rssi=-100, timeout=1): |
| 63 | + if cpb.switch: |
| 64 | + break |
| 65 | + now = time.monotonic() |
| 66 | + new = False |
| 67 | + if entry.address == closest: |
| 68 | + pass |
| 69 | + elif entry.rssi > closest_rssi or now - closest_last_time > 0.4: |
| 70 | + closest = entry.address |
| 71 | + else: |
| 72 | + continue |
| 73 | + closest_rssi = entry.rssi |
| 74 | + closest_last_time = now |
| 75 | + discrete_strength = min((100 + entry.rssi) // 5, 10) |
| 76 | + cpb.pixels.fill(0x000000) |
| 77 | + for i in range(0, discrete_strength): |
| 78 | + cpb.pixels[i] = entry.color |
| 79 | + cpb.pixels.show() |
| 80 | + |
| 81 | + # Clear the pixels if we haven't heard from anything recently. |
| 82 | + now = time.monotonic() |
| 83 | + if now - closest_last_time > 1: |
| 84 | + cpb.pixels.fill(0x000000) |
| 85 | + cpb.pixels.show() |
| 86 | + ble.stop_scan() |
0 commit comments