-
-
Save DenisFromHR/cc863375a6e19dce359d to your computer and use it in GitHub Desktop.
# requires RPi_I2C_driver.py | |
import RPi_I2C_driver | |
from time import * | |
mylcd = RPi_I2C_driver.lcd() | |
# test 2 | |
mylcd.lcd_display_string("RPi I2C test", 1) | |
mylcd.lcd_display_string(" Custom chars", 2) | |
sleep(2) # 2 sec delay | |
mylcd.lcd_clear() | |
# let's define a custom icon, consisting of 6 individual characters | |
# 3 chars in the first row and 3 chars in the second row | |
fontdata1 = [ | |
# Char 0 - Upper-left | |
[ 0x00, 0x00, 0x03, 0x04, 0x08, 0x19, 0x11, 0x10 ], | |
# Char 1 - Upper-middle | |
[ 0x00, 0x1F, 0x00, 0x00, 0x00, 0x11, 0x11, 0x00 ], | |
# Char 2 - Upper-right | |
[ 0x00, 0x00, 0x18, 0x04, 0x02, 0x13, 0x11, 0x01 ], | |
# Char 3 - Lower-left | |
[ 0x12, 0x13, 0x1b, 0x09, 0x04, 0x03, 0x00, 0x00 ], | |
# Char 4 - Lower-middle | |
[ 0x00, 0x11, 0x1f, 0x1f, 0x0e, 0x00, 0x1F, 0x00 ], | |
# Char 5 - Lower-right | |
[ 0x09, 0x19, 0x1b, 0x12, 0x04, 0x18, 0x00, 0x00 ], | |
# Char 6 - my test | |
[ 0x1f,0x0,0x4,0xe,0x0,0x1f,0x1f,0x1f], | |
] | |
# Load logo chars (fontdata1) | |
mylcd.lcd_load_custom_chars(fontdata1) | |
# Write first three chars to row 1 directly | |
mylcd.lcd_write(0x80) | |
mylcd.lcd_write_char(0) | |
mylcd.lcd_write_char(1) | |
mylcd.lcd_write_char(2) | |
# Write next three chars to row 2 directly | |
mylcd.lcd_write(0xC0) | |
mylcd.lcd_write_char(3) | |
mylcd.lcd_write_char(4) | |
mylcd.lcd_write_char(5) | |
sleep(2) | |
mylcd.lcd_clear() | |
mylcd.lcd_display_string_pos("Testing",1,1) # row 1, column 1 | |
sleep(1) | |
mylcd.lcd_display_string_pos("Testing",2,3) # row 2, column 3 | |
sleep(1) | |
mylcd.lcd_clear() | |
# Now let's define some more custom characters | |
fontdata2 = [ | |
# Char 0 - left arrow | |
[ 0x1,0x3,0x7,0xf,0xf,0x7,0x3,0x1 ], | |
# Char 1 - left one bar | |
[ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 ], | |
# Char 2 - left two bars | |
[ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 ], | |
# Char 3 - left 3 bars | |
[ 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c ], | |
# Char 4 - left 4 bars | |
[ 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e ], | |
# Char 5 - left start | |
[ 0x0,0x1,0x3,0x7,0xf,0x1f,0x1f,0x1f ], | |
# Char 6 - | |
# [ ], | |
] | |
# Load logo chars from the second set | |
mylcd.lcd_load_custom_chars(fontdata2) | |
block = chr(255) # block character, built-in | |
# display two blocks in columns 5 and 6 (i.e. AFTER pos. 4) in row 1 | |
# first draw two blocks on 5th column (cols 5 and 6), starts from 0 | |
mylcd.lcd_display_string_pos(block * 2,1,4) | |
# | |
pauza = 0.2 # define duration of sleep(x) | |
# | |
# now draw cust. chars starting from col. 7 (pos. 6) | |
pos = 6 | |
mylcd.lcd_display_string_pos(unichr(1),1,6) | |
sleep(pauza) | |
mylcd.lcd_display_string_pos(unichr(2),1,pos) | |
sleep(pauza) | |
mylcd.lcd_display_string_pos(unichr(3),1,pos) | |
sleep(pauza) | |
mylcd.lcd_display_string_pos(unichr(4),1,pos) | |
sleep(pauza) | |
mylcd.lcd_display_string_pos(block,1,pos) | |
sleep(pauza) | |
# and another one, same as above, 1 char-space to the right | |
pos = pos +1 # increase column by one | |
mylcd.lcd_display_string_pos(unichr(1),1,pos) | |
sleep(pauza) | |
mylcd.lcd_display_string_pos(unichr(2),1,pos) | |
sleep(pauza) | |
mylcd.lcd_display_string_pos(unichr(3),1,pos) | |
sleep(pauza) | |
mylcd.lcd_display_string_pos(unichr(4),1,pos) | |
sleep(pauza) | |
mylcd.lcd_display_string_pos(block,1,pos) | |
sleep(pauza) | |
# | |
# now again load first set of custom chars - smiley | |
mylcd.lcd_load_custom_chars(fontdata1) | |
mylcd.lcd_display_string_pos(unichr(0),1,9) | |
mylcd.lcd_display_string_pos(unichr(1),1,10) | |
mylcd.lcd_display_string_pos(unichr(2),1,11) | |
mylcd.lcd_display_string_pos(unichr(3),2,9) | |
mylcd.lcd_display_string_pos(unichr(4),2,10) | |
mylcd.lcd_display_string_pos(unichr(5),2,11) | |
sleep(2) | |
mylcd.lcd_clear() | |
sleep(1) | |
mylcd.backlight(0) |
# -*- coding: utf-8 -*- | |
""" | |
Compiled, mashed and generally mutilated 2014-2015 by Denis Pleic | |
Made available under GNU GENERAL PUBLIC LICENSE | |
# Modified Python I2C library for Raspberry Pi | |
# as found on http://www.recantha.co.uk/blog/?p=4849 | |
# Joined existing 'i2c_lib.py' and 'lcddriver.py' into a single library | |
# added bits and pieces from various sources | |
# By DenisFromHR (Denis Pleic) | |
# 2015-02-10, ver 0.1 | |
""" | |
# | |
# | |
import smbus | |
from time import * | |
class i2c_device: | |
def __init__(self, addr, port=1): | |
self.addr = addr | |
self.bus = smbus.SMBus(port) | |
# Write a single command | |
def write_cmd(self, cmd): | |
self.bus.write_byte(self.addr, cmd) | |
sleep(0.0001) | |
# Write a command and argument | |
def write_cmd_arg(self, cmd, data): | |
self.bus.write_byte_data(self.addr, cmd, data) | |
sleep(0.0001) | |
# Write a block of data | |
def write_block_data(self, cmd, data): | |
self.bus.write_block_data(self.addr, cmd, data) | |
sleep(0.0001) | |
# Read a single byte | |
def read(self): | |
return self.bus.read_byte(self.addr) | |
# Read | |
def read_data(self, cmd): | |
return self.bus.read_byte_data(self.addr, cmd) | |
# Read a block of data | |
def read_block_data(self, cmd): | |
return self.bus.read_block_data(self.addr, cmd) | |
# LCD Address | |
ADDRESS = 0x27 | |
# commands | |
LCD_CLEARDISPLAY = 0x01 | |
LCD_RETURNHOME = 0x02 | |
LCD_ENTRYMODESET = 0x04 | |
LCD_DISPLAYCONTROL = 0x08 | |
LCD_CURSORSHIFT = 0x10 | |
LCD_FUNCTIONSET = 0x20 | |
LCD_SETCGRAMADDR = 0x40 | |
LCD_SETDDRAMADDR = 0x80 | |
# flags for display entry mode | |
LCD_ENTRYRIGHT = 0x00 | |
LCD_ENTRYLEFT = 0x02 | |
LCD_ENTRYSHIFTINCREMENT = 0x01 | |
LCD_ENTRYSHIFTDECREMENT = 0x00 | |
# flags for display on/off control | |
LCD_DISPLAYON = 0x04 | |
LCD_DISPLAYOFF = 0x00 | |
LCD_CURSORON = 0x02 | |
LCD_CURSOROFF = 0x00 | |
LCD_BLINKON = 0x01 | |
LCD_BLINKOFF = 0x00 | |
# flags for display/cursor shift | |
LCD_DISPLAYMOVE = 0x08 | |
LCD_CURSORMOVE = 0x00 | |
LCD_MOVERIGHT = 0x04 | |
LCD_MOVELEFT = 0x00 | |
# flags for function set | |
LCD_8BITMODE = 0x10 | |
LCD_4BITMODE = 0x00 | |
LCD_2LINE = 0x08 | |
LCD_1LINE = 0x00 | |
LCD_5x10DOTS = 0x04 | |
LCD_5x8DOTS = 0x00 | |
# flags for backlight control | |
LCD_BACKLIGHT = 0x08 | |
LCD_NOBACKLIGHT = 0x00 | |
En = 0b00000100 # Enable bit | |
Rw = 0b00000010 # Read/Write bit | |
Rs = 0b00000001 # Register select bit | |
class lcd: | |
#initializes objects and lcd | |
def __init__(self): | |
self.lcd_device = i2c_device(ADDRESS) | |
self.lcd_write(0x03) | |
self.lcd_write(0x03) | |
self.lcd_write(0x03) | |
self.lcd_write(0x02) | |
self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) | |
self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) | |
self.lcd_write(LCD_CLEARDISPLAY) | |
self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) | |
sleep(0.2) | |
# clocks EN to latch command | |
def lcd_strobe(self, data): | |
self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT) | |
sleep(.0005) | |
self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT)) | |
sleep(.0001) | |
def lcd_write_four_bits(self, data): | |
self.lcd_device.write_cmd(data | LCD_BACKLIGHT) | |
self.lcd_strobe(data) | |
# write a command to lcd | |
def lcd_write(self, cmd, mode=0): | |
self.lcd_write_four_bits(mode | (cmd & 0xF0)) | |
self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0)) | |
# write a character to lcd (or character rom) 0x09: backlight | RS=DR< | |
# works! | |
def lcd_write_char(self, charvalue, mode=1): | |
self.lcd_write_four_bits(mode | (charvalue & 0xF0)) | |
self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0)) | |
# put string function | |
def lcd_display_string(self, string, line): | |
if line == 1: | |
self.lcd_write(0x80) | |
if line == 2: | |
self.lcd_write(0xC0) | |
if line == 3: | |
self.lcd_write(0x94) | |
if line == 4: | |
self.lcd_write(0xD4) | |
for char in string: | |
self.lcd_write(ord(char), Rs) | |
# clear lcd and set to home | |
def lcd_clear(self): | |
self.lcd_write(LCD_CLEARDISPLAY) | |
self.lcd_write(LCD_RETURNHOME) | |
# define backlight on/off (lcd.backlight(1); off= lcd.backlight(0) | |
def backlight(self, state): # for state, 1 = on, 0 = off | |
if state == 1: | |
self.lcd_device.write_cmd(LCD_BACKLIGHT) | |
elif state == 0: | |
self.lcd_device.write_cmd(LCD_NOBACKLIGHT) | |
# add custom characters (0 - 7) | |
def lcd_load_custom_chars(self, fontdata): | |
self.lcd_write(0x40); | |
for char in fontdata: | |
for line in char: | |
self.lcd_write_char(line) | |
# define precise positioning (addition from the forum) | |
def lcd_display_string_pos(self, string, line, pos): | |
if line == 1: | |
pos_new = pos | |
elif line == 2: | |
pos_new = 0x40 + pos | |
elif line == 3: | |
pos_new = 0x14 + pos | |
elif line == 4: | |
pos_new = 0x54 + pos | |
self.lcd_write(0x80 + pos_new) | |
for char in string: | |
self.lcd_write(ord(char), Rs) |
Hey all! I came across this looking for info on how to get my own LCD/I2C combo to work with my rpi 3, and thought it would be beneficial for enough people to make it into a pypi package. Any ideas/contributions/complaints are welcome!
I can't understand what mean: 0x00, 0x00, 0x03, 0x04, 0x08, 0x19, 0x11, 0x10
What is this? Coordonates? Or what? Because i don't understand how to edit this
Hi, can u help me ?
When i run this file, i've this messge !
Traceback (most recent call last):
.../exampleLCD.py", line 91, in
mylcd.lcd_display_string_pos(unichr(1),1,pos)
NameError: name 'unichr' is not defined
where do i defined this 'unichr' ?
thanks
@DenisFromHR any idea what these are for?
def __init__(self):
self.lcd_device = i2c_device(ADDRESS)
self.lcd_write(0x03) # what
self.lcd_write(0x03) # are
self.lcd_write(0x03) # these
self.lcd_write(0x02) # for
self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
Great stuff!
I live in Norway, but apparently I have the LCD ROM with the Japanese font set, which makes displaying the nordic characters (æ, ø, å) a real headache, at least when you're dealing with inconsistent ascii-tables and unicode (UTF8) conversions on top of that.
However, I made a custom font set with the "missing" norwegian characters, if anyone's interested.
I used this character generator, set it to "hex" datatype, and used @DenisFromHR 's example code to write the characters to the display.
Just out of curiousity:
Is there a an (relatively easy) way to "map" these custom characters to their unicode/UTF-8 counterparts?
Example: If I try to
"lcd_display_string("æøå", 1)
it would be GREAT if I somehow managed to automatically map the character "æ" to (for instance) chr(1), "ø" to chr(2) and so on.
And if everything else fails, shouldn't it be possible to do a "char.replace('ø', chr(1))", or something similar in Python? It would make the programming flow SO much easier.
Any thoughts?
Anyway, here's my (very basic) example code (updated):
import RPi_I2C_driver
lcd = RPi_I2C_driver.lcd()
fontdata1 = [
# Æ
[ 0x00,0x0C,0x13,0x12,0x1F,0x12,0x13,0x00 ],
# Ø
[ 0x01,0x0E,0x13,0x15,0x19,0x0E,0x10,0x00 ],
# Å
[ 0x04,0x00,0x0E,0x11,0x1F,0x11,0x11,0x00 ],
# æ
[ 0x00,0x00,0x1A,0x05,0x0F,0x14,0x1F,0x00 ],
# ø
[ 0x00,0x01,0x0E,0x15,0x15,0x0E,0x10,0x00 ],
# å
[ 0x04,0x00,0x0E,0x01,0x0F,0x11,0x0F,0x00 ],
# degrees Celsius
[ 0x08,0x14,0x0B,0x04,0x04,0x03,0x00,0x00 ],
# humidity
[ 0x00,0x04,0x0E,0x0E,0x1F,0x1F,0x0E,0x04 ],
# low batt.
[ 0x0E,0x11,0x11,0x11,0x11,0x11,0x1F,0x1F ],
# full batt.
[ 0x0E,0x11,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F ],
]
lcd.lcd_load_custom_chars(fontdata1)
#and to use the custom characters:
lcd.lcd_display_string(chr(2) + "some other random letters" , 1)
lcd.lcd_display_string("Batt: " + chr(9) , 2)
Update:
Here's how I managed to add the extra Norwegian characters to the font set. I know there are more efficient ways to do this, but I'm still learning Python, and for this purpose it kind of works:
lcd = RPi_I2C_driver.lcd()
LCD_NORCHARS = [
[ 0b00000,0b01111,0b10100,0b10100,0b11111,0b10100,0b10111,0b00000 ], # Æ
[ 0b00001,0b01110,0b10011,0b10101,0b10101,0b11001,0b01110,0b10000 ], # Ø
[ 0b01100,0b00000,0b01100,0b10010,0b11110,0b10010,0b10010,0b00000 ], # Å
[ 0b00000,0b00000,0b01101,0b00010,0b01111,0b10010,0b01101,0b00000 ], # æ
[ 0b00000,0b00000,0b00001,0b01110,0b10101,0b10101,0b01110,0b10000 ], # ø
[ 0b00000,0b01100,0b00000,0b01100,0b00010,0b11110,0b10010,0b01101 ], # å
]
lcd.lcd_load_custom_chars(LCD_NORCHARS)
string01 = "Æ Ø Å"
string02 = "æ ø å"
string01_N = string01.replace('Æ', chr(0)).replace('Ø', chr(1)).replace('Å', chr(2)).replace('æ', chr(3)).replace('ø', chr(4)).replace('å', chr(5)).decode('utf8') # This would be better with a "For X in array..." loop, but I can't figure out how to do it yet
string02_N = string02.replace('Æ', chr(0)).replace('Ø', chr(1)).replace('Å', chr(2)).replace('æ', chr(3)).replace('ø', chr(4)).replace('å', chr(5)).decode('utf8')
lcd.lcd_display_string(string01_N, 1)
lcd.lcd_display_string(string02_N, 2)
Good job, Denis!
I love the user defined characters. I've created a little php script to be able to create the code for the special characters in the syntax you are using here, e.g. [ 0x0E,0x11,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F ]
In case anyone is interested, here it is:
https://pastebin.com/EL1nR5MX
cheers, Martin
Hi Denis,
This looks awesome, thanks for putting this on github. I’m very interested in some examples that you mentioned in your first post to make a scrolling text.
Thanks a ton
Maikel
I'm trying to use this driver with PCF8574's younger sibling - PCA9554P with a 16x2 lcd display, but it does not work at all. I'm constantly getting a same error:
Traceback (most recent call last): File "examples.py", line 5, in <module> mylcd = RPi_I2C_driver.lcd() File "/home/pi/i2c/RPi_I2C_driver.py", line 105, in __init__ self.lcd_write(0x03) File "/home/pi/i2c/RPi_I2C_driver.py", line 130, in lcd_write self.lcd_write_four_bits(mode | (cmd & 0xF0)) File "/home/pi/i2c/RPi_I2C_driver.py", line 125, in lcd_write_four_bits self.lcd_device.write_cmd(data | LCD_BACKLIGHT) File "/home/pi/i2c/RPi_I2C_driver.py", line 24, in write_cmd self.bus.write_byte(self.addr, cmd) IOError: [Errno 121] Remote I/O error
with python 2.7.13
and
Traceback (most recent call last): File "examples.py", line 5, in <module> mylcd = RPi_I2C_driver.lcd() File "/home/pi/i2c/RPi_I2C_driver.py", line 105, in __init__ self.lcd_write(0x03) File "/home/pi/i2c/RPi_I2C_driver.py", line 130, in lcd_write self.lcd_write_four_bits(mode | (cmd & 0xF0)) File "/home/pi/i2c/RPi_I2C_driver.py", line 125, in lcd_write_four_bits self.lcd_device.write_cmd(data | LCD_BACKLIGHT) File "/home/pi/i2c/RPi_I2C_driver.py", line 24, in write_cmd self.bus.write_byte(self.addr, cmd) File "/usr/local/lib/python3.5/dist-packages/smbus/util.py", line 59, in validator return fn(*args, **kwdefaults) File "/usr/local/lib/python3.5/dist-packages/smbus/smbus.py", line 121, in write_byte raise IOError(ffi.errno) OSError: 121
with python 3.5.3
I'm using Raspbian stretch full, downloaded on 9.11.2018.
What's wrong with my situation?
Is it possible to run multiple LCD's each with a different address?
Thanks for the libs!
I've spend several hours for trying start libs working.. And I fix it with screwdriver.. Fix potentiometer brightness.
Thank you for posting this - I was pulling my hair out over the same thing.
Hi Denis
Thanks for the libs. I have two 16x2 LCD's from different shops.
It works fine on LCD 1, but LCD 2 displays sometimes a few characters, sometimes blocks, sometimes nothing, hanging for a few seconds. Switching on and off the display works. Probably a timing problem.
Any Idea where to change any delays?
Dear @DenisFromHR
Myself and some others use your code to make a contribution to the RPi-Jukebox-RFID project. This project allows to build a simple to use Jukebox like Music Player which is extremely interesting for small Kids. I use these Jukeboxes for my three Kids.
We developed a project specific script this uses directly your RPi_I2C_driver.py driver (renamed to i2c_lcd_driver.py to be compatible with the contribution regulations of the RPi-Jukebox-RFID project).
Current status is, that I placed a pull request to the RPi-Jukebox-RFID project which can be found under RPi-Jukebox-RFID Pill request #859.
Currently the decision is that the code cannot be added to the project as we have a license issue. The RPi-Jukebox-RFID projet uses MIT license, see also RPI-Jukebox-RFID License Page. Your driver is released under GPL.
I kindly ask you read the discussion on the pull request page related to the driver scripts/i2c_lcd_driver.py where the license issue is discussed. I hope that you could to a statement, that allows us to use your code in our Jukebox project.
Thank you very much for your help regarding this matter.
Best regards,
Simon
Dear @DenisFromHR ,how can a symbol be updated? For example from this
to this
My code
def get_cpu_temp():
tmp = open('/sys/class/thermal/thermal_zone0/temp')
cpu = tmp.read()
tmp.close()
return '{:.2f}'.format(float(cpu) / 1000)
mylcd.lcd_load_custom_chars(fontdata2)
mylcd.lcd_display_string(chr(2)+nowtime, 1)
cpu_temp = get_cpu_temp()
if cpu_temp > 50:
mylcd.lcd_display_string(chr(1)+cpu_temp, 2)
else:
mylcd.lcd_display_string(chr(0)+cpu_temp, 2)
My code work in while True: for update every second
And how i cant disable BACKLIGHT without blink every second?
if 15 < d.hour < 23:
mylcd.backlight(1)
else:
mylcd.backlight(0)
Complete
def lcd_strobe(self, data):
d = datetime.now()
if 10 > d.minute > 8:
self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
sleep(.0005)
self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
sleep(.0001)
else:
self.lcd_device.write_cmd(data | En | LCD_NOBACKLIGHT)
sleep(.0005)
self.lcd_device.write_cmd(((data & ~En) | LCD_NOBACKLIGHT))
sleep(.0001)
But I still don't understand how to change a character along with a string
Thanks for the libs!
I've spend several hours for trying start libs working.. And I fix it with screwdriver.. Fix potentiometer brightness.
Thanks, I'm new to this stuff, TIL about potentiometer from your comment
Hi Dennis,
Here Ger from Holland.
Can you tell me if you already made a scroll option.
Your scripts are very nice.
Thanks for an answer
Greetings Ger
Hi Dennis,
I'm using your library in my outdoor spa project. It works like a charm!
Thx.
Thanks for the code Dennis. Here's what I'm using for text scrolling in case it helps anyone:
async def display(lcd, display_queue):
ln1, ln2 = "", ""
ln1_i, ln2_i = 0, 0
while True:
if not display_queue.empty():
ln1, ln2 = await display_queue.get()
ln1_i, ln2_i = 0, 0
lcd.lcd_clear()
else:
ln1_i = 0 if len(ln1) <= 16 else (ln1_i + 1) % (len(ln1) - NUM_COLS + 1)
ln2_i = 0 if len(ln2) <= 16 else (ln2_i + 1) % (len(ln2) - NUM_COLS + 1)
lcd.lcd_display_string(ln1[ln1_i:ln1_i+NUM_COLS], 1)
lcd.lcd_display_string(ln2[ln2_i:ln2_i+NUM_COLS], 2)
await asyncio.sleep(0.5)
where display_queue is an asyncio.Queue.
To display something, call await display_queue.put(("Loooooooooooooooooonng", "Teeeeeeeeeeeeeeeeeeext"))
Hi Dennis,
Thank you for your work on this. I attempted to make this work in the 8-bit mode but have not been able to.
116 self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_8BITMODE)
How can the following write statements be condensed into 1 line instead of the two in 8 bit mode.
def lcd_write_char(self, charvalue, mode=1):
self.lcd_write_four_bits(mode | (charvalue & 0xF0))
self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0))
def lcd_write(self, cmd, mode=0):
self.lcd_write_four_bits(mode | (cmd & 0xF0))
self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
I tried this but it gives strange results even though the binary output is correct for this command:
def lcd_write(self, cmd, mode=0):
self.lcd_write_four_bits(mode | (cmd & 0xFF))
Looking for help or advice on this.
I have the RPi_I2C_driver working fine for basic use, but when I try to use it how I want, strange things happen similar to what raphaelyancey and Machacator mention about strange characters. I also experience it writing the strange characters to the wrong lines.
What I am trying to do is have 4 independent scripts, each write to one single line.
No scrolling code.
All text is being printed from each script is 20 or less characters.
Each script writes to one line directly using either:
mylcd.lcd_display_string("string here",1);
or
mylcd.lcd_display_string_pos("this string here",2,0);
Script 1 - python - real time date and time with seconds displayed on line 1 - script is always running and updates line one once a second. No lcd_clear() used.
Script 2 - outside temp and inside temp checked every minute displayed on line 2. No lcd_clear() used. Script is run from cron, writes to LCD and exits.
Script 3 - RSS feed tracking my favorite NHL team score updated every 3 minutes and displays on line 3. No lcd_clear() used. Script is run from cron, writes to LCD and exits.
Script 4 - a feedback line showing last home automation feature used and display on line 4. No lcd_clear() used. Script is run when a command is issued, so it can be anytime. Script outputs and then exits.
So, each one works perfectly on their own.
When I have two or more running, it will soon start writing random characters to the screen, mostly on the wrong lines (can be above or below). It can happen nearly instantly or start happening 20 minutes later.
When it starts, only a lcd_clear can fix it. Even if line 1 is being controlled by the always running date time script updating every second.
I have tried adding the screen clear to one or more of the scripts, but due to other issues (data not being present on the script run), lines won't update again until the next cron run or command. Not ideal, plus I hate the way the lcd_clear() looks when it happens.
I am sure it has to do something with two or more scripts trying to access the lcd at nearly the same time.
I assumed having the ability to address the writing per line wouldn't cause any issues, but it doesn't seem that way.
So, any ideas or suggestions to get 4 scripts each writing their own line, to play nice and not corrupt the display?
My only limitation I can think of is that combining the scripts into one is not an option.
Any thoughts are welcomed.
Thanks!