LCD Manager

Code example – simple clock

Code that shows how to use libraries to display a clock on 20×4 lcd screen via i2c bus.

import time
from charlcd.buffered import CharLCD
from charlcd.drivers.i2c import I2C
from lcdmanager.manager import Manager
from lcdmanager.widget.label import Label
from lcdmanager.display import Display


i2c_20x4 = I2C(0x3b, 1)
i2c_20x4.pins = {
    'RS': 6,
    'E': 4,
    'E2': None,
    'DB4': 0,
    'DB5': 1,
    'DB6': 2,
    'DB7': 3
}

First connection to screen is defined as I2C on port 0x3b and bus 1. Next we set pins.

lcd = CharLCD(20, 4, i2c_20x4, 0, 0)
lcd.init()

Here we initialize CharLCD with width 20 chars, height 4 chars, defined connection, without cursor.

lcd_manager = Manager(lcd)

label1 = Label(8, 1)
label1.label = "--------"
lcd_manager.add_widget(label1)

Now we create manger for given LCD. Create label widget on which we will display time. Label is positioned at (8, 1). Next we add this label to manager.

disp = Display(0.5, True)
disp.add(lcd_manager, 'one')
disp.start()

Almost done. This will create a display server that will keep refreshing at 0.5 fps. See that we send True as second parameter. This will render manager before flushing it.
Next we add our manager to display server and run it.

while True:
    label1.label = str(time.strftime('%H:%M:%S'))
    time.sleep(1)

This is a loop that is setting label to current time. We don’t have to call render manually. We are just changing label and all is done automatically.

Advertisements

LCDManager- fault protection

Imagine situation, you have a perfect LCD that displays a perfect content on a perfect day. On this perfect day some imperfect being trip and unplug power from a display. It goes off, RPi tries to send packet to it and a program explodes with:


IOError: [Errno 5] Input/output error

And the perfect day is back to a typical day. What can we do about it ? We may write a fault protection.
During usage I met mostly with two problems: one power was lost and second there were a some kind of interference and screen goes wild. How wild ? With each tick content was shifted or a gibberish was displayed. We can do something with both cases.

Download source from Bitbucket

(more…)

LCD Manager – display server

Our LCD Manager would be better with display server, something that will take care of rendering at constant FPS. If you look at Piader v2 you see that there is a main_loop function. In it we refresh lcd content at some fps but it is quite annoying because it shouldn’t be our problem. Manager should take care of it.

Our task for now: add display server.

LCDManager@Bitbucket
Piader@Bitbucket

(more…)

Piader v2 – scoreboard

We have a nice game and we may play it. But there is one problem we don’t see number of lives and our score. In this article we will create another view and position it on second display. I played with 40×4 for game and 20×4 for score but ended with one lcd with two vlcds 🙂

We will also do something with difficulty as we have such setting it would be nice that it would do something

And of course we can’t forget about bugs, we will fix few along the way.

(more…)

LCD Manager – part 8: widget canvas

Thinking about Piader game view brought me to conclusion that we need a canvas widget. The widget on which we can draw any char at any place. Something similar to pure lcd usage. We will be able to set cursor at any position in canvas and put char. Internal pointer will allow us to stream chars one after another.

(more…)

Piader v2 – views

In this article we will work on views and create our first view for home tab. We could put all code into one file but it would be a mess. So lets take a different approach, we will create a separate file for each view. Next we will work on button widget and create quit game action. Don’t forget that Piader is a tool to improve LCDManager.

(more…)