Text Output on LCD

From Nutwiki
Jump to: navigation, search

Description

In this example you'll see how to access the LCD of your board - if it works with the sbilcd driver that Nut/OS provides - and write to it in the same way as you'd write to a file. You'll also see how to use some of the supported VT52 control characters for enhanced output formatting.

Source Code

<source lang="c">

  1. include <cfg/os.h>
  2. include <dev/board.h>
  3. include <dev/st7036.h>
  4. include <dev/term.h>
  1. include <stdio.h>
  2. include <string.h>
  3. include <io.h>
  4. include <fcntl.h>
  5. include <errno.h>
  1. include <sys/thread.h>
  2. include <sys/timer.h>
  3. include <sys/event.h>
  1. define LCD_LF 10

char display[32];

int main(void) {

   u_long baud = 115200;
   FILE *fp = NULL;
   int i = 0;
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("\nGeneric LCD Demo");
   if (NutRegisterDevice(&devSbiLcd, 0, 0)) {
       puts("Failed to register LCD");
   }
   if ((fp = fopen("sbilcd", "w")) == 0) {
       puts("Failed to open device stream.");
   }
   fprintf(fp, "Hello world!");
   NutSleep(3000);
   fprintf(fp, ESC_CLR);
   for (i = 0; i < 25; i++) {
       sprintf(display, "Line: %d", i);
       fputc(LCD_LF, fp);
       fprintf(fp, display);
       NutSleep(250);
   }
   for (i = 0; i < 32; i++) {
       fprintf(fp, ESC_CLR);
       fprintf(fp, ESC_POS "%c" "%c", (i / 16) + 32, (i % 16) + 32);
       fprintf(fp, "*");
       NutSleep(500);
   }
   fprintf(fp, ESC_CLR);
   fprintf(fp, "End of demo!");
   fclose(fp);
   for (;;);
   return 0;

} </source>

Details

<source lang="c"> u_long baud = 115200; FILE *fp = NULL; int i = 0;

NutRegisterDevice(&DEV_DEBUG, 0, 0); freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); puts("\nGeneric LCD Demo"); </source>

We register the debug device and output the name of our application. The debug device is almost not used in this example but in case something goes wrong with preparing the LCD, we'll output error messages to it.

<source lang="c"> if (NutRegisterDevice(&devSbiLcd, 0, 0)) {

   puts("Failed to register LCD");

} </source>

Then we register the LCD driver. This works well with the SAM Internet Radio for example.

<source lang="c"> if ((fp = fopen("sbilcd", "w")) == 0) {

   puts("Failed to open device stream.");

} </source>

Here we open our LCD for writing, just as we'd open any other filestream. Yes, that means that all those nifty file handling commands work on the display, too!

<source lang="c"> fprintf(fp, "Hello world!");

NutSleep(3000); </source>

Take this, for example. We can print text to the display and it appears immediately. The driver takes care of the complicated groundwork.

<source lang="c"> fprintf(fp, ESC_CLR);

for (i = 0; i < 25; i++) {

   sprintf(display, "Line: %d", i);
   fputc(LCD_LF, fp);
   fprintf(fp, display);
   NutSleep(250);

} </source>

This first clears the screen (ESC_CLR, one of the preconfigured control characters) and then loops to write 25 lines beneath each other. The LCD will scroll automatically, just like a terminal.

<source lang="c"> for (i = 0; i < 32; i++) {

   fprintf(fp, ESC_CLR);
   fprintf(fp, ESC_POS "%c" "%c", (i / 16) + 32, (i % 16) + 32);
   fprintf(fp, "*");
   NutSleep(500);

} </source>

It gets a little tricker in this loop. What we do is let an asterisk (*) wander around the screen. This is achieved by first positioning the cursor with another control character and then printing the asterisk.

The cursor positioning works like this: First we print the control character ESC_POS to tell the display the next 2 bytes are a display position. Then we give it the y position, then the x position. Note that both positions have an added +32. This is necessary to make the placement work correctly.

<source lang="c"> fprintf(fp, ESC_CLR); fprintf(fp, "End of demo!");

fclose(fp); </source>

Finally, we clear the screen one last time, tell the user that the show's over and then close our file.

Output

See also





Template:Languages