Files
itgmania212121/stepmania/src/arch/Lights/LightsDriver_LinuxSerial.cpp
T

125 lines
3.1 KiB
C++
Raw Normal View History

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-20 06:44:54 +00:00
#include "RageTimer.h"
2003-11-16 07:43:39 +00:00
2003-11-20 06:44:54 +00:00
static int fd = -1;
2003-11-16 07:43:39 +00:00
2003-11-20 06:44:54 +00:00
void WriteString( const char *sz, int len )
2003-11-16 07:43:39 +00:00
{
int iOut;
2003-11-20 06:44:54 +00:00
if( fd == -1 )
2003-11-16 07:43:39 +00:00
{
LOG->Warn("Lights port is not open\n");
2003-11-20 06:44:54 +00:00
return;
2003-11-16 07:43:39 +00:00
}
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()
{
2003-11-20 06:44:54 +00:00
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); // COM1
2003-11-16 11:11:56 +00:00
if (fd < 0)
RageException::Throw( "open error %d %s\n", errno, strerror(errno) );
2003-11-16 07:43:39 +00:00
2003-11-20 06:44:54 +00:00
tcflush(fd, TCIFLUSH);
2003-11-16 11:11:56 +00:00
struct termios my_termios;
tcgetattr(fd, &my_termios);
2003-11-20 06:44:54 +00:00
// my_termios.c_iflag = IGNBRK;
// my_termios.c_lflag = 0;
// my_termios.c_oflag = 0;
// my_termios.c_cflag |= CREAD | CLOCAL;
my_termios.c_cflag &= ~CRTSCTS;
2003-11-19 09:25:17 +00:00
my_termios.c_iflag &= ~(IXON | IXOFF | IXANY); /* no flow control */
2003-11-20 06:44:54 +00:00
// my_termios.sg_ispeed = my_termios.sg_ispeed = B2400;
2003-11-19 09:25:17 +00:00
2003-11-20 06:44:54 +00:00
// my_termios.c_cflag = B2400 | CS8 | CREAD | CLOCAL | HUPCL;
// my_termios.c_oflag &= ~0; /* no flow control */
// fcntl(fd, F_SETFL, 0);
cfsetispeed(&my_termios, B2400);
2003-11-16 11:11:56 +00:00
cfsetospeed(&my_termios, B2400);
tcsetattr(fd, TCSANOW, &my_termios);
LOG->Info("Lights info:");
2003-12-19 07:31:11 +00:00
LOG->Info(" new cflag=%08x", (int) my_termios.c_cflag);
LOG->Info(" new oflag=%08x", (int) my_termios.c_oflag);
LOG->Info(" new iflag=%08x", (int) my_termios.c_iflag);
LOG->Info(" new lflag=%08x", (int) my_termios.c_lflag);
2003-11-16 11:11:56 +00:00
}
LightsDriver_LinuxSerial::~LightsDriver_LinuxSerial()
{
2003-11-27 03:58:48 +00:00
if( fd != -1 )
close( fd );
2003-11-16 11:11:56 +00:00
}
2003-11-19 08:53:50 +00:00
#define NUM_DATA_BYTES 2
2003-11-16 11:11:56 +00:00
2003-11-19 08:53:50 +00:00
char g_data[NUM_DATA_BYTES] = { 0, 0 };
2003-11-16 07:43:39 +00:00
void LightsDriver_LinuxSerial::SetLight( Light light, bool bOn )
{
int byte_index = light / 8;
2003-11-19 08:53:50 +00:00
ASSERT( byte_index <= NUM_DATA_BYTES );
2003-11-16 07:43:39 +00:00
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-19 08:53:50 +00:00
// temp HACK: only write once every 30 times
2003-11-20 06:44:54 +00:00
static RageTimer tm;
if( tm.PeekDeltaTime() < 0.02f )
2003-11-19 08:53:50 +00:00
return;
2003-11-20 06:44:54 +00:00
tm.Touch();
2003-11-19 07:16:17 +00:00
2003-11-20 06:44:54 +00:00
// LOG->Trace( "Entering LightsDriver_LinuxSerial::Flush()" );
2003-11-19 08:53:50 +00:00
char temp_data_to_write[2+NUM_DATA_BYTES] = "";
temp_data_to_write[0] = 'Y';
temp_data_to_write[1] = 'o';
2003-11-20 06:44:54 +00:00
2003-11-19 08:53:50 +00:00
for( int i=0; i<NUM_DATA_BYTES; i++ )
2003-11-19 09:04:25 +00:00
temp_data_to_write[2+i] = ~g_data[i]; // flip bits so that 0=on, 1=off.
2003-11-20 06:44:54 +00:00
RageTimer testing;
2003-11-19 09:04:25 +00:00
WriteString( temp_data_to_write, sizeof(temp_data_to_write) );
2003-11-20 06:44:54 +00:00
if( testing.PeekDeltaTime() > 0.1f )
LOG->Warn("WriteString took %F", testing.GetDeltaTime());
2003-11-19 08:53:50 +00:00
2003-11-20 06:44:54 +00:00
// LOG->Trace( "Leaving LightsDriver_LinuxSerial::Flush()" );
2003-11-16 07:43:39 +00:00
}