Sending updates to external screen + RGB LCD
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import time
|
||||
import sys
|
||||
from sensors.lock import grove_lock
|
||||
|
||||
if sys.platform == 'uwp':
|
||||
import winrt_smbus as smbus
|
||||
bus = smbus.SMBus(1)
|
||||
else:
|
||||
import smbus
|
||||
import RPi.GPIO as GPIO
|
||||
rev = GPIO.RPI_REVISION
|
||||
if rev == 2 or rev == 3:
|
||||
bus = smbus.SMBus(1)
|
||||
else:
|
||||
bus = smbus.SMBus(0)
|
||||
|
||||
# Device I2C addresses
|
||||
DISPLAY_RGB_ADDR = 0x62
|
||||
DISPLAY_TEXT_ADDR = 0x3e
|
||||
|
||||
|
||||
def setRGB(r, g, b, brightness=1.0):
|
||||
"""Set backlight to (R,G,B) with optional brightness level (0.0 to 1.0)."""
|
||||
brightness = max(0.0, min(1.0, float(brightness)))
|
||||
|
||||
r_scaled = int(max(0, min(255, r * brightness)))
|
||||
g_scaled = int(max(0, min(255, g * brightness)))
|
||||
b_scaled = int(max(0, min(255, b * brightness)))
|
||||
|
||||
with grove_lock:
|
||||
try:
|
||||
bus.write_byte_data(DISPLAY_RGB_ADDR, 0, 0)
|
||||
bus.write_byte_data(DISPLAY_RGB_ADDR, 1, 0)
|
||||
bus.write_byte_data(DISPLAY_RGB_ADDR, 0x08, 0xaa)
|
||||
bus.write_byte_data(DISPLAY_RGB_ADDR, 4, r_scaled)
|
||||
bus.write_byte_data(DISPLAY_RGB_ADDR, 3, g_scaled)
|
||||
bus.write_byte_data(DISPLAY_RGB_ADDR, 2, b_scaled)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def textCommand(cmd):
|
||||
"""Send command to display (internal use)."""
|
||||
bus.write_byte_data(DISPLAY_TEXT_ADDR, 0x80, cmd)
|
||||
|
||||
|
||||
def setText(text):
|
||||
"""Set display text (\n for second line or auto wrap)."""
|
||||
with grove_lock:
|
||||
try:
|
||||
textCommand(0x01) # Clear display
|
||||
time.sleep(0.05)
|
||||
textCommand(0x08 | 0x04) # Display on, no cursor
|
||||
textCommand(0x28) # 2 lines
|
||||
time.sleep(0.05)
|
||||
|
||||
count = 0
|
||||
row = 0
|
||||
for c in text:
|
||||
if c == '\n' or count == 16:
|
||||
count = 0
|
||||
row += 1
|
||||
if row == 2:
|
||||
break
|
||||
textCommand(0xc0)
|
||||
if c == '\n':
|
||||
continue
|
||||
count += 1
|
||||
bus.write_byte_data(DISPLAY_TEXT_ADDR, 0x40, ord(c))
|
||||
time.sleep(0.001) # Small pacing delay to prevent LCD buffer overflow
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def setText_norefresh(text):
|
||||
"""Update display text without full screen erase."""
|
||||
with grove_lock:
|
||||
try:
|
||||
textCommand(0x02) # Return home
|
||||
time.sleep(0.05)
|
||||
textCommand(0x08 | 0x04) # Display on, no cursor
|
||||
textCommand(0x28) # 2 lines
|
||||
time.sleep(0.05)
|
||||
|
||||
count = 0
|
||||
row = 0
|
||||
while len(text) < 32: # Clear rest of screen space
|
||||
text += ' '
|
||||
for c in text:
|
||||
if c == '\n' or count == 16:
|
||||
count = 0
|
||||
row += 1
|
||||
if row == 2:
|
||||
break
|
||||
textCommand(0xc0)
|
||||
if c == '\n':
|
||||
continue
|
||||
count += 1
|
||||
bus.write_byte_data(DISPLAY_TEXT_ADDR, 0x40, ord(c))
|
||||
time.sleep(0.001) # Small pacing delay to prevent LCD buffer overflow
|
||||
except OSError:
|
||||
pass
|
||||
Reference in New Issue
Block a user