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)

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 = "--------"

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')

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'))

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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s