-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
executable file
·154 lines (133 loc) · 4 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
For use with the CircuitPlayground Bluefruit. Polls the count of
local connectable devices and based on the count changes pixel colors.
"""
import time
import board
import neopixel
import analogio
import supervisor
from adafruit_ble import BLERadio
from adafruit_ble.advertising import Advertisement
# NEOPIXEL RGB values
COLORS = {
"RED": (255, 0, 0),
"YELLOW": (255, 150, 0),
"GREEN": (0, 255, 0),
"CYAN": (0, 255, 255),
"BLUE": (0, 0, 255),
"PURPLE": (180, 0, 255),
"WHITE": (255, 255, 255),
"ORANGE": (255, 165, 0),
"PINK": (255, 192, 203),
"OFF": (0, 0, 0),
}
def wheel(pos):
"""
Pick circuitplayground neopixel index.
"""
if pos < 0 or pos > 255:
return (0, 0, 0)
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
def color_chase(color, wait):
"""
Rotate through the neopixels setting the new color.
"""
for i in range(10):
pixels[i] = color
time.sleep(wait)
pixels.show()
time.sleep(0.5)
def rainbow(wait=0.1):
"""
Cycle through the RGB spectrum and set the new neopixel color.
"""
for j in range(255):
for i in range(len(pixels)):
idx = int(i + j)
pixels[i] = wheel(idx & 255)
pixels.show()
time.sleep(wait)
def local_scan(timeout=2):
"""
Find out how many devices are in the area that we can
connect to.
"""
print("\nscanning\n")
found = set()
resp = set()
for ad in ble.start_scan(Advertisement, timeout=timeout):
# dir(ad)
# ['__bytes__', '__class__', '__dict__', '__init__', '__len__',
# '__module__', '__qualname__', '__repr__', '__str__', 'address',
# 'connectable', 'matches', 'rssi', 'scan_response', 'tx_power',
# 'complete_name', 'from_entry', 'prefix', 'data_dict', 'flags',
# 'mutable', 'short_name', 'appearance', '_rssi']
#
# Most of the name related fields just return None
resp.add(ad.address)
if ad.connectable:
print(ad.__dict__)
found.add(ad.address)
print("\nscan done\n")
print(
"\ntotal: {total}\nconnectable: {found}\n".format(
found=len(found), total=len(resp)
)
)
return len(found)
light = analogio.AnalogIn(board.LIGHT)
ble = BLERadio()
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.2, auto_write=False)
pixels.fill((0, 0, 0))
pixels.show()
first_cycle = True
# Only want to scan a few times before idling
scan_count = 0
# For a "sleep" state we want to wake up to check
# the switch state.
sleeping = False
sleep_cycles = 0
while True:
print(light.value)
try:
if int(light.value) > 300:
while scan_count < 3:
scan_count += 1
found = local_scan()
if found == 0 or found < 2:
color_chase(COLORS["OFF"], wait=0.5)
if found > 0 and found < 5:
color_chase(COLORS["RED"], wait=0.5)
if found >= 5:
color_chase(COLORS["BLUE"], wait=0.5)
color_chase(COLORS["PURPLE"], wait=0.5)
time.sleep(5)
# Stop scanning for 15 minutes
sleeping = True
# Sleeping for 5 minutes, but waking to check the
# switch every minute.
if sleep_cycles < 5:
sleep_cycles += 1
time.sleep(60)
else:
sleeping = False
sleep_cycles = 0
scan_count = 0
rainbow()
color_chase(COLORS["OFF"], wait=0.5)
else:
# It's dark lights out, clear all pixels
color_chase(COLORS["OFF"], wait=0.5)
first_cycle = True
scan_count = 0
sleeping = False
sleep_cycles = 0
except Exception:
supervisor.reload()