Add serial buffer threshold check to avoid potential overflow

This commit is contained in:
Josh Stewart 2015-01-26 19:40:48 +11:00
parent 482618cf92
commit e00e29144d
2 changed files with 6 additions and 2 deletions

View File

@ -36,6 +36,8 @@ const byte page_size = 128;
#define CALIBRATION_TABLE_SIZE 512
#define CALIBRATION_TEMPERATURE_OFFSET 40 // All temperature measurements are stored offset by 40 degrees. This is so we can use an unsigned byte (0-255) to represent temperature ranges from -40 to 215
#define SERIAL_BUFFER_THRESHOLD 32 // When the serial buffer is filled to greater than this threshold value, the serial processing operations will be performed more urgently in order to avoid it overflowing. Serial buffer is 64 bytes long, so the threshold is set at half this as a reasonable figure
//The status struct contains the current values for all 'live' variables
//In current version this is 64 bytes
struct statuses {

View File

@ -226,8 +226,10 @@ void setup()
void loop()
{
mainLoopCount++;
//Check for any requets from serial
if ((mainLoopCount & 63) == 1) //Only check the serial buffer (And hence process serial commands) once every 64 loops (64 Is more than fast enough for TunerStudio). This function is equivalent to ((loopCount % 64) == 1) but is considerably faster due to not using the mod or division operations
//Check for any requets from serial. Serial operations are checked under 2 scenarios:
// 1) Every 64 loops (64 Is more than fast enough for TunerStudio). This function is equivalent to ((loopCount % 64) == 1) but is considerably faster due to not using the mod or division operations
// 2) If the amount of data in the serial buffer is greater than a set threhold (See globals.h). This is to avoid serial buffer overflow when large amounts of data is being sent
if ( ((mainLoopCount & 63) == 1) or (Serial.available() > SERIAL_BUFFER_THRESHOLD))
{
if (Serial.available() > 0)
{