-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
picomotodash_lcd_ws2.py
1124 lines (904 loc) · 28.2 KB
/
picomotodash_lcd_ws2.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Pico Motorcycle Dashboard LCD
"""
__author__ = "Salvatore La Bua"
__copyright__ = "Copyright 2021, Salvatore La Bua"
__license__ = "GPL"
__version__ = "0.2.2"
__maintainer__ = "Salvatore La Bua"
__email__ = "[email protected]"
__status__ = "Development"
import _thread
import gc
import math
from machine import ADC, Pin, PWM, Timer # enable_irq,; disable_irq,
import picomotodash_env as pmdenv
from picomotodash_ds18x20 import DS18X20 as pmdDS18X20
from picomotodash_utils import map_range, read_adc, read_builtin_temp
import qrcode
from picographics import (
DISPLAY_PICO_DISPLAY_2,
PEN_P4,
PicoGraphics,
)
from utime import (
sleep,
sleep_us,
ticks_ms,
ticks_us,
time,
)
# from pimoroni import RGBLED
from pimoroni_bus import SPIBus
gc.enable()
gc.threshold(100000)
# Parameters
STATE_FILE = "state.json"
CONFIG_FILE = "config.json"
USE_BG_IMAGE = False
BG_IMAGES = ["img0.jpg", "img1.jpg"]
LAYOUT_PEN_ID = 0
UPDATE_INTERVAL = 0.01
USE_TIMEOUT = 3
BUTTON_DEBOUNCE_TIME = 0.25
DS_RESOLUTION = 11
CLIP_MARGIN = 6
FUEL_RESERVE = 25
RPM_MAX = 12000
RPM_REDLINE = 10000
RPM_LAYOUT_ID = 2
SPLIT_BARS = True
LARGE_BATTERY = True
BATTERY_TH = [11, 12]
BATTERY_ICON_DISCRETE = False
TEMP_TH = [19, 24]
TEMP_X = 150
TEMP_X_OFFSET = 100
TEMP_X_SCROLL = -10
TEMP_BAR_OFFSET = 10
INFO_X = 250
INFO_X_MIN = -5000
INFO_X_SCROLL = -10
INFO_SCROLL_DELAY = 0.01
INFO_TEXT = "Salvatore La Bua - http://twitter.com/slabua"
QR_URL = "http://twitter.com/slabua"
PWM2RPM_FACTOR = 10
BACKLIGHT_VALUES = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
BV = 7
# TODO find a better order and make it dynamic (maybe as dictionary)
SCREENS = [
"HOME",
"BATTERY",
"FUEL",
"TEMPERATURE",
"RPM",
"STATS",
]
# Variables initialisation
[
LAYOUT_PEN_ID,
RPM_LAYOUT_ID,
SPLIT_BARS,
LARGE_BATTERY,
BATTERY_ICON_DISCRETE,
BV,
] = pmdenv.read_state(STATE_FILE)
[
USE_BG_IMAGE,
FUEL_RESERVE,
RPM_MAX,
RPM_REDLINE,
BATTERY_TH,
TEMP_TH,
INFO_TEXT,
QR_URL,
] = pmdenv.read_config(CONFIG_FILE)
temp_id = 0
onewire_sensors = 0
# TODO make this variable dynamic according to the number of sensors connected
temperature_matrix = [[], [], []] # type: list[list[float]]
in_use = False
temp_x_pos = TEMP_X
temp_x_shift = TEMP_X_SCROLL
info_x_pos = INFO_X
t = 0
current_screen = 0
last_press_time = 0
# Timer
timer = Timer()
start_time = time()
# Thread
RPM_ESTIMATE = 0
def thread1(PWM2RPM_FACTOR):
global RPM_ESTIMATE
n_repeats = 1
while True:
if pwm22.value() == 1:
cycle_start = ticks_us()
for _ in range(n_repeats):
while pwm22.value() == 1:
# pass
sleep_us(100)
while pwm22.value() == 0:
# pass
sleep_us(100)
cycle_stop = ticks_us()
cycle_duration = cycle_stop - cycle_start
cycle = 1000000 / (cycle_duration / n_repeats)
repeat_factor = ((50 - 20) / (1500 - 150)) * cycle + ( # y = mx + q
50 - ((50 - 20) / (1500 - 150) * 1500)
)
n_repeats = (
int((cycle) / repeat_factor) if int((cycle) / repeat_factor) != 0 else 1
)
RPM_ESTIMATE = cycle * PWM2RPM_FACTOR
# print("RPM: {:.2f}".format(RPM_ESTIMATE), n_repeats, repeat_factor)
# Utility functions
def set_in_use(_):
global in_use
if in_use:
in_use = not in_use
timer.deinit()
print("Timeout released.")
# def in_use_led(in_use):
# if in_use:
# led.set_rgb(64, 0, 0)
# else:
# led.set_rgb(0, 0, 0)
# def blink_led(duration, r, g, b):
# led.set_rgb(r, g, b)
# sleep(duration)
# led.set_rgb(0, 0, 0)
def set_temperature_pen(reading):
if reading < TEMP_TH[0]:
display.set_pen(bluePen)
elif reading >= TEMP_TH[0] and reading < TEMP_TH[1]:
display.set_pen(greenPen)
else:
display.set_pen(redPen)
def set_battery_pen(reading):
if reading < BATTERY_TH[0]:
display.set_pen(custom_pens[1])
elif reading >= BATTERY_TH[0] and reading < BATTERY_TH[1]:
display.set_pen(custom_pens[2])
else:
display.set_pen(custom_pens[3])
def measure_qr_code(size, code):
w, _ = code.get_size()
module_size = int(size / w)
return module_size * w, module_size
def draw_qr_code(ox, oy, size, code):
size, module_size = measure_qr_code(size, code)
display.set_pen(blackPen)
display.rectangle(ox, oy, size, size)
display.set_pen(whitePen)
for x in range(size):
for y in range(size):
if code.get_module(x, y):
display.rectangle(
ox + x * module_size, oy + y * module_size, module_size, module_size
)
# GPIO
temp_builtin = ADC(4) # Built-in temperature sensor
ds_pin = Pin(1) # 1-Wire temperature sensors
ds_sensor = pmdDS18X20(pin=ds_pin, resolution=DS_RESOLUTION)
adc0 = ADC(Pin(26, Pin.IN)) # Battery adc
adc1 = ADC(Pin(27, Pin.IN)) # Fuel adc
adc2 = ADC(Pin(28, Pin.IN)) # RPM adc
# TEST
pwm0 = PWM(Pin(0))
pwm0.freq(275)
pwm0.duty_u16(32768)
###
pwm22 = Pin(22, Pin.IN, Pin.PULL_DOWN) # RPM pwm
# Pico Display boilerplate
spibus = SPIBus(cs=9, dc=8, sck=10, mosi=11)
rst = Pin(12, Pin.OUT, value=1)
bl = Pin(13, Pin.OUT, value=1)
display = PicoGraphics(
display=DISPLAY_PICO_DISPLAY_2, bus=spibus, rotate=180, pen_type=PEN_P4
)
width, height = display.get_bounds()
w_factor = width / 240
h_factor = height / 135
# led = RGBLED(6, 7, 8)
# QR Code
code = qrcode.QRCode()
code.set_text(QR_URL)
max_size = min(width, height)
size, _ = measure_qr_code(max_size, code)
left = int((width // 2) - (size // 2))
top = int((height // 2) - (size // 2))
if USE_BG_IMAGE and width == 240: # TODO check
import jpegdec
j = jpegdec.JPEG(display)
j.open_file(BG_IMAGES[0])
for y in range(height / 20):
j.decode(0, height - (y * 20), jpegdec.JPEG_SCALE_FULL, dither=False)
display.update()
j.decode(0, 0, jpegdec.JPEG_SCALE_FULL, dither=False)
display.update()
sleep(2)
# TODO check
# j.open_file(BG_IMAGES[1])
whitePen = display.create_pen(255, 255, 255)
redPen = display.create_pen(255, 0, 0)
greenPen = display.create_pen(0, 255, 0)
bluePen = display.create_pen(0, 0, 255)
cyanPen = display.create_pen(0, 255, 255)
magentaPen = display.create_pen(255, 0, 255)
yellowPen = display.create_pen(255, 255, 0)
blackPen = display.create_pen(0, 0, 0)
customPen255196000 = display.create_pen(255, 196, 0)
customPen255010010 = display.create_pen(255, 10, 10)
customPen255128010 = display.create_pen(255, 128, 10)
customPen010255010 = display.create_pen(10, 255, 10)
pens = [
whitePen,
redPen,
greenPen,
bluePen,
cyanPen,
magentaPen,
yellowPen,
blackPen,
]
custom_pens = [
customPen255196000,
customPen255010010,
customPen255128010,
customPen010255010,
]
def display_clear():
display.set_pen(blackPen)
display.clear()
def display_init(bv):
display.set_backlight(bv)
display_clear()
display.update()
display_init(BACKLIGHT_VALUES[BV])
gc.collect()
# Buttons
button_a = Pin(2, Pin.IN, Pin.PULL_UP)
button_b = Pin(17, Pin.IN, Pin.PULL_UP)
button_x = Pin(3, Pin.IN, Pin.PULL_UP)
button_y = Pin(15, Pin.IN, Pin.PULL_UP)
# def int_a(pin):
# global in_use
# global current_screen
# button_a.irq(handler=None)
# # state = disable_irq()
# print("Interrupted (A)")
# if not in_use and current_screen != 0:
# current_screen = 0
# else:
# current_screen = (current_screen + 1) % len(SCREENS)
# in_use = True
# timer.init(
# freq=(1 / USE_TIMEOUT),
# mode=Timer.PERIODIC,
# callback=set_in_use,
# )
# display.remove_clip()
# display_clear()
# sleep(BUTTON_DEBOUNCE_TIME)
# button_a.irq(trigger=Pin.IRQ_FALLING, handler=int_a)
# # enable_irq(state)
def int_a(pin):
global in_use
global current_screen
global last_press_time
# button_a.irq(handler=None)
# state = disable_irq()
new_press_time = ticks_ms()
if (new_press_time - last_press_time) > (BUTTON_DEBOUNCE_TIME * 1000):
print("Interrupted (A)")
if not in_use and current_screen != 0:
current_screen = 0
else:
current_screen = (current_screen + 1) % len(SCREENS)
in_use = True
timer.init(
freq=(1 / USE_TIMEOUT),
mode=Timer.PERIODIC,
callback=set_in_use,
)
display.remove_clip()
display_clear()
# sleep(BUTTON_DEBOUNCE_TIME)
# button_a.irq(trigger=Pin.IRQ_FALLING, handler=int_a)
# enable_irq(state)
last_press_time = ticks_ms()
def int_b(pin):
global in_use
global BV
global SPLIT_BARS
global info_x_pos
global LAYOUT_PEN_ID
global last_press_time
# button_b.irq(handler=None)
new_press_time = ticks_ms()
if (new_press_time - last_press_time) > (BUTTON_DEBOUNCE_TIME * 1000):
print("Interrupted (B)")
set_in_use(in_use)
print("Used memory before cleanup: ", gc.mem_alloc())
print("Available memory before cleanup: ", gc.mem_free())
gc.collect()
print("Used memory before cleanup: ", gc.mem_alloc())
print("Available memory after cleanup: ", gc.mem_free())
gc.collect()
if not button_y.value():
info_x_pos = INFO_X
SPLIT_BARS = not SPLIT_BARS
display.remove_clip()
display_clear()
while not button_y.value():
if info_x_pos < INFO_X_MIN:
info_x_pos = INFO_X
else:
info_x_pos += INFO_X_SCROLL
display_clear()
display.set_pen(greenPen)
# display.text(INFO_TEXT, info_x_pos, 8, 10000, 16)
draw_qr_code(left, top, max_size, code)
display.update()
sleep(INFO_SCROLL_DELAY)
if not button_x.value():
if current_screen == 0:
LAYOUT_PEN_ID = (LAYOUT_PEN_ID + 1) % len(pens)
elif current_screen == 5:
# TODO Debouncing might cause the config file overwriting
# once again after initialisation
[
LAYOUT_PEN_ID,
RPM_LAYOUT_ID,
SPLIT_BARS,
LARGE_BATTERY,
BATTERY_ICON_DISCRETE,
BV,
] = pmdenv.initialise_state(STATE_FILE)
# blink_led(0.2, 0, 255, 255)
else:
BV = (BV + 1) % len(BACKLIGHT_VALUES)
display.set_backlight(BACKLIGHT_VALUES[BV])
# sleep(BUTTON_DEBOUNCE_TIME)
# button_b.irq(trigger=Pin.IRQ_FALLING, handler=int_b)
last_press_time = ticks_ms()
def int_x(pin):
global in_use
global temp_id
global temp_x_pos
global temp_x_shift
global BATTERY_ICON_DISCRETE
global RPM_LAYOUT_ID
global last_press_time
# button_x.irq(handler=None)
new_press_time = ticks_ms()
if (new_press_time - last_press_time) > (BUTTON_DEBOUNCE_TIME * 1000):
print("Interrupted (X)")
set_in_use(in_use)
if current_screen == 0:
if temp_x_shift == 0:
temp_x_shift = TEMP_X_SCROLL
else:
temp_x_shift = 0
temp_x_pos = TEMP_X
elif current_screen == 1:
BATTERY_ICON_DISCRETE = not BATTERY_ICON_DISCRETE
elif current_screen == 3:
temp_id = (temp_id + 1) % (1 + onewire_sensors)
elif current_screen == 4:
RPM_LAYOUT_ID = (RPM_LAYOUT_ID + 1) % 5
elif current_screen == 5:
pmdenv.write_state(
STATE_FILE,
LAYOUT_PEN_ID,
RPM_LAYOUT_ID,
SPLIT_BARS,
LARGE_BATTERY,
BATTERY_ICON_DISCRETE,
BV,
)
# blink_led(0.2, 0, 0, 255)
# sleep(BUTTON_DEBOUNCE_TIME)
# button_x.irq(trigger=Pin.IRQ_FALLING, handler=int_x)
last_press_time = ticks_ms()
def int_y(pin):
global in_use
global temp_id
global SPLIT_BARS
global LARGE_BATTERY
global start_time
global temperature_matrix
global last_press_time
# button_y.irq(handler=None)
new_press_time = ticks_ms()
if (new_press_time - last_press_time) > (BUTTON_DEBOUNCE_TIME * 1000):
print("Interrupted (Y)")
set_in_use(in_use)
if current_screen in [0, 2, 4]:
if onewire_sensors == 0 or temp_x_shift != 0:
SPLIT_BARS = not SPLIT_BARS
else:
temp_id = (temp_id + 1) % (1 + onewire_sensors)
elif current_screen == 1:
LARGE_BATTERY = not LARGE_BATTERY
elif current_screen == 3:
temperature_matrix[temp_id] = []
elif current_screen == 5:
start_time = time()
# sleep(BUTTON_DEBOUNCE_TIME)
# button_y.irq(trigger=Pin.IRQ_FALLING, handler=int_y)
last_press_time = ticks_ms()
button_a.irq(trigger=Pin.IRQ_FALLING, handler=int_a)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=int_b)
button_x.irq(trigger=Pin.IRQ_FALLING, handler=int_x)
button_y.irq(trigger=Pin.IRQ_FALLING, handler=int_y)
# Interface
def draw_home_layout(pen):
display.set_pen(pen)
display.rectangle(0, 0, width, 2)
display.rectangle(0, 0, 2, height)
display.rectangle(0, height - 2, width, 2)
display.rectangle(width - 2, 0, 2, height)
display.set_pen(blackPen)
display.rectangle(0, 0, 2, 2)
display.rectangle(width - 2, 0, 2, 2)
display.rectangle(0, height - 2, 2, 2)
display.rectangle(width - 2, height - 2, 2, 2)
display.set_pen(pen)
display.rectangle(1, 1, 2, 2)
display.rectangle(width - 3, 1, 2, 2)
display.rectangle(1, height - 3, 2, 2)
display.rectangle(width - 3, height - 3, 2, 2)
display.rectangle(0, round(height / 4), width, 2)
display.rectangle(0, round(height / 2), width, 2)
display.rectangle(0, round(height / 4 * 3), width, 2)
def draw_home_fuel():
reading = map_range(read_adc(adc1), (0, 65535), (0, 100))
if reading < FUEL_RESERVE:
display.set_pen(redPen)
else:
display.set_pen(custom_pens[0])
display.rectangle(
100,
round(h_factor * 5),
round((width - 100 - CLIP_MARGIN) * reading / 100),
round(h_factor * 25),
)
if SPLIT_BARS:
display.set_pen(blackPen)
for r in range(100, width - CLIP_MARGIN, 34):
display.rectangle(r, round(h_factor * 5), 2, round(h_factor * 25))
if reading < FUEL_RESERVE:
display.set_pen(redPen)
display.text("R", width - round(w_factor * 25), 8, width, round(h_factor * 3))
def draw_home_battery():
reading = map_range(read_adc(adc0), (0, 65535), (0, 16))
set_battery_pen(reading)
display.text(
"{:.2f}".format(reading),
round(w_factor * 150),
8 + 1 * round(h_factor * 34),
width,
round(h_factor * 3),
)
def draw_home_temperature():
global temp_x_pos
global temp_x_shift
if onewire_sensors == 0:
temp_x_shift = 0
if temp_x_shift == 0:
if temp_id == 0:
temperature = read_builtin_temp(temp_builtin)
else:
temperature = ds_sensor.update_temps()[temp_id - 1]
set_temperature_pen(temperature)
display.text(
"T" + str(temp_id) + ":",
temp_x_pos - 50,
8 + 2 * round(h_factor * 34),
width,
round(h_factor * 3),
)
display.text(
"{:.2f}".format(temperature),
round(w_factor * temp_x_pos) - round(w_factor * 3),
8 + 2 * round(h_factor * 34),
width,
round(h_factor * 3),
)
else:
temp_x_tn = TEMP_X_OFFSET
temp_x_pos += temp_x_shift
if temp_x_pos < -150:
temp_x_pos = 250
temperature = read_builtin_temp(temp_builtin)
set_temperature_pen(temperature)
display.text(
"{:.2f}".format(temperature),
round(w_factor * temp_x_pos),
8 + 2 * round(h_factor * 34),
width,
round(h_factor * 3),
)
temperatures = ds_sensor.update_temps()
for ti in range(len(temperatures)):
set_temperature_pen(temperatures[ti])
display.text(
"{:.2f}".format(temperatures[ti]),
round(w_factor * temp_x_pos) + (temp_x_tn * (ti + 1)),
8 + 2 * round(h_factor * 34),
width,
round(h_factor * 3),
)
def draw_home_rpm():
# reading = map_range(read_adc(adc2), (0, 65535), (0, RPM_MAX))
reading = RPM_ESTIMATE
at_redline_width = round((width - 100 - CLIP_MARGIN) * RPM_REDLINE / RPM_MAX)
rpm_width = round((width - 100 - CLIP_MARGIN) * reading / RPM_MAX)
redline_delta = rpm_width - at_redline_width
display.set_pen(cyanPen)
if reading > RPM_REDLINE:
display.rectangle(
100,
round(h_factor * 5) + 3 * round(h_factor * 34),
at_redline_width,
round(h_factor * 24),
)
display.set_pen(redPen)
display.rectangle(
100 + at_redline_width,
round(h_factor * 5) + 3 * round(h_factor * 34),
redline_delta,
round(h_factor * 24),
)
else:
display.rectangle(
100,
round(h_factor * 5) + 3 * round(h_factor * 34),
round((width - 100) * reading / RPM_MAX),
round(h_factor * 24),
)
if SPLIT_BARS:
display.set_pen(blackPen)
for r in range(100, width - CLIP_MARGIN, 10):
display.rectangle(
r,
round(h_factor * 5) + 3 * round(h_factor * 34),
2,
round(h_factor * 24),
)
# Screens
def screen_home():
display_clear()
# Home
draw_home_layout(pens[LAYOUT_PEN_ID])
if LAYOUT_PEN_ID == 7:
display.set_pen(whitePen)
display.text("F >", 10, 8, width, round(h_factor * 3))
display.text("B >", 10, 8 + 1 * round(h_factor * 34), width, round(h_factor * 3))
display.text(
"T >",
10,
8 + 2 * round(h_factor * 34),
width,
round(h_factor * 3),
)
display.text("R >", 10, 8 + 3 * round(h_factor * 34), width, round(h_factor * 3))
display.set_clip(100, 0, width - 100 - CLIP_MARGIN, height)
# Fuel
draw_home_fuel()
# Battery
draw_home_battery()
# Temperature
draw_home_temperature()
# RPM
draw_home_rpm()
display.remove_clip()
display.update()
def screen_battery():
display_clear()
reading = map_range(read_adc(adc0), (0, 65535), (0, 16))
print("ADC0: " + str(reading))
set_battery_pen(reading)
display.rectangle(0, 0, width, round(135 / 3))
display.set_pen(blackPen)
display.text(SCREENS[current_screen], 8, 6, width, 5)
if LARGE_BATTERY:
batt_w_diff = 0
else:
batt_w_diff = 40
set_battery_pen(reading)
display.rectangle(12, 60, 16, 3)
display.rectangle(19, 53, 3, 16)
display.rectangle(10, 72, 20, 4)
display.rectangle(0, 76, 80 - batt_w_diff, 49)
if LARGE_BATTERY:
display.rectangle(52 - batt_w_diff, 60, 16, 3)
display.rectangle(50 - batt_w_diff, 72, 20, 4)
display.set_pen(blackPen)
display.rectangle(14, 75, 12, 3)
display.rectangle(2, 78, 76 - batt_w_diff, 45)
display.rectangle(0, 76, 3, 2)
display.rectangle(77 - batt_w_diff, 76, 3, 2)
display.rectangle(0, 123, 3, 2)
display.rectangle(77 - batt_w_diff, 123, 3, 2)
if LARGE_BATTERY:
display.rectangle(54, 75, 12, 3)
set_battery_pen(reading)
if BATTERY_ICON_DISCRETE:
if reading < 3:
pass
elif reading > 3 and reading < 5:
display.rectangle(2, 116, 76 - batt_w_diff, 7)
elif reading > 5 and reading < 8:
display.rectangle(2, 103, 76 - batt_w_diff, 20)
elif reading > 8 and reading < 11:
display.rectangle(2, 90, 76 - batt_w_diff, 33)
else:
display.rectangle(2, 78, 76 - batt_w_diff, 45)
else:
if reading > 11:
batt_level = 11
else:
batt_level = reading
display.rectangle(
2,
78 + round(45 * (11 - batt_level) / 11),
76 - batt_w_diff,
45 - round(45 * (11 - batt_level) / 11),
)
display.rectangle(1, 77, 3, 2)
display.rectangle(76 - batt_w_diff, 77, 3, 2)
display.rectangle(1, 122, 3, 2)
display.rectangle(76 - batt_w_diff, 122, 3, 2)
if LARGE_BATTERY:
display.text(
"{:.1f}".format(reading) + "",
round(w_factor * 90),
round(h_factor * 71),
width,
9,
)
else:
display.text(
"{:.1f}".format(reading) + "",
round(w_factor * 60),
round(h_factor * 59),
width,
11,
)
display.set_pen(blackPen)
display.rectangle(0, 87, 80 - batt_w_diff, 3)
display.rectangle(0, 99, 80 - batt_w_diff, 3)
display.rectangle(0, 111, 80 - batt_w_diff, 3)
display.update()
def screen_fuel():
display_clear()
reading = map_range(read_adc(adc1), (0, 65535), (0, 100))
print("ADC1: " + str(reading))
if reading < FUEL_RESERVE:
display.set_pen(redPen)
else:
display.set_pen(custom_pens[0])
display.rectangle(
0,
round(height / 3 + 10),
round(width * reading / 100),
round(height / 3 * 2 - 10),
)
display.rectangle(0, 0, width, round(135 / 3))
display.set_pen(blackPen)
display.text(SCREENS[current_screen], 8, 6, width, 5)
if SPLIT_BARS:
for r in range(60, width, 60):
display.rectangle(
r,
round(height / 3 + 10),
3,
round(height / 3 * 2 - 10),
)
if reading < FUEL_RESERVE:
display.set_pen(redPen)
display.text(
"R",
width - round(w_factor * 55),
round(h_factor * 59),
width,
11,
)
display.update()
def screen_temperature():
global temperature_matrix
display_clear()
temperatures = ds_sensor.update_temps()
for ti in range(len(temperatures)):
if len(temperature_matrix[ti]) >= (width / TEMP_BAR_OFFSET):
temperature_matrix[ti].pop(0)
temperature_matrix[ti].append(temperatures[ti])
if isinstance(temperatures[temp_id], float):
print("T" + str(temp_id) + ": " + str(temperatures[temp_id]))
else:
print("Temperature acquisition failed, retrying...")
set_temperature_pen(temperatures[temp_id])
display.rectangle(0, 0, width, round(135 / 3))
curr_x = 0
for t in temperature_matrix[temp_id]:
set_temperature_pen(t)
display.rectangle(
curr_x,
height - (round(t) * 2),
TEMP_BAR_OFFSET - 2,
round(t) * 2,
)
curr_x += TEMP_BAR_OFFSET
display.set_pen(blackPen)
display.text(
"T" + str(temp_id) + ": " + "{:.1f}".format(temperatures[temp_id]) + " c",
8,
6,
width,
5,
)
display.update()
def screen_rpm():
display_clear()
# reading = map_range(read_adc(adc2), (0, 65535), (0, RPM_MAX))
# print("ADC2: " + str(reading))
reading = RPM_ESTIMATE
print("PWM0: " + str(reading))
at_redline_width = round(width * RPM_REDLINE / RPM_MAX)
rpm_width = round(width * reading / RPM_MAX)
redline_delta = rpm_width - at_redline_width
display.set_pen(cyanPen)
if reading > RPM_REDLINE:
display.rectangle(
0, round(height / 3 + 10), at_redline_width, round(height / 3 * 2 - 10)
)
display.set_pen(redPen)
display.rectangle(
at_redline_width,
round(height / 3 + 10),
redline_delta,
round(height / 3 * 2 - 10),
)
else:
display.rectangle(
0,
round(height / 3 + 10),
round(width * reading / RPM_MAX),
round(height / 3 * 2 - 10),
)
"""
if reading > RPM_REDLINE:
display.set_pen(redPen)
else:
display.set_pen(cyanPen)
display.rectangle(
0,
round(height / 3 + 10),
round(width * reading / RPM_MAX),
round(height / 3 * 2 - 10),
)
"""
if SPLIT_BARS:
display.set_pen(blackPen)
for r in range(20, width, 20):
display.rectangle(
r,
round(height / 3 + 10),
3,
round(height / 3 * 2 - 10),
)
display.set_pen(blackPen)
H = height / 3 * 2 - 10
if RPM_LAYOUT_ID == 0:
for x in range(0, width):
display.rectangle(
x,
round(height / 3 + 10),
1,
round((height / 3 * 2 - 10) - ((height / 3 * 2 - 10) * (x / width))),