2003-11-16 07:43:39 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
Class: LightsDriver_LinuxSerial
|
|
|
|
|
|
|
|
|
|
Desc: See header.
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
|
|
|
|
|
Chris Danford
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "LightsDriver_LinuxSerial.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <termios.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
2003-11-16 11:11:56 +00:00
|
|
|
#include "RageLog.h"
|
2003-11-16 07:43:39 +00:00
|
|
|
|
|
|
|
|
static int fd = 0;
|
|
|
|
|
|
|
|
|
|
void WriteString( char* sz, int len )
|
|
|
|
|
{
|
|
|
|
|
int iOut;
|
|
|
|
|
if (fd < 1)
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn("Lights port is not open\n");
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iOut = write(fd, sz, len);
|
|
|
|
|
if (iOut < 0)
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn("Lights write error %d %s\n", errno, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT( iOut > 0 );
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-16 11:11:56 +00:00
|
|
|
LightsDriver_LinuxSerial::LightsDriver_LinuxSerial()
|
|
|
|
|
{
|
|
|
|
|
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
RageException::Throw( "open error %d %s\n", errno, strerror(errno) );
|
2003-11-16 07:43:39 +00:00
|
|
|
|
2003-11-16 11:11:56 +00:00
|
|
|
struct termios my_termios;
|
|
|
|
|
tcgetattr(fd, &my_termios);
|
|
|
|
|
tcflush(fd, TCIFLUSH);
|
|
|
|
|
|
|
|
|
|
my_termios.c_cflag = B2400 | CS8 |CREAD | CLOCAL | HUPCL;
|
|
|
|
|
|
|
|
|
|
cfsetospeed(&my_termios, B2400);
|
|
|
|
|
tcsetattr(fd, TCSANOW, &my_termios);
|
|
|
|
|
|
2003-11-17 00:46:55 +00:00
|
|
|
LOG->Info("Lights info:");
|
2003-11-17 01:45:29 +00:00
|
|
|
LOG->Info(" new cflag=%08lx", my_termios.c_cflag);
|
|
|
|
|
LOG->Info(" new oflag=%08lx", my_termios.c_oflag);
|
|
|
|
|
LOG->Info(" new iflag=%08lx", my_termios.c_iflag);
|
|
|
|
|
LOG->Info(" new lflag=%08lx", my_termios.c_lflag);
|
2003-11-17 00:46:55 +00:00
|
|
|
// c_line dosen't seem to be defined in every termios.h file
|
|
|
|
|
// Is it really needed in info?
|
|
|
|
|
#if 0
|
|
|
|
|
LOG->Info(" new line=%02x", my_termios.c_line);
|
|
|
|
|
#endif
|
2003-11-16 11:11:56 +00:00
|
|
|
|
|
|
|
|
WriteString( "Yo", strlen("Yo") );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LightsDriver_LinuxSerial::~LightsDriver_LinuxSerial()
|
|
|
|
|
{
|
|
|
|
|
if (fd > 0)
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char g_data[2] = { 0, 0 };
|
2003-11-16 07:43:39 +00:00
|
|
|
|
|
|
|
|
void LightsDriver_LinuxSerial::SetLight( Light light, bool bOn )
|
|
|
|
|
{
|
|
|
|
|
int byte_index = light / 8;
|
|
|
|
|
int bit_index = light % 8;
|
|
|
|
|
char &data = g_data[byte_index];
|
2003-11-16 11:11:56 +00:00
|
|
|
char mask = 0x01 << bit_index;
|
2003-11-16 07:43:39 +00:00
|
|
|
if( bOn )
|
|
|
|
|
data |= mask;
|
|
|
|
|
else
|
|
|
|
|
data &= ~mask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LightsDriver_LinuxSerial::Flush()
|
|
|
|
|
{
|
2003-11-16 20:27:46 +00:00
|
|
|
// flip bits so that 0=on, 1=off.
|
|
|
|
|
char temp_data[2];
|
|
|
|
|
for( int i=0; i<2; i++ )
|
|
|
|
|
temp_data[i] = ~g_data[i];
|
|
|
|
|
|
|
|
|
|
WriteString( temp_data, sizeof(temp_data) );
|
2003-11-16 07:43:39 +00:00
|
|
|
}
|