-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
107 lines (86 loc) · 3.66 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
import time
import board
import digitalio
import neopixel
from adafruit_funhouse import FunHouse
funhouse = FunHouse(default_bg=0x000F20, scale=3)
running_state = False
tripped_led_color = [0xFF, 0xE5, 0x99]
reset_led_color = [0x00, 0x00, 0x00]
text_color = [0x60, 0x60, 0x60]
current_led_color = [0x00, 0x00, 0x00] # is constantly being modified towards reset/tripped colors
transition_dimming_time = 0.007 # in seconds
transition_shining_time = 0.005 # in seconds
stayOnTime = 0 # Global Variable: How long the lights stay up for in seconds
# NeoPixel Strip
pixel_pin = board.A0
pir_sensor_pin = board.A2
num_pixels = 30
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=1, auto_write=False)
pir_sensor = digitalio.DigitalInOut(pir_sensor_pin)
pir_sensor.direction = digitalio.Direction.INPUT
running_label = funhouse.add_text(
text="paused",
text_position=(2, 4),
text_color=text_color
)
def calculateColorDistance(currentColor, targetColor):
distance = [0, 0, 0]
for i in range(len(currentColor)):
distance[i] = abs(currentColor[i] - targetColor[i])
return distance
def transitionColor(currentColor, targetColor, distance):
for i in range(len(distance)):
if (currentColor[i] == targetColor[i]):
pass
elif (currentColor[i] > targetColor[i]):
currentColor[i] = (currentColor[i] - 1)
else:
currentColor[i] = (currentColor[i] + 1)
return currentColor
def turnLightsToColor(led_colors):
pixels.fill(led_colors)
pixels.show()
turnLightsToColor(reset_led_color)
while True:
if funhouse.peripherals.button_down:
if running_state is False:
running_state = True
funhouse.set_text('preparing...count to 3', running_label)
time.sleep(3) # enough time to get out of the way
funhouse.set_text('sensing...', running_label)
else:
running_state = False
funhouse.set_text('paused', running_label)
time.sleep(0.5)
if running_state is True:
if funhouse.peripherals.pir_sensor or pir_sensor.value:
tripped_text = "entrance" if pir_sensor.value else "hallway"
funhouse.set_text(tripped_text, running_label)
stayOnTime = time.monotonic() + 30 # in seconds
while stayOnTime > time.monotonic():
if funhouse.peripherals.button_down: # Manual Override - stop the entire process
stayOnTime = 0
running_state = False
funhouse.set_text('manually paused', running_label)
turnLightsToColor(reset_led_color)
time.sleep(0.5)
else:
if current_led_color is not tripped_led_color:
current_led_color = transitionColor(
current_led_color,
tripped_led_color,
calculateColorDistance(current_led_color, tripped_led_color)
)
turnLightsToColor(current_led_color)
time.sleep(transition_shining_time)
elif not funhouse.peripherals.pir_sensor or not pir_sensor.value:
funhouse.set_text('sensing...', running_label)
if current_led_color is not reset_led_color:
current_led_color = transitionColor(
current_led_color,
reset_led_color,
calculateColorDistance(current_led_color, reset_led_color)
)
turnLightsToColor(current_led_color)
time.sleep(transition_dimming_time)