/*
glcd.c: Demonstration of LCD/keypad use on Gryphon
20010418/js
lcd is accessed via node /gryphon/dev/glcd
keypad is accessed via node /gryphon/dev/gkeypad
The LCD device recognizes the following printf-like special
control character sequences:
\n new line
\f clear screen
\b move cursor left (but never up) one position
\r return cursor to home position (0,0)
mvXY move cursor to position X Y e.g.:
mv\011\001 moves cursor to the second line down, and 9 chars across
mb blinking cursor
mc normal cursor
mn no cursor
mf turn display off (reversed by any of above 3 commands)
where is the escape character (dec. 27, oct. \033, hex 0x1b)
*/
#define L_ARROW 0x7f
#define R_ARROW 0x7e
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/file.h>
/* Keypad bit definitions: */
#define UP 0x01
#define DOWN 0x02
#define LEFT 0x04
#define RIGHT 0x08
int main(void)
{
int lcd,key,exit;
unsigned char buf[256];
unsigned char kp;
/* Open the LCD device */
if (!(lcd = open ("/gryphon/dev/glcd", O_RDWR)))
{
printf ("\nCouldn't open lcd file.\n");
return(-1);
}
/* Open the keypad device */
if (!(key = open ("/gryphon/dev/gkeypad", O_RDWR)))
{
printf ("\nCouldn't open keypad file.\n");
return(-1);
}
/* Clear LCD, print banner on LCD */
sprintf(buf,"\f Gryphon LCD/Key Test\n--[%c and %c to exit]--",L_ARROW,R_ARROW);
write(lcd,buf,strlen(buf));
/* Read keypad, print message until exit
condition occurs (L and R keys simultaneously) */
exit=0;
do {
read(key,&kp,1); /* Get current key state */
kp&=0x0f; /* Upper 4 bits are undefined */
switch(kp)
{
case UP: /* UP key pressed? */
sprintf(buf,"\033mv\001\002 UP key pressed ");
break;
case DOWN: /* Down key pressed? */
sprintf(buf,"\033mv\001\002 DOWN key pressed ");
break;
case LEFT: /* Left key pressed? */
sprintf(buf,"\033mv\001\002 LEFT key pressed ");
break;
case RIGHT: /* Right key pressed? */
sprintf(buf,"\033mv\001\002RIGHT key pressed ");
break;
case LEFT|RIGHT: /* Both L and R keys? (exit) */
exit=1;
sprintf(buf,"\033mv\001\002Exiting demo program");
default:
break;
}
/* If key pressed, print a message on LCD */
if(kp) write(lcd,buf,strlen(buf));
} while(!exit);
/* Close files and exit */
close(lcd);
close(key);
return 0;
}
This document was obtained from:
Dearborn Group, Inc.
(248) 488-2080
dg@dgtech.com
http://www.dgtech.com
Copyright © 2003 Dearborn Group Inc.