forked from w84death/smolOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixel.py
45 lines (37 loc) · 1.53 KB
/
pixel.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
import neopixel
import utime
import _thread
class neo_pixel():
def __init__(self):
self.thread_running = False
self.power = machine.Pin(11,machine.Pin.OUT)
self.power.value(1)
self.grid = neopixel.NeoPixel(machine.Pin(12),1)
print("NeoPixel: Initialized.\bUse pixel.start(), pixel.stop(), pixel.color(\"r,g,b\").")
def color(self,rgb_color=""):
if rgb_color=="":
rgb_color=(0,0,0)
color = tuple(map(int, rgb_color.split(',')))
self.grid.fill(color)
self.grid.write()
def stop(self):
self.thread_running = False
print("NeoPixel: Hearthbeat stopped. Use pixel.start()")
def start(self):
if not self.thread_running:
self.thread_running = True
_thread.start_new_thread(self.hearthbeat,())
print("NeoPixel: Hearthbeat started in background. Use pixel.stop()")
else:
print("NeoPixel: Thread already used. Use pixel.stop()")
def hearthbeat(self):
heartbeat_pattern = [0, 10, 20, 50, 100, 255, 200, 100, 50, 30, 20, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # pattern for heartbeat
while self.thread_running:
for brightness in heartbeat_pattern:
red = int((255 * brightness) / 255)
green = int((105 * brightness) / 255)
blue = int((180 * brightness) / 255)
self.grid.pixels_fill((red, green, blue))
self.grid.pixels_show()
utime.sleep(0.05)
pixel = neo_pixel()