Skip to content

Instantly share code, notes, and snippets.

@azbesthu
Created November 27, 2012 21:26
Show Gist options
  • Save azbesthu/4157137 to your computer and use it in GitHub Desktop.
Save azbesthu/4157137 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
class HD44780:
def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22]):
self.pin_rs = pin_rs
self.pin_e = pin_e
self.pins_db = pins_db
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin_e, GPIO.OUT)
GPIO.setup(self.pin_rs, GPIO.OUT)
for pin in self.pins_db:
GPIO.setup(pin, GPIO.OUT)
self.clear()
def clear(self):
""" Blank / Reset LCD """
self.cmdinit(0x3) # $33 8-bit mode
self.cmdinit(0x3) # $32 8-bit mode
self.cmdinit(0x3) # $32 8-bit mode
self.cmdinit(0x2) # $32 8-bit mode
self.cmd(0x28) # $28 101000: 4bit,2line,5x8
self.cmd(0x0C) # $0C 1100: display on
self.cmd(0x06) # $06 110: cursor inc
self.cmd(0x01) # $01 1: clear
def cmdinit(self, bits):
""" Send command to LCD """
sleep(0.01)
bits=bin(bits)[2:].zfill(8)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4,8):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i-4], True)
GPIO.output(self.pin_e, True)
GPIO.output(self.pin_e, False)
sleep(0.01)
def cmd(self, bits, char_mode=False):
""" Send command to LCD """
sleep(0.002)
bits=bin(bits)[2:].zfill(8)
GPIO.output(self.pin_rs, char_mode)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i], True)
GPIO.output(self.pin_e, True)
GPIO.output(self.pin_e, False)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4,8):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i-4], True)
GPIO.output(self.pin_e, True)
GPIO.output(self.pin_e, False)
def message(self, text):
""" Send string to LCD. Newline wraps to second line"""
self.cmd(2)
i = 0
for char in text:
if char == '\n':
self.cmd(0xC0) # next line
else:
self.cmd(ord(char),True)
i = i + 1
if i == 16:
self.cmd(0xC0)
def customchars(self, data = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]):
i = 0
self.cmd(0x40)
for character in data:
for line in character:
self.cmd(line,True)
if __name__ == '__main__':
import random
from datetime import datetime
charmap = [ [0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0],
[0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1f],
[0x0,0x0,0x0,0x0,0x0,0x0,0x1f,0x1f],
[0x0,0x0,0x0,0x0,0x0,0x1f,0x1f,0x1f],
[0x0,0x0,0x0,0x0,0x1f,0x1f,0x1f,0x1f],
[0x0,0x0,0x0,0x1f,0x1f,0x1f,0x1f,0x1f],
[0x0,0x0,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f],
[0x0,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f]
]
lcd = HD44780()
lcd.customchars(charmap)
while 1:
x = datetime.now().strftime('%b %d %H:%M:%S\n')
msg=''
for num in range(16):
msg = msg + chr(random.randint(0,7))
lcd.message(x+msg)
#sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment