-
Notifications
You must be signed in to change notification settings - Fork 0
/
artcar-ultrasonic-dist-rp2.py
1194 lines (1060 loc) · 48.7 KB
/
artcar-ultrasonic-dist-rp2.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
# Raspberry Pi Pico 2 project - ArtCar ultrasonic - Oct 2024
#
# Uses 3 ultrasonic sensors, uses oled SPI display to show measured
# temp & humid and uses them to calculate speeed of sound for accurate
# distance measurement.
#
# based on Brad's Arduino UNO and 128x64 OLED Display for rear&front parking sensor
# - speed of sound with temp & humidity correction
# - front- & Rear-facing changed with button #2
# - on Raspberry Pi recalc offsets, and flip sensor graphics
# - on arduino could rotate display and flip,text,some graphics(not sensor)
# - in/cm F/C changed with button #1
# - rear-sensors/front-sebnsors changed with button #2
# - buttons debounced with efficient rp2 interrupts -- nice!
# - ssd1309 SDI or I2C code (sw is ssd1306)
#
# Raspberry Pi GitHub: https://github.com/bradcar/artcar-ultrasonic-dist-rp2
# Arduino GitHub: https://github.com/bradcar/art-car-ultrasonic-dist
# # by bradcar
#
# project Based on GREAT work by upir!!!
# YouTube full video: https://youtu.be/gg08H-6Z1Lo
# created by upir, 2022
#
# https://micropython.org/download/RPI_PICO2/ for latest .uf2 preview
# https://docs.micropython.org/en/latest/esp8266/tutorial/ssd1306.html
# pycharm stubs
# : https://micropython-stubs.readthedocs.io/en/main/packages.html#mp-packages
#
# TODOs
# * add button #3 for switching between showing car on oled or
import time
from math import sqrt, log
from os import uname
from sys import implementation
from time import sleep as zzz
from bme680 import *
# import dht
import ds18x20
import machine
import onewire
import utime
# import RPi.GPIO as GPIO
from framebuf import FrameBuffer, MONO_HLSB
from machine import Pin, I2C
# ic2
# from machine import I2C
# from ssd1306 import SSD1306_I2C
from ssd1306 import SSD1306_SPI
# Constants for setup, for >3 sensors need rewrite
DISP_WIDTH = 128
DISP_HEIGHT = 64
NUMBER_OF_SENSORS = 3
class SensorData:
def __init__(self, echo_pin, trig_pin):
self.trig_pin = trig_pin # TRIG pin for the ultrasonic distance
self.echo_pin = echo_pin # ECHO pin for the ultrasonic distance
self.cm = None # measured distance in CM
self.inch = None # measured distance in inches
self.label_xpos = 0 # x position of the distance label
self.label_ypos = 0 # y position of the distance label
self.label_width = 0 # calculated width of the distance string
self.label_startpos_x = 0 # start X position for the label
self.label_startpos_y = 0 # start Y position for the label
self.label_endpos_x = 0 # end X position for the label
self.label_endpos_y = 0 # end Y position for the label
# === PINS ===
# internal pins
on_pico_temp = machine.ADC(4)
# external pins
uart0 = machine.UART(0, 115200, tx=Pin(0), rx=Pin(1))
button_1 = Pin(2, Pin.IN, Pin.PULL_UP) # interrupt cm/in button pins
button_2 = Pin(3, Pin.IN, Pin.PULL_UP) # interrupt rear/front button pins
button_3 = Pin(15, Pin.IN, Pin.PULL_UP) # extra
# https://www.tomshardware.com/how-to/oled-display-raspberry-pi-pico
# i2c=I2C(0,sda=Pin(12), scl=Pin(13), freq=400000)
# oled = SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, i2c)
# ssd1306 SDI SW setup for ssd1309 SDI
cs = machine.Pin(9) # dummy (any un-used pin), no connection
# scl (SCLK) gp10
# SDA (MOSI) gp11
res = machine.Pin(12) # RES (RST) gp12
dc = machine.Pin(13) # DC gp13
# testing config: set up pins fo ultrasonic sensors using SensorData objects
# sensor = [
# SensorData(echo_pin=4, trig_pin=3),
# SensorData(echo_pin=20, trig_pin=21),
# SensorData(echo_pin=16, trig_pin=17)
# ]
# final config: set up pins fo ultrasonic sensors using SensorData objects
sensor = [
SensorData(echo_pin=20, trig_pin=21),
SensorData(echo_pin=18, trig_pin=19),
SensorData(echo_pin=16, trig_pin=17)
]
trigger = []
echo = []
# set up trigger & echo pings
for i in range(len(sensor)):
trigger_pin = Pin(sensor[i].trig_pin, Pin.OUT)
echo_pin = Pin(sensor[i].echo_pin, Pin.IN)
trigger.append(trigger_pin)
echo.append(echo_pin)
ds_pin = machine.Pin(22)
led = Pin(25, Pin.OUT)
# Pin assignment i2c1
i2c = I2C(id=1, scl=Pin(27), sda=Pin(26))
buzzer = Pin(28, Pin.OUT)
bme = BME680_I2C(i2c=i2c)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
oled_spi = machine.SPI(1)
# print(f"oled_spi:{oled_spi}")
oled = SSD1306_SPI(DISP_WIDTH, DISP_HEIGHT, oled_spi, dc, res, cs)
# === Bitmap DISPLAY IMAGES ====
# image2cpp (convert png into C code): https://javl.github.io/image2cpp/
# const unsigned char bitmap_artcar_image[] PROGMEM = {0xc9,0x04,0x52, ...
# can be bitmap_artcar_image=bytearray(b'\xc9\x04\x59
#
# 'art-car-imag', 56x15px
bitmap_artcar_image_back = bytearray([
0xc9,0x04,0x52,0x91,0x0c,0x08,0x43,0xc8,0x82,0x8e,0x90,0x93,0x10,0x93,0xe0,0x63,
0x08,0x78,0xe0,0xe3,0x07,0xff,0x9f,0xff,0xff,0xff,0xfc,0xff,0xc0,0x00,0x00,0x00,
0x00,0x00,0x03,0x40,0x1c,0xf3,0xe3,0x8e,0x78,0x02,0x42,0x22,0x88,0x84,0x51,0x44,
0x42,0x42,0x22,0x88,0x84,0x11,0x44,0x42,0x42,0x22,0xf0,0x84,0x11,0x78,0x42,0x22,
0x3e,0x88,0x84,0x1f,0x44,0x44,0x10,0x22,0x88,0x84,0x51,0x44,0x08,0x0c,0x22,0x88,
0x83,0x91,0x44,0x30,0x03,0x00,0x00,0x00,0x00,0x00,0xc0,0x00,0xff,0xff,0xff,0xff,
0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
])
# for front sensor
# 'art-car-image-flip', 56x15px
bitmap_artcar_image_front = bytearray([
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0x00,0x03,0x00,
0x00,0x00,0x00,0x00,0xc0,0x0c,0x1c,0xf3,0xe3,0x8e,0x78,0x30,0x10,0x22,0x88,0x84,
0x51,0x44,0x08,0x20,0x22,0x88,0x84,0x11,0x44,0x04,0x4f,0x22,0xf0,0x84,0x11,0x78,
0xf2,0x4f,0x3e,0x88,0x84,0x1f,0x44,0xf2,0x4f,0x22,0x88,0x84,0x51,0x44,0xf2,0x40,
0x22,0x88,0x83,0x91,0x44,0x02,0xc0,0x00,0x00,0x00,0x00,0x00,0x03,0xff,0x9f,0xff,
0xff,0xff,0xf9,0xff,0xe0,0x63,0x08,0x78,0xe0,0xe7,0x07,0xc8,0x82,0x8e,0x90,0x93,
0x10,0x93,0xc9,0x04,0x52,0x91,0x0c,0x08,0x43
])
# 'sensor_0a_off', 32x14px
bitmap_sensor_0a_off = bytearray([
0x00,0x40,0x00,0x00,0x02,0xa8,0x00,0x00,0x05,0x55,0x00,0x00,0x02,0xaa,0xa0,0x00,
0x05,0x55,0x54,0x00,0x0a,0xaa,0xaa,0xa0,0x15,0x55,0x55,0x50,0x0a,0xaa,0xaa,0xa0,
0x01,0x55,0x55,0x40,0x00,0x2a,0xaa,0xa0,0x00,0x15,0x55,0x40,0x00,0x02,0xaa,0xa0,
0x00,0x00,0x15,0x40,0x00,0x00,0x02,0xa0
])
# 'sensor_0a_on', 32x14px
bitmap_sensor_0a_on = bytearray([
0x00,0xc0,0x00,0x00,0x03,0xf8,0x00,0x00,0x07,0xff,0x00,0x00,0x07,0xff,0xe0,0x00,
0x0f,0xff,0xfe,0x00,0x0f,0xff,0xff,0xf0,0x1f,0xff,0xff,0xf0,0x0f,0xff,0xff,0xe0,
0x03,0xff,0xff,0xe0,0x00,0x7f,0xff,0xe0,0x00,0x1f,0xff,0xe0,0x00,0x03,0xff,0xe0,
0x00,0x00,0x3f,0xe0,0x00,0x00,0x03,0xe0
])
# 'sensor_0b_off', 32x16px
bitmap_sensor_0b_off = bytearray([
0x02,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x02,0xa0,0x00,0x00,0x05,0x54,0x00,0x00,
0x0a,0xaa,0x80,0x00,0x15,0x55,0x50,0x00,0x0a,0xaa,0xaa,0x80,0x15,0x55,0x55,0x50,
0x0a,0xaa,0xaa,0xa8,0x05,0x55,0x55,0x50,0x00,0xaa,0xaa,0xa8,0x00,0x15,0x55,0x50,
0x00,0x02,0xaa,0xa0,0x00,0x00,0x55,0x50,0x00,0x00,0x0a,0xa0,0x00,0x00,0x00,0x50
])
# 'sensor_0b_on', 32x16px
bitmap_sensor_0b_on = bytearray([
0x02,0x00,0x00,0x00,0x07,0x80,0x00,0x00,0x07,0xf0,0x00,0x00,0x0f,0xfc,0x00,0x00,
0x0f,0xff,0x80,0x00,0x1f,0xff,0xf8,0x00,0x1f,0xff,0xff,0x80,0x3f,0xff,0xff,0xf8,
0x1f,0xff,0xff,0xf8,0x07,0xff,0xff,0xf8,0x01,0xff,0xff,0xf8,0x00,0x3f,0xff,0xf8,
0x00,0x07,0xff,0xf0,0x00,0x00,0xff,0xf0,0x00,0x00,0x0f,0xf0,0x00,0x00,0x00,0xf0
])
# 'sensor_0c_off', 32x17px
bitmap_sensor_0c_off = bytearray([
0x08,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0a,0x80,0x00,0x00,0x15,0x50,0x00,0x00,
0x2a,0xaa,0x00,0x00,0x15,0x55,0x40,0x00,0x2a,0xaa,0xaa,0x00,0x55,0x55,0x55,0x40,
0x2a,0xaa,0xaa,0xaa,0x15,0x55,0x55,0x54,0x02,0xaa,0xaa,0xa8,0x00,0x55,0x55,0x54,
0x00,0x0a,0xaa,0xa8,0x00,0x01,0x55,0x54,0x00,0x00,0x2a,0xa8,0x00,0x00,0x05,0x54,
0x00,0x00,0x00,0x28
])
# 'sensor_0c_on', 32x17px
bitmap_sensor_0c_on = bytearray([
0x08,0x00,0x00,0x00,0x0f,0x00,0x00,0x00,0x1f,0xc0,0x00,0x00,0x1f,0xf8,0x00,0x00,
0x3f,0xff,0x00,0x00,0x3f,0xff,0xe0,0x00,0x7f,0xff,0xfe,0x00,0x7f,0xff,0xff,0xe0,
0x7f,0xff,0xff,0xfe,0x1f,0xff,0xff,0xfe,0x03,0xff,0xff,0xfc,0x00,0xff,0xff,0xfc,
0x00,0x1f,0xff,0xfc,0x00,0x03,0xff,0xfc,0x00,0x00,0x3f,0xfc,0x00,0x00,0x07,0xfc,
0x00,0x00,0x00,0x3c
])
# 'sensor_0d_off', 32x18px
bitmap_sensor_0d_off = bytearray([
0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x55,0x00,0x00,0x00,0x2a,0x80,0x00,0x00,
0x55,0x50,0x00,0x00,0xaa,0xaa,0x00,0x00,0x55,0x55,0x50,0x00,0xaa,0xaa,0xaa,0x00,
0x55,0x55,0x55,0x50,0x2a,0xaa,0xaa,0xaa,0x05,0x55,0x55,0x54,0x02,0xaa,0xaa,0xaa,
0x00,0x55,0x55,0x54,0x00,0x0a,0xaa,0xaa,0x00,0x01,0x55,0x54,0x00,0x00,0x2a,0xa8,
0x00,0x00,0x01,0x54,0x00,0x00,0x00,0x08
])
# 'sensor_0d_on', 32x18px
bitmap_sensor_0d_on = bytearray([
0x20,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x7f,0x00,0x00,0x00,0x7f,0xc0,0x00,0x00,
0xff,0xf8,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0xf0,0x00,0xff,0xff,0xfe,0x00,
0x7f,0xff,0xff,0xf0,0x3f,0xff,0xff,0xfe,0x0f,0xff,0xff,0xfe,0x03,0xff,0xff,0xfe,
0x00,0x7f,0xff,0xfe,0x00,0x1f,0xff,0xfe,0x00,0x01,0xff,0xfc,0x00,0x00,0x3f,0xfc,
0x00,0x00,0x03,0xfc,0x00,0x00,0x00,0x1c
])
# 'sensor_1a_off', 32x9px
bitmap_sensor_1a_off = bytearray([
0x05,0x55,0x55,0x40,0x0a,0xaa,0xaa,0xa0,0x05,0x55,0x55,0x40,0x0a,0xaa,0xaa,0xa0,
0x05,0x55,0x55,0x40,0x0a,0xaa,0xaa,0xa0,0x05,0x55,0x55,0x40,0x0a,0xaa,0xaa,0xa0,
0x15,0x55,0x55,0x50
])
# 'sensor_1a_on', 32x9px
bitmap_sensor_1a_on = bytearray([
0x07,0xff,0xff,0xc0,0x0f,0xff,0xff,0xe0,0x0f,0xff,0xff,0xe0,0x0f,0xff,0xff,0xe0,
0x0f,0xff,0xff,0xe0,0x0f,0xff,0xff,0xe0,0x0f,0xff,0xff,0xe0,0x0f,0xff,0xff,0xe0,
0x1f,0xff,0xff,0xf0
])
# 'sensor_1b_off', 32x9px
bitmap_sensor_1b_off = bytearray([
0x15,0x55,0x55,0x50,0x0a,0xaa,0xaa,0xa0,0x15,0x55,0x55,0x50,0x0a,0xaa,0xaa,0xa0,
0x15,0x55,0x55,0x50,0x2a,0xaa,0xaa,0xa8,0x15,0x55,0x55,0x50,0x2a,0xaa,0xaa,0xa8,
0x05,0x55,0x55,0x40
])
# 'sensor_1b_on', 32x9px
bitmap_sensor_1b_on = bytearray([
0x1f,0xff,0xff,0xf0,0x1f,0xff,0xff,0xf0,0x1f,0xff,0xff,0xf0,0x1f,0xff,0xff,0xf0,
0x3f,0xff,0xff,0xf8,0x3f,0xff,0xff,0xf8,0x3f,0xff,0xff,0xf8,0x3f,0xff,0xff,0xf8,
0x0f,0xff,0xff,0xe0
])
# 'sensor_1c_off', 32x10px
bitmap_sensor_1c_off = bytearray([
0x10,0x00,0x00,0x04,0x0a,0xaa,0xaa,0xa8,0x15,0x55,0x55,0x54,0x2a,0xaa,0xaa,0xaa,
0x15,0x55,0x55,0x54,0x2a,0xaa,0xaa,0xaa,0x15,0x55,0x55,0x54,0x2a,0xaa,0xaa,0xaa,
0x15,0x55,0x55,0x54,0x0a,0xaa,0xaa,0xa8
])
# 'sensor_1c_on', 32x10px
bitmap_sensor_1c_on = bytearray([
0x18,0x00,0x00,0x0c,0x1f,0xff,0xff,0xfc,0x3f,0xff,0xff,0xfe,0x3f,0xff,0xff,0xfe,
0x3f,0xff,0xff,0xfe,0x3f,0xff,0xff,0xfe,0x3f,0xff,0xff,0xfe,0x3f,0xff,0xff,0xfe,
0x3f,0xff,0xff,0xfe,0x0f,0xff,0xff,0xf8
])
# 'sensor_1d_off', 40x10px
bitmap_sensor_1d_off = bytearray([
0x02,0x80,0x00,0x00,0x28,0x01,0x55,0x55,0x55,0x50,0x02,0xaa,0xaa,0xaa,0xa8,0x01,
0x55,0x55,0x55,0x50,0x02,0xaa,0xaa,0xaa,0xa8,0x05,0x55,0x55,0x55,0x54,0x02,0xaa,
0xaa,0xaa,0xa8,0x05,0x55,0x55,0x55,0x54,0x02,0xaa,0xaa,0xaa,0xa8,0x00,0x55,0x55,
0x55,0x40
])
# 'sensor_1d_on', 40x10px
bitmap_sensor_1d_on = bytearray([
0x03,0x80,0x00,0x00,0x38,0x03,0xff,0xff,0xff,0xf8,0x03,0xff,0xff,0xff,0xf8,0x03,
0xff,0xff,0xff,0xf8,0x03,0xff,0xff,0xff,0xf8,0x07,0xff,0xff,0xff,0xfc,0x07,0xff,
0xff,0xff,0xfc,0x07,0xff,0xff,0xff,0xfc,0x07,0xff,0xff,0xff,0xfc,0x00,0xff,0xff,
0xff,0xe0
])
# 'sensor_2a_off', 32x14px
bitmap_sensor_2a_off = bytearray([
0x00,0x00,0x04,0x00,0x00,0x00,0x2a,0x80,0x00,0x01,0x55,0x40,0x00,0x0a,0xaa,0x80,
0x00,0x55,0x55,0x40,0x0a,0xaa,0xaa,0xa0,0x15,0x55,0x55,0x50,0x0a,0xaa,0xaa,0xa0,
0x05,0x55,0x55,0x00,0x0a,0xaa,0xa8,0x00,0x05,0x55,0x50,0x00,0x0a,0xaa,0x80,0x00,
0x05,0x50,0x00,0x00,0x0a,0x80,0x00,0x00
])
# 'sensor_2a_on', 32x14px
bitmap_sensor_2a_on = bytearray([
0x00,0x00,0x06,0x00,0x00,0x00,0x3f,0x80,0x00,0x01,0xff,0xc0,0x00,0x0f,0xff,0xc0,
0x00,0xff,0xff,0xe0,0x1f,0xff,0xff,0xe0,0x1f,0xff,0xff,0xf0,0x0f,0xff,0xff,0xe0,
0x0f,0xff,0xff,0x80,0x0f,0xff,0xfc,0x00,0x0f,0xff,0xf0,0x00,0x0f,0xff,0x80,0x00,
0x0f,0xf8,0x00,0x00,0x0f,0x80,0x00,0x00
])
# 'sensor_2b_off', 32x16px
bitmap_sensor_2b_off = bytearray([
0x00,0x00,0x00,0x40,0x00,0x00,0x00,0xa0,0x00,0x00,0x05,0x40,0x00,0x00,0x2a,0xa0,
0x00,0x01,0x55,0x50,0x00,0x0a,0xaa,0xa8,0x01,0x55,0x55,0x50,0x0a,0xaa,0xaa,0xa8,
0x15,0x55,0x55,0x50,0x0a,0xaa,0xaa,0xa0,0x15,0x55,0x55,0x00,0x0a,0xaa,0xa8,0x00,
0x05,0x55,0x40,0x00,0x0a,0xaa,0x00,0x00,0x05,0x50,0x00,0x00,0x0a,0x00,0x00,0x00
])
# 'sensor_2b_on', 32x16px
bitmap_sensor_2b_on = bytearray([
0x00,0x00,0x00,0x40,0x00,0x00,0x01,0xe0,0x00,0x00,0x0f,0xe0,0x00,0x00,0x3f,0xf0,
0x00,0x01,0xff,0xf0,0x00,0x1f,0xff,0xf8,0x01,0xff,0xff,0xf8,0x1f,0xff,0xff,0xfc,
0x1f,0xff,0xff,0xf8,0x1f,0xff,0xff,0xe0,0x1f,0xff,0xff,0x80,0x1f,0xff,0xfc,0x00,
0x0f,0xff,0xe0,0x00,0x0f,0xff,0x00,0x00,0x0f,0xf0,0x00,0x00,0x0f,0x00,0x00,0x00
])
# 'sensor_2c_off', 32x17px
bitmap_sensor_2c_off = bytearray([
0x00,0x00,0x00,0x10,0x00,0x00,0x00,0xa0,0x00,0x00,0x01,0x50,0x00,0x00,0x0a,0xa8,
0x00,0x00,0x55,0x54,0x00,0x02,0xaa,0xa8,0x00,0x55,0x55,0x54,0x02,0xaa,0xaa,0xaa,
0x55,0x55,0x55,0x54,0x2a,0xaa,0xaa,0xa8,0x15,0x55,0x55,0x40,0x2a,0xaa,0xaa,0x00,
0x15,0x55,0x50,0x00,0x2a,0xaa,0x80,0x00,0x15,0x54,0x00,0x00,0x2a,0xa0,0x00,0x00,
0x14,0x00,0x00,0x00
])
# 'sensor_2c_on', 32x17px
bitmap_sensor_2c_on = bytearray([
0x00,0x00,0x00,0x10,0x00,0x00,0x00,0xf0,0x00,0x00,0x03,0xf8,0x00,0x00,0x1f,0xf8,
0x00,0x00,0xff,0xfc,0x00,0x07,0xff,0xfc,0x00,0x7f,0xff,0xfe,0x07,0xff,0xff,0xfe,
0x7f,0xff,0xff,0xfe,0x7f,0xff,0xff,0xf8,0x3f,0xff,0xff,0xc0,0x3f,0xff,0xff,0x00,
0x3f,0xff,0xf8,0x00,0x3f,0xff,0xc0,0x00,0x3f,0xfc,0x00,0x00,0x3f,0xe0,0x00,0x00,
0x3c,0x00,0x00,0x00
])
# 'sensor_2d_off', 32x18px
bitmap_sensor_2d_off = bytearray([
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x01,0x54,0x00,0x00,0x02,0xa8,
0x00,0x00,0x15,0x54,0x00,0x00,0xaa,0xaa,0x00,0x15,0x55,0x54,0x00,0xaa,0xaa,0xaa,
0x15,0x55,0x55,0x54,0xaa,0xaa,0xaa,0xa8,0x55,0x55,0x55,0x40,0xaa,0xaa,0xaa,0x80,
0x55,0x55,0x54,0x00,0xaa,0xaa,0xa0,0x00,0x55,0x55,0x00,0x00,0x2a,0xa8,0x00,0x00,
0x55,0x00,0x00,0x00,0x20,0x00,0x00,0x00
])
# 'sensor_2d_on', 32x18px
bitmap_sensor_2d_on = bytearray([
0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x38,0x00,0x00,0x01,0xfc,0x00,0x00,0x07,0xfc,
0x00,0x00,0x3f,0xfe,0x00,0x01,0xff,0xfe,0x00,0x1f,0xff,0xfe,0x00,0xff,0xff,0xfe,
0x1f,0xff,0xff,0xfc,0xff,0xff,0xff,0xf8,0xff,0xff,0xff,0xe0,0xff,0xff,0xff,0x80,
0xff,0xff,0xfc,0x00,0xff,0xff,0xf0,0x00,0x7f,0xff,0x00,0x00,0x7f,0xf8,0x00,0x00,
0x7f,0x80,0x00,0x00,0x70,0x00,0x00,0x00
])
# Declare global variables for flipped 1 bitmaps
flipped_bitmap_sensor_0a_off = bytearray()
flipped_bitmap_sensor_0a_on = bytearray()
flipped_bitmap_sensor_0b_off = bytearray()
flipped_bitmap_sensor_0b_on = bytearray()
flipped_bitmap_sensor_0c_off = bytearray()
flipped_bitmap_sensor_0c_on = bytearray()
flipped_bitmap_sensor_0d_off = bytearray()
flipped_bitmap_sensor_0d_on = bytearray()
# Declare global variables for flipped 2 bitmaps
flipped_bitmap_sensor_1a_off = bytearray()
flipped_bitmap_sensor_1a_on = bytearray()
flipped_bitmap_sensor_1b_off = bytearray()
flipped_bitmap_sensor_1b_on = bytearray()
flipped_bitmap_sensor_1c_off = bytearray()
flipped_bitmap_sensor_1c_on = bytearray()
flipped_bitmap_sensor_1d_off = bytearray()
flipped_bitmap_sensor_1d_on = bytearray()
# Declare global variables for flipped 3 bitmaps
flipped_bitmap_sensor_2a_off = bytearray()
flipped_bitmap_sensor_2a_on = bytearray()
flipped_bitmap_sensor_2b_off = bytearray()
flipped_bitmap_sensor_2b_on = bytearray()
flipped_bitmap_sensor_2c_off = bytearray()
flipped_bitmap_sensor_2c_on = bytearray()
flipped_bitmap_sensor_2d_off = bytearray()
flipped_bitmap_sensor_2d_on = bytearray()
#
# rear 'unit_cm', 24x10px
bitmap_unit_cm = bytearray([
0xf0,0x03,0xc0,0x80,0x00,0x40,0xbe,0xff,0x40,0xb6,0xdb,0x40,0x30,0xdb,0x00,0x30,
0xdb,0x00,0xb6,0xdb,0x40,0xbe,0xdb,0x40,0x80,0x00,0x40,0xf0,0x03,0xc0
])
# 'unit in', 24x10px
bitmap_unit_in = bytearray([
0xf0,0x03,0xc0,0x80,0x00,0x40,0x8c,0x7c,0x40,0x8c,0x6c,0x40,0x0c,0x6c,0x00,0x0c,
0x6c,0x00,0x8c,0x6c,0x40,0x8c,0x6c,0x40,0x80,0x00,0x40,0xf0,0x03,0xc0
])
# 'degree-temp', 24x10px
degree_temp = bytearray([
0xf0,0x00,0x0f,0x80,0x00,0x01,0x80,0x00,0x0d,0x80,0x00,0x0d,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x00,0x01,0x80,0x00,0x01,0x80,0x00,0x01,0xf0,0x00,0x0f
])
MIN_CM_DIST = 2.0 # cm min for tiles
MAX_CM_DIST = 100.0 # cm max where text stops moving for tiles
DWELL_MS_LOOP = 100
SPEED_SOUND_20C_70H = 343.294
PDX_SLP_1013 = 1011.00
OVER_TEMP_WARNING = 70.0
# Button debouncer with efficient interrupts, which don't take CPU cycles!
# https://electrocredible.com/raspberry-pi-pico-external-interrupts-button-micropython/
def callback(pin):
global button_1_pushed, button_2_pushed, button_3_pushed
global debounce_1_time, debounce_2_time, debounce_3_time
if pin == button_1 and (int(time.ticks_ms()) - debounce_1_time) > 500:
button_1_pushed = True
debounce_1_time = time.ticks_ms()
elif pin == button_2 and (int(time.ticks_ms()) - debounce_2_time) > 500:
button_2_pushed = True
debounce_2_time = time.ticks_ms()
elif pin == button_3 and (int(time.ticks_ms()) - debounce_3_time) > 500:
button_3_pushed = True
debounce_3_time = time.ticks_ms()
button_1.irq(trigger=Pin.IRQ_FALLING, handler=callback)
button_2.irq(trigger=Pin.IRQ_FALLING, handler=callback)
button_3.irq(trigger=Pin.IRQ_FALLING, handler=callback)
# Functions =================================================
def button1():
global button_1_pushed
if button_1_pushed:
button_1_pushed = False
print("button 1 pushed")
return True
else:
return False
def button2():
global button_2_pushed
if button_2_pushed:
button_2_pushed = False
print("button 2 pushed")
return True
else:
return False
def button3():
global button_3_pushed
if button_3_pushed:
button_3_pushed = False
return True
else:
return False
def initialize_flipped_bitmaps():
"""
vertically flip the bitmaps and store them in global variables.
"""
global flipped_bitmap_sensor_0a_off, flipped_bitmap_sensor_0a_on
global flipped_bitmap_sensor_0b_off, flipped_bitmap_sensor_0b_on
global flipped_bitmap_sensor_0c_off, flipped_bitmap_sensor_0c_on
global flipped_bitmap_sensor_0d_off, flipped_bitmap_sensor_0d_on
global flipped_bitmap_sensor_1a_off, flipped_bitmap_sensor_1a_on
global flipped_bitmap_sensor_1b_off, flipped_bitmap_sensor_1b_on
global flipped_bitmap_sensor_1c_off, flipped_bitmap_sensor_1c_on
global flipped_bitmap_sensor_1d_off, flipped_bitmap_sensor_1d_on
global flipped_bitmap_sensor_2a_off, flipped_bitmap_sensor_2a_on
global flipped_bitmap_sensor_2b_off, flipped_bitmap_sensor_2b_on
global flipped_bitmap_sensor_2c_off, flipped_bitmap_sensor_2c_on
global flipped_bitmap_sensor_2d_off, flipped_bitmap_sensor_2d_on
# flip bitmaps: for front tile 1, use rear flipped tile 3
flipped_bitmap_sensor_0a_off = flip_bitmap_vert(bitmap_sensor_2a_off, 32, 14)
flipped_bitmap_sensor_0a_on = flip_bitmap_vert(bitmap_sensor_2a_on, 32, 14)
flipped_bitmap_sensor_0b_off = flip_bitmap_vert(bitmap_sensor_2b_off, 32, 16)
flipped_bitmap_sensor_0b_on = flip_bitmap_vert(bitmap_sensor_2b_on, 32, 16)
flipped_bitmap_sensor_0c_off = flip_bitmap_vert(bitmap_sensor_2c_off, 32, 17)
flipped_bitmap_sensor_0c_on = flip_bitmap_vert(bitmap_sensor_2c_on, 32, 17)
flipped_bitmap_sensor_0d_off = flip_bitmap_vert(bitmap_sensor_2d_off, 32, 18)
flipped_bitmap_sensor_0d_on = flip_bitmap_vert(bitmap_sensor_2d_on, 32, 18)
# flip bitmaps: middle tile 2 is same for front/rear
flipped_bitmap_sensor_1a_off = flip_bitmap_vert(bitmap_sensor_1a_off, 32, 9)
flipped_bitmap_sensor_1a_on = flip_bitmap_vert(bitmap_sensor_1a_on, 32, 9)
flipped_bitmap_sensor_1b_off = flip_bitmap_vert(bitmap_sensor_1b_off, 32, 9)
flipped_bitmap_sensor_1b_on = flip_bitmap_vert(bitmap_sensor_1b_on, 32, 9)
flipped_bitmap_sensor_1c_off = flip_bitmap_vert(bitmap_sensor_1c_off, 32, 10)
flipped_bitmap_sensor_1c_on = flip_bitmap_vert(bitmap_sensor_1c_on, 32, 10)
flipped_bitmap_sensor_1d_off = flip_bitmap_vert(bitmap_sensor_1d_off, 40, 10)
flipped_bitmap_sensor_1d_on = flip_bitmap_vert(bitmap_sensor_1d_on, 40, 10)
# flip bitmaps: for front tile 1, use rear flipped tile 3
flipped_bitmap_sensor_2a_off = flip_bitmap_vert(bitmap_sensor_0a_off, 32, 14)
flipped_bitmap_sensor_2a_on = flip_bitmap_vert(bitmap_sensor_0a_on, 32, 14)
flipped_bitmap_sensor_2b_off = flip_bitmap_vert(bitmap_sensor_0b_off, 32, 16)
flipped_bitmap_sensor_2b_on = flip_bitmap_vert(bitmap_sensor_0b_on, 32, 16)
flipped_bitmap_sensor_2c_off = flip_bitmap_vert(bitmap_sensor_0c_off, 32, 17)
flipped_bitmap_sensor_2c_on = flip_bitmap_vert(bitmap_sensor_0c_on, 32, 17)
flipped_bitmap_sensor_2d_off = flip_bitmap_vert(bitmap_sensor_0d_off, 32, 18)
flipped_bitmap_sensor_2d_on = flip_bitmap_vert(bitmap_sensor_0d_on, 32, 18)
return
# Helper functions to replace Arduino-specific functions
def constrain(val, min_val, max_val):
return max(min_val, min(val, max_val))
def map_range(value, in_min, in_max, out_min, out_max):
return int((value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min)
def get_str_width(text):
# MicroPython has 8x8 fonts x*9-1 ?
return len(text)
def onboard_temperature():
"""
# pico data pico 2 rp2350 data sheet on page 1068
# data sheet says 27C is 0.706v, with a slope of -1.721mV per degree
"""
adc_value = on_pico_temp.read_u16()
volt = (3.3 / 65535) * adc_value
celsius = 27 - (volt - 0.706) / 0.001721
if debug: print(f"on chip temp = {celsius:.3f}C")
return celsius
def calc_sea_level_pressure(hpa, meters):
"""
Calculate the sea level pressure from the hpa pressure at a known elevation
:param :sea_level: sea level hpa from closest airport
:returns: sea level hpa based on known altitude
"""
sea_level_pressure_hpa = hpa / (1.0 - (meters / 44330.0)) ** 5.255
return sea_level_pressure_hpa
def bme_temp_humid_hpa_iaq_alt(sea_level):
"""
read temp, humidity, pressure, Indoor Air Qualtiy (IAQ) from the BME680 sensor
measurement takes ~189ms
IAQ: 0- 50 good
51-100 average
101-150 poor
151-200 bad
201-300 worse
301-500 very bad
:param :sea_level: sea level hpa from closest airport
:returns: temp_c, percent_humidity, hpa_pressure, iaq, meters, error string
"""
# Read sensor data
debug = True
try:
temp_c = bme.temperature
percent_humidity = bme.humidity
hpa_pressure = bme.pressure
gas_resist = bme.gas/100
# derived sensor data
meters = 44330.0 * (1.0 - (hpa_pressure/sea_level)**(1.0/5.255) )
iaq = log(gas_resist) + 0.04 * percent_humidity
if debug:
print(f"BME680 Temp °C = {temp_c:.2f} C")
print(f"BME680 Humidity = {percent_humidity:.1f} %")
print(f"BME680 Pressure = {hpa_pressure:.2f} hPA")
print(f"BME680 iaq = {iaq:.1f} IAQ lower better")
print(f"BME680 Alt = {meters * 3.28084:.2f} feet \n")
except OSError as e:
print("BME680: Failed to read sensor.")
return None, None, None, None, None, "ERROR_BME680:" + str(e)
return temp_c, percent_humidity, hpa_pressure, iaq, meters, None
def outside_temp_ds_init():
"""
init onewire, this is special code for this use we are only asking outside temp
every 3 seconds so do init in setup, then read temp and prepare for next in
outside_temp_ds()
:returns: error string if happens
"""
try:
ds_sensor.convert_temp()
except onewire.OneWireError as e:
print("DS temp onewire: " + str(e) + '\n')
return "ERROR_DS_TEMP_ONEWIRE:" + str(e)
return None
def outside_temp_ds():
"""
The ouside temp DS sensor (waterproof DS18B20), measurement takes ~6ms
Because we must call ds_sensor.convert_temp() at least 750ms before read data,
we will call outside_temp_ds_init() in setup (it wraps this function)
Notice at the end of this function we call ds_sensor.convert_temp() in order to
have enough time before we read again.
We ONLY call this function once every 3 seconds, which means there will
always be at least 3 seconds between .convert_temp() & .read_temp(rom)
:returns: temp Celsius & error string
"""
celsius = False
# NOTE: if function is used to both setup & test temp, need to uncomment code below
# try:
# ds_sensor.convert_temp()
# except onewire.OneWireError as e:
# print("DS temp onewire: " + str(e) + '\n')
# return None, "ERROR_DS_TEMP_ONEWIRE:"+ str(e)
# # must sleep 750ms before read 1st value
# zzz(.75)
for rom_id in roms:
celsius = ds_sensor.read_temp(rom_id)
if debug: print(f"rom_id={rom_id}\n tempC={celsius:.2f}")
# prepare for next call, which in this main loop is ~3 seconds later
try:
ds_sensor.convert_temp()
except onewire.OneWireError as e:
print("DS temp onewire: " + str(e) + '\n')
return None, "ERROR_DS_TEMP_ONEWIRE:" + str(e)
return celsius, None
def calc_speed_sound(celsius, percent_humidity):
"""
calc to update speed of sound based on temp & humidity from the DHT22 sensor
:param celsius: temp in Celsius
:param percent_humidity: % humidity
:returns: speed meter/sec, error string
"""
if celsius > 0 and percent_humidity > 0:
# Speed sound with temp correction: (20.05 * sqrt(273.16 + temp_c))
# online temp/humid calc:
# http://resource.npl.co.uk/acoustics/techguides/speedair/
# created spreadsheet of diffs between above temp formula and online temp/humid
# did a 2d linear fit to create my own correction, error is now +/-0.07%
# valid for 0C to 30C temp & 75 to 102 kPa pressure
meter_per_sec = (20.05 * sqrt(273.16 + celsius)) \
+ (0.0006545 * percent_humidity + 0.00475) * celsius \
+ (0.001057 * percent_humidity + 0.07121)
return meter_per_sec, None
else:
return None, f"ERROR_INVALID_SOUND_SPEED:temp={celsius}C,humidity={percent_humidity}%"
def calculate_checksum(data_buffer):
"""
Function to calculate checksum, make sure only 1 byte returned
"""
return (data_buffer[0] + data_buffer[1] + data_buffer[2]) & 0x00ff
def ultrasonic_distance_pwm(j, speed_of_sound, timeout=50000):
"""
Get ultrasonic distance from a sensor where ping and measure with a timeout.
HC-SR04: most Send a 10uS high to trigger (default mode 1)
JSN-SR04T: Send a 20uS high to trigger, instead of 10
A02YYUW: only outputs UART serial data, must use ultrasonic_distance_uart(i)
https://dronebotworkshop.com/waterproof-ultrasonic/
:param j: index for ultrasonic sensor.
:param speed_of_sound: speed of sound for distance calculation
:param timeout: Max usecs to wait for the echo signal return. default 50ms.
:returns: cm distance or error string.
"""
trigger[j].low()
utime.sleep_us(2)
trigger[j].high()
utime.sleep_us(20) # JSN-SR04T: best with 20uS high to trigger instead of 10
trigger[j].low()
# Wait for echo to go high, with timeout
start = utime.ticks_us()
while echo[j].value() == 0:
if utime.ticks_diff(utime.ticks_us(), start) > timeout:
return 0.0, f"ULTRASONIC_ERROR: Sensor {i} - too close or malfunction."
signal_off = utime.ticks_us()
# Wait for echo to go low, with timeout
start = utime.ticks_us()
while echo[j].value() == 1:
if utime.ticks_diff(utime.ticks_us(), start) > timeout:
return 300.0, f"ULTRASONIC_ERROR: Sensor {i} - no signal returned, too far."
signal_on = utime.ticks_us()
# Calculate cm distance
distance_in_cm = utime.ticks_diff(signal_on, signal_off) \
* (speed_of_sound / 20000.0)
return distance_in_cm, None
def flip_bitmap_vert(bitmap, width, height):
"""
Flip a byte array bitmap vertically.
:param bitmap: byte array of image
:param width: pixel width of the image (must be divisible by 8)
:param height: pixel height of the image
:return: vertically flipped bytearray (top for bottom)
"""
# Each row has (width // 8), 8 pixels = 1 byte
row_size = width // 8
flipped_bitmap = bytearray(len(bitmap))
# reverse the order of each row
for y in range(height):
# Copy each row from the original image
start_index = y * row_size
end_index = start_index + row_size
row_data = bitmap[start_index:end_index]
# Place it in the flipped position in the new byte array
flipped_start = (height - y - 1) * row_size
flipped_bitmap[flipped_start:flipped_start + row_size] = row_data
return flipped_bitmap
def blit_white_only(source_fb, w, h, x, y):
"""
only send white pixels one by one
:param source_fb: framebuffer to flip
:param w: width of the framebuffer
:param h: height of the framebuffer
:param x: x-position for display
:param y: y-position for display
"""
for row in range(h):
for col in range(w):
# Extract pixel from the source frame buffer
pixel = source_fb.pixel(col, row)
if pixel == 1: # Only copy white pixels
oled.pixel(x + col, y + row, 1) # Set the pixel on the OLED
def display_car(celsius, fahrenheit):
"""
display_car & temp, Need oled.fill(0) before call & oled.show() after call
:param celsius: temp in C to display
:param fahrenheit: temp in F to display
"""
if rear:
oled.blit(FrameBuffer(bitmap_artcar_image_back, 56, 15, MONO_HLSB), 36, 0)
oled.blit(FrameBuffer(degree_temp, 24, 10, MONO_HLSB), 104, 0)
if metric:
oled.blit(FrameBuffer(bitmap_unit_cm, 24, 10, MONO_HLSB), 0, 0)
if celsius:
oled.text(f"{celsius:.0f}", 108, 2)
else:
oled.text("xx", 108, 2)
else:
oled.blit(FrameBuffer(bitmap_unit_in, 24, 10, MONO_HLSB), 0, 0)
if fahrenheit:
oled.text(f"{fahrenheit:.0f}", 108, 2)
else:
oled.text("xx", 108, 2)
else:
oled.blit(FrameBuffer(bitmap_artcar_image_front, 56, 15, MONO_HLSB), 36, DISP_HEIGHT - 15)
oled.blit(FrameBuffer(degree_temp, 24, 10, MONO_HLSB), 104, DISP_HEIGHT - 10)
if metric:
oled.blit(FrameBuffer(bitmap_unit_cm, 24, 10, MONO_HLSB), 0, DISP_HEIGHT - 10)
if celsius:
oled.text(f"{celsius:.0f}", 108, DISP_HEIGHT - 8)
else:
oled.text("xx", 108, DISP_HEIGHT - 8)
else:
oled.blit(FrameBuffer(bitmap_unit_in, 24, 10, MONO_HLSB), 0, DISP_HEIGHT - 10)
if fahrenheit:
oled.text(f"{fahrenheit:.0f}", 108, DISP_HEIGHT - 8)
else:
oled.text("xx", 108, DISP_HEIGHT - 8)
return
def display_tiles_dist():
"""
display_car & temp, Need oled.show() after call
"""
if not working_ultrasonics:
oled.text(" No Ultrasonic", 5, 30)
oled.text("Sensors Working", 5, 40)
return
if rear:
# Middle
if 1 in working_ultrasonics:
# Display bitmap for sensor 1, since no pixels overlap we can use blit directly
if sensor[1].cm > dist_step_1:
oled.blit(FrameBuffer(bitmap_sensor_1a_on, 32, 9, MONO_HLSB), 48, 23)
else:
oled.blit(FrameBuffer(bitmap_sensor_1a_off, 32, 9, MONO_HLSB), 48, 23)
if sensor[1].cm > dist_step_2:
oled.blit(FrameBuffer(bitmap_sensor_1b_on, 32, 9, MONO_HLSB), 48, 33)
else:
oled.blit(FrameBuffer(bitmap_sensor_1b_off, 32, 9, MONO_HLSB), 48, 33)
if sensor[1].cm > dist_step_3:
oled.blit(FrameBuffer(bitmap_sensor_1c_on, 32, 10, MONO_HLSB), 47, 42)
else:
oled.blit(FrameBuffer(bitmap_sensor_1c_off, 32, 10, MONO_HLSB), 47, 42)
if sensor[1].cm > dist_step_4:
oled.blit(FrameBuffer(bitmap_sensor_1d_on, 40, 10, MONO_HLSB), 42, 52)
else:
oled.blit(FrameBuffer(bitmap_sensor_1d_off, 40, 10, MONO_HLSB), 42, 52)
# left sensor
if 0 in working_ultrasonics:
# Display bitmap for sensor 0
if sensor[0].cm > dist_step_1:
bmp_1a = FrameBuffer(bitmap_sensor_0a_on, 32, 14, MONO_HLSB) # , 24, 17)
else:
bmp_1a = FrameBuffer(bitmap_sensor_0a_off, 32, 14, MONO_HLSB) # , 24, 17)
if sensor[0].cm > dist_step_2:
bmp_1b = FrameBuffer(bitmap_sensor_0b_on, 32, 16, MONO_HLSB) # , 21, 25)
else:
bmp_1b = FrameBuffer(bitmap_sensor_0b_off, 32, 16, MONO_HLSB) # , 21, 25)
if sensor[0].cm > dist_step_3:
bmp_1c = FrameBuffer(bitmap_sensor_0c_on, 32, 17, MONO_HLSB) # , 18, 34)
else:
bmp_1c = FrameBuffer(bitmap_sensor_0c_off, 32, 17, MONO_HLSB) # , 18, 34)
if sensor[0].cm > dist_step_4:
bmp_1d = FrameBuffer(bitmap_sensor_0d_on, 32, 18, MONO_HLSB) # , 16, 43)
else:
bmp_1d = FrameBuffer(bitmap_sensor_0d_off, 32, 18, MONO_HLSB) # , 16, 43)
blit_white_only(bmp_1a, 32, 14, 24, 17)
blit_white_only(bmp_1b, 32, 16, 21, 25)
blit_white_only(bmp_1c, 32, 17, 18, 34)
blit_white_only(bmp_1d, 32, 18, 16, 43)
# right sensor
if 2 in working_ultrasonics:
# Display bitmap for sensor 2
if sensor[2].cm > dist_step_1:
bmp_3a = FrameBuffer(bitmap_sensor_2a_on, 32, 14, MONO_HLSB) # , 72, 17)
else:
bmp_3a = FrameBuffer(bitmap_sensor_2a_off, 32, 14, MONO_HLSB) # , 72, 17)
if sensor[2].cm > dist_step_2:
bmp_3b = FrameBuffer(bitmap_sensor_2b_on, 32, 16, MONO_HLSB) # , 74, 25)
else:
bmp_3b = FrameBuffer(bitmap_sensor_2b_off, 32, 16, MONO_HLSB) # , 74, 25)
if sensor[2].cm > dist_step_3:
bmp_3c = FrameBuffer(bitmap_sensor_2c_on, 32, 17, MONO_HLSB) # , 77, 34)
else:
bmp_3c = FrameBuffer(bitmap_sensor_2c_off, 32, 17, MONO_HLSB) # , 77, 34)
if sensor[2].cm > dist_step_4:
bmp_3d = FrameBuffer(bitmap_sensor_2d_on, 32, 18, MONO_HLSB) # , 80, 43)
else:
bmp_3d = FrameBuffer(bitmap_sensor_2d_off, 32, 18, MONO_HLSB) # , 80, 43)
blit_white_only(bmp_3a, 32, 14, 72, 17)
blit_white_only(bmp_3b, 32, 16, 74, 25)
blit_white_only(bmp_3c, 32, 17, 77, 34)
blit_white_only(bmp_3d, 32, 18, 80, 43)
# This is for flipped displays
else:
# middle sensor
if 1 in working_ultrasonics:
# Display bitmap for sensor 1, since not pixels overlap, we can blit directly
if sensor[1].cm > dist_step_1:
oled.blit(FrameBuffer(flipped_bitmap_sensor_1a_on, 32, 9, MONO_HLSB), 48, DISP_HEIGHT - 23 - 9)
else:
oled.blit(FrameBuffer(flipped_bitmap_sensor_1a_off, 32, 9, MONO_HLSB), 48, DISP_HEIGHT - 23 - 9)
if sensor[1].cm > dist_step_2:
oled.blit(FrameBuffer(flipped_bitmap_sensor_1b_on, 32, 9, MONO_HLSB), 48, DISP_HEIGHT - 33 - 9)
else:
oled.blit(FrameBuffer(flipped_bitmap_sensor_1b_off, 32, 9, MONO_HLSB), 48, DISP_HEIGHT - 33 - 9)
if sensor[1].cm > dist_step_3:
oled.blit(FrameBuffer(flipped_bitmap_sensor_1c_on, 32, 10, MONO_HLSB), 47, DISP_HEIGHT - 42 - 10)
else:
oled.blit(FrameBuffer(flipped_bitmap_sensor_1c_off, 32, 10, MONO_HLSB), 47, DISP_HEIGHT - 42 - 10)
if sensor[1].cm > dist_step_4:
oled.blit(FrameBuffer(flipped_bitmap_sensor_1d_on, 40, 10, MONO_HLSB), 42, DISP_HEIGHT - 52 - 10)
else:
oled.blit(FrameBuffer(flipped_bitmap_sensor_1d_off, 40, 10, MONO_HLSB), 42, DISP_HEIGHT - 52 - 10)
# left sensor
if 0 in working_ultrasonics:
# Display bitmap for sensor 2
if sensor[0].cm > dist_step_1:
bmp_3a = FrameBuffer(flipped_bitmap_sensor_2a_on, 32, 14, MONO_HLSB)
else:
bmp_3a = FrameBuffer(flipped_bitmap_sensor_2a_off, 32, 14, MONO_HLSB)
if sensor[0].cm > dist_step_2:
bmp_3b = FrameBuffer(flipped_bitmap_sensor_2b_on, 32, 16, MONO_HLSB)
else:
bmp_3b = FrameBuffer(flipped_bitmap_sensor_2b_off, 32, 16, MONO_HLSB)
if sensor[0].cm > dist_step_3:
bmp_3c = FrameBuffer(flipped_bitmap_sensor_2c_on, 32, 17, MONO_HLSB)
else:
bmp_3c = FrameBuffer(flipped_bitmap_sensor_2c_off, 32, 17, MONO_HLSB)
if sensor[0].cm > dist_step_4:
bmp_3d = FrameBuffer(flipped_bitmap_sensor_2d_on, 32, 18, MONO_HLSB)
else:
bmp_3d = FrameBuffer(flipped_bitmap_sensor_2d_off, 32, 18, MONO_HLSB)
blit_white_only(bmp_3a, 32, 14, 24, DISP_HEIGHT - 17 - 14)
blit_white_only(bmp_3b, 32, 16, 21, DISP_HEIGHT - 25 - 16)
blit_white_only(bmp_3c, 32, 17, 18, DISP_HEIGHT - 34 - 17)
blit_white_only(bmp_3d, 32, 18, 16, DISP_HEIGHT - 43 - 18)
# right sensor
if 2 in working_ultrasonics:
# Display bitmap for sensor 0
if sensor[2].cm > dist_step_1:
bmp_1a = FrameBuffer(flipped_bitmap_sensor_0a_on, 32, 14, MONO_HLSB)
else:
bmp_1a = FrameBuffer(flipped_bitmap_sensor_0a_off, 32, 14, MONO_HLSB)
if sensor[2].cm > dist_step_2:
bmp_1b = FrameBuffer(flipped_bitmap_sensor_0b_on, 32, 16, MONO_HLSB)
else:
bmp_1b = FrameBuffer(flipped_bitmap_sensor_0b_off, 32, 16, MONO_HLSB)
if sensor[2].cm > dist_step_3:
bmp_1c = FrameBuffer(flipped_bitmap_sensor_0c_on, 32, 17, MONO_HLSB)
else:
bmp_1c = FrameBuffer(flipped_bitmap_sensor_0c_off, 32, 17, MONO_HLSB)
if sensor[2].cm > dist_step_4:
bmp_1d = FrameBuffer(flipped_bitmap_sensor_0d_on, 32, 18, MONO_HLSB)
else:
bmp_1d = FrameBuffer(flipped_bitmap_sensor_0d_off, 32, 18, MONO_HLSB)
blit_white_only(bmp_1a, 32, 14, 72, DISP_HEIGHT - 17 - 14)
blit_white_only(bmp_1b, 32, 16, 74, DISP_HEIGHT - 25 - 16)
blit_white_only(bmp_1c, 32, 17, 77, DISP_HEIGHT - 34 - 17)
blit_white_only(bmp_1d, 32, 18, 80, DISP_HEIGHT - 43 - 18)
# round to nearest
for j in working_ultrasonics:
if metric:
int_string = str(int(sensor[j].cm + 0.5))
else:
int_string = str(int(sensor[j].inch + 0.5))
digits = get_str_width(int_string)
startpos_x = sensor[j].label_startpos_x + (3 - digits) * 4
endpos_x = sensor[j].label_endpos_x + (3 - digits) * 4
# Display distance label
xpos = int(map_range(constrain(sensor[j].cm, MIN_CM_DIST, MAX_CM_DIST),
MIN_CM_DIST, MAX_CM_DIST, startpos_x, endpos_x))
ypos = int(map_range(constrain(sensor[j].cm, MIN_CM_DIST, MAX_CM_DIST),
MIN_CM_DIST, MAX_CM_DIST, sensor[j].label_startpos_y, sensor[j].label_endpos_y))
# print black box slightly larger than digits, then print digits
if rear:
oled.fill_rect(xpos, ypos - 1, 8 * digits, 9, 0)
oled.text(int_string, xpos, ypos)
else:
oled.fill_rect(xpos, DISP_HEIGHT - ypos - 1 - 8, 8 * digits, 9, 0)
oled.text(int_string, xpos, DISP_HEIGHT - ypos - 8)
return
def display_environment_details(dist, buzz):
"""
display just environment readings & car image(for fun)
No need to oled.fill(0) before or oled.show() after call
param:dist: distance if dist = -1.0 then display error
param:buzz: distance if dist = -1.0 then display error
"""
oled.fill(0)
if dist < 12.0 and buzz:
buzzer.on()
elif buzz:
buzzer.off()
if temp_c:
if metric:
oled.text(f"Temp = {temp_c:.1f}C", 0, 0)
else:
oled.text(f"Temp = {temp_f:.1f}F", 0, 0)
if humidity: oled.text(f"Humid= {humidity:.1f}%", 0, 12)
else:
oled.text(f"No Temp/Humidity", 0, 10)
oled.text(f" Sensor Working", 0, 20)
if metric:
oled.text(f"Sound={speed_sound:.1f}m/s", 0, 24)
if dist:
oled.text(f"Dist= {dist:.0f}cm", 0, 55)
else:
oled.text(f"No ultrasonic", 0, 55)
else:
oled.text(f"Sound={speed_sound * 3.28084:.0f}ft/s", 0, 24)
if dist:
oled.text(f"Dist= {dist / 2.54:.1f}in", 0, 55)
else:
oled.text(f"No ultrasonic", 0, 55)
oled.blit(FrameBuffer(bitmap_artcar_image_back, 56, 15, MONO_HLSB), 22, 36)
oled.show()
return
# ========================= startup code =========================
rear = True
show_env = False
buzzer_sound = True
metric = False
dht_error = False
ds_error = False
debug = False
dist_step_1 = MIN_CM_DIST + round((MAX_CM_DIST - MIN_CM_DIST) / 4.0 * 1.0) # tile1 step
dist_step_2 = MIN_CM_DIST + round((MAX_CM_DIST - MIN_CM_DIST) / 4.0 * 2.0) # tile2 step
dist_step_3 = MIN_CM_DIST + round((MAX_CM_DIST - MIN_CM_DIST) / 4.0 * 3.0) # tile3 step
dist_step_4 = MIN_CM_DIST + round((MAX_CM_DIST - MIN_CM_DIST) / 4.0 * 4.0) # tile4 step
# numeric label position, to print distance # over tiles
sensor[0].label_startpos_x = 30 # was 41 (-11)
sensor[0].label_startpos_y = 20
sensor[0].label_endpos_x = 18 # was 30, adjust 1 more for symmetry
sensor[0].label_endpos_y = 56 # was 58
sensor[1].label_startpos_x = 52 # was 63 (-11) 52px 3 dig, 56 2 dig, 60 for 1 digit