From 52e63a3cc90813ada37ba4929d2745a88ec7bc7f Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Mon, 12 Feb 2018 16:56:48 +0200 Subject: tempmodule: implement indicator leds. --- src/tempmodule/indicator.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++ src/tempmodule/indicator.h | 11 ++++++ src/tempmodule/main.ino | 3 ++ 3 files changed, 103 insertions(+) create mode 100644 src/tempmodule/indicator.c create mode 100644 src/tempmodule/indicator.h (limited to 'src/tempmodule') 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; + } +} diff --git a/src/tempmodule/indicator.h b/src/tempmodule/indicator.h new file mode 100644 index 0000000..21eec29 --- /dev/null +++ b/src/tempmodule/indicator.h @@ -0,0 +1,11 @@ +#ifdef __cplusplus +extern "C" { +#endif + +void indicator_init(unsigned int temp, unsigned int const * const calibration); +void indicator_update(unsigned int temp); +void indicator_calibrate(unsigned int const * const calibration); + +#ifdef __cplusplus +} +#endif diff --git a/src/tempmodule/main.ino b/src/tempmodule/main.ino index dd2d723..7f8bf3f 100644 --- a/src/tempmodule/main.ino +++ b/src/tempmodule/main.ino @@ -23,6 +23,7 @@ #include #include #include +#include "indicator.h" struct temptuple { short res; /* Resistance in ohms. */ @@ -55,6 +56,7 @@ void setup(void) static const char * const password = "password"; pinMode(iled, OUTPUT); + indicator_init(0, NULL); wifi_connect(ssid, password, 1, iled); Udp.begin(port); discover_client(); @@ -84,6 +86,7 @@ void loop(void) sleep(); } + indicator_update(data); blink_led(iled, 20, 80); ++ticker; } -- cgit v1.2.3