-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedometer.py
432 lines (361 loc) · 11.1 KB
/
speedometer.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
"""
A speedometer for the narrowboat.
Using a Waveshare LCD display
<https://files.waveshare.com/upload/2/2d/ILI9488_Data_Sheet.pdf>
and a Quectel L76 GPS module
<https://www.quectel.com/ProductDownload/L76-LB.html>
connected to a raspberry pi pico
Author: Jennifer Liddle <[email protected]>
Jenny Bailey <[email protected]>
"""
from machine import Pin,SPI,PWM, UART
import framebuf
import time
import random
from micropython import const
# Initialise the UART so we can read the NMEA of the GPS from the serial port
UARTx = const(0)
BAUDRATE = const(9600)
PICO_UART_TX_PIN=const(16)
PICO_UART_RX_PIN=const(17)
GPS = UART(UARTx,baudrate=BAUDRATE,tx=Pin(PICO_UART_TX_PIN),rx=Pin(PICO_UART_RX_PIN),timeout=100 )
# Initialise the LCD
LCD_DC = const(8)
LCD_CS = const(9)
LCD_SCK = const(10)
LCD_MOSI = const(11)
LCD_MISO = const(12)
LCD_BL = const(13)
LCD_RST = const(15)
class LCD_3inch5(framebuf.FrameBuffer):
def __init__(self):
self.RED = const(0x07E0)
self.GREEN = const(0x001f)
self.BLUE = const(0xf800)
self.WHITE = const(0xffff)
self.BLACK = const(0x0000)
self.width = const(240)
self.height = const(320)
self.cs = Pin(LCD_CS,Pin.OUT)
self.rst = Pin(LCD_RST,Pin.OUT)
self.dc = Pin(LCD_DC,Pin.OUT)
self.cs(1)
self.dc(1)
self.rst(1)
self.spi = SPI(1,60_000_000,sck=Pin(LCD_SCK),mosi=Pin(LCD_MOSI),miso=Pin(LCD_MISO))
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
#self.spi.write(bytearray([0X00]))
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize display"""
self.rst(1)
time.sleep_ms(5)
self.rst(0)
time.sleep_ms(10)
self.rst(1)
time.sleep_ms(5)
self.write_cmd(0x21)
self.write_cmd(0xC2)
self.write_data(0x33)
self.write_cmd(0XC5)
self.write_data(0x00)
self.write_data(0x1e)
self.write_data(0x80)
self.write_cmd(0xB1)
self.write_data(0xB0)
self.write_cmd(0x36)
self.write_data(0x28)
self.write_cmd(0XE0)
self.write_data(0x00)
self.write_data(0x13)
self.write_data(0x18)
self.write_data(0x04)
self.write_data(0x0F)
self.write_data(0x06)
self.write_data(0x3a)
self.write_data(0x56)
self.write_data(0x4d)
self.write_data(0x03)
self.write_data(0x0a)
self.write_data(0x06)
self.write_data(0x30)
self.write_data(0x3e)
self.write_data(0x0f)
self.write_cmd(0XE1)
self.write_data(0x00)
self.write_data(0x13)
self.write_data(0x18)
self.write_data(0x01)
self.write_data(0x11)
self.write_data(0x06)
self.write_data(0x38)
self.write_data(0x34)
self.write_data(0x4d)
self.write_data(0x06)
self.write_data(0x0d)
self.write_data(0x0b)
self.write_data(0x31)
self.write_data(0x37)
self.write_data(0x0f)
self.write_cmd(0X3A)
self.write_data(0x55)
self.write_cmd(0x11)
time.sleep_ms(120)
self.write_cmd(0x29)
self.write_cmd(0xB6)
self.write_data(0x00)
self.write_data(0x62)
self.write_cmd(0x36)
self.write_data(0x28)
def show_left(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0xef)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x01)
self.write_data(0xdf)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
def show_right(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0xf0)
self.write_data(0x01)
self.write_data(0xdf)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x01)
self.write_data(0xdf)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
def bl_ctrl(self,duty):
pwm = PWM(Pin(LCD_BL))
pwm.freq(1000)
if(duty>=100):
pwm.duty_u16(65535)
else:
pwm.duty_u16(655*duty)
def draw_point(self,x,y,color):
self.write_cmd(0x2A)
self.write_data((x-2)>>8)
self.write_data((x-2)&0xff)
self.write_data(x>>8)
self.write_data(x&0xff)
self.write_cmd(0x2B)
self.write_data((y-2)>>8)
self.write_data((y-2)&0xff)
self.write_data(y>>8)
self.write_data(y&0xff)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
for i in range(0,9):
h_color = bytearray(color>>8)
l_color = bytearray(color&0xff)
self.spi.write(h_color)
self.spi.write(l_color)
self.cs(1)
# These are the line segments used to display our digits 0..9
LINE_LENGTH = const(90)
LINE_WIDTH = const(15)
CHARACTER_START_X = const(70)
CHARACTER_START_Y = const(50)
def draw_top():
x = CHARACTER_START_X + LINE_WIDTH
y = CHARACTER_START_Y
LCD.fill_rect(x,y,LINE_LENGTH,LINE_WIDTH,LCD.BLACK)
def draw_top_left():
x = CHARACTER_START_X
y = CHARACTER_START_Y + LINE_WIDTH
LCD.fill_rect(x,y,LINE_WIDTH, LINE_LENGTH, LCD.BLACK)
def draw_top_right():
x = CHARACTER_START_X + LINE_LENGTH + LINE_WIDTH
y = CHARACTER_START_Y + LINE_WIDTH
LCD.fill_rect(x,y,LINE_WIDTH, LINE_LENGTH, LCD.BLACK)
def draw_middle():
x = CHARACTER_START_X + LINE_WIDTH
y = CHARACTER_START_Y + LINE_LENGTH + LINE_WIDTH
LCD.fill_rect(x,y,LINE_LENGTH,LINE_WIDTH,LCD.BLACK)
def draw_bottom_left():
x = CHARACTER_START_X
y = CHARACTER_START_Y + LINE_LENGTH + LINE_WIDTH + LINE_WIDTH
LCD.fill_rect(x,y,LINE_WIDTH, LINE_LENGTH, LCD.BLACK)
def draw_bottom_right():
x = CHARACTER_START_X + LINE_LENGTH + LINE_WIDTH
y = CHARACTER_START_Y + LINE_LENGTH + LINE_WIDTH + LINE_WIDTH
LCD.fill_rect(x,y,LINE_WIDTH, LINE_LENGTH, LCD.BLACK)
def draw_bottom():
x = CHARACTER_START_X + LINE_WIDTH
y = CHARACTER_START_Y + LINE_LENGTH + LINE_LENGTH + LINE_WIDTH + LINE_WIDTH
LCD.fill_rect(x,y,LINE_LENGTH,LINE_WIDTH,LCD.BLACK)
# We have a separate function to display each digit
def draw_zero():
draw_top()
draw_top_left()
draw_top_right()
draw_bottom_left()
draw_bottom_right()
draw_bottom()
def draw_one():
draw_top_right()
draw_bottom_right()
def draw_two():
draw_top()
draw_top_right()
draw_middle()
draw_bottom_left()
draw_bottom()
def draw_three():
draw_top()
draw_top_right()
draw_middle()
draw_bottom_right()
draw_bottom()
def draw_four():
draw_top_left()
draw_top_right()
draw_middle()
draw_bottom_right()
def draw_five():
draw_top()
draw_top_left()
draw_middle()
draw_bottom_right()
draw_bottom()
def draw_six():
draw_top()
draw_top_left()
draw_middle()
draw_bottom_left()
draw_bottom_right()
draw_bottom()
def draw_seven():
draw_top()
draw_top_right()
draw_bottom_right()
def draw_eight():
draw_top()
draw_top_left()
draw_top_right()
draw_middle()
draw_bottom_left()
draw_bottom_right()
draw_bottom()
def draw_nine():
draw_top()
draw_top_left()
draw_top_right()
draw_middle()
draw_bottom_right()
def draw_point(): # the decimal point
LCD.fill_rect(0,230,40,40,LCD.BLACK)
NUMBERS = [draw_zero, draw_one, draw_two, draw_three, draw_four, draw_five, draw_six, draw_seven, draw_eight, draw_nine]
""" This was only used during testing and development
def test_numbers():
while True:
n = random.choice(NUMBERS)
LCD.fill(LCD.WHITE)
n()
LCD.show_left()
n = random.choice(NUMBERS)
LCD.fill(LCD.WHITE)
n()
draw_point()
LCD.show_right()
time.sleep(1)
"""
# extract the 'number of satellites' count from the GGA sentence
def get_satellites(nmea):
#print(nmea)
return int(nmea.split(",")[7])
# extract the speed (in kph) from the VTG sentence, but return mph
def get_speed(nmea):
#print(nmea)
speed = float(nmea.split(",")[7]) # speed in k/h
speed = speed * 0.62137119 # speed in mph
return speed
# display a 'J2' at startup, because....well, why not?
def display_logo():
LCD.fill(LCD.WHITE)
draw_top()
draw_top_right()
draw_bottom_right()
draw_bottom()
draw_bottom_left()
LCD.show_left()
LCD.fill(LCD.WHITE)
draw_two()
LCD.show_right()
time.sleep(5)
# Main display showing speed and number of satellites
def display_all(satellites, speed):
if (speed >= 10):
speed = 9.9
digit = int(speed)
LCD.fill(LCD.WHITE)
NUMBERS[digit]()
LCD.show_left()
digit = int(speed * 10) % 10
LCD.fill(LCD.WHITE)
NUMBERS[digit]()
for x in range(0,satellites):
LCD.fill_rect(LCD.width-((x+1)*20),10,10,10,LCD.BLACK)
draw_point()
LCD.show_right()
if __name__=='__main__':
opt_level(3)
LCD = LCD_3inch5()
LCD.bl_ctrl(100)
#print(opt_level())
#print(mem_info())
display_logo()
satellites = 0
speed = 0
new_satellites = 0
new_speed = 0
display_all(satellites,speed)
# Read GPS, extract values, display if values have changed
while True:
nmea = GPS.readline()
if (nmea):
#print(nmea)
try:
nmea = nmea.decode("utf-8")
except:
nmea = " "
nmea = nmea[3:-2]
if (nmea[0:3] == "GGA"):
new_satellites = get_satellites(nmea)
if (nmea[0:3] == "VTG"):
new_speed = get_speed(nmea)
if ( (new_speed != speed) or (new_satellites != satellites) ):
speed = new_speed
satellites = new_satellites
display_all(satellites,speed)