Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
Convert tty_type to be a bitmasked value
Browse files Browse the repository at this point in the history
  • Loading branch information
mesheets committed Mar 12, 2020
1 parent 7eaaa55 commit 3df4a6d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Include/lnphost.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ struct ringbuf {
unsigned char data[RINGBUFSIZE];
};

// Bitmask the tty types
typedef enum { tty_t_serial = 1,
tty_t_usb = 2,
tty_t_tcp = 4 } tty_type_t;

typedef void (*lnp_raw_handler_t) (unsigned char *data, unsigned length,
int isvalid);
typedef void (*lnp_lego_handler_t) (unsigned char *data, unsigned length);
Expand All @@ -55,7 +60,8 @@ typedef void (*lnp_addressing_handler_t) (unsigned char *data,

struct lnptower {
FILEHANDLE fd;
int usb,keepalive;
tty_type_t tty_type;
int keepalive;
unsigned timeout;
volatile int keepalivetimer;
volatile int threadsactive;
Expand Down
12 changes: 6 additions & 6 deletions Modules/lnphost.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ int lnp_raw_recvchar(struct lnptower *tower)
COMMTIMEOUTS CommTimeouts;
DWORD res=0;

if (tower->usb) {
if (tty_t_usb == tower->tty_type) {
gettimeofday(&timebegin,0);
while (1) {
if (ReadFile(tower->fd,&data,1,&res,NULL)==0) {
Expand Down Expand Up @@ -461,7 +461,7 @@ int lnp_open(struct lnptower *tower, char *device, unsigned flags)
return(-1);
}
if (GetCommState(tower->fd,&dcb)) {
tower->usb=0;
tower->tty_type=tty_t_serial;
FillMemory(&dcb,sizeof(dcb),0);
dcb.fBinary=TRUE;
dcb.ByteSize=8;
Expand All @@ -479,15 +479,15 @@ int lnp_open(struct lnptower *tower, char *device, unsigned flags)
PurgeComm(tower->fd,PURGE_RXABORT|PURGE_TXABORT|
PURGE_RXCLEAR|PURGE_TXCLEAR);
} else {
tower->usb=1;
tower->tty_type=tty_t_usb;
}
#else
if ((tower->fd=open(device,O_RDWR))<0) {
perror(device);
return(-1);
}
if (isatty(tower->fd)) {
tower->usb=0;
tower->tty_type=tty_t_serial;
memset(&ios,0,sizeof(ios));
ios.c_cflag=CREAD|CLOCAL|CS8;
if ((flags & LNP_FAST)==0) {
Expand All @@ -505,11 +505,11 @@ int lnp_open(struct lnptower *tower, char *device, unsigned flags)
}
tcflush(tower->fd,TCIOFLUSH);
} else {
tower->usb=1;
tower->tty_type=tty_t_usb;
}
#endif
/* Set up variables for the threads before they start */
tower->keepalive=((flags & LNP_NOKEEPALIVE)==0)?(!(tower->usb)):0;
tower->keepalive=((flags & LNP_NOKEEPALIVE)==0)?(tty_t_serial == tower->tty_type):0;
if ((flags&LNP_FAST)!=0)
tower->timeout=34;
else if ((flags&LNP_NOPARITY)!=0)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# lnphost
A library supporting IR communication over the Lego MindStorms IR tower. It can be used from C programs on Posix compliant platforms, supports multiple protocols and is designed to be a replacement for lnpd.

Original Website – http://lnphost.sourceforge.net/
Original Website – http://lnphost.sf.net/
* Includes descriptive background information on both the Lego protocol and the BrickOS (formerly LegOS) protocol.

### Project Note
The project brickOS-bibo now includes the program ir-server, which may supercede both lnpd and this program.
11 changes: 10 additions & 1 deletion lnpdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include "lnphost.h"


#if defined(_WIN32) & ( (! defined(__CYGWIN__)) | defined(__MINGW32__) )
#define usleep(t) Sleep((t) / 1000)
#endif // Windows



/* ---- IR callback routines -------------------------------------------- */

int lnpmask=0xf0;
Expand Down Expand Up @@ -154,7 +161,9 @@ int main(int argc, char *argv[]) {
}

/* ---- Program never terminates -------------------------------------- */
while (1) ;
while (1){
usleep(1000);
}
lnp_close(&tower);
return(EXIT_SUCCESS);
}

0 comments on commit 3df4a6d

Please sign in to comment.