@ -9,7 +9,8 @@ struct Task *Kernel_get_task(int pid);
struct Task tasks [ MAX_TASKS ] ;
int next_pid = 1 ;
unsigned long last_tick ;
unsigned long last_tick_start ;
unsigned long last_tick_duration ;
bool inputs [ 2 ] = { false , false } ;
void Kernel_panic ( char * message )
@ -25,7 +26,8 @@ void Kernel_panic(char* message)
void Kernel_setup ( )
{
last_tick = millis ( ) ;
last_tick_start = millis ( ) ;
last_tick_duration = 0 ;
//Serial.begin(115200);
}
@ -158,6 +160,11 @@ bool Kernel_read_input(int input)
return value ;
}
unsigned long Kernel_get_last_tick_duration ( )
{
return last_tick_duration ;
}
struct Task * Kernel_get_task ( int pid )
{
for ( int i = 0 ; i < MAX_TASKS ; i + + )
@ -174,10 +181,10 @@ struct Task *Kernel_get_task(int pid)
void Kernel_tick ( )
{
unsigned long duration = millis_since ( last_tick ) ;
last_tick_ duration = millis_since ( last_tick_start ) ;
// Store the last runtime so we can calculate duration next tick. We do this at the beginning of the method
// so it counts the time spent running tasks as time elapsed.
last_tick = millis ( ) ;
last_tick_start = millis ( ) ;
unsigned long next_tick_due = ULONG_MAX ;
int running_tasks = 0 ;
@ -195,7 +202,7 @@ void Kernel_tick()
// run_interval 0 is a special case of "run whenever we run", so set TICK any time we get here
if ( tasks [ i ] . run_interval > 0 )
{
if ( tasks [ i ] . run_accumulator + duration > tasks [ i ] . run_interval )
if ( tasks [ i ] . run_accumulator + last_tick_ duration > tasks [ i ] . run_interval )
{
// Set signal
tasks [ i ] . signal | = SIGNAL_TICK ;
@ -205,7 +212,7 @@ void Kernel_tick()
else
{
// Otherwise, accumulate time
tasks [ i ] . run_accumulator + = duration ;
tasks [ i ] . run_accumulator + = last_tick_ duration;
}
}
else