@ -4,6 +4,12 @@
byte SerialChannel : : nextId = 1 ;
byte SerialChannel : : nextId = 1 ;
SerialChannel * SerialChannel : : first = 0 ;
SerialChannel * SerialChannel : : first = 0 ;
SerialChannel : : Message SerialChannel : : buffer1 [ SerialChannel : : MaxPendingMessages ] ;
SerialChannel : : Message SerialChannel : : buffer2 [ SerialChannel : : MaxPendingMessages ] ;
SerialChannel : : Message * SerialChannel : : backBuffer ;
byte SerialChannel : : backBufferPos ;
byte SerialChannel : : frontBufferSize ;
SerialChannel : : SerialChannel ( const char * name_ )
SerialChannel : : SerialChannel ( const char * name_ )
: next ( 0 )
: next ( 0 )
, id ( ( byte ) - 1 )
, id ( ( byte ) - 1 )
@ -21,6 +27,14 @@ SerialChannel::SerialChannel(const char* name_)
id = nextId + + ;
id = nextId + + ;
}
}
void SerialChannel : : beginWriteInChannel ( byte id , short byteCount , unsigned long time )
{
Serial . write ( " START " ) ;
Serial . write ( id ) ;
writeULong ( time ) ;
writeShort ( byteCount ) ;
}
void SerialChannel : : write ( byte * data , short byteCount , unsigned long time )
void SerialChannel : : write ( byte * data , short byteCount , unsigned long time )
{
{
beginWrite ( byteCount , time ) ;
beginWrite ( byteCount , time ) ;
@ -34,10 +48,7 @@ void SerialChannel::beginWrite(short byteCount, unsigned long time)
handleConnection ( ) ;
handleConnection ( ) ;
Serial . write ( " START " ) ;
beginWriteInChannel ( id , byteCount , time ) ;
Serial . write ( id ) ;
writeULong ( time ) ;
writeShort ( byteCount ) ;
}
}
void SerialChannel : : continueWrite ( byte * data , short byteCount )
void SerialChannel : : continueWrite ( byte * data , short byteCount )
@ -50,6 +61,44 @@ void SerialChannel::write(const char* text, unsigned long time)
write ( ( byte * ) text , strlen ( text ) , time ) ;
write ( ( byte * ) text , strlen ( text ) , time ) ;
}
}
void SerialChannel : : append ( byte * data , short byteCount , unsigned long time )
{
if ( time = = ( unsigned long ) - 1 )
time = micros ( ) ;
Message & msg = backBuffer [ backBufferPos + + ] ;
if ( backBufferPos > = MaxPendingMessages )
backBufferPos = MaxPendingMessages - 1 ;
msg . id = id ;
msg . data = data ;
msg . byteCount = byteCount ;
msg . time = time ;
}
void SerialChannel : : append ( const char * text , unsigned long time )
{
append ( ( byte * ) text , strlen ( text ) , time ) ;
}
void SerialChannel : : swap ( )
{
backBuffer = backBuffer = = buffer1 ? buffer2 : buffer1 ;
frontBufferSize = backBufferPos ;
backBufferPos = 0 ;
}
void SerialChannel : : flush ( )
{
handleConnection ( ) ;
Message * frontBuffer = backBuffer = = buffer1 ? buffer2 : buffer1 ;
for ( Message * msg = frontBuffer ; msg < frontBuffer + frontBufferSize ; + + msg )
{
beginWriteInChannel ( msg - > id , msg - > byteCount , msg - > time ) ;
Serial . write ( msg - > data , msg - > byteCount ) ;
}
}
void SerialChannel : : writeShort ( short num )
void SerialChannel : : writeShort ( short num )
{
{
Serial . write ( ( byte * ) & num , 2 ) ;
Serial . write ( ( byte * ) & num , 2 ) ;
@ -77,6 +126,7 @@ void SerialChannel::handleConnection()
Serial . write ( c - > name ) ;
Serial . write ( c - > name ) ;
c = c - > next ;
c = c - > next ;
}
}
Serial . flush ( ) ;
}
}
}
}