Raspberry Pi with Philips PCD8544

- tags: cpp, hardware, programming, raspberry pi, rpi-hw - 2 comments

Rpi-hw library supports Philips PCD8544-based LCDs (e.g. Nokia 5110/3310 LCD) from version 0.3, so that you can easily draw texts, shapes, and images.

Philips PCD8544 - Video

All you need to do is instantiate the class display::pcd8544, derived from designer and defined in “rpi-hw/display/pcd8544.hpp”. It can handle multiple image formats and text fonts using Magick++ APIs and FreeType.

Its constructor method takes the list of GPIO pins used by the device:

display::pcd8544( SCLK, SDIN, DC, SCE, RST );

In this tutorial I chose to use the following: 23 (SCLK), 24 (SDIN), 25 (DC), 8 (SCE), and 7 (RST).

Philips PCD8544 interfacing - Circuit

Philips PCD8544 interfacing - Circuit

Here is a simple program that draws some shapes on the display:

// Include Rpi-hw headers
#include <rpi-hw.hpp>
#include <rpi-hw/display/pcd8544.hpp>

// Use Rpi-hw namespace
using namespace rpihw;

int
main( int argc, char *args[] ) {

    // Create the display controller
    display::pcd8544 dev( 23, 24, 25, 8, 7 );

    // Initialize the display
    dev.init();

    // Draw a line
    dev.drawLine( 42, 0, 42, 47 );

    // Draw a rectangle
    dev.drawRect( 0, 0, 83, 47 );

    // Draw an ellipe
    dev.drawEllipse( 42, 24, 10, 5 );

    // Draw a circle
    dev.drawCircle( 42, 24, 20 );

    // Update the screen
    dev.redraw();

    return 0;
}

To compile it (or any other programs using the library) use the command:

g++ `pkg-config --libs --cflags rpi-hw` <SOURCE> -o <TARGET>

In a similar way you can draw a text string, but first you need to choose a font to be used, for example by loading it from file:

// Include Rpi-hw headers
#include <rpi-hw.hpp>
#include <rpi-hw/font/freetype.hpp>
#include <rpi-hw/display/pcd8544.hpp>

// Use Rpi-hw namespace
using namespace rpihw;

int
main( int argc, char *args[] ) {

    // Load the font
    font::freetype myfont( "fonts/DroidSans.ttf", 10, font::RENDER_MONOCHROME );

    // Create the display controller
    display::pcd8544 dev( 23, 24, 25, 8, 7 );

    // Initialize the display
    dev.init();

    // Set the text font
    dev.setFont( myfont );

    // Write a text string
    dev.drawText( "Hello world!" );
    dev.redraw();

    return 0;
}

The class font::freetype, defined in “rpi-hw/font/freetype.hpp”, is used to load font from file. In this example the used font is Droid Sans and it is contained in the file “fonts/DroidSans.ttf”. This file uses TrueType format, but FreeType allows you to handle many other formats, including OpenType and PostScript Type 1.

Philips PCD8544 - Text

Philips PCD8544 - Text

You can draw an image on the display using the class image::bitmap defined in “rpi-hw/image/bitmap.hpp”. It is a template class that takes a single template parameter with which you can choose the data type used to store the image. In this case you have to use the boolean type because the display is monochrome.

// Include Rpi-hw headers
#include <rpi-hw.hpp>
#include <rpi-hw/image/bitmap.hpp>
#include <rpi-hw/display/pcd8544.hpp>

// Use Rpi-hw namespace
using namespace rpihw;

int
main( int argc, char *args[] ) {

    // Create the display controller
    display::pcd8544 dev( 23, 24, 25, 8, 7 );

    // Initialize the display
    dev.init();

    // Load the image
    image::bitmap< bool > img( "images/tao.png" );

    // Draw the image on the display
    dev.drawImage( img, 0, 0 );
    dev.redraw();

    return 0;
}

Its instance uses Magick++ APIs to handle multiple image formats, including BMP, GIF, PNG, and JPEG. However, make sure that the image you want to draw is in black and white, so that it is compatible with the display.

Philips PCD8544 - Eye

Philips PCD8544 - Eye

You can also load a color image using the native Magick++ class Magick::Image and convert it to monochrome with a dithering algorithm.

// Include Rpi-hw headers
#include <rpi-hw.hpp>
#include <rpi-hw/image/bitmap.hpp>
#include <rpi-hw/display/pcd8544.hpp>

// Use Rpi-hw namespace
using namespace rpihw;

int
main( int argc, char *args[] ) {

    // Create the display controller
    display::pcd8544 dev( 23, 24, 25, 8, 7 );

    // Initialize the display
    dev.init();

    // Load the image using Magick++ API and convert it to black and white 
    Magick::Image img_magick( "images/eye.png" );
    img_magick.colorSpace( Magick::GRAYColorspace );
    img_magick.randomThreshold( Magick::Geometry( 4, 4 ) );

    // Convert the image into the bitmap object 
    image::bitmap< bool > img( img_magick );

    // Draw the image on the display
    dev.drawImage( img, 0, 0 );
    dev.redraw();

    return 0;
}

That’s all! :D

You can find more information on the reference manual and on the wiki.

I’m sorry for the low-resolution photos, but at the time I couldn’t do better. As soon as possible I will post better photos! ;)

Did you find this article helpful?

2 comments

  1. Thank you for posting this. it really helped me to get started with the display. :)

    Walter on Mon, 16 Sep 2013 @ 4:58 pm user image
  2. I’m glad that it was helpful to you :)

    admin on Mon, 16 Sep 2013 @ 10:12 pm user image