manufacturer of I/O-rich SBCs, operator interfaces, handheld instruments, and development tools for embedded control low cost single board computers, embedded controllers, and operator interfaces for scientific instruments & industrial control development tools for embedded control order our low cost I/O-rich embedded control products embedded controller manufacturer profile single board computers & embedded controllers development tools & starter kits for your embedded design operator interfaces with touchscreens and graphical user interface plug-in expansion modules for digital & analog I/O C language & Forth language integrated development tools, IDE single board and embedded computer accessories embedded controller enclosures, bezels, environmental gaskets

The C Programmer’s Guide to the QVGA Controller

Table of Contents

PART 1 GETTING STARTED

Introduction. How to Use This Manual

Chapter 1: Getting to Know Your QVGA

PART 2 PROGRAMMING THE QVGA CONTROLLER

Chapter 2: Your First Program

Chapter 3: The IDE: Writing, Compiling, Downloading and Debugging Programs

Chapter 4: Making Effective Use of Memory

Chapter 5: Programming the Graphical User Interface

The Structure Of The GUI Toolkit

A Closer Look At Objects

The GUI Toolkit Objects

Building Your Application

Designing Your User Interface

Transferring Your Images to the QVGA Controller

Coding Your Application

Handling Errors

Expanding the GUI Toolkit’s Objects and Methods

GUI Objects in Detail

Chapter 6: Real Time Programming

Chapter 7: Failure and Run-Time Error Recovery

PART 3 COMMUNICATIONS, MEASUREMENT, AND CONTROL

Chapter 8: Digital and Timer-Controlled I/O

Chapter 9: Data Acquisition Using the QVGA Controller

Chapter 10: Outputting Voltages with Digital to Analog Conversion

Chapter 11: Serial Communications

Chapter 12: The Battery-Backed Real Time Clock

PART 4: PUTTING IT ALL TOGETHER

Chapter 13: A Turnkeyed Application

PART 5: REFERENCE DATA

Appendix A: QVGA Electrical Specifications

Appendix B: Connector Pinouts

Appendix C: Physical Dimensions

Appendix D: Schematics (pdf)

Chapter 5

<< Previous | Next>>

Chapter 5: Programming the Graphical User Interface

The Graphical User Interface (GUI) Toolkit is a suite of programming tools that gives you the ability to build an informative and interactive graphical user interface to monitor and control your instrument. This chapter

Introduces the structure of the GUI Toolkit;

Takes you on a step-by-step guide to building your interactive application; and

Provides you with detailed descriptions of each component of the GUI Toolkit.

The Structure Of The GUI Toolkit

A graphical user interface is created from building blocks such as bitmapped images and ASCII strings.  These building blocks, or data, must be organized in an intuitive way so users can easily run your instrument. Object oriented concepts are the key to organizing this data and make it easy for you to design and implement your user interface.  Object oriented programming allows you to organize data hierarchically based on objects and manipulate the data using methods. With the GUI Toolkit, it is simple to create elementary objects such as graphics that contain bitmapped image data and textboxes that contain ASCII string data.  You can load those objects into other objects such as screens so that they can be shown on the display.  You can create yet another type of object, a control, which can execute functions that acquire data from a user or actuate hardware when a user touches the touchscreen.  A button is one kind of a control.

A Closer Look At Objects

The two concepts of object oriented programming essential to modern programs with user interfaces are “Objects” and “Events”.  An object is an association of properties and methods, and may respond to events. An event is an external action that prompts an object into action.  GUI Toolkit objects that can respond to events are called controls.

Properties

Properties describe an object’s physical attributes like its size, location, or image.  Objects contain a data structure that holds its properties.  Properties generally don’t do anything by themselves; they are more like read/write variables.  However, properties may qualify the object’s behavior when the object does do something or has something done to it.

Some properties may be read only while others may be read/write.  GUI Toolkit properties are always 32-bit numbers and are accessed or modified by calling the methods Get_Property or Set_Property. Set_Property requires a reference to an object, its property, and the new value, while Get_Property requires a reference to an object and its property as parameters and returns the property value. 

Methods

Methods are actions the object can do or have done to it. Many methods, like Load, are overloaded; that is, they are applicable to several different types of objects.  For example, you can load buttons, graphics, and textboxes into a screen.  Overloaded methods allow you to use a single simple syntax with respect to many different objects.

Events

Events are external actions that an object responds to by executing a user defined function called an event procedure.  Objects automatically recognize a predefined set of events, it is up to you to decide if and how they respond to those events.  You specify the event procedure called when the event happens (or fires) by storing your function into the appropriate property using Set_Property. Event procedures are written, for example, to specify the program actions that occur in response to the press of a button.

The GUI Toolkit Objects

The following table lists the objects in the GUI Toolkit.

 

Object

Description

GUI_TOOLKIT

The central object that contains properties relevant to all other objects.

GUI_DISPLAY

The object tied to the physical display.

GUI_SCREEN0 to GUI_SCREEN5

