Skip to content

Commit 8132d1a

Browse files
author
raspberry@adafruit.com
committed
added mikey's char LCD + example code
1 parent 974ef93 commit 8132d1a

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

Adafruit_CharLCD/Adafruit_CharLCD.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/python
2+
3+
#
4+
# based on code from lrvick and LiquidCrystal
5+
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
6+
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
7+
#
8+
9+
import RPi.GPIO as GPIO
10+
from time import sleep
11+
12+
class Adafruit_CharLCD:
13+
14+
def __init__(self, pin_rs=24, pin_e=23, pins_db=[17, 21, 22, 25]):
15+
16+
self.pin_rs = pin_rs
17+
self.pin_e = pin_e
18+
self.pins_db = pins_db
19+
20+
GPIO.setmode(GPIO.BCM)
21+
GPIO.setup(self.pin_e, GPIO.OUT)
22+
GPIO.setup(self.pin_rs, GPIO.OUT)
23+
24+
for pin in self.pins_db:
25+
GPIO.setup(pin, GPIO.OUT)
26+
27+
self.write4bits(0x33) # initialization
28+
self.write4bits(0x32) # initialization
29+
self.write4bits(0x28) # 2 line 5x7 matrix
30+
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
31+
self.write4bits(0x06) # shift cursor right
32+
self.clear()
33+
34+
35+
def clear(self):
36+
37+
self.write4bits(0x01) # command to clear display
38+
39+
# 2000 microsecond sleep, clearing the display takes a long time
40+
sleep(.002)
41+
#self.delayMicroseconds(2000)
42+
43+
def write4bits(self, bits, char_mode=False):
44+
""" Send command to LCD """
45+
46+
# 1000 microseconds sleep
47+
# sleep(.001)
48+
self.delayMicroseconds(1000)
49+
50+
bits=bin(bits)[2:].zfill(8)
51+
52+
GPIO.output(self.pin_rs, char_mode)
53+
54+
for pin in self.pins_db:
55+
GPIO.output(pin, False)
56+
57+
for i in range(4):
58+
if bits[i] == "1":
59+
GPIO.output(self.pins_db[::-1][i], True)
60+
61+
self.pulseEnable()
62+
63+
for pin in self.pins_db:
64+
GPIO.output(pin, False)
65+
66+
for i in range(4,8):
67+
if bits[i] == "1":
68+
GPIO.output(self.pins_db[::-1][i-4], True)
69+
70+
self.pulseEnable()
71+
72+
73+
def delayMicroseconds(self, microseconds):
74+
seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
75+
sleep(seconds)
76+
77+
78+
def pulseEnable(self):
79+
GPIO.output(self.pin_e, False)
80+
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
81+
GPIO.output(self.pin_e, True)
82+
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
83+
GPIO.output(self.pin_e, False)
84+
self.delayMicroseconds(1) # commands need > 37us to settle
85+
86+
87+
def message(self, text):
88+
""" Send string to LCD. Newline wraps to second line"""
89+
90+
for char in text:
91+
if char == '\n':
92+
self.write4bits(0xC0) # next line
93+
else:
94+
self.write4bits(ord(char),True)
95+
96+
97+
if __name__ == '__main__':
98+
99+
lcd = Adafruit_CharLCD()
100+
101+
lcd.clear()
102+
lcd.message(" Adafruit 16x2\n Standard LCD")
103+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python
2+
3+
from Adafruit_CharLCD import Adafruit_CharLCD
4+
from subprocess import *
5+
from time import sleep, strftime
6+
from datetime import datetime
7+
8+
lcd = Adafruit_CharLCD()
9+
10+
cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"
11+
12+
def run_cmd(cmd):
13+
p = Popen(cmd, shell=True, stdout=PIPE)
14+
output = p.communicate()[0]
15+
return output
16+
17+
while 1:
18+
lcd.clear()
19+
ipaddr = run_cmd(cmd)
20+
lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
21+
lcd.message('My IP: %s' % ( ipaddr ) )
22+
sleep(2)

0 commit comments

Comments
 (0)