test turn off display, stub color methods
This commit is contained in:
parent
1dd871da07
commit
76715d1dc2
1 changed files with 165 additions and 139 deletions
92
leds.py
92
leds.py
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
class Adafruit_CharLCD:
|
class Adafruit_CharLCD:
|
||||||
|
|
||||||
# commands
|
# commands
|
||||||
|
@ -52,8 +53,6 @@ class Adafruit_CharLCD:
|
||||||
LCD_5x10DOTS = 0x04
|
LCD_5x10DOTS = 0x04
|
||||||
LCD_5x8DOTS = 0x00
|
LCD_5x8DOTS = 0x00
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, pin_rs=24, pin_e=23, pins_db=[17, 18, 27, 22], GPIO=None):
|
def __init__(self, pin_rs=24, pin_e=23, pins_db=[17, 18, 27, 22], GPIO=None):
|
||||||
# Emulate the old behavior of using RPi.GPIO if we haven't been given
|
# Emulate the old behavior of using RPi.GPIO if we haven't been given
|
||||||
# an explicit GPIO interface to use
|
# an explicit GPIO interface to use
|
||||||
|
@ -85,11 +84,11 @@ class Adafruit_CharLCD:
|
||||||
|
|
||||||
""" Initialize to default text direction (for romance languages) """
|
""" Initialize to default text direction (for romance languages) """
|
||||||
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
|
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
|
||||||
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
|
self.write4bits(self.LCD_ENTRYMODESET |
|
||||||
|
self.displaymode) # set the entry mode
|
||||||
|
|
||||||
self.clear()
|
self.clear()
|
||||||
|
|
||||||
|
|
||||||
def begin(self, cols, lines):
|
def begin(self, cols, lines):
|
||||||
|
|
||||||
if (lines > 1):
|
if (lines > 1):
|
||||||
|
@ -97,18 +96,16 @@ class Adafruit_CharLCD:
|
||||||
self.displayfunction |= self.LCD_2LINE
|
self.displayfunction |= self.LCD_2LINE
|
||||||
self.currline = 0
|
self.currline = 0
|
||||||
|
|
||||||
|
|
||||||
def home(self):
|
def home(self):
|
||||||
|
|
||||||
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
|
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
|
||||||
self.delayMicroseconds(3000) # this command takes a long time!
|
self.delayMicroseconds(3000) # this command takes a long time!
|
||||||
|
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
|
|
||||||
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
|
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
|
||||||
self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time
|
# 3000 microsecond sleep, clearing the display takes a long time
|
||||||
|
self.delayMicroseconds(3000)
|
||||||
|
|
||||||
def setCursor(self, col, row):
|
def setCursor(self, col, row):
|
||||||
|
|
||||||
|
@ -119,88 +116,77 @@ class Adafruit_CharLCD:
|
||||||
|
|
||||||
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
|
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
|
||||||
|
|
||||||
|
|
||||||
def noDisplay(self):
|
def noDisplay(self):
|
||||||
""" Turn the display off (quickly) """
|
""" Turn the display off (quickly) """
|
||||||
|
|
||||||
self.displaycontrol &= ~self.LCD_DISPLAYON
|
self.displaycontrol &= ~self.LCD_DISPLAYON
|
||||||
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
||||||
|
|
||||||
|
|
||||||
def display(self):
|
def display(self):
|
||||||
""" Turn the display on (quickly) """
|
""" Turn the display on (quickly) """
|
||||||
|
|
||||||
self.displaycontrol |= self.LCD_DISPLAYON
|
self.displaycontrol |= self.LCD_DISPLAYON
|
||||||
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
||||||
|
|
||||||
|
|
||||||
def noCursor(self):
|
def noCursor(self):
|
||||||
""" Turns the underline cursor on/off """
|
""" Turns the underline cursor on/off """
|
||||||
|
|
||||||
self.displaycontrol &= ~self.LCD_CURSORON
|
self.displaycontrol &= ~self.LCD_CURSORON
|
||||||
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
||||||
|
|
||||||
|
|
||||||
def cursor(self):
|
def cursor(self):
|
||||||
""" Cursor On """
|
""" Cursor On """
|
||||||
|
|
||||||
self.displaycontrol |= self.LCD_CURSORON
|
self.displaycontrol |= self.LCD_CURSORON
|
||||||
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
||||||
|
|
||||||
|
|
||||||
def noBlink(self):
|
def noBlink(self):
|
||||||
""" Turn on and off the blinking cursor """
|
""" Turn on and off the blinking cursor """
|
||||||
|
|
||||||
self.displaycontrol &= ~self.LCD_BLINKON
|
self.displaycontrol &= ~self.LCD_BLINKON
|
||||||
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
||||||
|
|
||||||
|
|
||||||
def noBlink(self):
|
def noBlink(self):
|
||||||
""" Turn on and off the blinking cursor """
|
""" Turn on and off the blinking cursor """
|
||||||
|
|
||||||
self.displaycontrol &= ~self.LCD_BLINKON
|
self.displaycontrol &= ~self.LCD_BLINKON
|
||||||
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
|
||||||
|
|
||||||
|
|
||||||
def DisplayLeft(self):
|
def DisplayLeft(self):
|
||||||
""" These commands scroll the display without changing the RAM """
|
""" These commands scroll the display without changing the RAM """
|
||||||
|
|
||||||
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
|
self.write4bits(self.LCD_CURSORSHIFT |
|
||||||
|
self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
|
||||||
|
|
||||||
def scrollDisplayRight(self):
|
def scrollDisplayRight(self):
|
||||||
""" These commands scroll the display without changing the RAM """
|
""" These commands scroll the display without changing the RAM """
|
||||||
|
|
||||||
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
|
self.write4bits(self.LCD_CURSORSHIFT |
|
||||||
|
self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT)
|
||||||
|
|
||||||
def leftToRight(self):
|
def leftToRight(self):
|
||||||
""" This is for text that flows Left to Right """
|
""" This is for text that flows Left to Right """
|
||||||
|
|
||||||
self.displaymode |= self.LCD_ENTRYLEFT
|
self.displaymode |= self.LCD_ENTRYLEFT
|
||||||
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
|
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
||||||
|
|
||||||
|
|
||||||
def rightToLeft(self):
|
def rightToLeft(self):
|
||||||
""" This is for text that flows Right to Left """
|
""" This is for text that flows Right to Left """
|
||||||
self.displaymode &= ~self.LCD_ENTRYLEFT
|
self.displaymode &= ~self.LCD_ENTRYLEFT
|
||||||
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
||||||
|
|
||||||
|
|
||||||
def autoscroll(self):
|
def autoscroll(self):
|
||||||
""" This will 'right justify' text from the cursor """
|
""" This will 'right justify' text from the cursor """
|
||||||
|
|
||||||
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
|
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
|
||||||
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
||||||
|
|
||||||
|
|
||||||
def noAutoscroll(self):
|
def noAutoscroll(self):
|
||||||
""" This will 'left justify' text from the cursor """
|
""" This will 'left justify' text from the cursor """
|
||||||
|
|
||||||
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
|
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
|
||||||
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
|
||||||
|
|
||||||
|
|
||||||
def write4bits(self, bits, char_mode=False):
|
def write4bits(self, bits, char_mode=False):
|
||||||
""" Send command to LCD """
|
""" Send command to LCD """
|
||||||
|
|
||||||
|
@ -228,21 +214,21 @@ class Adafruit_CharLCD:
|
||||||
|
|
||||||
self.pulseEnable()
|
self.pulseEnable()
|
||||||
|
|
||||||
|
|
||||||
def delayMicroseconds(self, microseconds):
|
def delayMicroseconds(self, microseconds):
|
||||||
seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds
|
# divide microseconds by 1 million for seconds
|
||||||
|
seconds = microseconds / float(1000000)
|
||||||
sleep(seconds)
|
sleep(seconds)
|
||||||
|
|
||||||
|
|
||||||
def pulseEnable(self):
|
def pulseEnable(self):
|
||||||
self.GPIO.output(self.pin_e, False)
|
self.GPIO.output(self.pin_e, False)
|
||||||
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
|
# 1 microsecond pause - enable pulse must be > 450ns
|
||||||
|
self.delayMicroseconds(1)
|
||||||
self.GPIO.output(self.pin_e, True)
|
self.GPIO.output(self.pin_e, True)
|
||||||
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
|
# 1 microsecond pause - enable pulse must be > 450ns
|
||||||
|
self.delayMicroseconds(1)
|
||||||
self.GPIO.output(self.pin_e, False)
|
self.GPIO.output(self.pin_e, False)
|
||||||
self.delayMicroseconds(1) # commands need > 37us to settle
|
self.delayMicroseconds(1) # commands need > 37us to settle
|
||||||
|
|
||||||
|
|
||||||
def message(self, text):
|
def message(self, text):
|
||||||
""" Send string to LCD. Newline wraps to second line"""
|
""" Send string to LCD. Newline wraps to second line"""
|
||||||
|
|
||||||
|
@ -252,13 +238,53 @@ class Adafruit_CharLCD:
|
||||||
else:
|
else:
|
||||||
self.write4bits(ord(char), True)
|
self.write4bits(ord(char), True)
|
||||||
|
|
||||||
|
|
||||||
|
def lightlevel(lcd, level):
|
||||||
|
print("display level")
|
||||||
|
lcd.clear()
|
||||||
|
lcd.message("Light Level:\n]" + "-"*level + "[")
|
||||||
|
|
||||||
|
def querylightlevel():
|
||||||
|
print("NYI")
|
||||||
|
return 7
|
||||||
|
|
||||||
|
def color(lcd, col):
|
||||||
|
lcd.clear()
|
||||||
|
lcd.message("new color:\n#" + col)
|
||||||
|
print("NYI")
|
||||||
|
|
||||||
|
|
||||||
|
def querycolor():
|
||||||
|
print("NYI")
|
||||||
|
return "ffffff"
|
||||||
|
|
||||||
|
|
||||||
def loop():
|
def loop():
|
||||||
lcd = Adafruit_CharLCD()
|
lcd = Adafruit_CharLCD()
|
||||||
while True:
|
while True:
|
||||||
|
level = 0
|
||||||
|
level_max = 15
|
||||||
|
idle = 0
|
||||||
|
idle_max = 15
|
||||||
|
cur_color = "ffffff"
|
||||||
print("loop")
|
print("loop")
|
||||||
lcd.clear()
|
query_color = querycolor()
|
||||||
lcd.message(" LCD 1602 Test \n123456789ABCDEF")
|
level = querylightlevel()
|
||||||
sleep(2)
|
idle = idle + 1
|
||||||
|
if idle >= idle_max:
|
||||||
|
idle = idle_max
|
||||||
|
lcd.noDisplay()
|
||||||
|
elif query_color != cur_color:
|
||||||
|
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol:
|
||||||
|
lcd.display()
|
||||||
|
ccolor = query_color
|
||||||
|
color(lcd, cur_color)
|
||||||
|
else:
|
||||||
|
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol:
|
||||||
|
lcd.display()
|
||||||
|
lightlevel(lcd, level)
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
loop()
|
loop()
|
||||||
|
|
Loading…
Reference in a new issue