Program to write display text on Nokia 5110 LCD

Before running any SPI based program, we need to enable the SPI pins. There is a need to load a device tree overlay related to SPI from /lib/firmware. Run the following command as the root in the BeagleBone console:

 ls /lib/firmware | grep -i SPI

This command should output something like "ADAFRUIT-SPI0-00A0.dtbo" and "ADAFRUIT-SPI1-00A0.dtbo" on the terminal. Then run the following command. The change string is getting echoed in the following command according to the output of the previous command.

 sudo sh –c "echo ADAFRUIT-SPI0 > /sys/devices/bone_capemgr.8/slots"

Writing about all the LCD logic drivers is out of the scope of this book. Let us use the Nokia LCD Python library made by Adafruit. This library has a driver to draw text and images on the 5110 LCD. Let us install the library and its dependency module pillow:

git clone https://github.com/adafruit/Adafruit_Nokia_LCD.git
cd Adafruit_Nokia_LCD sudo python setup.py install
sudo pip install pillow

Connect Nokia 5110 LCD to BeagleBone as shown in the diagram. If you cannot see LEDs on the display lit, connect the LED pin to GND. Type the following program in Cloud9, save it as Nokia5110.py and run. You should see the text "Hello World" on the LCD display:

import math,time
from PIL import Image, ImageFont, ImageDraw
import Adafruit_Nokia_LCD as LCD
import Adafruit_GPIO.SPI as SPI

# Beaglebone Black hardware SPI config:
DC = 'P9_15'
RST = 'P9_12'
SPI_PORT = 1
SPI_DEVICE = 0 text = 'Hello World'

disp = LCD.PCD8544(DC, RST, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=4000000))
disp.begin(contrast=60)
disp.clear()
disp.display()
image = Image.new('1', (LCD.LCDWIDTH, LCD.LCDHEIGHT))
font = ImageFont.load_default()
draw = ImageDraw.Draw(image)

print 'Press Ctrl-C to quit.'
while True:
        draw.rectangle((0,0,83,47), outline=255, fill=255)
        x=0
        for c in text:
                x = x + 7
                draw.text((x,0),c,font=font,fill=0)
        disp.image(image)
        disp.display()
        time.sleep(0.1)

Explanation

This program gives BeagleBone the ability to display any text on the LCD display. Ultimately we are creating a rectangle image of pixel size 47x83 with our text inside and then displaying it on screen. The same program can be used to display the output of our previous programs that we were printing on the console, for example printing temperature and accelerometer value on LCD.

We are making use of the Adafruit_Nokia_LCD library. When you download this library using Git in the above steps, you get source code available on BeagleBone. The LCD interfacing code is available in file PCD8544.py inside the source code. This file calls the spi.BitBang() and spi.write() functions from the Adafruit_BBIO library internally to send data to LCD via the SPI protocol at the time of initialization and when the function display() is getting called.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.90