1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/*
* 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"
#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);
void indicator_init(int temp, unsigned int const * const calibration)
{
static const 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(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);
}
}
void indicator_calibrate(int const * const calibration)
{
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;
}
}
|