An object that contains a collection of other functionally related objects such as graphics, textboxes, buttons, and plots.

GUI_TOUCHSCREEN

The object tied to the physical touchscreen.

GUI_PEN

A tool object that is used in conjunction with the Draw method to draw lines and geometrical figures onto screens.

GUI_BUZZER

The object tied to the physical buzzer.

GUI_FONT

The 8-pixel wide by 10-pixel tall Courier New font that is used to render strings in textboxes by default for the GUI Toolkit.

GRAPHIC

An object that contains a single image as a property.

FONT

An object that contains a single image of the 95 ASCII characters that is used to render strings in textboxes.

TEXTBOX

An object that uses a font object to render strings onto a screen.

BUTTON

An object with an associated graphic that responds to a user’s touch on the touchscreen.

PLOT

An object used to render numerical data into graphical form onto a screen.

Some of these objects are created when you initialize the GUI Toolkit while others are created by functions that you write.  All GUI objects reside in their own task with their own heap and some respond to events.  In the next section, we’ll show you how to create your user interface and then how to write your application using the GUI Toolkit.

Building Your Application

In this section, we’ll explore the GUI Toolkit in the order in which you would typically build your application.  We’ll show you how to:

  Design and create your user interface on a PC;

  Transfer the images that you created for your user interface from the PC to the QVGA Controller;

  Write your application using the demo program as a reference guide;

  Handle errors; and,

  Expand the capabilities of the GUI Toolkit.

The demo program shows you how to build a multitasking application with three simple screens.  The first screen is a numeric keypad that allows you to enter up to 20 digits and render them in a textbox.  The second screen displays three different size Courier New fonts ranging from the default font of 8 pixels wide by 10 pixels tall to a large font that is 32 pixels wide by 40 pixels tall.  The third screen plots an 8-bit A/D input every 10 seconds.  As we describe each part of the GUI Toolkit, we’ll use the numeric keypad screen of the demo to illustrate the important concepts.

Designing Your User Interface

Before you start coding your application, it’s a good idea to sketch out how you want your user interface to look.  Figure 5‑1  shows a rough sketch of the first screen of the demo program.  Sketching out your user interface before you start programming will help you to think about how you want your application to be organized and how you want to present information to your user.

Figure 5‑1    The sketch of the first screen of the demo program.

Drawing Your Screens

Once you’ve sketched out and designed your user interface, you need to create bitmapped images of your interface using an image-editing program.  Images may be created using Microsoft Paint, which is included with all versions of Windows.  You can also use other image-editing programs such as Corel Draw, Photoshop, and Paint Shop Pro.  In this section and throughout this chapter, we provide step-by-step instructions for creating and manipulating images using Photoshop.  However, all of the image-editing programs have similar tools and functionality.  For example, the pencil tool in Photoshop is simply called the pencil in Paint and the line tool in Photoshop is called the line in Paint.  Thus, the steps listed in the examples, although specific to Photoshop, apply to all image-editing programs.

To create an image of a screen from your sketch using Photoshop:  

    1. Create a new file with a width of 320 pixels and a height of 240 pixels, a resolution of 72 pixels per inch, and using the bitmap image mode.  320x240 is the size of the display and the size of a screen.  The resolution is not important because it refers to the number of pixels per inch for the monitor.  However, the image mode is important and must be set to bitmap if you are using a monochrome or EL display.  This configures the image to have one bit per pixel.

    2. Draw all lines using the pencil tool with a brush size of one pixel.  The line tool, paint brush tool, and airbrush tool are also useful in drawing shapes and patterns. 

    3. Draw text using the type tool. 

    4. Save the image as a windows bitmap.  A dialog box may appear informing you that some image data, such as printer settings, cannot be saved with this format.  This does not effect the image so you can just click on OK.

The image that we created based on the sketch in Figure 5‑1  is shown in Figure 5‑2 .

Figure 5‑2    The bitmap image of the first screen of the demo program that was created using Adobe Photoshop.

Creating Your Images

Once you have created the screens for your user interface, you need to ungroup them.  Ungrouping the screens into smaller images will allow you to save space in Flash memory since you don’t want to store images with a lot of white space.  It will also allow you to associate the images with individual buttons.  To ungroup an image using Photoshop:

    1. Select the marquee tool;

    2. Left click on the upper left corner of an image you want to ungroup and drag the mouse pointer to the lower right corner of the image.  This will create a box with a moving dashed line around the image;

    3. Note the location of the upper left corner of the image; this location will be needed when you want to load a graphic containing the image onto a screen;

    4. Under the Edit Menu, select the cut option to move the image to the clipboard.  The image will disappear from the screen;

    5. Under the File Menu, select the create new file option;

    6. Name the file, based on the image.  The height and width will automatically be set to the height and width of the image that is in the clipboard;

    7. Under the Edit Menu; select paste; and,

    8. Save the ungrouped image in the Windows bitmap format.

