diff --git a/lambda/rules.c b/lambda/rules.c index a36b214..97e4d78 100644 --- a/lambda/rules.c +++ b/lambda/rules.c @@ -19,7 +19,6 @@ uint8_t measCount = MEAS_INT; FireState state = undefined; -// uint8_t airgate = 100; static int32_t deltaAvg = 0; static int16_t tempIMax = TEMP_INIT; @@ -29,7 +28,7 @@ * Pushes the given new value in the queue of temperature differences * and returns the oldest value being pushed off the array. */ -static int16_t pushQueue(const int16_t value) { +static int16_t pushQueue(int16_t const value) { int16_t last = tempIDiffQueue[QUEUE_SIZE - 1]; for (size_t i = QUEUE_SIZE - 1; i > 0; i--) { tempIDiffQueue[i] = tempIDiffQueue[i - 1]; @@ -43,13 +42,26 @@ * Initializes all elements in the queue of temperature differences * to the given value. */ -static void initQueue(const int16_t value) { +static void initQueue(int16_t const value) { for (size_t i = 0; i < QUEUE_SIZE; i++) { tempIDiffQueue[i] = value; } } /** + * Closes the airgate and puts the motor driver in sleep mode after a delay, + * giving the motor sufficient time. + */ +static void closeAirgateAndSleep(void) { + setAirgate(AIRGATE_CLOSE); + void func(void) { + setSleepMode(true); + } + // put stepper motor driver in sleep mode in 60 seconds + scheduleTask(func, 60); +} + +/** * Reminds to set the air gate to 50% when the fire is still firing up * and the temperature has reached TEMP_AIRGATE_50. */ @@ -86,12 +98,7 @@ if (state == burning_down && meas.tempI < TEMP_AIRGATE_0 && meas.lambda >= LAMBDA_MAX && getAirgate() > AIRGATE_CLOSE) { setHeaterState(heaterStateOff); - setAirgate(AIRGATE_CLOSE); - void func(void) { - setSleepMode(true); - } - // put stepper motor driver in sleep mode in 60 seconds - scheduleTask(func, 60); + closeAirgateAndSleep(); alert_P(BEEPS, LENGTH, TONE, PSTR(MSG_AIRGATE_CLOSE_0), PSTR(""), false); *fired = true; @@ -151,7 +158,6 @@ static void warmStart(bool* const fired, Measurement const meas) { if (! *fired && state == firing_up && meas.tempI > TEMP_FIRE_OUT && tempIMax >= TEMP_AIRGATE_50) { - // TODO wake up driver here? setSleepMode(false); resetRules(false); tempIMax = meas.tempI; @@ -211,12 +217,12 @@ if (heaterUptime >= 1800 && meas.tempI < TEMP_FIRE_OUT && meas.lambda >= LAMBDA_MAX) { setHeaterState(heaterStateOff); - setSleepMode(true); + closeAirgateAndSleep(); } if (heaterUptime >= 10800 && meas.tempI < TEMP_AIRGATE_0 && meas.lambda >= LAMBDA_MAX) { setHeaterState(heaterStateOff); - setSleepMode(true); + closeAirgateAndSleep(); } } @@ -247,7 +253,7 @@ } // called about every second -void reason(Measurement meas) { +void reason(Measurement const meas) { tempIMax = MAX(tempIMax, meas.tempI); diff --git a/lambda/scheduler.c b/lambda/scheduler.c index 7f3b1bb..a0a8024 100644 --- a/lambda/scheduler.c +++ b/lambda/scheduler.c @@ -1,6 +1,11 @@ /* * scheduler.c * + * Very basic task scheduler allowing to schedule up to three functions at a + * time to be executed when a given delay in seconds has passed. When a function + * was executed the task is set to done, freeing up a slot for another task to + * be scheduled. + * * Created on: 09.03.2016 * Author: dode@luniks.net */ diff --git a/lambda/scheduler.h b/lambda/scheduler.h index b5511d2..30c772c 100644 --- a/lambda/scheduler.h +++ b/lambda/scheduler.h @@ -37,6 +37,14 @@ * Schedules the given function for execution after the given delay in seconds. * Returns false if the function could not be scheduled because there were * no free slots available, true otherwise. + * The function must have no return value and take no arguments. Arbitrary code + * can be passed for execution by wrapping it in such a function: + * + * void task(void) { + * // some code here + * } + * // run task in 30 seconds + * scheduleTask(task, 30); */ bool scheduleTask(void (*func)(void), uint16_t const delay);