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:
Run your script as root.
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:
Type
sudo raspi-config
in Terminal.Go to
Interface Options
.Choose
I2C
Select
Yes
, and thenOK
.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.
-
exception
screen2c.
CommunicationError
(addr, port, message='Error communicating with device!')¶
-
class
screen2c.
CursorMode
¶ An enum representing the cursor modes available on the display.
-
BLINK
= 1¶
-
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
NoDeviceError – If there is no I2C device at the specified address
PermissionDeniedError – If smbus is denied permission to connect to the device
I2CDisabledError – If I2C is disabled on this device
-
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
¶