2016-07-15 00:23:30 -07:00
/*
Speeduino - Simple engine management for the Arduino Mega 2560 platform
Copyright ( C ) Josh Stewart
A full copy of the license may be found in the projects root directory
*/
/*
* Sets the next available error
* Returns the error number or 0 if there is no more room for errors
*/
2017-01-17 22:37:55 -08:00
# include "errors.h"
2017-02-13 20:21:38 -08:00
2016-07-15 00:23:30 -07:00
byte setError ( byte errorID )
{
if ( errorCount < MAX_ERRORS )
{
errorCodes [ errorCount ] = errorID ;
errorCount + + ;
2017-02-13 20:21:38 -08:00
if ( errorCount = = 1 ) { BIT_SET ( currentStatus . spark , BIT_SPARK_ERROR ) ; } //Enable the error indicator
2016-07-15 00:23:30 -07:00
}
2017-06-15 23:41:45 -07:00
return errorCount ;
2016-07-15 00:23:30 -07:00
}
void clearError ( byte errorID )
{
2017-06-15 23:41:45 -07:00
byte clearedError = 255 ;
2017-02-13 20:21:38 -08:00
2016-07-15 00:23:30 -07:00
if ( errorID = = errorCodes [ 0 ] ) { clearedError = 0 ; }
else if ( errorID = = errorCodes [ 1 ] ) { clearedError = 1 ; }
else if ( errorID = = errorCodes [ 2 ] ) { clearedError = 2 ; }
else if ( errorID = = errorCodes [ 3 ] ) { clearedError = 3 ; }
2017-06-15 23:41:45 -07:00
if ( clearedError < MAX_ERRORS )
2016-07-15 00:23:30 -07:00
{
2017-06-15 23:41:45 -07:00
errorCodes [ clearedError ] = ERR_NONE ;
//Clear the required error and move any from above it 'down' in the error array
for ( byte x = clearedError ; x < ( errorCount - 1 ) ; x + + )
{
errorCodes [ x ] = errorCodes [ x + 1 ] ;
errorCodes [ x + 1 ] = ERR_NONE ;
}
errorCount - - ;
if ( errorCount = = 0 ) { BIT_CLEAR ( currentStatus . spark , BIT_SPARK_ERROR ) ; } //Enable the error indicator
2016-07-15 00:23:30 -07:00
}
}
byte getNextError ( )
{
packedError currentError ;
2017-02-13 20:21:38 -08:00
2016-07-15 00:23:30 -07:00
//We alternate through the errors once per second
2017-02-13 20:21:38 -08:00
byte currentErrorNum = currentStatus . secl % MAX_ERRORS ; //Which error number will be returned. This changes once per second. Note that as long as MAX_ERRORS is a power of 2, this % operation is performed very quickly.
2016-07-15 00:23:30 -07:00
currentError . errorNum = currentErrorNum ;
currentError . errorID = errorCodes [ currentErrorNum ] ;
2017-02-13 20:21:38 -08:00
return * ( byte * ) & currentError ; //Ugly, but this forces the cast of the currentError struct to a byte.
2016-07-15 00:23:30 -07:00
}