The width of an image must be a multiple of 8 pixels and the image must be positioned on an 8 pixel horizontal grid in a screen.  Finer horizontal pixel placement and image widths would require bit shifting of each byte of the image every time it is drawn, significantly slowing performance of the GUI Toolkit.  There are no limitations on the height of an image or the placement of an image in the vertical direction.

Figure 5‑3    The images included with the demo program that you can use in your application.

The demo program also provides a set of images that you can use with your application.  The images are shown in Figure 5‑3 .

Transferring Your Images to the QVGA Controller

Once you have ungrouped all the images for your user interface, assemble them into a single directory.  Images must be processed before you can use them with the GUI Toolkit; Mosaic’s Image Conversion Program concatenates images into a single text file called an Image Data File. The Image Data File is stored in the same directory as your images and when it is transferred to the QVGA Controller, it stores your image data in RAM and then copies it into Flash.  You must transfer the Image Data File using QEDTerm, Mosaic’s Terminal Program, to the QVGA Controller before you transfer your application code.  For more information on the Image Conversion Program, see the section on the Image Conversion Program .

The Image Header File

An Image Header File is also created with the Image Conversion Program and stored in the same directory as your images.  The Image Header File associates the names of your images with the location of their image data in Flash.  The Image Header File must be included in your application in order for you to create graphic objects.

The following shows the first few lines of the Image Header File used by the demo.  The Image Header File defines a few constants, named after the image files down.bmp, button_press.bmp, and one.bmp shown in Figure 5‑3 .  The constants contain the addresses of the image data in Flash memory on the QVGA Controller.  For more information on the memory map of the QVGA Controller see Chapter 4: Making Effective Use of Memory[pkc1] .

Listing 5‑1     The first few lines of the Image Header File.

#define BOTTOM_EDGE_BMP 0x700000

#define BUTTON_PRESS_BMP 0x700110

.

.

.

#define ONE_BMP 0x705806

Listing 51    The first few lines of the Image Header File.

hex

0000 70 xconstant BOTTOM_EDGE_BMP

0110 70 xconstant BUTTON_PRESS_BMP

.

.

.

5806 70 xconstant ONE_BMP

Coding Your Application

Once you have designed and built the user interface for your application and you have transferred the images to the QVGA Controller, you can start writing the code to animate your instrument.  All GUI applications need the following components:

     The pre-loaded GUI Toolkit driver software;

     The Image Data and Image Header Files from the Image Conversion Program;

     An initialization routine that initializes the GUI Toolkit and sets up a task to run your application’s user interface.

     Routines that create and configure objects;

     Event procedures that animate controls; and,

     Routines that load objects onto screens.

Each of these components are described in the following sections.

The GUI Toolkit Driver Software

The GUI Toolkit driver software is pre-installed on your QVGA Controller.  However, you will need to copy the library files (which define constants, structures, and headers for the GUI Toolkit methods) to the same directory as your source code as described below.  To reload the driver software, please see section XX.

The GUI Toolkit driver software is provided as a pre-coded modular runtime library, known as a “kernel extension” because it enhances the on-board kernel's capabilities.  The library functions are accessible from C and Forth. 

The kernel extension for the GUI Toolkit is available from Mosaic Industries on the Demo and Drivers media (diskette or CD).  Look in the Drivers directory, in the subdirectory corresponding to your hardware, in the GUI Toolkit folder.

The kernel extension is shipped as a “zipped” file named “packages.zip”. Unzipping it (using, for example, winzip or pkzip) extracts the following files:

  readme.txt - Provides summary documentation about the library.

  install.txt   - The installation file, to be loaded to COLD-started QED Board.

  library.4th - Forth name headers and utilities; prepend to Forth programs.

  library.c    - C callers for all functions in library; #include in C code.

  library.h    - C prototypes for all functions; #include in extra C files.

Library.c and library.h are only needed if you are programming in C. Library.4th is only needed if you are programming in Forth.  The uses of all of these files are explained below. 

We recommend that you move the relevant files to the same directory that contains your application source code; the references to the library files in the demo assume the library files are in the same directory as the source code.

Using the Driver Code with C

Move the library.c and library.h files into the same directory as your other C source code files and use the following directive in your source code file:

 

#include “library.c”

This file contains calling primitives that implement the functions in the kernel extension package.  The library.c file automatically includes the library.h header file.  If you have a project with multiple source code files, you should only include library.c once, but use the directive

 

#include “library.h”

in every additional source file that references the GUI Toolkit functions.

Note that all of the functions in the kernel extension are of the _forth type.  While they are fully callable from C, there are two important restrictions.  First, _forth functions may not be called as part of a parameter list of another _forth function.  Second, _forth functions may not be called from within an interrupt service routine unless the instructions found in the file named

            \MOSAIC\DEMOS_AND_DRIVERS\MISC\C EXAMPLES\forthirq.c

are followed.

NOTE: If your compiler was purchased before June 2002, you must update the files, qlink.bat and qmlink.bat in your /fabius/bin directory on your installation before using the kernel extension. You can download a zip file of new versions from

http://www.mosaic-industries.com/Download/new_qlink.zip

