Using the C-Berry screen for graphics output

Basic Software Required

The official Debian image of Raspbian [3] was running for the test and for the operation of the QR code player. After the image was unpacked using dd, it was transferred to a 2GB large SD card.

$ dd if=2014-01-07-wheezy-raspbian.img of=/dev/sdb bs=1M

Make sure you have selected the correct device file for the SD card. In the example provided, this is the /dev/sdb file. The file name of the image will change once an updated file is available on the website.

After copying the image and connecting the LCD module, you should restart the mini-PC. Provided the Rasp Pi is connected to the local network, configuration can then proceed via SSH from another computer. I tested this process using a FRITZ!Box router. Immediately after installation, the Raspberry Pi became available via ssh pi@192.168.178.101.

If you use an SD card that has a capacity much larger than 2GB, it makes sense to call rasp-config and expand the filesystem (Expand Filesystem) on the entire SD card. You should update the operating system once it runs, then install the packages for developing C programs as follows:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install cpp gcc

Next, you will need the driver for the graphics controller. You should download and compile the current source code from the website of the developer [4] so you can reference the libraries in your own programs later. After downloading and unpacking, you should call three commands as follows:

$ tar xvfz bcm2835-1.36.tar.gz
$ cd bcm2835-1.36
$ ./configure & make & sudo make install

Once you do this, the driver libraries will also become available for the C-Berry software. The manufacturer of the screen provides these for download [5]. As before, you should compile the code after unpacking:

$ tar xvfz C-Berry.tar.gz
$ cd C-Berry/SW/tft_test
$ make

Calling the test program with ./tft_test will show whether the procedure has been successful. The results will be displayed on the screen at intervals of five seconds. Additionally, the test program initializes the display and calls up the functions from the examples.c library one after the other.

The code in tft_test provides the basic procedure for programming your own applications. You should include the bcm2835.h, tft.h, and RAIO8870.h libraries and initialize the display with the TFT_init_board(), TFT_hard_reset(), and RAIO_init() functions. Then, call the functions for drawing and log out the driver with bcm2835_close().

Your Own Programs

The functions in the manufacturer's source code provide the starting point for a new program that shows pictures on the screen. Going through the project will help you understand the basic procedures. Listing 1 illustrates how the loadbmp.c feature of the C program loads an image in bitmap format and then displays it on the screen.

Listing 1

Loading a Bitmap Image

/* File: loadbmp.c */
#include "loadbmp.h"
void CB_DepictBMP( char const *file_name  )
{
  uint16_t picture[1][ PICTURE_PIXELS ];
  Read_bmp2memory ( file_name, &picture[0][ PICTURE_PIXELS-1 ] );
  RAIO_Write_Picture ( &picture[0][0], PICTURE_PIXELS );
}
int main( int argc, char **argv )
{
  if(!argv[1]) return 1;
  char *my_filename = malloc(strlen(argv[1]));
  strcpy(my_filename,argv[1]);
  printf("using: %s\n", my_filename);
  if (!bcm2835_init()) return 1;
  TFT_init_board();
  TFT_hard_reset();
  RAIO_init();
  CB_DepictBMP( &my_filename[0] );
  bcm2835_close();
  return 0;
}

Listing 2 contains the corresponding header file. The bitmap must be 320 pixels wide and 240 pixels high and have a color depth of 24 bits. The bitmap header cannot be compressed, and it cannot contain further metadata. You would need to integrate additional graphics libraries if you want to process formats that don't follow these specifications.

Listing 2

Header File

/* File: loadbmp.h */
#ifndef LOADBMP_H
#define LOADBMP_H
#include <bcm2835.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "tft.h"
#include "RAIO8870.h"
#include "bmp.h"
void CB_DepictBMP( char const *file_name );
#endif

To compile the code, you will still need to get the sources for the tft.c, RAIO8870.c, and bmp.c libraries, as well as the accompanying header files from the Admatec example. Next, you should compile the program with the Makefile from Listing 3. With the binary executable, you can load the bitmap file using the call:

$ ./loadbmp <filename>

Listing 3

Makefile

all: loadbmp
loadbmp: loadbmp.o tft.o RAIO8870.o bmp.o
  gcc loadbmp.o tft.o RAIO8870.o bmp.o -lbcm2835 -lrt -lm -o loadbmp
tft.o: tft.c tft.h
  gcc -Os -c tft.c
loadbmp.o: loadbmp.c loadbmp.h
  gcc -Os -c loadbmp.c
RAIO8870.o: RAIO8870.c RAIO8870.h
  gcc -Os -c RAIO8870.c
bmp.o: bmp.c bmp.h RAIO8870.h
  gcc -Os -c bmp.c
clean:
  rm -rf *o loadbmp

If you don't call the program with a file name, it will stop without any further indication. You could call the software from a script and have status reports displayed as images on the screen. Figure 3 shows the output of the bitmap file that will later serve as the splash screen for the player.

Figure 3: A bitmap file is displayed on the screen.

The script in Listing 4 contains a script based on ImageMagick that converts arbitrary image formats into the desired format. The conversion of image files into corresponding bitmaps occurs with the following call:

$ ./convert_to_bmp.sh <filename>

Listing 4

Converting Image Formats

#!/bin/bash
# Script: convert_to_bmp.sh
if [[ -f $1 ]]; then
  IMGFILE=$1
  BMPFILE=${IMGFILE%.*}.bmp
  OPTIONS="-resize 320x240 -background black -compose Copy -gravity center \
    -extent 320x240 -depth 24 -alpha remove -alpha off -compress none BMP3:"
  convert ${IMGFILE} ${OPTIONS}${BMPFILE}
else
  echo "Cannot read file"
fi
RESULT=$(identify ${BMP} | cut -d ' ' -f 2)
if [[ ${RESULT}='BMP' ]]; then
  echo "Conversion OK: ${BMPFILE}"
else
  echo "Something went awry: ${RESULT}"
fi

Next, the script (together with the program from Listing 1) shows the file that has been created on the screen. You can find both the code that you will need to display bitmaps from the listings as well as the script for converting image files into matching bitmaps online [6].

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content