summaryrefslogtreecommitdiffstats
path: root/src/tempmodule/indicator.c
blob: 3991f9332ce2b92a331db76d3731cf5c1b011975 (plain)
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
#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;
	}
}