Graphics: Initialization

In this section, you will learn how to:

1. The first step to utilizing graphics in your projects is to import the graphics library. At the top of your file (along with the other includes), insert the following line:

#include <ti/grlib/grlib.h>

2. Next, we need to import the drivers for the LCD screen. Find a project that already has graphics implemented and locate the "LcdDriver" folder. The example repository shown to the right (GuessTheColor) has its folder located under the "HAL" folder. You can find this repository on the Repositories page (or here: https://github.com/ECE2564-VT/example_GuessTheColor).

Copy the "LcdDriver" folder and paste it into your project. Then insert the following line with your includes:

#include "LcdDriver/Crystalfontz128x128_ST7735.h"

Congrats! You now have access to all of the graphics library and Crystalfontz functions you will need to interface with the LCD screen.

3. Before the LCD screen can be used, it needs to be initialized. Fortunately, initializing it is as simple as calling these two lines of code:

Crystalfontz128x128_Init();

Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP);

The first function doesn't take parameters, so no worries there. The second function does have a parameter, and its possible values can be seen by Ctrl + clicking on the function's name:

... But typically, you'll usually want to opt for LCD_ORIENTATION_UP. It will usually make your life and the TAs' lives easier.

We're not quite ready to start using graphics yet. We need to initialize a context first! Let's take a look at the grlib.h file located in the TI library to see what functions we have available.

The first thing to note is that the context is a struct:

4. As such, you'll want to create a new struct variable for the graphics context, like so:

Graphics_Context g_sContext;

Create this pointer somewhere that can be easily accessed, since this will need to be passed into every graphics function.

We're not done yet, though. Using a brand-new struct without initializing its values usually never goes well, but luckily, the graphics library has a function that takes care of that:

5. For our purposes, calling this line of code will be sufficient for your projects:

Graphics_initContext(&g_sContext, &g_sCrystalfontz128x128, &g_sCrystalfontz128x128_funcs);

(Be sure to swap g_sContext for the name of your Graphics_Context variable!)

Your graphics context may be initialized with some default values, but it's good practice to establish your own default background and foreground colors, as well as a default font. The graphics library contains functions that allow you to easily change these.

6. The usage of these functions will covered in more detail in the Colors and Fonts pages. Here's what such a default initialization may look like:

Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_WHITE);

Graphics_setBackgroundColor(&g_sContext, GRAPHICS_COLOR_BLACK);

Graphics_setFont(&g_sContext, &g_sFontFixed6x8);

7. Finally, for good measure, we should clear the screen to finish off the initialization. This ensures that no graphics from any previously loaded projects remain.

Graphics_clearDisplay(&g_sContext);

Here's an example of what a very generic initialization would look like:

#include <ti/grlib/grlib.h>

#include "LcdDriver/Crystalfontz128x128_ST7735.h"


int main(void)

{

    // Initialize the LCD

    Crystalfontz128x128_Init();

    Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP);


    // Create context

    Graphics_Context g_sContext;


    // Initialize context

    Graphics_initContext(&g_sContext, &g_sCrystalfontz128x128, &g_sCrystalfontz128x128_funcs);


    // Set colors and fonts

    Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_WHITE);

    Graphics_setBackgroundColor(&g_sContext, GRAPHICS_COLOR_BLACK);

    Graphics_setFont(&g_sContext, &g_sFontFixed6x8);


    // Clear the screen

    Graphics_clearDisplay(&g_sContext);

}