-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.py
executable file
·181 lines (156 loc) · 4.76 KB
/
lcd.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
#!/usr/bin/python
#
# HD44780 LCD Test Script for
# Raspberry Pi
#
# Author : Matt Hawkins
# Site : http://www.raspberrypi-spy.co.uk
#
# Date : 03/08/2012
#
# The wiring for the LCD is as follows:
# 1 : GND
# 2 : 5V
# 3 : Contrast (0-5V)*
# 4 : RS (Register Select)
# 5 : R/W (Read Write) - GROUND THIS PIN
# 6 : Enable or Strobe
# 7 : Data Bit 0 - NOT USED
# 8 : Data Bit 1 - NOT USED# 9 : Data Bit 2 - NOT USED
# 10: Data Bit 3 - NOT USED
# 11: Data Bit 4
# 12: Data Bit 5
# 13: Data Bit 6
# 14: Data Bit 7
# 15: LCD Backlight +5V**
# 16: LCD Backlight GND
# Code was modified to use it as a library
import time
try:
import RPi.GPIO as GPIO
except RuntimeError:
print('Not running on the pi, some features won\'t be available')
# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18
LED_ON = 15
# Define some device constants
LCD_WIDTH = 16 # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
# LCD RAM address for the 2 lines
LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0
# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005
class LCD:
def __init__(self):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7
GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable
# Initialise display
self._lcd_byte(0x33, LCD_CMD)
self._lcd_byte(0x32, LCD_CMD)
self._lcd_byte(0x28, LCD_CMD)
self._lcd_byte(0x0C, LCD_CMD)
self._lcd_byte(0x06, LCD_CMD)
self._lcd_byte(0x01, LCD_CMD)
self.blight(1)
def _clean_up(self):
GPIO.cleanup()
def _lcd_byte(self, bits, mode):
# Send byte to data pins
# bits = data
# mode = True for character
# False for command
GPIO.output(LCD_RS, mode) # RS
# High bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits & 0x10 == 0x10:
GPIO.output(LCD_D4, True)
if bits & 0x20 == 0x20:
GPIO.output(LCD_D5, True)
if bits & 0x40 == 0x40:
GPIO.output(LCD_D6, True)
if bits & 0x80 == 0x80:
GPIO.output(LCD_D7, True)
# Toggle 'Enable' pin
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)
# Low bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits & 0x01 == 0x01:
GPIO.output(LCD_D4, True)
if bits & 0x02 == 0x02:
GPIO.output(LCD_D5, True)
if bits & 0x04 == 0x04:
GPIO.output(LCD_D6, True)
if bits & 0x08 == 0x08:
GPIO.output(LCD_D7, True)
# Toggle 'Enable' pin
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)
def _displace(self, text):
# takes a 16 char string and formats it to show it in movement
if len(text) > 16:
print ('String cannot be longer than 16 characters')
return
ret = []
text = list(text)
for x in range(1, 16):
ret.append(text[x])
ret.append(text[0])
ret = ''.join(ret)
return ret
def _lcd_string(self, message, style):
# Send string to display
if style == 1:
message = message.ljust(LCD_WIDTH, " ")
elif style == 2:
message = message.center(LCD_WIDTH, " ")
elif style == 3:
message = message.rjust(LCD_WIDTH, " ")
for i in range(LCD_WIDTH):
self._lcd_byte(ord(message[i]), LCD_CHR)
def send_text(self, text, row=1, justification=2):
# ROW: send 'text' to row number 1 or 2
# Justification: left(1), center(2), (3)right
if (len(text) > 16):
print "Text not valid. Longer than 16 characters"
text = 'ERR:line too long'
rows = {
1: LCD_LINE_1,
2: LCD_LINE_2
}
self._lcd_byte(rows.get(row, 1), LCD_CMD)
self._lcd_string(text, justification)
def blight(self, state):
GPIO.output(LED_ON, state)
def move(self, seconds, text):
for x in range(seconds * 2):
self.send_text(text, 1)
time.sleep(0.5)
text = self._displace(text)