Last active
March 29, 2026 16:48
-
-
Save ooliver1/a9917e5a0c43c5974cc73cb765bbcc37 to your computer and use it in GitHub Desktop.
Updated grove_rgb_lcd.py for display v5 - rgb addr changed, rgb data addresses changed, reset sequence changed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_rgb_lcd/grove_rgb_lcd.py | |
| # Updated to display v5 as per addresses listed in https://docs.rs/crate/grove-lcd-rgb/0.1.0 | |
| # RGB_ADDR is 0x30, and the data addresses are 6, 7, 8. | |
| import sys | |
| import time | |
| 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) | |
| # this device has two I2C addresses | |
| DISPLAY_RGB_ADDR = 0x30 | |
| DISPLAY_TEXT_ADDR = 0x3e | |
| # set backlight to (R,G,B) (values from 0..255 for each) | |
| def setRGB(r,g,b): | |
| bus.write_byte_data(DISPLAY_RGB_ADDR,0,0x07) | |
| bus.write_byte_data(DISPLAY_RGB_ADDR,4, 0x15) | |
| bus.write_byte_data(DISPLAY_RGB_ADDR,0x08,0xaa) | |
| bus.write_byte_data(DISPLAY_RGB_ADDR,0x06,r) | |
| bus.write_byte_data(DISPLAY_RGB_ADDR,0x07,g) | |
| bus.write_byte_data(DISPLAY_RGB_ADDR,0x08,b) | |
| # send command to display (no need for external use) | |
| def textCommand(cmd): | |
| bus.write_byte_data(DISPLAY_TEXT_ADDR,0x80,cmd) | |
| # set display text \n for second line(or auto wrap) | |
| def setText(text): | |
| textCommand(0x01) # clear display | |
| time.sleep(.05) | |
| textCommand(0x08 | 0x04) # display on, no cursor | |
| textCommand(0x28) # 2 lines | |
| time.sleep(.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)) | |
| #Update the display without erasing the display | |
| def setText_norefresh(text): | |
| textCommand(0x02) # return home | |
| time.sleep(.05) | |
| textCommand(0x08 | 0x04) # display on, no cursor | |
| textCommand(0x28) # 2 lines | |
| time.sleep(.05) | |
| count = 0 | |
| row = 0 | |
| while len(text) < 32: #clears the rest of the screen | |
| 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)) | |
| # Create a custom character (from array of row patterns) | |
| def create_char(location, pattern): | |
| """ | |
| Writes a bit pattern to LCD CGRAM | |
| Arguments: | |
| location -- integer, one of 8 slots (0-7) | |
| pattern -- byte array containing the bit pattern, like as found at | |
| https://omerk.github.io/lcdchargen/ | |
| """ | |
| location &= 0x07 # Make sure location is 0-7 | |
| textCommand(0x40 | (location << 3)) | |
| bus.write_i2c_block_data(DISPLAY_TEXT_ADDR, 0x40, pattern) | |
| # example code | |
| if __name__=="__main__": | |
| setText("Hello world\nThis is an LCD test") | |
| setRGB(0,128,64) | |
| time.sleep(2) | |
| for c in range(0,255): | |
| setText_norefresh("Going to sleep in {}...".format(str(c))) | |
| setRGB(c,255-c,0) | |
| time.sleep(0.1) | |
| setRGB(0,255,0) | |
| setText("Bye bye, this should wrap onto next line") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment