// Tests the basic functionality of push/pop/peek/size/etc. functions. void BasicSerialBufferTest(void** state) { (void)state; // Unused. // Create a serial buffer of size 32. struct SerialBuffer buf; SerialBufferInit(&buf, 32); // Check the capacity and that it should be empty by default. assert_int_equal(SerialBufferMaxCapacity(&buf), 32); assert_int_equal(SerialBufferIsEmpty(&buf), 1); // Save formatted text to the buffer. SerialBufferPrintf(&buf, "Hello World! %f\n", 3.14152); // Ensure the correct number of bytes were saved to the buffer. assert_int_equal(SerialBufferSize(&buf), 21); // Peek into the buffer. assert_int_equal(SerialBufferPeek(&buf, 0), 'H'); assert_int_equal(SerialBufferPeek(&buf, 7), 'o'); // Fill the buffer to it's max. SerialBufferPrintf(&buf, "Foo! Foo! Foo! Foo!\n"); assert_int_equal(SerialBufferIsFull(&buf), 1); uint8_t i = 0; // Keep removing items from the buffer until it's empty. while (!SerialBufferIsEmpty(&buf)) { SerialBufferPop(&buf); i++; } // Ensure the correct number of items were removed. assert_int_equal(i, 32); // Fill the buffer again. (0 = no error). for (i = 0; i < 32; i++) assert_int_equal(SerialBufferPush(&buf, i), 0); // Ensure it's full again. assert_int_equal(SerialBufferIsFull(&buf), 1); assert_int_equal(SerialBufferSize(&buf), 32); // Empty the buffer, ensuring all the data was correct. for (i = 0; i < 32; i++) assert_int_equal(SerialBufferPop(&buf), i); assert_int_equal(SerialBufferIsEmpty(&buf), 1); }
void ReceiveSerialCmds(void) { static char ReceiveCmdState = 0; static char CurrentCmd[23] = {0}; char c; static int charCount = 0; static char deviceNum = 0; static char deviceType = 0; //TODO: Clear the overflow flag if set if(_OERR) { _OERR = 0; #ifdef __DEBUG while(1); // Freeze here if it occurs while debugging! #endif } switch (ReceiveCmdState) { default: case 0: // Check if the buffer contains data if (SerialBufferIsEmpty()) return; charCount = 0; // Buffer has data, get the first item CurrentCmd[0] = SerialBuffer[SB_Read++]; SB_Read %= SERIAL_BUFFER_SIZE; if(CurrentCmd[charCount] == '<') { ReceiveCmdState = 1; // Increment to the next state // DON'T BREAK, FALL THROUGH TO NEXT STATE } else { break; } case 1: // Check if the buffer contains data if (SerialBufferIsEmpty()) return; // Read the next value from the buffer and Change the buffer read pointer c = SerialBuffer[SB_Read++]; SB_Read %= SERIAL_BUFFER_SIZE; // Check if it's 'F' if(c == 'F' || c == 'R') { ReceiveCmdState = 2; deviceType = c; // DON'T BREAK, FALL THROUGH TO NEXT STATE } else { // unrecognized command just start the cycle over... sendStringUART(ERR_CMD_UNKNOWN, 1); ReceiveCmdState = 0; break; } case 2: // Check if the buffer contains data if (SerialBufferIsEmpty()) return; charCount = 0; // Read the next value from the buffer and Change the buffer read pointer c = SerialBuffer[SB_Read++]; SB_Read %= SERIAL_BUFFER_SIZE; // Check if it's '1' or '2' if(c == '1' || c == 'H') { deviceNum = c; charCount = 0; ReceiveCmdState = 3; // DON'T BREAK, FALL THROUGH TO NEXT STATE } else { // Unrecognized command received. Send error sendStringUART( ERR_CMD_UNKNOWN, 1); ReceiveCmdState = 0; break; } case 3: // Now we are shifting in the command data, stay here until // the end command or begin command is received. if (SerialBufferIsEmpty()) return; // Read the next value from the buffer and Change the buffer read pointer c = SerialBuffer[SB_Read++]; SB_Read %= SERIAL_BUFFER_SIZE; if(charCount > (FOCUSER_COMMAND_SIZE + 4)) { // Send command too long error sendStringUART(ERR_CMD_TOO_LONG, 1); // Wait for new command ReceiveCmdState = 0; } else if(c == '>') { // THE COMMAND IS COMPLETE CurrentCmd[charCount] = 0; sendStringUART("!", 1); ReceiveCmdState = 4; } else if ( c == '<') { ReceiveCmdState = 1; } else { CurrentCmd[charCount++] = c; } break; case 4: { char errorMsg[] = {0x00}; // We have a command. Determine what it is. focusingRotatorCommand newCmd; memset(&newCmd, 0x00, sizeof(focusingRotatorCommand)); newCmd.source = SERIAL; newCmd.deviceType = deviceType; InterpretCommandString(CurrentCmd, charCount, &newCmd, errorMsg); // Make sure a valid command has been set if(newCmd.command == CMD_NONE) sendStringUART(ERR_CMD_UNKNOWN, 1); else if(newCmd.command == CMD_ERROR) sendStringUART(errorMsg, 1); else AddCmdToBfr(deviceNum, &newCmd); ReceiveCmdState = 0; } break; } }