-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpicomotodash_oled_ws10d.py
435 lines (350 loc) · 11.2 KB
/
picomotodash_oled_ws10d.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# -*- coding: utf-8 -*-
"""Pico Motorcycle Dashboard OLED
"""
__author__ = "Salvatore La Bua"
__copyright__ = "Copyright 2021, Salvatore La Bua"
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "Salvatore La Bua"
__email__ = "[email protected]"
__status__ = "Development"
import _thread
import framebuf
import gc
import math
import sh1107
from picomotodash_gps import GPS as pmdGPS
from picomotodash_mpu9250 import MPU as pmdMPU
from picomotodash_neopx import NEOPX as pmdNEOPX
from picomotodash_rpm import RPM as pmdRPM
from picomotodash_utils import moving_avg, normalise_avg, read_gps, read_mpu
from machine import Pin, PWM, SPI
from utime import sleep, sleep_us
gc.enable()
gc.threshold(100000)
# GPIO setup
"""
pwm0 = PWM(Pin(0))
pwm0.freq(300)
pwm0.duty_u16(32768)
pwm1 = PWM(Pin(1))
pwm1.freq(600)
pwm1.duty_u16(32768)
"""
pwm2 = PWM(Pin(2))
pwm2.freq(800)
pwm2.duty_u16(32768)
# Display setup
key0 = Pin(15, Pin.IN, Pin.PULL_UP)
# key1 = Pin(17, Pin.IN, Pin.PULL_UP)
spi1 = SPI(1, baudrate=4_000_000, sck=Pin(10), mosi=Pin(11), miso=None)
display = sh1107.SH1107_SPI(128, 64, spi1, Pin(8), Pin(12), Pin(9), rotate=0)
display.contrast(0x80)
display.fill(0)
display.show()
# GPS setup
gps = pmdGPS(local_offset=9, location_formatting="dd")
# Magnetometer setup
mpu = pmdMPU()
# x_off = 28099.99
# y_off = 30178.57
# z_off = 43987.61
HEADING = 0
headings = []
labels = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"]
# Neopixel setup
PIN_NUM = 3
NUM_LEDS = 37
neopixel_ring = pmdNEOPX(pin=PIN_NUM, n=NUM_LEDS)
# RPM setup
PWM2RPM_FACTOR = 10
RPM_ESTIMATE = 0
rpm_estimates = []
pause_rpm_readings = True
def startup_rpm():
global RPM_ESTIMATE
global rpm_estimates
global pause_rpm_readings
rpm_estimates = []
while RPM_ESTIMATE < 12000:
sleep_us(50)
RPM_ESTIMATE += 1
while RPM_ESTIMATE > 1:
sleep_us(50)
RPM_ESTIMATE -= 1
pause_rpm_readings = False
def decrease_rpm():
global RPM_ESTIMATE
global rpm_estimates
rpm_estimates = []
while RPM_ESTIMATE > 1:
sleep_us(200)
RPM_ESTIMATE -= 1
def show_logo():
with open("SLBLogo128x64_L.pbm", "rb") as f:
f.readline() # Magic number
f.readline() # Creator comment
f.readline() # Dimensions
data = bytearray(f.read())
fbuf_L = framebuf.FrameBuffer(data, 34, 64, framebuf.MONO_HLSB)
with open("SLBLogo128x64_C.pbm", "rb") as f:
f.readline() # Magic number
f.readline() # Creator comment
f.readline() # Dimensions
data = bytearray(f.read())
fbuf_C = framebuf.FrameBuffer(data, 58, 64, framebuf.MONO_HLSB)
with open("SLBLogo128x64_R.pbm", "rb") as f:
f.readline() # Magic number
f.readline() # Creator comment
f.readline() # Dimensions
data = bytearray(f.read())
fbuf_R = framebuf.FrameBuffer(data, 34, 64, framebuf.MONO_HLSB)
display.contrast(255)
# display.invert(1)
# display.show()
x_L = 32
x_C = 35
x_R = 62
# display.blit(fbuf_C, x_C, 0)
"""
display.blit(fbuf_L, x_L, 0)
display.blit(fbuf_R, x_R, 0)
display.fill_rect(62, 2, 2, 60, 1)
display.show()
utime.sleep(1)
"""
for c in range(0, 128, 2):
display.blit(fbuf_L, x_L, 0)
display.blit(fbuf_R, x_R, 0)
display.fill_rect(62, 2, 2, 60, 1)
for r in range(0, 64, 2):
display.line(c, r, 127, r, 0)
display.line(0, r + 1, 127 - c, r + 1, 0)
display.show()
delay = 0.0000001
for x in range(2, 33, 2): # 34
display.fill(0)
delay = delay**0.88
display.blit(fbuf_C, x_C, 0)
display.blit(fbuf_L, round(x_L - x), 0)
display.blit(fbuf_R, round(x_R + x), 0)
# display.text("Loading...", 26, 30, 1)
# display.text("Loading...", 25, 29, 0)
display.show()
sleep(delay)
display.fill_rect(26, 30, 77, 8, 0)
display.fill_rect(28, 30, 4, 8, 1)
display.fill_rect(96, 30, 4, 8, 1)
display.show()
sleep(1)
"""
display.blit(fbuf_L, 0, 0)
display.blit(fbuf_C, x_C, 0)
display.blit(fbuf_R, 94, 0)
display.show()
utime.sleep(10)
"""
for c in range(0, 128, 2):
display.blit(fbuf_L, 0, 0)
display.blit(fbuf_C, x_C, 0)
display.blit(fbuf_R, 94, 0)
for r in range(0, 64, 2):
display.line(0, r, c, r, 0)
display.line(127 - c, r + 1, 127, r + 1, 0)
display.show()
display.fill(1)
display.text("READY!", 41, 30, 1)
display.text("READY!", 40, 29, 0)
display.show()
sleep(1)
display.contrast(5)
# display.invert(0)
display.fill(0)
display.show()
def draw_compass():
x_pos = round((360 - HEADING) / 360 * 240)
for i in range(24):
x_pos_off = (64 + (i * 10) + x_pos) % 240
# display.line(x_post_off, 8, x_post_off, 28, 1)
if x_pos_off >= 0 and x_pos_off <= 127:
if x_pos_off < 64:
x = x_pos_off / 64
x = math.pow(abs(x), 2) * 64
else:
x = (128 - x_pos_off) / 64
x = (64 - (math.pow(abs(x), 2) * 64)) + 64
x = round(x)
if i % 3 == 0:
display.line(x, 3, x, 12, 1)
label = labels[round(i / 3)]
if len(label) == 1:
laboff = 4
else:
laboff = 8
display.text(label, x - laboff, 16, 1)
else:
display.line(x, 4, x, 8, 1)
# display.text(str(round(x)), x, 36, 1)
# display.show()
display.line(0, 0, 127, 0, 1)
display.fill_rect(62, 0, 5, 21, 0)
display.triangle(56, 0, 72, 0, 64, 12, 0, True)
display.triangle(60, 0, 68, 0, 64, 8, 1, True)
display.pixel(59, 0, 1)
# display.pixel(66, 5, 1)
display.pixel(62, 5, 0)
display.pixel(67, 3, 1)
display.pixel(68, 1, 1)
display.pixel(69, 0, 1)
display.pixel(59, 1, 1)
display.pixel(69, 1, 1)
display.pixel(60, 2, 1)
display.pixel(68, 2, 1)
display.fill_rect(63, 0, 3, 20, 1)
display.fill_rect(64, 6, 1, 13, 0)
display.pixel(64, 20, 1)
def draw_icm():
print(
"Acceleration: X = %.3f,\tY = %.3f,\tZ = %.3f"
% (mpu.imu[0], mpu.imu[1], mpu.imu[2])
)
print(
"Gyroscope: X = %.3f,\tY = %.3f,\tZ = %.3f"
% (mpu.imu[3], mpu.imu[4], mpu.imu[5])
)
print(
"Magnetic: X = %.3f,\tY = %.3f,\tZ = %.3f"
% (mpu.imu[6], mpu.imu[7], mpu.imu[8])
)
display.line(0, 26, 127, 26, 1)
display.text("%.1f, %.1f, %.1f" % (mpu.imu[0], mpu.imu[1], mpu.imu[2]), 0, 30)
display.text("%.1f, %.1f, %.1f" % (mpu.imu[3], mpu.imu[4], mpu.imu[5]), 0, 39)
display.text("%.1f, %.1f, %.1f" % (mpu.imu[6], mpu.imu[7], mpu.imu[8]), 0, 48)
def draw_infobox():
display.rect(0, 26, 128, 64 - 48, 1)
# display.fill_rect(1, 29, 126, 64 - 50, 0)
if len(str(round(HEADING))) == 1:
laboff = 4
elif len(str(round(HEADING))) == 2:
laboff = 8
else:
laboff = 12
# display.text(str(round(HEADING)), 66-laboff, 54, 1)
# display.text(str(round(HEADING)), 65-laboff, 53, 0)
display.text(str(round(HEADING)), 64 - 32 - laboff, 31, 1)
if HEADING > 10 and HEADING < 180:
display.triangle(4, 34, 8, 29, 8, 38, 1, True)
display.text("N", 10, 31, 1)
elif HEADING > 180 and HEADING < 350:
display.triangle(64 - 3, 34, 64 - 8, 29, 64 - 8, 38, 1, True)
display.pixel(64 - 3, 34, 0)
display.text("N", 47, 31, 1)
display.fill_rect(64, 28, 1, 12, 1)
# display.text("R:" + f"{str(round(RPM_ESTIMATE)):>5}", 69, 31, 1)
display.text("kph:" + f"{str(round(gps.speed)):>3}", 69, 31, 1)
def draw_gps():
display.text(str(gps.longitude[1]), 0, 0, 1)
display.text(str(gps.latitude[1]), 0, 9, 1)
display.text(str(gps.longitude[0]), 20, 0, 1)
display.text(str(gps.latitude[0]), 20, 9, 1)
# display.text("Time:", 1, 40, 1)
display.text(
"GPS %02d:%02d:%02d"
% (gps.timestamp[0], gps.timestamp[1], gps.timestamp[2]),
0,
48,
1,
)
display.text("Alt %d" % (gps.altitude), 0, 57, 1)
def draw_rpm():
# print("RPM: %.3f" % RPM_ESTIMATE)
display.rect(0, 45, 128, 64 - 45, 1)
display.line(1, 45, 126, 45, 0)
display.fill_rect(0, 47, int(12000 * 128 / 12000), 15, 1)
x = int(RPM_ESTIMATE * 128 / 12000)
display.triangle(x - 9, 47 + 15, x, 47 + 15, x, 47, 0, True)
display.fill_rect(x, 47, 126 - x, 15, 0)
display.line(0, 46, 0, 62, 1)
display.line(127, 46, 127, 62, 1)
for div in range(3, 28):
display.line(div * 4 - 0, 46, div * 4 - 10, 62, 0)
display.triangle(1, 46, 1, 58, 110 - 10, 46, 0, True)
# display.line(104, 47, 104, 61, 1)
for i in range(13):
display.line(i * 11 - 1, 46, i * 11 - 1, 49, 0)
display.pixel(i * 11 - 2, 49, 0)
display.line(i * 11 - 2, 46, i * 11 - 2, 48, 1)
display.line(1, 46, 1, 62, 0)
display.line(126, 46, 126, 62, 0)
"""
display.pixel(113, 51, (int(RPM_ESTIMATE) % 2))
display.pixel(114, 51, (int(RPM_ESTIMATE) % 2))
display.pixel(116, 51, (int(RPM_ESTIMATE) % 2))
display.pixel(117, 51, (int(RPM_ESTIMATE) % 2))
display.fill_rect(112, 52, 7, 2, (int(RPM_ESTIMATE) % 2))
display.fill_rect(113, 54, 5, 1, (int(RPM_ESTIMATE) % 2))
display.fill_rect(114, 55, 3, 1, (int(RPM_ESTIMATE) % 2))
display.pixel(115, 56, (int(RPM_ESTIMATE) % 2))
"""
display.text("R:" + f"{str(round(RPM_ESTIMATE)):>5}", 71, 54, 0)
display.text("R:" + f"{str(round(RPM_ESTIMATE)):>5}", 69, 52, 1)
neopixel_ring.set_np_rpm(RPM_ESTIMATE)
def read_rpm():
global RPM_ESTIMATE
global rpm_estimates
RPM_ESTIMATE = moving_avg(rpm.RPM_ESTIMATE, rpm_estimates, 10)
if rpm.timeout:
# rpm.stop()
# decrease_rpm()
# rpm_estimates = []
rpm.reset()
# rpm.start()
rpm = pmdRPM(pin=22, factor=PWM2RPM_FACTOR)
show_logo()
gc.collect()
# sleep(0.5)
_thread.start_new_thread(startup_rpm, [])
rpm.start()
PAGE_ID = 0
PAGES = 4
# Main loop
try:
while True:
# print(gc.mem_alloc())
if key0.value() == 1:
# sleep(0.1)
read_gps(gps)
read_mpu(mpu)
HEADING = mpu.heading
HEADING = moving_avg(HEADING, headings, 5) # 9
HEADING = normalise_avg(HEADING, headings, neopixel_ring)
if not pause_rpm_readings:
read_rpm()
# neopixel_ring.update(RPM_ESTIMATE, HEADING)
# print(key0.value(), key1.value())
display.fill(0)
if PAGE_ID == 0:
draw_compass()
draw_infobox()
draw_rpm()
pass
elif PAGE_ID == 1:
draw_compass()
draw_icm()
# draw_infobox()
# draw_rpm()
elif PAGE_ID == 2:
# draw_compass()
draw_gps()
draw_infobox()
# draw_rpm()
elif PAGE_ID == 3:
# draw_compass()
# draw_infobox()
draw_rpm()
display.show()
else:
sleep(0.2)
PAGE_ID = (PAGE_ID + 1) % PAGES
except KeyboardInterrupt:
exit()