The two new files should be placed in c:\Fabius\bin.  This upgrade only has to be done once for a given installation of the C compiler.

Using the Driver Code with Forth

To use the driver code with Forth, include the following directive as your first line of code in your source code file:

 

#include “library.4th”

Library.4th will set up a reasonable memory map and then define the constants, structures, and name headers used by the GUI Toolkit kernel extension. Library.4th leaves the memory map in the download map.

Be sure that your software doesn't initialize the memory management variables DP, VP, or NP, as this could cause memory conflicts.  If you wish to change the memory map, edit the memory map commands at the top of the library.4th file itself.  For more information on the memory map of the QVGA Controller see Chapter 4: Making Effective Use of Memory .  The definitions in library.4th share memory with your Forth code, and are therefore vulnerable to corruption due to a crash while testing.  However, the kernel extension runtime code is immune to corruption because it is stored in Flash and it is stored on a page that is not typically accessed by code downloads. 

Using the Image Data and Image Header Files

Before writing your application code, be sure to transfer the Image Data File, created with the Image Conversion Program, to the QVGA Controller.  Then, be sure to include the Image Header File in your application as shown below:

Listing 5‑2     The first few lines of the demo program.

#include <\mosaic\allqed.h>  // Include all of the Mosaic utilities.

#include “library.c”         // Include the headers for the GUI Toolkit’s methods.

#include “image_header.h”    // Include constants that define location of your images

Listing 52    The first few lines of the demo program.

#include “library.4th" \ Define constants, structures, and name heads for the 

\ GUI Toolkit and set up the memory map.

#include “image_header.4th" \ Load constants that define the location of your images.

Writing Your Application’s Initialization Routine

The initialization routine of your application needs to initialize the GUI Toolkit and setup a task to control the GUI Toolkit before any GUI Methods are called.

Initialize_GUI  must be called before any other GUI method!

To initialize the GUI Toolkit, call Initialize_GUI, passing it a heap start address and a heap end address as shown in the demo.

Listing 5‑3     Initializing the GUI Toolkit

#define GUI_HEAP_START 0x0F3000L

#define GUI_HEAP_END  0x0F6FFFL

.

.

void main( void )

{

  Initialize_GUI ( GUI_HEAP_START, GUI_HEAP_END);

  .

  .

}

Listing 53  Initializing the GUI Toolkit

hex

3000 0F xconstant START_GUI_HEAP

6FFF 0F xconstant END_GUI_HEAP

.

.

: GUI_Demo ( -- )

  START_GUI_HEAP END_GUI_HEAP Initialize_GUI

  .

  .

;

Initialize_GUI performs three actions: it sets up a heap memory structure for the GUI Toolkit; it sets up and activates a task for the GUI Toolkit; and it configures several critical objects, called the GUI Toolkit’s pre-instantiated objects.

All GUI objects and their associated properties are stored in a heap accessed only by the GUI Toolkit.  This GUI Heap can be located in any contiguous block of RAM (i.e. it may span consecutive pages of RAM) but it is typically placed on page 0x0F from 0x3000 to 0x6FFF as shown in the example above.  The size of the heap that is needed by the GUI Toolkit depends on your application.  As a benchmark, the demo program uses approximately 4k of heap space.  Because all objects are stored in the GUI Heap, it is easy to add, modify, or resize objects without worrying about the specifics of their memory allocation.

The GUI Toolkit runs in a stand-alone task called the GUI Task.  The GUI Task executes an endless loop that:

  continuously checks to see if new objects need to be created or existing objects need to be modified;

  polls the touchscreen looking for events;

  services events when they occur; and,

  calls Pause.

When a touchscreen event occurs, the GUI Task sends a message indicating that the event procedure of the responsible control should be executed.  Service_GUI_Events listens for messages from the GUI Task and executes the appropriate event procedure when commanded.  Service_GUI_Events must be included in the infinite loop of a task that you create (separate from the GUI Toolkit’s Task) as shown in the following example.  This task should also be in charge of creating and initializing the objects for your application.

Listing 5‑4     Servicing GUI Events

TASK taskbase;

.

.

void GUI_Monitor ( void )

{

  .

  .

  while(1)

  {

    .

    .

    Service_GUI_Events();

    Pause();

  }

}

void main( void )

{

  Initialize_GUI ( GUI_HEAP_START, GUI_HEAP_END);

  .

  .

  Init_Keypad();                    // create & init objects for keypad

  .

  .

  BUILD_C_TASK(0, 0, &taskbase);    // build task

  ACTIVATE( GUI_Monitor,&taskbase); // activate task

  StartTimeslicer();                // starts elapse time clock, enables interrupts

  Pause();                          // start next task immediately

}

Listing 54  Servicing GUI Events

decimal

\ Define a constant for reserving 1k of space in the variable area for the task.

1024 constant ONE_KILOBYTE

\ Allocate space in the variable area for our task in common memory.

ONE_KILOBYTE v.instance: taskbase

.

.

.

: Build_Task ( -- )

  0\0 0\0 0\0 taskbase build.standard.task

