Auxiliaries I use for CreaBot
Diff: Bot_Auxiliaries.cpp
- Revision:
- 0:32b17da1ddae
diff -r 000000000000 -r 32b17da1ddae Bot_Auxiliaries.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bot_Auxiliaries.cpp Wed Oct 31 14:22:26 2018 +0000 @@ -0,0 +1,197 @@ +// ***************************************************************** +// Handle all the LEDs with some general function wrappers functions +// ***************************************************************** +#include "Bot_Auxiliaries.h" + +// Added a lookup function from a color number to a color, used by Creabot +int LED_CAR::ColNr2Color( char ColNr) +{ switch (ColNr) { + case 0: return BLACK; + case 1: return WHITE; + case 2: return RED; + case 3: return GREEN; + case 4: return BLUE; + case 5: return PURPLE; + default: return BLACK; + } +} + +void LED_CAR::LEDsOff( void ) { + StopRotation(); + StopBlink() ; + for (int lNr=0; lNr<NumLEDs; lNr++) + { SetColor(BLACK,lNr); } +} + +void LED_CAR::LEDsRainbow( void ) +{ for (int Nr=0; Nr<NumLEDs; Nr++) + { SetColor(ColNr2Color(Nr + 1),Nr); } +} + +void LED_CAR::LEDNrCol(LED_Nr aNr, int parameter) +{ + SetColor( ColNr2Color( aNr ), aNr); +} + +void LED_CAR::LEDsRainbowMove( double speed ) +{ LEDsRainbow( ); + StartRotation(0.3); +} + +void LED_CAR::LEDClignote(LED_Nr aNr, int OnOff) { + if (OnOff == 1) { + SetColor( ORANGE, aNr); + StartBlink(0.5) ;} + else { LEDsOff(); } +} + +// ***************************************************************** +// Handle all the LEDs specifically with the LED Position definitions +// ***************************************************************** + +void LED_CAR::LEDFront(int ColNr) +{ + SetColor( ColNr2Color( ColNr ), ledAvD); + SetColor( ColNr2Color( ColNr ), ledAvG); +} + +void LED_CAR::LEDRear(int ColNr) +{ + SetColor( ColNr2Color( ColNr ), ledArD); + SetColor( ColNr2Color( ColNr ), ledArG); +} + +void LED_CAR::LEDCligR(int speed) { + if ( (speed>0) && (speed<4) ) { + SetColor(ORANGE, ledAvD) ; + SetColor(ORANGE, ledArD) ; + StartBlink(float(speed)/4) ;} + else { LEDsOff(); } + } + +void LED_CAR::LEDCligL(int speed) { + if ( (speed>0) && (speed<4) ) { + SetColor(ORANGE, ledAvG) ; + SetColor(ORANGE, ledArG) ; + StartBlink(float(speed)/4) ;} + else { LEDsOff(); } +} + +void LED_CAR::LEDAnim(int speed) { + if ( (speed>0) && (speed<4) ) + { LEDsRainbowMove(float(speed)/4); } + else { LEDsOff(); } +} + + +//************************************ +// Interpreter Class Creation +//************************************ +// Strategy: the interpreter accumulates characters in its input buffer +// it flags the presence of complete lines +// it also flags Overflow of the buffer in which case all subsequent characters +// are lost until the queue is emptied, and a CR-LF is received +// Defined States of the interpreter: +// MyState = isStartNew, isLineFilling, isOverflow, isWaitNewLine}; + + +Interpreter::Interpreter() +{ Reinit( ); + MyState = isStartNew; +} + +void Interpreter::Reinit( void ) { + MyState = isWaitNewLine; // indicates no buffer overflow + WriteIndex = 0; // points to next index to write to + LinesComplete = 0; // indicates that no complete line is currently available + // Start Scanning at the start + ScanIndex = 0; + BufAvail = BuffLen;// the full buffer size is available + } + +// Barebone function, assumes that checks havee been performed by writeBuf! +void Interpreter::AddChar( char aChar ) { + if (WriteIndex == BuffLen) {WriteIndex=0;} + else {WriteIndex++;} + RingBuf[WriteIndex]=aChar; // all right, buffer it! + BufAvail--; // Buffer is shrinking + if (BufAvail==0) { MyState = isOverflow; } +} + +// High level method to add a Char to the buffer, +// Separates at Chars 10, 13, 0; replaced by a single 0 char +// Blocking write when buffer has overflowed +void Interpreter::writeBuf(char aChar) { + bool LineEnds = aChar==10 || aChar==13 || aChar==0; + switch (MyState) { + case isOverflow: break; + case isStartNew: + // avoid that consecutive CR LF are counted as multiple lines + if (!LineEnds) {AddChar(aChar ); MyState = isLineFilling; } + break; + case isLineFilling: + if (!LineEnds) {AddChar(aChar);} + else + {MyState = isStartNew; // ready for the next line + // Between consecutive commands, the endstring=NULL character is inserted + // this is to indicate that line was already counted as completed + AddChar(0 ); // append a line end char, will detect bufferFull! + LinesComplete++; } // count completed lines + break; + case isWaitNewLine: // waiting for a new line end to arrive after an overflow + if (LineEnds) { MyState = isStartNew; } + break; + default: MyState = isOverflow; // goes into error state, should never happen though + } +} // writeBuf + +// Barebone function, that performs the checks to be performed! +// Passes back the actC, and reads already the nextChar into actC +char Interpreter::GetAChar( void ) { +char oldC = actC; + if (BufAvail==BuffLen) { actC = 0; } // buffer is empty + else // something is in the buffer + { if (ScanIndex == BuffLen) {ScanIndex=0;} + else {ScanIndex++;} + actC=RingBuf[ScanIndex]; // all right, get it + BufAvail++; // Buffer is increasing + } // something is in the buffer + return oldC; +} + +// skip true blank, but also Tab characters +void Interpreter::SkipBlanks( void ) { + while(BufAvail<BuffLen && (actC==' ' || actC==9|| actC==0)) { GetAChar(); } +} // SkipBlanks + +int Interpreter::ReadAnInt( void ) { +bool Negative = false; +int Result = 0; + if (actC=='-') {Negative = true; GetAChar(); } + else if (actC=='+') {Negative = false; GetAChar(); } + while(BufAvail<BuffLen && actC>='0' && actC<='9') + { Result = Result * 10 + (GetAChar()-'0') ; } + return Negative? -Result : Result; +} // ReadAnInt + + +RD_CMD_TYPE Interpreter::ParseCommand( void ) { + RD_CMD_TYPE cmd; // locally built command + actC=RingBuf[ScanIndex]; + SkipBlanks(); + // Next Character is the command + cmd.Command = GetAChar(); + // Next Blanks are to be omitted, but are not even mandatory + SkipBlanks(); + if ((actC>='0' && actC<='9') || actC=='-' || actC=='+' ) + { cmd.Parameter= ReadAnInt(); cmd.NumParam = 1; } + else { cmd.Parameter= 0; cmd.NumParam = 0; } + SkipBlanks( ); // There should be at least a trailing NUL character to be removed + LinesComplete--; // one line less in the storage + + return cmd; // return the built command +} + + + +