forked from mattdy/flightpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLcdThread.py
157 lines (122 loc) · 4.73 KB
/
LcdThread.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
"""
LcdThread.py
Accepts details of a flight to display, and outputs them to an LCD
Inspiration taken from https://www.raspberrypi-spy.co.uk/2015/05/using-an-i2c-enabled-lcd-screen-with-the-raspberry-pi/
Matt Dyson
13/02/18
Part of FlightPi - http://github.com/mattdy/flightpi
"""
import threading
import logging
import time
import smbus
from FlightDetails import FlightDetails
log = logging.getLogger('root')
# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
LCD_BACKLIGHT_ON = 0x08 # On
LCD_BACKLIGHT_OFF = 0x00 # Off
ENABLE = 0b00000100 # Enable bit
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
class LcdThread(threading.Thread):
def __init__(self, address=0x20, width=20):
threading.Thread.__init__(self)
self.stopping = False
self.display = None
self.backlight = LCD_BACKLIGHT_OFF
self.bus = smbus.SMBus(1) #I2C interface
self.address = address
self.width = width
self.data = FlightDetails()
def processFlight(self, flight):
""" Take the given flight details, translate it into what we want to show on the LCD """
lines = {
LCD_LINE_1: "",
LCD_LINE_2: "",
LCD_LINE_3: "",
LCD_LINE_4: "",
}
if(flight is not None):
type = self.data.getType(flight['icao24'])
if type is None: type=""
speed = flight['groundSpeed']
if speed is None:
speed = ""
else:
speed += "kts"
altLine = ""
if(flight['altitude'] is not None):
altLine = self.getLevel(flight['altitude'])
# Pad out line with appropriate number of spaces
altLine = altLine.ljust(self.width - len(speed))
altLine += speed
squawk = flight['squawk']
if(squawk is None):
squawk = ""
lines = {
LCD_LINE_1: '{}'.format(flight['callsign'].center(self.width)),
LCD_LINE_2: altLine,
LCD_LINE_3: '{}'.format(type.center(self.width)),
LCD_LINE_4: '{}'.format(squawk.center(self.width))
}
self.backlight = LCD_BACKLIGHT_ON
else:
self.backlight = LCD_BACKLIGHT_OFF
for i in lines:
self.__lcd_line(lines[i], i)
def getLevel(self, level):
""" Turn a given level into an altitude or flight level display accordingly """
if int(level)<6000: # Transition level
return "A{}".format(int(level)/100)
else:
return "FL{}".format(int(level)/100)
def __lcd_write(self, bits, mode):
""" Send byte to data pins on the LCD """
# bits = the data
# mode = 1 for data
# 0 for command
bits_high = mode | (bits & 0xF0) | self.backlight
bits_low = mode | ((bits<<4) & 0xF0) | self.backlight
# High bits
self.bus.write_byte(self.address, bits_high)
self.__lcd_toggle(bits_high)
# Low bits
self.bus.write_byte(self.address, bits_low)
self.__lcd_toggle(bits_low)
def __lcd_toggle(self, bits):
""" Toggle enable off and on to force update """
time.sleep(E_DELAY)
self.bus.write_byte(self.address, (bits | ENABLE))
time.sleep(E_PULSE)
self.bus.write_byte(self.address, (bits & ~ENABLE))
time.sleep(E_DELAY)
def __lcd_line(self, message, line):
""" Send message to display on given line """
message = message.ljust(self.width," ")
self.__lcd_write(line, LCD_CMD)
for i in range(self.width):
self.__lcd_write(ord(message[i]),LCD_CHR)
def stop(self):
self.stopping = True
def run(self):
log.info("LcdThread starting")
# Initialise display
self.__lcd_write(0x33, LCD_CMD) # 110011 Initialise
self.__lcd_write(0x32, LCD_CMD) # 110010 Initialise
self.__lcd_write(0x06, LCD_CMD) # 000110 Cursor move direction
self.__lcd_write(0x0C, LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
self.__lcd_write(0x28, LCD_CMD) # 101000 Data length, number of lines, font size
self.__lcd_write(0x01, LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)
while not self.stopping:
# Updating logic done through processMessage, so nothing to do here
time.sleep(1)
self.__lcd_write(0x01, LCD_CMD)
log.info("LcdThread shut down")