diff options
Diffstat (limited to 'src/tempmodule/indicator.c')
-rw-r--r-- | src/tempmodule/indicator.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/tempmodule/indicator.c b/src/tempmodule/indicator.c new file mode 100644 index 0000000..3991f93 --- /dev/null +++ b/src/tempmodule/indicator.c @@ -0,0 +1,89 @@ +#include "indicator.h" + +#define HIGH 1 +#define LOW 0 +#define OUTPUT 1 + +struct indicator { + unsigned int red; + unsigned int green; + unsigned int blue; + unsigned int current_temp; + unsigned int calibration[4]; +}; + +static struct indicator state; +static const unsigned int redled = 5; /* D1 */ +static const unsigned int greenled = 4; /* D2 */ +static const unsigned int blueled = 14; /* D5 */ + +static void indicator_set_state(unsigned int red, unsigned int green, unsigned int blue); + +void indicator_init(unsigned int temp, unsigned int const * const calibration) +{ + static const unsigned int default_calibration[] = {3430, 3330, 3230, 3130}; + + pinMode(redled, OUTPUT); + pinMode(greenled, OUTPUT); + pinMode(blueled, OUTPUT); + + state.current_temp = temp; + + if (calibration) { + indicator_calibrate(calibration); + } else { + indicator_calibrate(default_calibration); + } + +} + +void indicator_update(unsigned int temp) +{ + if (temp > state.calibration[0]) { + indicator_set_state(1, 0, 0); + } else if (temp > state.calibration[1]) { + indicator_set_state(1, 1, 0); + } else if (temp >= state.calibration[2]) { + indicator_set_state(0, 1, 0); + } else if (temp >= state.calibration[3]) { + indicator_set_state(0, 1, 1); + } else if (temp < state.calibration[3]) { + indicator_set_state(0, 0, 1); + } +} + +void indicator_calibrate(unsigned int const * const calibration) +{ + memcpy(state.calibration, calibration, sizeof(state.calibration)); + indicator_update(state.current_temp); +} + +static void indicator_set_state(unsigned int red, unsigned int green, unsigned int blue) +{ + if (red != state.red) { + if (red) { + digitalWrite(redled, HIGH); + } else { + digitalWrite(redled, LOW); + } + state.red = red; + } + + if (green != state.green) { + if (green) { + digitalWrite(greenled, HIGH); + } else { + digitalWrite(greenled, LOW); + } + state.green = green; + } + + if (blue != state.blue) { + if (blue) { + digitalWrite(blueled, HIGH); + } else { + digitalWrite(blueled, LOW); + } + state.blue = blue; + } +} |