harmonize coding style
This commit is contained in:
+48
-95
@@ -35,11 +35,7 @@ unsigned char GLKBufferEmpty = 0xff ;
|
|||||||
* Open and configure a serial port for communication with
|
* Open and configure a serial port for communication with
|
||||||
* a Matrix Orbital module (GLK or otherwise)
|
* a Matrix Orbital module (GLK or otherwise)
|
||||||
*/
|
*/
|
||||||
GLKDisplay *
|
GLKDisplay *glkopen(char *name, tcflag_t speed)
|
||||||
glkopen(
|
|
||||||
char * name,
|
|
||||||
tcflag_t speed
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
struct termios new;
|
struct termios new;
|
||||||
@@ -49,12 +45,12 @@ GLKDisplay *
|
|||||||
/* Invalid arguments */
|
/* Invalid arguments */
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return(NULL);
|
return(NULL);
|
||||||
};
|
}
|
||||||
|
|
||||||
fd = open(name, O_RDWR | O_NOCTTY);
|
fd = open(name, O_RDWR | O_NOCTTY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
return(NULL);
|
return(NULL);
|
||||||
};
|
}
|
||||||
|
|
||||||
/* Get current settings */
|
/* Get current settings */
|
||||||
if (tcgetattr(fd, &new) < 0) {
|
if (tcgetattr(fd, &new) < 0) {
|
||||||
@@ -62,13 +58,13 @@ GLKDisplay *
|
|||||||
close(fd);
|
close(fd);
|
||||||
errno = errsave;
|
errno = errsave;
|
||||||
return(NULL);
|
return(NULL);
|
||||||
};
|
}
|
||||||
|
|
||||||
retval = malloc(sizeof(GLKDisplay));
|
retval = malloc(sizeof(GLKDisplay));
|
||||||
if (retval == NULL) {
|
if (retval == NULL) {
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return(NULL);
|
return(NULL);
|
||||||
};
|
}
|
||||||
retval->fd = fd;
|
retval->fd = fd;
|
||||||
retval->saved = new;
|
retval->saved = new;
|
||||||
retval->ungetin = retval->ungetout = 0;
|
retval->ungetin = retval->ungetout = 0;
|
||||||
@@ -106,7 +102,7 @@ GLKDisplay *
|
|||||||
glkclose(retval);
|
glkclose(retval);
|
||||||
errno = errsave;
|
errno = errsave;
|
||||||
return(NULL);
|
return(NULL);
|
||||||
};
|
}
|
||||||
|
|
||||||
return(retval);
|
return(retval);
|
||||||
}
|
}
|
||||||
@@ -117,28 +113,25 @@ GLKDisplay *
|
|||||||
* a read will block (glkget) waiting on data.
|
* a read will block (glkget) waiting on data.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glktimeout(
|
glktimeout(GLKDisplay *fd, int timeout)
|
||||||
GLKDisplay * fd,
|
|
||||||
int timeout
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
struct termios t;
|
struct termios t;
|
||||||
|
|
||||||
if (timeout < 0 || timeout > 255) {
|
if (timeout < 0 || timeout > 255) {
|
||||||
errno = EINVAL; /* Invalid argument */
|
errno = EINVAL; /* Invalid argument */
|
||||||
return(1); /* Failure */
|
return(1); /* Failure */
|
||||||
};
|
}
|
||||||
|
|
||||||
if (tcgetattr(fd->fd, &t) < 0) {
|
if (tcgetattr(fd->fd, &t) < 0) {
|
||||||
return(1);
|
return(1);
|
||||||
};
|
}
|
||||||
|
|
||||||
fd->timeout = timeout;
|
fd->timeout = timeout;
|
||||||
t.c_cc[VTIME] = timeout;
|
t.c_cc[VTIME] = timeout;
|
||||||
|
|
||||||
if (tcsetattr(fd->fd, TCSANOW, &t) < 0) {
|
if (tcsetattr(fd->fd, TCSANOW, &t) < 0) {
|
||||||
return(1);
|
return(1);
|
||||||
};
|
}
|
||||||
|
|
||||||
return(0); /* Success */
|
return(0); /* Success */
|
||||||
}
|
}
|
||||||
@@ -151,25 +144,20 @@ int
|
|||||||
*/
|
*/
|
||||||
#define GLKBUF (96)
|
#define GLKBUF (96)
|
||||||
int
|
int
|
||||||
glkflow(
|
glkflow(GLKDisplay *fd, int full, int empty)
|
||||||
GLKDisplay * fd,
|
|
||||||
int full,
|
|
||||||
int empty
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
struct termios t;
|
struct termios t;
|
||||||
|
|
||||||
if( full > GLKBUF -1
|
if ((full > GLKBUF -1)
|
||||||
|| empty > GLKBUF -1
|
|| (empty > GLKBUF -1)
|
||||||
|| full + empty > GLKBUF -1
|
|| (full + empty > GLKBUF -1)) {
|
||||||
) {
|
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return(1);
|
return(1);
|
||||||
};
|
}
|
||||||
|
|
||||||
if (tcgetattr(fd->fd, &t) < 0) {
|
if (tcgetattr(fd->fd, &t) < 0) {
|
||||||
return(1); /* Failure */
|
return(1); /* Failure */
|
||||||
};
|
}
|
||||||
|
|
||||||
if (full < 0 || empty < 0) {
|
if (full < 0 || empty < 0) {
|
||||||
/* Turn it off */
|
/* Turn it off */
|
||||||
@@ -178,7 +166,8 @@ int
|
|||||||
t.c_cc[VSTART] = GLKBufferEmpty;
|
t.c_cc[VSTART] = GLKBufferEmpty;
|
||||||
t.c_cc[VSTOP] = GLKBufferFull;
|
t.c_cc[VSTOP] = GLKBufferFull;
|
||||||
fd->flow = GLKFLOW_DISABLE;
|
fd->flow = GLKFLOW_DISABLE;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
/* Turn it on */
|
/* Turn it on */
|
||||||
glkputl(fd, GLKCommand, 0x3a, full, empty, EOF);
|
glkputl(fd, GLKCommand, 0x3a, full, empty, EOF);
|
||||||
/* Control what we send */
|
/* Control what we send */
|
||||||
@@ -189,11 +178,11 @@ int
|
|||||||
t.c_cc[VSTART] = GLKBufferEmpty;
|
t.c_cc[VSTART] = GLKBufferEmpty;
|
||||||
t.c_cc[VSTOP] = GLKBufferFull;
|
t.c_cc[VSTOP] = GLKBufferFull;
|
||||||
fd->flow = GLKFLOW_OK;
|
fd->flow = GLKFLOW_OK;
|
||||||
};
|
}
|
||||||
|
|
||||||
if (tcsetattr(fd->fd, TCSANOW, &t) < 0) {
|
if (tcsetattr(fd->fd, TCSANOW, &t) < 0) {
|
||||||
return(1);
|
return(1);
|
||||||
};
|
}
|
||||||
|
|
||||||
return(0); /* Success */
|
return(0); /* Success */
|
||||||
}
|
}
|
||||||
@@ -203,16 +192,14 @@ int
|
|||||||
* Close a serial port and restore settings if provided.
|
* Close a serial port and restore settings if provided.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkclose(
|
glkclose(GLKDisplay *fd)
|
||||||
GLKDisplay * fd
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
if (fd->fd < 0) {
|
if (fd->fd < 0) {
|
||||||
/* Probably already closed */
|
/* Probably already closed */
|
||||||
return(0); /* Success? */
|
return(0); /* Success? */
|
||||||
};
|
}
|
||||||
|
|
||||||
/* Trash any unread data */
|
/* Trash any unread data */
|
||||||
tcflush(fd->fd, TCIFLUSH);
|
tcflush(fd->fd, TCIFLUSH);
|
||||||
@@ -234,10 +221,7 @@ int
|
|||||||
* Send a character to the GLK
|
* Send a character to the GLK
|
||||||
*/
|
*/
|
||||||
int INLINE
|
int INLINE
|
||||||
glkput(
|
glkput(GLKDisplay *fd, int c)
|
||||||
GLKDisplay * fd,
|
|
||||||
int c
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
unsigned char val;
|
unsigned char val;
|
||||||
ssize_t retval;
|
ssize_t retval;
|
||||||
@@ -262,15 +246,12 @@ int INLINE
|
|||||||
* flow control signals
|
* flow control signals
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkunget(
|
glkunget(GLKDisplay *fd, int c)
|
||||||
GLKDisplay * fd,
|
|
||||||
int c
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
/* Is buffer already full? */
|
/* Is buffer already full? */
|
||||||
if (((fd->ungetin + 1) & (~UNGETBUFSIZE)) == fd->ungetout) {
|
if (((fd->ungetin + 1) & (~UNGETBUFSIZE)) == fd->ungetout) {
|
||||||
return(1); /* Failure */
|
return(1); /* Failure */
|
||||||
};
|
}
|
||||||
|
|
||||||
fd->ungetbuf[fd->ungetin] = c;
|
fd->ungetbuf[fd->ungetin] = c;
|
||||||
fd->ungetin = (fd->ungetin + 1) & (~UNGETBUFSIZE);
|
fd->ungetin = (fd->ungetin + 1) & (~UNGETBUFSIZE);
|
||||||
@@ -283,9 +264,7 @@ int
|
|||||||
* Read a character from the GLK
|
* Read a character from the GLK
|
||||||
*/
|
*/
|
||||||
int INLINE
|
int INLINE
|
||||||
glkget(
|
glkget(GLKDisplay *fd)
|
||||||
GLKDisplay * fd
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
unsigned char c;
|
unsigned char c;
|
||||||
ssize_t retval;
|
ssize_t retval;
|
||||||
@@ -295,7 +274,7 @@ int INLINE
|
|||||||
return(-1);
|
return(-1);
|
||||||
} else {
|
} else {
|
||||||
return((int) c);
|
return((int) c);
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* glkpoll
|
/* glkpoll
|
||||||
@@ -304,10 +283,7 @@ int INLINE
|
|||||||
* GLK.
|
* GLK.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkpoll(
|
glkpoll(GLKDisplay *fd, int timeout)
|
||||||
GLKDisplay * fd,
|
|
||||||
int timeout
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
struct pollfd fds;
|
struct pollfd fds;
|
||||||
int retval;
|
int retval;
|
||||||
@@ -319,7 +295,7 @@ int
|
|||||||
if (retval < 0) {
|
if (retval < 0) {
|
||||||
/* Some error. Ignore it, and return "no data" */
|
/* Some error. Ignore it, and return "no data" */
|
||||||
retval = 0;
|
retval = 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
return(retval);
|
return(retval);
|
||||||
}
|
}
|
||||||
@@ -332,9 +308,7 @@ int
|
|||||||
* GLKBufferFull and GLKBufferEmpty whereas glkgetc wil NOT.
|
* GLKBufferFull and GLKBufferEmpty whereas glkgetc wil NOT.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkgetc(
|
glkgetc(GLKDisplay *fd)
|
||||||
GLKDisplay * fd
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
@@ -351,20 +325,19 @@ int
|
|||||||
} else if (c == GLKBufferEmpty) {
|
} else if (c == GLKBufferEmpty) {
|
||||||
fd->flow = GLKFLOW_OK;
|
fd->flow = GLKFLOW_OK;
|
||||||
continue;
|
continue;
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/* Must be what we're looking for */
|
/* Must be what we're looking for */
|
||||||
break;
|
break;
|
||||||
};
|
}
|
||||||
|
}
|
||||||
} else {
|
else {
|
||||||
|
|
||||||
/* Get one of the "ungotten" char's */
|
/* Get one of the "ungotten" char's */
|
||||||
c = fd->ungetbuf[fd->ungetout];
|
c = fd->ungetbuf[fd->ungetout];
|
||||||
fd->ungetout = (fd->ungetout + 1) & (~UNGETBUFSIZE);
|
fd->ungetout = (fd->ungetout + 1) & (~UNGETBUFSIZE);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
return(c);
|
return(c);
|
||||||
}
|
}
|
||||||
@@ -375,10 +348,7 @@ int
|
|||||||
* confirmation of echo
|
* confirmation of echo
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkput_confirm(
|
glkput_confirm(GLKDisplay *fd, int c)
|
||||||
GLKDisplay * fd,
|
|
||||||
int c
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
int echo;
|
int echo;
|
||||||
|
|
||||||
@@ -391,7 +361,7 @@ int
|
|||||||
echo = glkget(fd);
|
echo = glkget(fd);
|
||||||
if (echo < 0) {
|
if (echo < 0) {
|
||||||
return(1); /* Failure */
|
return(1); /* Failure */
|
||||||
};
|
}
|
||||||
|
|
||||||
if (echo == c) {
|
if (echo == c) {
|
||||||
glkput(fd, GLKConfirm);
|
glkput(fd, GLKConfirm);
|
||||||
@@ -408,11 +378,7 @@ int
|
|||||||
* confirmation.
|
* confirmation.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkputa_confirm(
|
glkputa_confirm(GLKDisplay *fd, int len, unsigned char *str)
|
||||||
GLKDisplay * fd,
|
|
||||||
int len,
|
|
||||||
unsigned char * str
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int retval;
|
int retval;
|
||||||
@@ -420,7 +386,7 @@ int
|
|||||||
retval = 0; /* Assume Success */
|
retval = 0; /* Assume Success */
|
||||||
for (i = len; !retval && i; ++str, --i) {
|
for (i = len; !retval && i; ++str, --i) {
|
||||||
retval = glkput_confirm(fd, *str);
|
retval = glkput_confirm(fd, *str);
|
||||||
};
|
}
|
||||||
|
|
||||||
return(retval);
|
return(retval);
|
||||||
}
|
}
|
||||||
@@ -430,10 +396,7 @@ int
|
|||||||
* Send a character to the GLK on fd with echo expected
|
* Send a character to the GLK on fd with echo expected
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkput_echo(
|
glkput_echo(GLKDisplay *fd, int c)
|
||||||
GLKDisplay * fd,
|
|
||||||
int c
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
int echo;
|
int echo;
|
||||||
|
|
||||||
@@ -446,7 +409,7 @@ int
|
|||||||
echo = glkget(fd);
|
echo = glkget(fd);
|
||||||
if (echo < 0) {
|
if (echo < 0) {
|
||||||
return(1); /* Failure */
|
return(1); /* Failure */
|
||||||
};
|
}
|
||||||
|
|
||||||
if (echo == c) {
|
if (echo == c) {
|
||||||
return(0); /* Success */
|
return(0); /* Success */
|
||||||
@@ -461,10 +424,7 @@ int
|
|||||||
* terminated by an EOF valued argument.
|
* terminated by an EOF valued argument.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkputl(
|
glkputl(GLKDisplay *fd, ...)
|
||||||
GLKDisplay * fd,
|
|
||||||
...
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
int value;
|
int value;
|
||||||
@@ -477,7 +437,7 @@ int
|
|||||||
while (!retval && value != EOF) {
|
while (!retval && value != EOF) {
|
||||||
retval = glkput(fd, value);
|
retval = glkput(fd, value);
|
||||||
value = va_arg(ap, int);
|
value = va_arg(ap, int);
|
||||||
};
|
}
|
||||||
|
|
||||||
return(retval);
|
return(retval);
|
||||||
}
|
}
|
||||||
@@ -488,10 +448,7 @@ int
|
|||||||
* GLK.
|
* GLK.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkputs(
|
glkputs(GLKDisplay *fd, char *str)
|
||||||
GLKDisplay * fd,
|
|
||||||
char * str
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
register char *p = str;
|
register char *p = str;
|
||||||
int retval;
|
int retval;
|
||||||
@@ -499,7 +456,7 @@ int
|
|||||||
retval = 0; /* Success */
|
retval = 0; /* Success */
|
||||||
for (p = str; !retval && *p; ++p) {
|
for (p = str; !retval && *p; ++p) {
|
||||||
retval = glkput(fd, *p);
|
retval = glkput(fd, *p);
|
||||||
};
|
}
|
||||||
|
|
||||||
return(retval);
|
return(retval);
|
||||||
}
|
}
|
||||||
@@ -509,11 +466,7 @@ int
|
|||||||
* Send an array of characters.
|
* Send an array of characters.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
glkputa(
|
glkputa(GLKDisplay *fd, int len, unsigned char *str)
|
||||||
GLKDisplay * fd,
|
|
||||||
int len,
|
|
||||||
unsigned char * str
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
register unsigned char *p = str;
|
register unsigned char *p = str;
|
||||||
int i;
|
int i;
|
||||||
@@ -522,7 +475,7 @@ int
|
|||||||
retval = 0; /* Assume Success */
|
retval = 0; /* Assume Success */
|
||||||
for (i = len; !retval && i; ++p, --i) {
|
for (i = len; !retval && i; ++p, --i) {
|
||||||
retval = glkput(fd, *p);
|
retval = glkput(fd, *p);
|
||||||
};
|
}
|
||||||
|
|
||||||
return(retval);
|
return(retval);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user