world leader in high performance signal processing
Trace: » text

ADSP-BF548 EZ-KIT Quick Start: Peripheral Demos: text

www.analog.com_static_imported-files_images_product_descriptions_680755565image1a.jpg

What is text

An application that uses SDL_ttf to draw some basic text on a drawing buffer for the LCD. It demonstrates some very basic text rasterisation using SDL_ttf.

Download text

The demo comes with the customised Ubuntu ISO and can be found in the folder /home/Blackfin/demos. Alternatively it is already installed on the demo uClinux image (demo-uclinux.img) that you can get on ADSP-BF548 EZ-KIT Quick Start: Booting uClinux.

If you just want to download the demos on their own and get their source code you can download the compressed archive (demos-R1.tar.gz) here bf548-quick-start .

How to run the text program

  • Change directory to /demos/lcd

    root:/> cd /demos/lcd
  • Make sure SDL knows that there is no mouse available otherwise it won't run

    root:/> export SDL_NOMOUSE=1

    Note: You only need to do this once per uClinux boot. If you want your program to make use of the touch screen, do not run this environment variable and instead run the commands found in the touchscreen demos.

  • Run text

    root:/> ./text
    Loading... 
    Done!
    Drawing text... 
    Displayed. Interrupt to exit.
  • Exit (Ctrl + C)

How the text demo works

Required header files

#include <stdio.h>
#include <stdlib.h>

// Necessary Font Libraries
#include <SDL_ttf.h>

As you can see, this program only grabs SDL_ttf.h really for the low level text API. It doesn't explicitly grab SDL.h as that will be done by SDL_ttf.

Main function

int main(int argc, char* argv[])
{
  int screen_width = 480;
  int screen_height = 272;
  int screen_bitdepth = 24;

  SDL_Surface *screen;
  SDL_Event input_event;
  
  char tuffy_font[100] = "Tuffy.ttf";
  int font_size = 28;

  int running = 0;

  printf("Loading... \n");

This section highlights how the programs run are just like normal C programs on your desktop machine. You have your normal main function, and some variable declarations at the top of the function. The screen dimensions provided match up with the LCD on the BF548 EZ-KIT. The tuffy_font string just points to the font file to use (Tuffy is provided with this demo).

Initializing the video subsystem

if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
  fprintf(stderr, "Error: Unable to init SDL: %s\n", SDL_GetError());
  exit(1);
}
atexit(SDL_Quit);

screen = SDL_SetVideoMode(width, height, depth, SDL_DOUBLEBUF);
if (screen == NULL)
{
  fprintf(stderr, "Error: Unable to grab screen\n");
  SDL_Quit();
  exit(1);
}

This section of code will initialise the video subsystem of SDL so that SDL knows it will have a graphics buffer to write to. If it fails, it will abort the program. The atexit call is added so that when the program closes, SDL will clean itself up.

It also grabs the screen to write to by making a call to SDL_SetVideoMode and provides the screen details, note that width, height and depth are provided at the start of this program and are set to the LCD screens actual dimensions so this creates a full screen window. If this fails, the program will abort.

This section will be quite common for any SDL program using the LCD and the SDL_Init call might have to OR other subsystems as well if you're using things like audio.

Initializing the ttf font subsystem

if(TTF_Init() == -1) 
{ 
  fprintf(stderr, "Error: Unable to init TTF: %s\n", SDL_GetError());
  SDL_Quit();
  exit(1);
}

text_font = TTF_OpenFont(tuffy_font, font_size); 
if(text_font == NULL) 
{ 
  fprintf(stderr, "Error: Unable to open font file!\n");
  SDL_Quit();
  exit(1);
}

printf("Done!\n");

This section of code will initialise the font subsystem. This is for intiiating SDL_ttf and getting it ready for use. It also opens the Tuffy font file and loads it as the true type font for drawing, and aborts the program if the font file cannot be opened.

Drawing text

printf("Drawing shapes... \n");
// Draw white background
SDL_Rect rect0 = {0,0,480,272};
SDL_FillRect(screen, &rect0, 0xFFFFFF);

// Drawing text
SDL_Color color = {0, 0, 0};
SDL_Surface *text_surface = TTF_RenderText_Solid(text_font, "Hello World!", color);
SDL_BlitSurface(text_surface, NULL, screen, NULL);
SDL_FreeSurface(text_surface);

// display surface on LCD
SDL_Flip(screen);
printf("Displayed. Interrupt to exit.\n");

This section merely draws a white rectangle to the screen, then draws the string “Hello World!” on top of it of varying colours. You can see that the color provided is an SDL_Color struct, which is an RGB colour. It blits this text surface to the screen surface, and then displays it on the LCD.

SDL_ttf has many low level drawing functions and you can learn more about them on the official SDL_ttf documentation at http://www.libsdl.org/projects/SDL_ttf/

Input handling loop

memset(&input_event, 0, sizeof(input_event));
while (running == 0)
{
  if (SDL_PollEvent(&input_event))
  {
    if (input_event.type == SDL_QUIT)
    {
      running = 1;
    }
  }
}

This section is common in all SDL programs and it is a loop that will continuously check for any program input. This one is looking for an SDL_Quit call (from program termination) to perform an exit.

Exiting

TTF_Quit();
SDL_Quit();
return 0;

This section just does some basic clean up and exits the program.

Compiling text

First make sure that your kernel has the libraries libSDL, SDL_ttf, SDL_gfx and tslib enabled.

Secondly, make sure your vendor staging install is complete (ADSP-BF548 EZ-KIT Quick Start: Compiling uClinux).

root:/> make vendor_staging_install

Run the following compile command in the same directory as the source code:

root:/> bfin-linux-uclibc-gcc -O2 \
$(~/Blackfin/blackfin-linux-dist/staging/usr/bin/sdl-config --cflags) \
$(~/Blackfin/blackfin-linux-dist/staging/usr/bin/sdl-config --libs) \ 
-lSDL_ttf \ 
text.c -o text

What this compile command is doing is it is calling the uclibc cross compiler that is set up from the Blackfin toolchains to compile our application.

The calls to sdl-config are a special case for SDL because SDL places its header & library files in non-root locations so this will load them properly.

The parameters prefixed with -l are to indicate we need to load that library, so in this case the library SDL_ttf is loaded. Without this library, our program would not have access to the required font rendering functions.

The final parameters match up to standard gcc compiling; “text.c” is the source file while ”-o text” indicates the output file is “text”.

A standard make file is accompanies this demo and you can run that by simply calling

root:/> make text

Further Reading

SDL_ttf

SDL_ttf is a popular SDL true type font rendering api, and its documentation is located at http://www.libsdl.org/projects/SDL_ttf/docs/index.html

LCD Notes

The LCD on the BF548 EZ-KIT is a 24bit lcd (8bit blue, 8bit green, 8bit red). This means some programs hard coded for the frame buffer do not work, for example pngview. If you run pngview and it appears wonky on the BF548 that is because it was made for the BF537 and a 16bit LCD. Since it does not write enough data to fill a pixel and will encode for rgb and not bgr, it will be interpreted wrongly, so be aware of this when working with programs that write directly to the frame buffer.

If the colour on the LCD screen appears wonky, this may be due to the LCD screen settings defined by SW17. These switches can set the bitrate of the LCD and if set differently than to what the program expecs will lead to a bit of mayham. Check the development environment page of this guide to make sure it is set to the default 24bit.

Information on the LCD driver for the BF548 EZ-KIT can be found here Linux framebuffer driver for ADSP-BF54x