;

 

: GUI_Monitor ( -- )

  .

  .

  .

  begin

    .

    .

    .

    Service_GUI_Events

    Pause

  again

;

 

: GUI_Demo ( -- )

  START_GUI_HEAP END_GUI_HEAP Initialize_GUI

  .

  .

  .

  Init_Keypad \ create & init objects for the keypad

  Build_Task \ build task

  CFA.For GUI_Monitor taskbase Activate   \ activate task

Start.Timeslicer              \ start the timeslicer for timesliced multitasking

Pause                         \ give other tasks a chance to run

;

The GUI Toolkit is a resource whose access must be controlled.  Only the GUI Toolkit (running in the GUI Task) polls the touchscreen, writes to the display, creates objects, or modifies objects.  We recommend that you use only one task to run the your application’s user interface so that only one task interacts with the GUI Toolkit.  However, if multiple tasks in your application need to use the GUI Toolkit, you must use the resource variable gui_resource to assure that only one task accesses the GUI Toolkit at a time.  See section XX  for more information on using resource variables or the glossary entry for gui_resource.

The GUI Task should have an opportunity to run approximately once every 15 milliseconds. Calling the GUI Task more frequently will not improve performance because the GUI Task polls the processor’s free running counter, TCNT, to assure it is not called more than once every 10 milliseconds.  Calling the GUI Task less frequently will not cause any errors but may result in sluggish performance.  The GUI Task takes approximately one millisecond to execute if no events occur, no new object needs to be created, and no existing object needs to be modified.  When an event fires, the time the GUI Task takes to execute is proportional to the number of items that are loaded onto the visible screen.  For a screen with 15 objects, it will take approximately 5 milliseconds to pass the event to the responsible control. Additional time is required if the responsible control has an image that has to be redrawn.

The GUI Toolkit’s Pre-Instantiated Objects

The GUI Toolkit has a set of special objects that are created and configured only when you initialize the GUI Toolkit.  These special objects are called the pre-instantiated objects because they are instantiated or created before the GUI Toolkit starts it’s task.  These objects include the GUI Toolkit itself, the display, six predefined screens, the touchscreen, the pen, the buzzer, and the default font.  You can not create additional instances of these objects because they are tied to a specific piece of hardware or unique function of the GUI Toolkit.

The GUI Toolkit is itself an object with properties that influence the operation of the GUI Task and all other objects.  The GUI Toolkit Object is called the GUI TOOLKIT and has properties that determine how objects such as graphics and controls are loaded into screens, whether touchscreen events are serviced, and the behavior of the GUI Task when an error occurs. 

The Display Object is called the GUI_DISPLAY, and is tied to the physical display.  Properties of the GUI_DISPLAY control the behavior of the display such as blanking and inverting.  The GUI_DISPLAY contains six pre-instantiated screens called GUI_SCREEN0 to GUI_SCREEN5. A property of GUI Screens is their Visibility property.  Only one of the GUI Screens may be made visible at a time.  When it is made visible, the other GUI Screens are automatically made not visible and the display suddenly shows the visible screen’s objects.  GUI Screens are the only objects that are shown on the physical display.  To show an object like a graphic on the display, the object must be loaded into the visible GUI Screen.  The visible screen after Initialize_GUI is called is GUI_SCREEN0.

The Touchscreen Object is also known as the GUI_TOUCHSCREEN and corresponds to the physical touchscreen.  The GUI_TOUCHSCREEN generates events when a user places a finger on the physical touchscreen (press event), while the user continues pressing the physical touchscreen (held event), and when the user removes their finger (release event).  Properties of the GUI_TOUCHSCREEN contain the identity and pixel coordinates of the last event.

The Pen Object is called the GUI_PEN. This useful tool generates geometric shapes and line drawings on a screen.  The GUI_PEN has properties that control how the points and lines are drawn so you don’t have to pass all the optional parameters to the drawing method each time it is called. 

The Buzzer Object is called the GUI_BUZZER and it is associated with the piezo electric beeper.  Properties of the GUI_BUZZER control the length of time of a beep and turn the buzzer on and off.

The Default Font Object is called the GUI_FONT and is associated with the 8-pixel wide by 10-pixel tall Courier New font that renders string data in a textbox onto a screen.

Creating New Objects

The GUI Toolkit also has other objects, like graphics and buttons, that you can create using the New_Object method.  New_Object requires one input parameter that specifies the type of object you want to create; GRAPHIC, FONT, TEXTBOX, BUTTON, and PLOT are all valid object types.  New_Object returns an integer that you store into a variable.  This variable (which we call an object reference because it contains a reference to the object) is used to set properties of the object and perform actions on the object like loading the object onto a screen.

The following code from the demo, creates a new graphic object and stores its object reference into a variable named graphicOne. Later, we’ll set the image of the graphic and then configure the “one” button of the keypad to use this graphic as its DRAW_GRAPHIC.

Listing 5‑5     Creating a New Graphic.

int graphicOne;

.

.

.

