/* * Hot Beverage Companion – indicator module * * Copyright (C) 2018 Gediminas Jakutis * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; version 2.1 * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ #include "indicator.h" #include "configuration.h" #include "volatile_ops.h" #define HIGH 1 #define LOW 0 #define OUTPUT 1 struct indicator { unsigned int red; unsigned int green; unsigned int blue; int current_temp; 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); static void indicator_calibrate(int const * const calibration); void indicator_init(int temp) { static const int default_calibration[] = {3430, 3330, 3230, 3130}; pinMode(redled, OUTPUT); pinMode(greenled, OUTPUT); pinMode(blueled, OUTPUT); state.current_temp = temp; indicator_calibrate(configuration.calibration); } void indicator_update(const int temp, const unsigned int on) { if (!on) { indicator_set_state(0, 0, 0); return; } if (temp == -1) { indicator_set_state(1, 1, 1); } else 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); } } static void indicator_calibrate(int const * const calibration) { volatile_memcpy(state.calibration, calibration, sizeof(state.calibration)); indicator_update(state.current_temp, 1); } 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; } }