-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
picomotodash_neopx.py
118 lines (85 loc) · 2.52 KB
/
picomotodash_neopx.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
# -*- coding: utf-8 -*-
"""Pico Motorcycle Dashboard NEOPX
"""
__author__ = "Salvatore La Bua"
from machine import Pin
import neopixel
PIN_NUM = 3
NUM_LEDS = 37
RGBW = False
MODE_RPM = 0
MODE_COMPASS = 1
MODE_BLEND = 2
MODE_BACKLIGHT = 3
class NEOPX:
def __init__(self, pin=PIN_NUM, n=NUM_LEDS, rgbw=RGBW):
self.n = n
self.np = neopixel.NeoPixel(Pin(pin), n, bpp=4 if rgbw else 3, timing=1)
self.mode = MODE_COMPASS
self.off()
def map_range(self, value, in_range, out_range):
(a, b), (c, d) = in_range, out_range
return (value - a) / (b - a) * (d - c) + c
def off(self):
for i in range(self.n):
self.np[i] = (0, 0, 0)
self.np.write()
def set_np(self, i, rgb_tuple):
self.np[i] = rgb_tuple
self.np.write()
def set_np_rpm(self, rpm):
upto = rpm // 1000
for i in range(24, self.n):
if i < upto + 24:
self.np[i] = (2, 2, 0)
else:
self.np[i] = (0, 0, 0)
self.np.write()
def set_np_compass(self, heading):
# PLACEHOLDER
# TODO Update logic
for i in range(24, self.n):
self.np[i] = (0, 0, 0)
i = int(self.map_range(heading, (0, 360), (34, 24)))
self.np[i] = (0, 0, 5)
self.np.write()
def set_np_blend(self, rpm, heading):
# TODO Update logic
self.off()
upto = rpm // 1000
for i in range(24, upto + 24):
self.np[i] = (2, 2, 0)
i = int(self.map_range(heading, (0, 360), (34, 24)))
self.np[i] = (0, 0, 5)
self.np.write()
def update(self, rpm, heading):
# TODO Update logic
if self.mode == MODE_RPM:
self.set_np_rpm(rpm)
elif self.mode == MODE_COMPASS:
self.set_np_compass(heading)
elif self.mode == MODE_BLEND:
self.set_np_blend(rpm, heading)
def __enter__(self):
return self
def __exit__(self, exception_type, exception_value, traceback):
pass
if __name__ == "__main__":
off = (0, 0, 0)
red = (10, 0, 0)
green = (0, 10, 0)
blue = (0, 0, 10)
cyan = (0, 10, 10)
yellow = (10, 10, 0)
orange = (10, 2, 0)
ring = NEOPX(3, 38)
ring.np[0 + 21] = green
ring.np[1 + 21] = blue
ring.np[2 + 21] = yellow
for i in range(3, 11):
ring.np[i + 21] = cyan
for i in range(11, 14):
ring.np[i + 21] = orange
ring.np[14 + 21] = yellow
ring.np[15 + 21] = red
ring.np.write()