void Init_Keypad_Images ( void )

{

  graphicOne = New_Object ( GRAPHIC );

  .

  .

  .

}

Listing 55   Creating a New Graphic.

variable graphicOne

.

.

.

: Init_Keypad_Images ( -- )

  GRAPHIC New_Object graphicOne !

  .

  .

  .

;

Objects can be created but not destroyed.  Because objects are stored in the heap, you can only create a finite number of them.  Different objects use different amounts of heap space.  For example, graphics and fonts only use a few bytes of heap space while plots use hundreds of bytes depending on the size of its buffer.

Setting the Property of Objects

All objects have their properties set to default values when they are created so that you need to set as few properties as possible to have a fully functional object.  However, certain objects like graphics require you to set its image before the graphic can be loaded onto a screen or into a button.

In the demo, Init_Keypad_Images creates a graphic and then sets it’s image property using a constant defined in the Image Header File.

Listing 5‑6     Setting the Property of a Graphic.

int graphicOne;

.

.

.

void Init_Keypad_Images ( void )

{

  graphicOne = New_Object ( GRAPHIC );

  Set_Property ( graphicOne, IMAGE, ONE_BMP );

  .

  .

  .

}

Listing 56   Setting the Property of a Graphic.

variable graphicOne

.

.

.

: Init_Keypad_Images ( -- )

  GRAPHIC New_Object graphicOne !

  graphicOne IMAGE ONE_BMP Set_Property

  .

  .

  .

;

Creating and Initializing Controls

Now that you’re familiar with creating graphic objects, we’ll show you how to create a button.  To create a button:

    1. Use the New_Object method to create a button and store the object reference to the button into a variable.  By default, the button will beep when it receives a press event. You can change this behavior by setting the BEEP_ON_PRESS property to GUI_FALSE.

    2. Create and define a graphic for the DRAW_GRAPHIC property of the button.  A button needs at least one graphic to define it’s size and active area.

    3. Create and define a graphic for the PRESS_GRAPHIC property of the button.  By defining a PRESS_GRAPHIC that is different from the DRAW_GRAPHIC, you will provide the user with visual feedback for press events, thus enhancing your user interface.

    4. Define the PRESS_EVENT_PROCEDURE for the button.  Writing event procedures is covered in the next section.

    5. Initialize the DRAW_GRAPHIC, PRESS_GRAPHIC, RELEASE_GRAPHIC, and PRESS_EVENT_PROCEDURE properties of the button using Set_Property. Typically, the RELEASE_GRAPHIC is set to the DRAW_GRAPHIC to provide visual feedback for release events.

The following code shows the Init_Keypad_Buttons function from the demo that illustrates how to create a button that, when pressed, will beep, show a filled-in image of button on the display, and execute an event procedure.  When the button is released, the filled-in image of the button is replaced with the DRAW_GRAPHIC.

Listing 5‑7     Creating and Initializing a Control.

int buttonOne;

.

.

.

void Init_Keypad_Buttons ( void )

// Init_Keypad_Images() must be called before this routine

{

  buttonOne = New_Object( BUTTON );

  Set_Property(buttonOne, DRAW_GRAPHIC, (long) graphicOne);

  Set_Property(buttonOne, PRESS_GRAPHIC, (long) graphicButton_Press);

  Set_Property(buttonOne, RELEASE_GRAPHIC, (long) graphicOne);

  Set_Property(buttonOne, PRESS_EVENT_PROCEDURE, (long) one_event_procedure_ptr);

  .

  .

  .

}

Listing 57   Creating and Initializing a Control.

variable buttonOne

.

.

.

: Init_Keypad_Buttons ( -- )

\ Init_Keypad_Images must be called before this routine

  BUTTON New_Object buttonOne !

  buttonOne @ DRAW_GRAPHIC graphicOne @ u>d Set_Property

  buttonOne @ PRESS_GRAPHIC graphicButton_Press @ u>d Set_Property

  buttonOne @ RELEASE_GRAPHIC graphicOne @ u>d Set_Property

  buttonOne @ PRESS_EVENT_PROCEDURE cfa.for One_Event_Procedure Set_Property

  .

  .

  .

;

Remember that all properties are 32-bits in size so when setting any of the graphic properties of the button, the graphic object reference must be cast into a 32-bit number.

Writing Event Procedures

Event procedures are functions that you write to animate your controls.  Event procedures cannot take any parameters or return any values, but they can set variables and call other GUI methods like Clear and Refresh. In the demo, the event procedure for the one button of the keypad must write the number one into the textbox of the keypad at the correct position and then refresh the keypad screen so the new number will appear.  The following shows the code for the one button’s event procedure.

Listing 5‑8     Writing the Event Procedure.

// Define a constant to represent the maximum number of numbers we can enter

// using the keypad.

#define BUFFER_LENGTH 20

// Declare a variable that contains the next position we’re writing to the textbox

int buffer_pos;

.

.

.

void One_Event_Procedure ( void )

