forked from pimoroni/inventorhatmini-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_rainbow.py
More file actions
53 lines (38 loc) · 1.5 KB
/
led_rainbow.py
File metadata and controls
53 lines (38 loc) · 1.5 KB
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
import time
from inventorhatmini import InventorHATMini, NUM_LEDS
"""
Displays a rotating rainbow pattern on Inventor HAT Mini's onboard LED bars.
Press "User" to exit the program.
"""
# Constants
SPEED = 5 # The speed that the LEDs will cycle at
BRIGHTNESS = 0.4 # The brightness of the LEDs
UPDATES = 50 # How many times the LEDs will be updated per second
UPDATE_RATE = 1 / UPDATES
AUTO_SHOW = True # Whether to update each LED as they are set
# Create a new InventorHATMini
board = InventorHATMini()
# Variables
offset = 0.0
# Sleep until a specific time in the future. Use this instead of time.sleep() to correct for
# inconsistent timings when dealing with complex operations or external communication
def sleep_until(end_time):
time_to_sleep = end_time - time.monotonic()
if time_to_sleep > 0.0:
time.sleep(time_to_sleep)
# Make rainbows until the user button is pressed
while not board.switch_pressed():
# Record the start time of this loop
start_time = time.monotonic()
offset += SPEED / 1000.0
# Update all the LEDs
for i in range(NUM_LEDS):
hue = float(i) / NUM_LEDS
board.leds.set_hsv(i, hue + offset, 1.0, BRIGHTNESS, show=AUTO_SHOW)
# If the LEDs were not updated when they were set, update them all now
if not AUTO_SHOW:
board.leds.show()
# Sleep until the next update, accounting for how long the above operations took to perform
sleep_until(start_time + UPDATE_RATE)
# Turn off the LED bars
board.leds.clear()