2020-11-03 23:51:13 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
#
|
|
|
|
# based on code from lrvick and LiquidCrystal
|
|
|
|
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
|
|
|
|
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
|
|
|
|
#
|
|
|
|
|
|
|
|
from time import sleep
|
2020-11-07 01:12:24 +00:00
|
|
|
import board
|
|
|
|
import neopixel
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
|
2020-11-03 23:51:13 +00:00
|
|
|
class Adafruit_CharLCD:
|
|
|
|
|
|
|
|
# commands
|
2020-11-04 01:38:58 +00:00
|
|
|
LCD_CLEARDISPLAY = 0x01
|
|
|
|
LCD_RETURNHOME = 0x02
|
|
|
|
LCD_ENTRYMODESET = 0x04
|
|
|
|
LCD_DISPLAYCONTROL = 0x08
|
|
|
|
LCD_CURSORSHIFT = 0x10
|
|
|
|
LCD_FUNCTIONSET = 0x20
|
|
|
|
LCD_SETCGRAMADDR = 0x40
|
|
|
|
LCD_SETDDRAMADDR = 0x80
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
# flags for display entry mode
|
2020-11-04 01:38:58 +00:00
|
|
|
LCD_ENTRYRIGHT = 0x00
|
|
|
|
LCD_ENTRYLEFT = 0x02
|
|
|
|
LCD_ENTRYSHIFTINCREMENT = 0x01
|
|
|
|
LCD_ENTRYSHIFTDECREMENT = 0x00
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
# flags for display on/off control
|
2020-11-04 01:38:58 +00:00
|
|
|
LCD_DISPLAYON = 0x04
|
|
|
|
LCD_DISPLAYOFF = 0x00
|
|
|
|
LCD_CURSORON = 0x02
|
|
|
|
LCD_CURSOROFF = 0x00
|
|
|
|
LCD_BLINKON = 0x01
|
|
|
|
LCD_BLINKOFF = 0x00
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
# flags for display/cursor shift
|
2020-11-04 01:38:58 +00:00
|
|
|
LCD_DISPLAYMOVE = 0x08
|
|
|
|
LCD_CURSORMOVE = 0x00
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
# flags for display/cursor shift
|
2020-11-04 01:38:58 +00:00
|
|
|
LCD_DISPLAYMOVE = 0x08
|
|
|
|
LCD_CURSORMOVE = 0x00
|
|
|
|
LCD_MOVERIGHT = 0x04
|
|
|
|
LCD_MOVELEFT = 0x00
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
# flags for function set
|
2020-11-04 01:38:58 +00:00
|
|
|
LCD_8BITMODE = 0x10
|
|
|
|
LCD_4BITMODE = 0x00
|
|
|
|
LCD_2LINE = 0x08
|
|
|
|
LCD_1LINE = 0x00
|
|
|
|
LCD_5x10DOTS = 0x04
|
|
|
|
LCD_5x8DOTS = 0x00
|
|
|
|
|
2020-11-07 01:12:24 +00:00
|
|
|
def __init__(self, pin_rs=24, pin_e=23, pin_b=4, pins_db=[17, 25, 27, 22], GPIO=None):
|
2020-11-04 01:38:58 +00:00
|
|
|
# Emulate the old behavior of using RPi.GPIO if we haven't been given
|
|
|
|
# an explicit GPIO interface to use
|
|
|
|
if not GPIO:
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
self.GPIO = GPIO
|
2020-11-03 23:51:13 +00:00
|
|
|
self.pin_rs = pin_rs
|
|
|
|
self.pin_e = pin_e
|
2020-11-04 02:08:49 +00:00
|
|
|
self.pin_b = pin_b
|
2020-11-03 23:51:13 +00:00
|
|
|
self.pins_db = pins_db
|
|
|
|
|
|
|
|
self.GPIO.setwarnings(False)
|
|
|
|
self.GPIO.setmode(GPIO.BCM)
|
|
|
|
self.GPIO.setup(self.pin_e, GPIO.OUT)
|
|
|
|
self.GPIO.setup(self.pin_rs, GPIO.OUT)
|
2020-11-04 02:08:49 +00:00
|
|
|
self.GPIO.setup(self.pin_b, GPIO.OUT)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
for pin in self.pins_db:
|
|
|
|
self.GPIO.setup(pin, GPIO.OUT)
|
|
|
|
|
2020-11-04 02:08:49 +00:00
|
|
|
self.GPIO.output(self.pin_b, True)
|
2020-11-06 23:01:22 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.write4bits(0x33) # initialization
|
|
|
|
self.write4bits(0x32) # initialization
|
|
|
|
self.write4bits(0x28) # 2 line 5x7 matrix
|
|
|
|
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
|
|
|
|
self.write4bits(0x06) # shift cursor right
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
|
|
|
|
self.displayfunction |= self.LCD_2LINE
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
""" Initialize to default text direction (for romance languages) """
|
|
|
|
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
|
|
|
|
self.write4bits(self.LCD_ENTRYMODESET |
|
|
|
|
self.displaymode) # set the entry mode
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
self.clear()
|
|
|
|
|
|
|
|
def begin(self, cols, lines):
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
if (lines > 1):
|
|
|
|
self.numlines = lines
|
|
|
|
self.displayfunction |= self.LCD_2LINE
|
|
|
|
self.currline = 0
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def home(self):
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
|
|
|
|
self.delayMicroseconds(3000) # this command takes a long time!
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
|
|
|
|
# 3000 microsecond sleep, clearing the display takes a long time
|
|
|
|
self.delayMicroseconds(3000)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def setCursor(self, col, row):
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.row_offsets = [0x00, 0x40, 0x14, 0x54]
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
if (row > self.numlines):
|
|
|
|
row = self.numlines - 1 # we count rows starting w/0
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
def noDisplay(self):
|
|
|
|
""" Turn the display off (quickly) """
|
2020-11-04 02:08:49 +00:00
|
|
|
self.GPIO.output(self.pin_b, False)
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaycontrol &= ~self.LCD_DISPLAYON
|
|
|
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def display(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" Turn the display on (quickly) """
|
2020-11-04 02:08:49 +00:00
|
|
|
self.GPIO.output(self.pin_b, True)
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaycontrol |= self.LCD_DISPLAYON
|
|
|
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def noCursor(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" Turns the underline cursor on/off """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaycontrol &= ~self.LCD_CURSORON
|
|
|
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def cursor(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" Cursor On """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaycontrol |= self.LCD_CURSORON
|
|
|
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def noBlink(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" Turn on and off the blinking cursor """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaycontrol &= ~self.LCD_BLINKON
|
|
|
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def noBlink(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" Turn on and off the blinking cursor """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaycontrol &= ~self.LCD_BLINKON
|
|
|
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def DisplayLeft(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" These commands scroll the display without changing the RAM """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.write4bits(self.LCD_CURSORSHIFT |
|
|
|
|
self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def scrollDisplayRight(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" These commands scroll the display without changing the RAM """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.write4bits(self.LCD_CURSORSHIFT |
|
|
|
|
self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def leftToRight(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" This is for text that flows Left to Right """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaymode |= self.LCD_ENTRYLEFT
|
|
|
|
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def rightToLeft(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" This is for text that flows Right to Left """
|
|
|
|
self.displaymode &= ~self.LCD_ENTRYLEFT
|
|
|
|
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def autoscroll(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
""" This will 'right justify' text from the cursor """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
|
|
|
|
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
def noAutoscroll(self):
|
|
|
|
""" This will 'left justify' text from the cursor """
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
|
|
|
|
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def write4bits(self, bits, char_mode=False):
|
|
|
|
""" Send command to LCD """
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.delayMicroseconds(1000) # 1000 microsecond sleep
|
2020-11-03 23:51:13 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
bits = bin(bits)[2:].zfill(8)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
self.GPIO.output(self.pin_rs, char_mode)
|
|
|
|
|
|
|
|
for pin in self.pins_db:
|
|
|
|
self.GPIO.output(pin, False)
|
|
|
|
|
|
|
|
for i in range(4):
|
|
|
|
if bits[i] == "1":
|
|
|
|
self.GPIO.output(self.pins_db[::-1][i], True)
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.pulseEnable()
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
for pin in self.pins_db:
|
|
|
|
self.GPIO.output(pin, False)
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
for i in range(4, 8):
|
2020-11-03 23:51:13 +00:00
|
|
|
if bits[i] == "1":
|
|
|
|
self.GPIO.output(self.pins_db[::-1][i-4], True)
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
self.pulseEnable()
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def delayMicroseconds(self, microseconds):
|
2020-11-04 01:38:58 +00:00
|
|
|
# divide microseconds by 1 million for seconds
|
|
|
|
seconds = microseconds / float(1000000)
|
|
|
|
sleep(seconds)
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def pulseEnable(self):
|
2020-11-04 01:38:58 +00:00
|
|
|
self.GPIO.output(self.pin_e, False)
|
|
|
|
# 1 microsecond pause - enable pulse must be > 450ns
|
|
|
|
self.delayMicroseconds(1)
|
|
|
|
self.GPIO.output(self.pin_e, True)
|
|
|
|
# 1 microsecond pause - enable pulse must be > 450ns
|
|
|
|
self.delayMicroseconds(1)
|
|
|
|
self.GPIO.output(self.pin_e, False)
|
|
|
|
self.delayMicroseconds(1) # commands need > 37us to settle
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
def message(self, text):
|
|
|
|
""" Send string to LCD. Newline wraps to second line"""
|
|
|
|
|
|
|
|
for char in text:
|
|
|
|
if char == '\n':
|
2020-11-04 01:38:58 +00:00
|
|
|
self.write4bits(0xC0) # next line
|
2020-11-03 23:51:13 +00:00
|
|
|
else:
|
2020-11-04 01:38:58 +00:00
|
|
|
self.write4bits(ord(char), True)
|
|
|
|
|
2020-11-07 01:22:15 +00:00
|
|
|
def defaultPattern(n, t):
|
|
|
|
return ((n + t) % 255, (n + t) % 255, (n + t) % 255)
|
2020-11-07 01:12:24 +00:00
|
|
|
|
|
|
|
class LightStrip:
|
|
|
|
def __init__(self, data_pin = board.D18, string_length = 300, brightness = 1, pixel_order = neopixel.GRB):
|
|
|
|
# Emulate the old behavior of using RPi.GPIO if we haven't been given
|
|
|
|
# an explicit GPIO interface to use
|
|
|
|
|
|
|
|
self.np = neopixel.NeoPixel(data_pin, string_length, brightness = brightness, auto_write=False, pixel_order = pixel_order)
|
|
|
|
self.pattern = defaultPattern
|
2020-11-07 01:25:20 +00:00
|
|
|
self.cur_tick = 0
|
2020-11-07 01:12:24 +00:00
|
|
|
|
2020-11-07 01:22:15 +00:00
|
|
|
def set_light_level(self, level):
|
|
|
|
self.np.brightness = level
|
2020-11-07 01:12:24 +00:00
|
|
|
def pattern(self, pattern_callback):
|
|
|
|
self.pattern = pattern_callback
|
|
|
|
|
|
|
|
def tick(self):
|
|
|
|
np = self.np
|
2020-11-07 01:25:20 +00:00
|
|
|
t = self.cur_tick
|
2020-11-07 01:12:24 +00:00
|
|
|
n = np.n
|
|
|
|
for i in range(n):
|
2020-11-07 01:22:15 +00:00
|
|
|
col = self.pattern(i, t)
|
2020-11-07 01:12:24 +00:00
|
|
|
if np[i] != col:
|
|
|
|
np[i] = col
|
2020-11-07 01:25:20 +00:00
|
|
|
self.cur_tick = t + 1
|
2020-11-04 01:38:58 +00:00
|
|
|
|
2020-11-04 01:44:08 +00:00
|
|
|
debug_statements = True
|
|
|
|
|
|
|
|
|
|
|
|
def debug(msg):
|
|
|
|
if debug_statements:
|
|
|
|
print(msg)
|
|
|
|
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
def lightlevel(lcd, level):
|
2020-11-04 01:44:08 +00:00
|
|
|
debug("display level")
|
2020-11-04 01:38:58 +00:00
|
|
|
lcd.clear()
|
|
|
|
lcd.message("Light Level:\n]" + "-"*level + "[")
|
|
|
|
|
2020-11-04 01:44:08 +00:00
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
def querylightlevel():
|
2020-11-04 01:44:08 +00:00
|
|
|
debug("NYI")
|
|
|
|
return 7
|
|
|
|
|
2020-11-04 01:38:58 +00:00
|
|
|
|
2020-11-07 01:22:15 +00:00
|
|
|
def color(lcd):
|
2020-11-04 01:38:58 +00:00
|
|
|
lcd.clear()
|
2020-11-07 01:22:15 +00:00
|
|
|
lcd.message("new pattern loaded.")
|
2020-11-04 01:44:08 +00:00
|
|
|
debug("NYI")
|
2020-11-04 01:38:58 +00:00
|
|
|
|
|
|
|
|
2020-11-03 23:51:13 +00:00
|
|
|
def loop():
|
2020-11-04 01:38:58 +00:00
|
|
|
lcd = Adafruit_CharLCD()
|
2020-11-07 01:12:24 +00:00
|
|
|
lights = LightStrip()
|
2020-11-04 01:48:08 +00:00
|
|
|
level = 0
|
2020-11-06 23:01:22 +00:00
|
|
|
level_max = 14
|
2020-11-04 01:48:08 +00:00
|
|
|
idle = 0
|
|
|
|
idle_max = 15
|
2020-11-07 01:22:15 +00:00
|
|
|
cur_color = (255, 255, 255)
|
2020-11-04 01:38:58 +00:00
|
|
|
while True:
|
2020-11-04 01:44:08 +00:00
|
|
|
debug("loop")
|
|
|
|
query_level = querylightlevel()
|
2020-11-04 01:38:58 +00:00
|
|
|
idle = idle + 1
|
2020-11-04 01:44:08 +00:00
|
|
|
debug("idle value: {}".format(idle))
|
2020-11-07 01:22:15 +00:00
|
|
|
lights.tick()
|
|
|
|
if query_level != level:
|
2020-11-04 01:44:08 +00:00
|
|
|
level = query_level
|
2020-11-07 01:22:15 +00:00
|
|
|
lights.set_light_level(level / level_max)
|
2020-11-04 01:44:08 +00:00
|
|
|
idle = 0
|
2020-11-04 01:38:58 +00:00
|
|
|
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol:
|
|
|
|
lcd.display()
|
|
|
|
lightlevel(lcd, level)
|
2020-11-04 01:44:08 +00:00
|
|
|
elif idle >= idle_max:
|
|
|
|
debug("hit idle threshold")
|
|
|
|
idle = idle_max
|
|
|
|
lcd.noDisplay()
|
|
|
|
else:
|
|
|
|
lightlevel(lcd, level)
|
2020-11-04 01:38:58 +00:00
|
|
|
sleep(1)
|
|
|
|
|
2020-11-03 23:51:13 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-11-04 01:38:58 +00:00
|
|
|
loop()
|