{

  if(buffer_pos < BUFFER_LENGTH-1)

  {

    STRING_TO_TEXTBOX(textboxBuffer, " 1", 0, buffer_pos);

    buffer_pos++;

    Refresh(GUI_SCREEN0);

  }

}

// We need to store the address of the event procedure into a function pointer which

// will be used in Set_Property.

#include “begin_event_procedure_pointers.h”

.

.

.

xaddr (*one_event_procedure_ptr)(void) = One_Event_Procedure;

.

.

.

#include “end_event_procedure_pointers.h”

Listing 58   Writing the Event Procedure.

\ Define a constant to represent the maximum number of numbers we can enter

\ using the keypad.

20 constant BUFFER_LENGTH

\ Declare a variable that contains the next position we’re writing into the textbox

variable buffer_pos

.

.

.

\ Add the number one at the current position in the keypad textbox,

\ increment the pointer to the next position, and redraw the

\ textbox containing the new number onto the first screen.

: One_Event_Procedure ( -- )

buffer_pos @ locals{ &pos }

  &pos BUFFER_LENGTH 1 - <

  if

    textboxBuffer @ " 1" 0 &pos String_To_Textbox

    1 buffer_pos +!

    GUI_SCREEN0 Refresh

  endif

;

Loading Objects into Screens

The final step in creating a GUI application is loading the objects onto the screen.  To load an object onto a screen, use the Load method.  The following code initializes the graphics, buttons, and textbox for the keypad and then loads them onto the first visible screen, GUI_SCREEN0.

Listing 5‑9     Loading a Control into a Screen.

void Init_Keypad ( void )

{

  Init_Keypad_Images();

  Init_Keypad_Buttons();

  Init_Keypad_Textbox();

  buffer_pos = 0; // Init pointer to the current position within the keypad textbox.

  Load ( GUI_SCREEN0, buttonOne, 24, 126 );

  .

  .

  .

}

Listing 59    Loading a Control into a Screen.

: Init_Keypad ( -- )

  Init_Keypad_Images

  Init_Keypad_Buttons

  Init_Keypad_Textbox

  0 buffer_pos ! \ Init pointer to the current position within the keypad textbox

  GUI_SCREEN0 buttonOne 24 126 Load

  .

  .

  .

;

We've now shown you how to design and build your user interface, initialize the GUI Toolkit, create and initialize objects, write event procedures, and load objects onto screens.  This final section covers how to handle errors that may occur during your application.

Handling Errors

The GUI Toolkit has extensive error handling and reporting abilities.  When an error occurs, an error code is logged and an attempt is made to recover from the error.  For example, if you try to load a graphic that contains an image which is 24 pixels wide to the coordinates (304,0), 8 pixels of the image will hang off the edge of the screen (since the width of a screen is 320 pixels).  This will generate an X_OUT_OF_RANGE error, and the graphic will instead be loaded to the right-most position on the screen at (296,0).  You can read the error code by using the Read_Error method of the GUI Toolkit object.  Because only the last error is recorded, you must call Clear_Error after you read the error to prevent servicing the same error more than once.  The following code shows how to create a custom error handling and reporting routine:

Listing 5‑10   Handling Errors.

void Print_Error ( void )

{

  switch (Read_Error())

  {

    case HEAP_FULL: printf(“Can’t create any more new objects.\n”); break;

    case X_OUT_OF_RANGE: printf(“X coordinate is out of range.\n”); break;

   // You would put other error codes and error messages here.

  }

  Clear_Error(); // Be sure to clear the error after we read it; 

                 // we don’t want to respond to the same error more than once.

}

Listing 510   Handling Errors.

: Print_Error ( -- )

  Read_Error

  case

    HEAP_FULL of .” Can’t create any more new objects” cr endof

    X_OUT_OF_RANGE of .” X coordinate is out of range” cr endof

    \ You would put other error codes and error messages here. 

  endcase 

  Clear_Error \ Be sure to clear the error after we read it; we don’t want

              \ to respond to the same error more than once.

;

Multiple errors can occur for each GUI Toolkit method but only one can occur at a time.  It is up to you to decide if and when you should check for errors.

A useful debugging tool that comes with the GUI Toolkit is the property ABORT_ON_ERROR. If true, an error causes the controller to disable interrupts, stop multitasking, and send a descriptive error message to the primary serial port, Serial 1.  The error message identifies the calling method and parameters that triggered the error.  ABORT_ON_ERROR helps you to quickly identify the source of the error; this is especially useful when developing a complex application.  This property should be set to GUI_FALSE for the production version of your application.

Expanding the GUI Toolkit’s Objects and Methods

We understand that each GUI application you write has unique requirements.  Although it is possible for you to implement many things like custom plotting routines or drawing geometrical shapes by writing high level code, there maybe a significant performance advantage in our providing pre-packaged objects to you that perform the desired function.  Please contact Mosaic Industries about adding additional objects, properties, and methods to the GUI Toolkit.  We are planning to add many features to the toolkit to enhance it’s capabilities; the features you are looking for may already exist.

GUI Objects in Detail

