forked from digicosmos86/wiolink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
displays.py
156 lines (132 loc) · 4.97 KB
/
displays.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
155
156
from machine import Pin, PWM
from ssd1306 import SSD1306_I2C
from neopixel import NeoPixel
from wio_link import PORT_MAPPING, DEFAULT_PORTS, GroveDevice, i2c
import time
class Display(GroveDevice):
def __init__(self, type, port):
GroveDevice.__init__(self, type, port)
class OledScreen(Display, SSD1306_I2C):
def __init__(self, port=DEFAULT_PORTS["OledScreen"], width=128, height=64, address=0x3c):
if address not in i2c.scan():
raise OSError("Please check if the OLED Screen is connected to Port 6 or an I2C hub")
Display.__init__(self, "OledScreen", port)
SSD1306_I2C.__init__(self, width, height, i2c, addr=address)
self.max_line = int(self.height/8)
def clear(self):
self.fill(0)
def _check_max_line(self, line):
if line > self.max_line:
raise ValueError("The {0}x{1} display can only support {2} lines of texts".format(self.width, self.height, max_line))
def clear_line(self, line):
self._check_max_line(line)
for y in range(8*(line-1), 8*line):
for x in range(self.width):
self.pixel(x, y, 0)
def write_line(self, line, message):
if not isinstance(message, str):
raise TypeError("The message to be shown can only be strings")
self._check_max_line(line)
self.text(message, 0, 8*(line-1))
def show_line(self, line, message):
self.clear_line(line)
self.write_line(line, message)
self.show()
def show_sensor_data(self, sensor, line):
reading = sensor.show_data(self, line)
return reading
class GrowLight(Display, NeoPixel):
def __init__(self, port=DEFAULT_PORTS["GrowLight"], n=30):
Display.__init__(self, "GrowLight", port)
NeoPixel.__init__(self, pin=Pin(PORT_MAPPING[port]), n=n)
self._on=False
def on(self):
blue_modulo = 3
for i in range(self.n):
NeoPixel.__setitem__(self, i, (255, 0, 0))
if i % blue_modulo == 0:
NeoPixel.__setitem__(self, i, (61, 0, 255))
NeoPixel.write(self)
self._on=True
def off(self):
NeoPixel.fill(self, (0, 0, 0))
NeoPixel.write(self)
self._on=False
def is_on(self):
on = self._on
return on
def blink(self, color=[255, 255, 255], times=3, interval=0.5):
try:
self.off()
for i in range(times):
NeoPixel.fill(self, color)
NeoPixel.write(self)
time.sleep(interval)
NeoPixel.fill(self, [0, 0, 0])
NeoPixel.write(self)
time.sleep(interval)
finally:
self.off()
def demo(self, program="cycle"):
self.off()
try:
n = self.n
if program == "cycle":
for i in range(4 * n):
for j in range(n):
NeoPixel.__setitem__(self, j, [0, 0, 0])
NeoPixel.__setitem__(self, i % n, [255, 255, 255])
NeoPixel.write(self)
time.sleep_ms(25)
elif program == "bounce":
for i in range(4 * n):
for j in range(n):
NeoPixel.__setitem__(self, j, [0, 0, 128])
if (i // n) % 2 == 0:
NeoPixel.__setitem__(self, i % n, [0, 0, 0])
else:
NeoPixel.__setitem__(self, n - 1 - (i % n), [0, 0, 0])
NeoPixel.write(self)
time.sleep_ms(60)
elif program == "fade":
for i in range(0, 4 * 256, 8):
for j in range(n):
if (i // 256) % 2 == 0:
val = i & 0xff
else:
val = 255 - (i & 0xff)
NeoPixel.__setitem__(self, j, [val, 0, 128])
NeoPixel.write(self)
finally:
self.off()
class Led(Display):
def __init__(self, port=DEFAULT_PORTS["Led"]):
Display.__init__(self, "Led", port)
if on:
self.led = PWM(Pin(PORT_MAPPING[port]), freq=50, duty=1023)
else:
self.led = PWM(Pin(PORT_MAPPING[port]), freq=50, duty=0)
self._on = False
def on(self, fade=False, duration=1):
if self._on:
return
if fade:
for i in range(50):
self.led.duty(int(1023*((i+1)/50)))
time.sleep_ms(int(duration*1000/50))
else:
self.led.duty(1023)
self._on=True
def off(self, fade=False, duration=1):
if not self._on:
return
if fade:
for i in range(50):
self.led.duty(1023-int(1023*((i+1)/50)))
time.sleep_ms(int(duration*1000/50))
else:
self.led.duty(0)
self._on=False
def is_on(self):
on = self._on
return on