-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·225 lines (161 loc) · 5.36 KB
/
code.py
File metadata and controls
executable file
·225 lines (161 loc) · 5.36 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from time import sleep, time
from math import sin
import alarm
import board
import displayio
from analogio import AnalogIn
from busio import SPI
from digitalio import DigitalInOut, Pull
from terminalio import FONT
from vectorio import Rectangle
import adafruit_displayio_ssd1305
from DS1302 import DS1302 as Clock
from adafruit_display_text.label import Label
#------------------------------------------------
# For potentiometer
POTENTIOMETER_PIN = board.GP26
potent_analog_in = AnalogIn(POTENTIOMETER_PIN)
def get_voltage(analog_in):
return((analog_in.value * 3.3) / 65025)
# MAX_VOL = 3.3
# MIN_VOL = 0.01
# -----------------------------------------------
# For buttons
FORWARD_PIN = board.GP0
BACK_PIN = board.GP1
SWITCH_PIN = board.GP20
def init_input(PIN):
_input = DigitalInOut(PIN)
_input.switch_to_input(pull=Pull.DOWN)
return _input
forward = init_input(FORWARD_PIN)
back = init_input(BACK_PIN)
switch = init_input(SWITCH_PIN)
#------------------------------------------------
# For RTC / Clock
rtc = Clock()
#------------------------------------------------
# For display
BLACK = displayio.Palette(1)
BLACK[0] = 0x000000
WHITE = displayio.Palette(1)
WHITE[0] = 0xFFFFFF
WIDTH = 128
HEIGHT = 32
OLED_DC_PIN = board.GP8
OLED_CS_PIN = board.GP9
OLED_CLK_PIN = board.GP10
OLED_MOSI_PIN = board.GP11
OLED_RESET_PIN = board.GP12
displayio.release_displays()
spi = SPI(clock=OLED_CLK_PIN, MOSI=OLED_MOSI_PIN)
display_bus = displayio.FourWire(spi, command=OLED_DC_PIN, chip_select=OLED_CS_PIN, baudrate=1000000, reset=OLED_RESET_PIN)
display = adafruit_displayio_ssd1305.SSD1305(display_bus, width=WIDTH, height=HEIGHT)
#-----------------------------------------------
# Draw background
canvas = displayio.Group()
display.show(canvas)
display.rotation = 180
background = displayio.Bitmap(display.width, display.height, 1)
bg_sprite = displayio.TileGrid(background, pixel_shader=BLACK, x=0, y=0)
canvas.append(bg_sprite)
#-----------------------------------------------
def to_sleep():
"""Put microcontroller to sleep"""
switch.deinit()
switch_alarm = alarm.pin.PinAlarm(SWITCH_PIN, value=True, edge=True)
alarm.light_sleep_until_alarms(switch_alarm)
return init_input(SWITCH_PIN)
def draw_text_center(text, y=0):
text_area = Label(FONT, text=text, color=WHITE[0])
text_width = text_area.bounding_box[2]
x_pos = (display.width // 2) - (text_width // 2)
text_group = displayio.Group(x=x_pos, y=y)
text_group.append(text_area)
return text_group
def convert_voltage(voltage):
# return int(voltage//0.033)
return int(voltage//0.066) * 2
def input_status():
return not any([forward.value, back.value, switch.value])
def clock():
global canvas
clock_index = len(canvas)
date_index = clock_index + 1
while input_status():
year, month, day, _, hour, minute, second = rtc.DateTime()
_time = f"{hour:02d}:{minute:02d}:{second:02d}"
_date = f"{day:02d}/{month:02d}/{year}"
clock_text = draw_text_center(_time, 5)
date_text = draw_text_center(_date, 20)
try:
canvas[clock_index] = clock_text
except IndexError:
canvas.append(clock_text)
canvas.append(date_text)
else:
_ = canvas.pop(date_index)
_ = canvas.pop(clock_index)
return
def volume():
global canvas
text_index = len(canvas)
bar_index = text_index + 1
text_group = draw_text_center("Volume", y=11)
canvas.append(text_group)
while input_status():
volts = get_voltage(potent_analog_in)
vol = max(convert_voltage(volts), 1)
vol_bar = Rectangle(pixel_shader=WHITE, width=vol, height=10, x=14, y=21)
try:
canvas[bar_index] = vol_bar
except IndexError:
canvas.append(vol_bar)
else:
_ = canvas.pop(bar_index)
_ = canvas.pop(text_index)
return
def frequency():
global canvas
sine_index = len(canvas)
sine_palette = displayio.Palette(2)
sine_palette[0] = BLACK[0]
sine_palette[1] = WHITE[0]
sine_wave = displayio.Bitmap(display.width, 20, 2)
sw_sprite = displayio.TileGrid(sine_wave, pixel_shader=sine_palette, x=0, y=11)
canvas.append(sw_sprite)
start = time()
while input_status():
h = (start - time()) * 10
volts = get_voltage(potent_analog_in)
b = max(convert_voltage(volts), 2)
sine_wave.fill(0)
for x in range(display.width):
y = int(10 * sin((x - h) / b) + 10)
try:
sine_wave[x,y] = 1
except IndexError:
pass
sine_wave.dirty()
sleep(0.2)
else:
_ = canvas.pop(sine_index)
return
menu = {
0 : clock,
1 : volume,
2 : frequency
}
window = 0
num_windows = len(menu) - 1
while True:
sleep(0.1)
menu[window]()
if forward.value:
window += 1 if window < num_windows else -num_windows
elif back.value:
window -= 1 if window > 0 else -num_windows
while forward.value or back.value:
pass
if switch.value:
switch = to_sleep()