There are many kinds of GUI objects.  Some are tied tightly to particular hardware (the GUI_TOUCHSCREEN and GUI_DISPLAY), some collect other objects together (GUI Screens), some respond to events (buttons), some are tools (the GUI_PEN), and some simply contain data for display (graphics and textboxes).  Several of the GUI objects are created and configured when Initialize_GUI is called.  These objects include the GUI_DISPLAY, GUI_TOUCHSCREEN, GUI_BUZZER, GUI_FONT, GUI Screens, and GUI_PEN. You can not create more of these object types.  For other object such as graphics, textboxes, buttons, and plots, you can create as many as you like. All of the GUI Toolkit objects, their 32-bit properties, associated methods, and possible errors are described below.

GUI_TOOLKIT

The GUI Toolkit is itself an object that contains properties that effect all other objects.  The GUI Toolkit Object is called GUI_TOOLKIT. These are its properties, methods, and possible errors:

Properties

Table 5‑1      Properties of the GUI_TOOLKIT.

Property

Values

Meaning

HORIZONTAL_SNAP_TO_GRID

GUI_TRUE

Read only.  Causes all objects loaded onto a screen to be located on the nearest 8-pixel horizontal boundary.  This property is always true for speed.

VERICAL_SNAP_TO_GRID

GUI_TRUE, GUI_FALSE

If true, causes all objects loaded on to a screen to be located on the nearest 8-pixel vertical boundary.

SERVICE_EVENTS

GUI_TRUE, GUI_FALSE

If false, events are not serviced.  All events generated by the touchscreen are ignored.

STANDARD_FONT

GUI_FONT or custom font object

Contains the reference to the font object used to render ASCII string data in textboxes.  At initialization, STANDARD_FONT is set to GUI_FONT which is Courier New with a width of 8 pixels and a height of 10 pixels.  You can set this property to your own custom font, if desired.

ABORT_ON_ERROR

GUI_TRUE, GUI_FALSE

If true, disables interrupts, stops multitasking, and sends a detailed message to serial 1 when an error occurs.

AVAILABLE_HEAP_SPACE

Long

Read only.  Contains the number of bytes remaining in the GUI Heap.

Methods

Table 5‑2      Methods of the GUI_TOOLKIT.

Method

Parameters

Return Value

Action

Initialize_GUI

start of the GUI Heap, end of the GUI Heap

none

Initializes the GUI_TOOLKIT, GUI_DISPLAY, GUI_TOUCHSCREEN, GUI_PEN, GUI_FONT, GUI_BUZZER, and GUI Screens.  Sets up and activates a task in common memory.  Calling any other GUI Toolkit method before calling Initialize_GUI will crash the controller.

Service_GUI_Events

none

none

Executes the event procedures of controls.  This method must be included in a task as part of the infinite loop to service events generated from the GUI Task.

Read_Error

none

error code

Reads the last reported error.

Clear_Error

none

none

Clears the last reported error.

New_Object

object type

object reference

Create a new object and return an object reference which is stored in an object variable for later use.

Set_Property

object reference, property, value

none

Set an object’s property as a 32-bit value.

Get_Property

object reference, property

value

Get an object’s property as a 32-bit value.

Errors

Table 5‑3      Errors of the GUI_TOOLKIT.

Error

Associated Method

Meaning

HEAP_FULL

Initialize_GUI, New_Object

Heap is full.  There is no room to create the GUI Toolkit, create new objects, or resize existing objects.

GUI_EXISTS

Initialize_GUI

The GUI Task has already been activated.  Do not call Initialize_GUI more than once!

DISPLAY_INITIALIZATION_FAILURE

Initialize_GUI

There was a problem initializing the display.  This error occurs if there was a hardware communications problem talking to the display controller.  If the problem persists, contact Mosaic Industries.

INVALID_OBJECT

New_Object, Set_Property, Get_Property

The object reference is not valid.

INVALID_PROPERTY

Get_Property, Set_Property

The property is not valid.

INVALID_IMAGE

Set_Property

There is no valid image at the specified address.  This error is returned after attempting to set the IMAGE property of a graphic or a font.

INVALID_GRAPHIC

Set_Property

The object reference is not a graphic, setting the DRAW_GRAPHIC, PRESS_GRAPHIC, or RELEASE_GRAPHIC property of a button.

INVALID_FONT

 Set_Property

The object reference is not a font, setting the STANDARD_FONT property of the GUI Toolkit or setting the TEXTBOX_FONT property of a textbox.

BUFFER_SIZE_OUT_OF_RANGE

Set_Property

The size of the plot object buffer must be greater than or equal to the width of the plot in pixels

COLUMN_OUT_OF_RANGE

Set_Property

The number of columns will create a textbox that is wider than the width of the screen.

ROW_OUT_OF_RANGE

Set_Property

The number of rows will create a textbox that is taller than the height of the screen.

WIDTH_OUT_OF_RANGE

Set_Property

The width will create a plot that is wider than the width of the screen.

HEIGHT_OUT