screen2c

PCF8574 + LCD = fun

What is it?

Exactly what it sounds like. I couldn’t find any good libraries to do this, so I modified one and made it better. Much thanks to the original dev, Denis Pleic.

How do I get it?

pip install screen2c

How do I use it?

Easy-peasy.

1. Import the module, and create a Display object. This is what you’ll use to do all the stuff to the screen.

import screen2c

display = screen2c.Display()

2. Do stuff with the Display! To display text, call write(), with the text as the first argument and line as the second one.

display.write("Hello", 1)
display.write("World!", 2)
# Infinite loop so that the program doesn't instantly stop
while True:
        pass

Examples

Display “Hello World” on the screen

import screen2c

display = screen2c.Display()

display.write("Hello World!!!", 1)

while True:
        pass

Typewriter out text

import screen2c
import time

display = screen2c.Display()
display.setCursor(screen2c.CursorMode.BLINK)

typewriter = "Hello again!"
for x in range(0, len(typewriter)):
        display.write(typewriter[x], 1, x)
        time.sleep(0.2)

while True:
        pass

Disco party, because why not

import screen2c
import time
display = screen2c.Display()

while True:
        display.setBacklight(not display.backlightOn)
        time.sleep(0.2)

Troubleshooting

Oh noes! Something’s gone wrong, and you’ve encountered that most dreaded of Python classes, an Exception! Don’t worry, it’s not usually a severe issue. Keep reading!

PermissionDeniedError

This one’s pretty self-explanatory. There are two fixes:

  1. Run your script as root.

  2. Add yourself to the GPIO and I2C groups using:

sudo usermod -aG gpio yourusername
sudo usermod -aG i2c yourusername

and reboot.

NoDeviceError

Again, self-explanatory. Check your screen is connected to the right GPIO pins and that no cables are loose! If it is, then you may have the wrong address for the screen. To check the address, type:

sudo i2cdetect -y 1
# or, if you have a really old RaspPi
sudo i2cdetect -y 0

You’ll get output that looks like this:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- 1a -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

My Pi has 2 devices, one at address 27 and one at address 1a. 27 is my screen, and also happens to be the default for the Display. Keey note of the screen address. (If you have multiple and don’t know which it is, then try each. XD)

Then, change the code for your Display object to look like this

display = screen2c.Display(0xYourAddress)

For me, it would be

display = screen2c.Display(0x27)

CommunicationError

This error generally occurs when one of the cables connecting the Pi to the screen has become disconnected between Display object creation and a method being called. Plug the cables back in and try again.

Very rarely this error can occur due to other factors. If this happens, file an issue on GitHub.

I2CDisabledError

This error occurs because you have the I2C interface disabled on your RaspPi (this is how it is by default.) To fix it, follow this simple five-step process:

  1. Type sudo raspi-config in Terminal.

  2. Go to Interface Options.

  3. Choose I2C

  4. Select Yes, and then OK.

  5. Select Finish.

Piece of cake.

Anything else

Basically, this is an issue either with something in the depths of the Linux kernel underworld or an issue with screen2c itself. In both cases, file an issue on GitHub and I’ll see what I can do about it.

API

screen2c 0.1.1, made by Denis Pleic and The Ginger.

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

Code modified and packaged 2021 by The Ginger
Credit to everyone who had a hand in making the original
LONG LIVE LINUX
2021-04-12
exception screen2c.CommunicationError(addr, port, message='Error communicating with device!')
class screen2c.CursorMode

An enum representing the cursor modes available on the display.

Variables
  • NONE – No cursor

  • LINE – A line cursor

  • BLINK – A blinking block-shaped cursor.

LINE = 2
NONE = 0
class screen2c.Display(address=39)

The actual display.

Variables

backlightOn – Whether or not the backlight is on. DO NOT SET THIS PROPERTY

Parameters

address (hex, optional) – The hex address of the screen (Defaults to 0x27)

Raises
clear()

Clear the display.

lcd_load_custom_chars(fontdata)
send(cmd, mode=0)

Sends a hexadecimal command to the display.

Parameters
  • cmd (hex) – The hexadeximal command to send.

  • mode (int, optional) – Good question.

Raises

CommunicationError – If the data fails to be sent.

setBacklight(state)

Changes the state of the backlight.

Parameters

state (bool) – Whether or not the backlight is on.

setCursor(mode)

Change the cursor mode.

Parameters

mode (screen2c.CursorMode) – The mode of the cursor.

write(string, line=1, pos=0)

Displays a string on the display.

Parameters
  • string (str) – The text to display

  • line (int, optional) – The line number (Defaults to 1)

  • pos (int, optional) – The position of the start of the string (Defaults to 0)

exception screen2c.I2CDisabledError(port)
exception screen2c.I2CError(message, port)
exception screen2c.NoDeviceError(addr, port)
exception screen2c.PermissionDeniedError
exception screen2c.ScreenError

Indices and tables