CFA-635 Linux Application & Library

ScottishCaptain

New member
Greetings to all.

I was recently working on a project based on Linux that basically spawned this iteration of a Linux Crystalfontz Library and command-line program. Figured I'd release the changes to the library and the command line application here incase anyone else wants to use it.

Essentially, the package consists of a C++ library, CFLIB, and a command-line application, CFONT.

CFLIB was based off some other work on the forum, but heavily modified (and nearly re-written) so that the library itself does no range checking and really isn't LCD specific, save for the functions that are physically on the LCD (ie, the 4 LED's on the CFA-635).

A lot of the basic LCD functionality as found in Crystalfont's specifications is implemented in CFLIB. The following functions are available via CFLIB- though most of these commands require a program linked to the library to listen for any returned data, if applicable (ie, like the ping reply).

cfont_ping (int ping_number) - Sends a ping command to the LCD, LCD replies with ping_number if alive
cfont_get_version () - Sends a command to retrieve software/hardware versions from the CFA
cfont_set_flash (char *string) - Write a 16-byte string to the LCD's user flash area
cfont_get_flash () - Sends a command to retrieve the stored LCD data (16 bytes)
cfont_store_boot_state() - Tells the CFA LCD to write the current state to the bootup state
cfont_reboot () - Reboots the CFONT display (ie, reset to boot state)
cfont_clear () - Clears the CFONT display
cfont_set_cursor_position (int x, int y) - Sets cursor position to x,y
cfont_set_cursor_style (int style) - Sets the cursor style to the integer "style"
cfont_set_contrast (int level_percent) - Sets contrast to the level level_percent (0-100)
cfont_set_background (int level_percent) - Sets backlight to the level level_percent (0-100)
cfont_set_led (int led, int g_level, int r_level) - Sets the CFA-635 LED number (1-4) to the GREEN (g_level) and RED (r_level) as specified, since these are dual-color LED's...
cfont_write_xy (int x, int y, char *s, int l) - Writes a character string to x,y from s, with the length of l
cfont_write_char_xy (int x, int y, uint8 c) - Writes a character from the CGROM to x,y, where the character is an integer (explained in CF's LCD specification PDF) "c"
cfont_define_char (int index, uint8 matrix[8] ) - Defines a custom character in the LCD's RAM (which can be printed by using cfont_write_char_xy)
cfont_get_ddcgram (int addr) - Retrieves the CURRENT TEXT displayed on the LCD display, see CFONT program for an example of retrieving what is currently being displayed
cfont_open (char *device) - Opens communication with the CFONT display
cfont_close () - Closes communication with the CFONT display
cfont_register_event_handler (void *event_handler) - Registers a data event handler for incomming data from the CFA display (keypress, reading the flash, DOW events, etc)

CFLIB is pretty complete and does most of everything you'd ever need to do on a CFONT display. It's designed to be as minimalistic as possible, serving only to offer the basic functions needed to properly communicate with the LCDs. Everything else (in my view) should be contained in an application linked to CFLIB (like scrolling text, LCD boundry checking, etc).

Also included, is the command-line program CFONT.

CFONT links to CFLIB and provides complete control over the CFA-635 series of displays (that's what it was designed for- it might handle a 631...) from the command-line, typically used in a bash script to interface with the LCD.

CFONT takes several "commands", parses them, and executes them in order. The commands available are:

--dev "/dev/LCD" : Tells CFONT where the dev entry is for the LCD device
--ping : Sends a ping command to the LCD. Prints "alive" if it recieves a response, "timeout" if no response
--version : Prints the hardware and software revision of the CFA display
--readflash: Prints the flash contents (16 bytes) of the LCD display
--writeflash "my16characters" : Writes a 16-byte (character) string to the LCD display's flash
--get line : Prints the contents of the LCD RAM for the line number specified
--storeboot : Tells the LCD to store the current state as the boot state
--reboot : Reboots the LCD to the boot state
--clear : Clears the LCD display
--cursor x y : Sets the LCD cursor to x,y
--cstyle style : Sets the cursor style to the style number as specified
--contrast value : Sets the contrast to value (0-100)
--backlight value : Sets the backlight to value (0-100)
--led led green red : Sets "led" (1-4) to "green" (0-100) and "red" (0-100) colors. When called without an LED number, --led sets ALL the led's to the color specified by green and red.
--print x y "Hello World" : prints a string to x,y
--printc x y integer : Prints the character represented by an integer to x,y
--getkey timeout : Waits for a CF display keypress, and prints the key number as a result. This also returns "timeout" if the value specified by timeout is exceeded, in seconds.
--silent : This supresses all errors. In some scripts I found this useful as sometimes my script would call CFONT with an invalid --print line, which would later become valid- but I didn't want CFONT complaining about it.

An example of CFONT operation would be:
cfont --dev /dev/ttyUSB0 --led 1 100 100 --print 1 1 "Hello World"

This would tell cfont to open the device /dev/ttyUSB0, set LED 1 on the CFA-635 to yellow (Green at 100%, Red at 100%), and print "Hello World" at X,Y coords 1,1 (top-left corner).

You can chain as many commands as you want on the command line. If you're using CFONT in a bash script, this allows for faster operation as you don't need to open and close communication with the LCD every time you want to change something- you can update the LCD, change the LED colors, write characters, and wait for a keypress all with one command.

I modified CFONT so that it didn't handle things like Menu's and IP settings, as I figured these things would be best handled by a bash script externally that talks to CFONT, rather then inside the program itself (at the time, CFONT needed to be as minimal and stable as possible).

Since CFONT handles commands left to right, you could also call it with:
cfont --dev /dev/ttyUSB0 --print 1 1 "Reading Flash" --readflash --clear --print 1 1 "Flash read..."

This would print "Reading Flash" on the LCD screen, while --readflash reads the 16 bytes of flash memory stored on the LCD (this takes like half a second or so), and then once that's complete, --clear clears the LCD after readflash completes (and prints the contents of the flash on the command line- which can be read by a bash script easily) and then prints "Flash read..." as an indicator that the LCD's flash contents were read sucsessfully.

If you wanted to read the entire contents of the LCD display- what's currently being displayed on the LCD screen, you'd call CFONT with:
cfont --dev /dev/ttyUSB0 --get 1 --get 2 --get 3 --get 4

And CFONT would print (on the command line):
Hello World
This is line 1
This is line 2
Goodbye World

If the corresponding lines on the LCD were as indicated above (I can't think of an example in practical use for this- but in the project I was developing, I used this to determine if the LCD had been programmed with the appliance's default LCD boot screen or not).

Anyways.

There's a shell script called Makefile also included- just run that, it'll compile CFLIB, CFONT, and link them together (statically). You should then be able to call CFONT and control your CFA-635, providing it's plugged in and working.

-SC
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.
 

Attachments

Top