📄 lcmlinux.c
字号:
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <termio.h>
#include<string.h>
#include<stdio.h>
struct termios oldtermios;
static int fd;
int ttyraw(int fd , int baud)
{
struct termios newtermios;
if (tcgetattr(fd,&oldtermios)<0) return -1;
newtermios=oldtermios;
// all the basic magic for raw mode
cfmakeraw(&newtermios);
// input speed setting
switch(baud){
case 38400:
if (cfsetispeed(&newtermios,B38400)<0)
return -1;
if(cfsetospeed(&newtermios,B38400)<0)
return -1;
break;
case 57600:
if(cfsetispeed(&newtermios,B57600)<0)
return -1;
if(cfsetospeed(&newtermios,B57600)<0)
return -1;
break;
case 115200:
if(cfsetispeed(&newtermios,B115200)<0)
return -1;
if(cfsetospeed(&newtermios,B115200)<0)
return -1;
break;
case 9600:
if(cfsetispeed(&newtermios,B9600)<0)
return -1;
if(cfsetospeed(&newtermios,B9600)<0)
return -1;
break;
case 2400:
if(cfsetispeed(&newtermios,B2400)<0)
return -1;
if(cfsetospeed(&newtermios,B2400)<0)
return -1;
break;
case 19200:
if(cfsetispeed(&newtermios,B19200)<0)
return -1;
if(cfsetospeed(&newtermios,B19200)<0)
return -1;
break;
case 300:
if(cfsetispeed(&newtermios,B300)<0)
return -1;
if(cfsetospeed(&newtermios,B300)<0)
return -1;
break;
}
newtermios.c_cc[VMIN] =1;
newtermios.c_cc[VTIME] = 0;
newtermios.c_cflag &= ~PARENB;
newtermios.c_cflag &= ~CSTOPB;
newtermios.c_cflag &= ~CSIZE;
newtermios.c_cflag |= CS8;
newtermios.c_cflag &= ~CRTSCTS;
/*
* *Choosing Raw Input and Output
* */
newtermios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtermios.c_oflag &= ~OPOST;
/*
* *To disable software flow control simply mask those bits:
* */
//newtermios.c_iflag &= ~(IXON | IXOFF | IXANY);
newtermios.c_iflag &= (IXON | IXOFF | IXANY);
if(tcsetattr(fd, TCSAFLUSH, &newtermios) < 0) return -1;
return 0;
}
void SetEnvironment () {
/* system("stty ispeed 2400 < /dev/ttyS1");
system("stty raw < /dev/ttyS1");
*/
}
int Cmd = 254; /* GTLCM2X16b Command */
int cls = 1; /* Clear screen */
void Cls () {
write(fd,&Cmd,1);
write(fd,&cls,1);
}
int init = 0x28;
void Init () {
write(fd,&Cmd,1);
write(fd,&init,1);
}
int stopsend = 0x37;
void StopSend () {
write(fd,&Cmd,1);
write(fd,&init,1);
}
int home = 2 ; /* Home cursor */
void Home () {
write(fd,&Cmd,1);
write(fd,&home,1);
}
int readkey = 6 ; /* Read key */
void ReadKey () {
write(fd,&Cmd,1);
write(fd,&readkey,1);
}
int blank = 8 ; /* Blank display */
void Blank () {
write(fd,&Cmd,1);
write(fd,&blank,1);
}
int hide = 12 ; /* Hide cursor & display blanked characters */
void Hide () {
write(fd,&Cmd,1);
write(fd,&hide,1);
}
int turn = 13 ; /* Turn On (blinking block cursor) */
void TurnOn () {
write(fd,&Cmd,1);
write(fd,&turn,1);
}
int show = 14 ; /* Show underline cursor */
void Show () {
write(fd,&Cmd,1);
write(fd,&show,1);
}
int movel = 16 ; /* Move cursor 1 character left */
void MoveL () {
write(fd,&Cmd,1);
write(fd,&movel,1);
}
int mover = 20 ; /* Move cursor 1 character right */
void MoveR () {
write(fd,&Cmd,1);
write(fd,&mover,1);
}
int scl = 24; /* Scroll cursor 1 character left */
void ScrollL(){
write(fd,&Cmd,1);
write(fd,&scl,1);
}
int scr = 28; /* Scroll cursor 1 character right */
void ScrollR(){
write(fd,&Cmd,1);
write(fd,&scr,1);
}
int setdis = 64;/* Command */
void SetDis(){
write(fd,&Cmd,1);
write(fd,&setdis,1);
}
/* Add or Change Show Message here */
char mes1[] = "Welcome Gtech";
char mes2[] = "*************";
char mes3[] = "Up is selected";
char mes4[] = "Down is selected";
char mes5[] = "Enter is selected";
char mes6[] = "ESC is selected";
char nul[] = " ";
int a,b;
void ShowMessage (char *str1 , char *str2) {
a = strlen(str1);
b = 40 - a;
write(fd,str1,a);
write(fd,nul,b);
write(fd,str2,strlen(str2));
}
int main (int argc,char *argv[]) {
//SetEnvironment(); /* Set RAW mode */
int baud;
if(argc<2)
{
printf("uagse:./lcd /dev/ttyS1 2400\n");
exit(2);
}
baud=atoi(argv[2]);
printf("baud:%d\n",baud);
fd = open(argv[1] ,O_RDWR);/** Open Serial port (COM2) */
if (ttyraw(fd,baud)!=0) {
perror("Cannot set raw mode");
exit(1);
}
Init(); /* Initialize GTLCM2X16b twice */
Init();
Cls(); /* Clear screen */
ShowMessage(mes1,mes2);
while (1) {
int res;
char buf[255];
SetDis();
ReadKey(); /* sub-routine to send "read key" command */
res = read(fd,buf,255); /* read response from GTLCM2X16b */
switch(buf[1]) { /* Switch the Read command */
case 0x4D : /* Up Botton was received */
Cls();
ShowMessage(mes1,mes3); /** display "Welcome Gtech" */
break; /** display "Up is selected */
case 0x47 : /** Down Botton was received */
Cls();
ShowMessage(mes1,mes4); /** display "Welcome Gtech" */
break; /** display "Down is selected" */
case 0x4B : /** Enter Botton was received */
Cls();
ShowMessage(mes1,mes5); /** display "Welcome Gtech" */
break; /** display "Enter is selected" */
case 0x4E : /** Escape Botton was received */
Cls();
ShowMessage(mes1,mes6); /** display "Welcome Gtech" */
break; /** display "Escape is selected */
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -