/*
We need your help to stop forest fires and bake tasty cookies!
See `requirements.md` for how to help.
Then check `notes.md`.
*/
#include "api.h"
#include "app.h"
#include "statemachine.h"
#include <math.h>
static void print_test_result(uint16_t sensor_mv, uint16_t vref_mv, int16_t expected_temp_deg_c)
{
int16_t deg_c = app_calc_temperature(sensor_mv, vref_mv);
const bool test_pass = abs(deg_c - expected_temp_deg_c) <= 1;
serial_printf("TEST %s. sensor_mv %i, vref_mv %i, calc_deg_c %i, exp_deg_c %i\n", test_pass? "PASS": "FAIL", sensor_mv, vref_mv, deg_c, expected_temp_deg_c);
}
static void run_unit_tests(void)
{
print_test_result(500, 5000, -10);
print_test_result(4500, 5000, 300);
print_test_result(2500, 5000, 145);
print_test_result(2250, 4500, 145);
print_test_result(2750, 5500, 145);
// invalid sensor tests
print_test_result(0, 5000, 32767);
print_test_result(2500, 5501, 32767);
print_test_result(2500, 4459, 32767);
}
static void print_voltages(void)
{
uint16_t sensor_mv = read_voltage(TEMPERATURE_SENSOR);
uint16_t vref_mv = read_voltage(TEMPERATURE_SENSOR_REFERENCE);
uint16_t door_mv = read_voltage(DOOR_SENSOR);
serial_printf("sensor_mv %i, vref_mv %i, door_mv %i", sensor_mv, vref_mv, door_mv);
}
void setup() {
Serial.begin(115200);
run_unit_tests();
Serial.println();
Serial.println("Elf oven 2000 starting up.");
serial_printf("Days without fire incident: %i\n", 0);
print_voltages();
Serial.println();
setup_api();
}
void loop() {
app_iterate();
print_voltages();
serial_printf(", deg C %i, sm %i, door open %i\n", g_temperature_deg_c, g_sm_state_id, g_door_is_open);
delay(200);
}