This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
176 lines (148 loc) · 3.8 KB
/
main.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "boot.h"
#include "test.h"
#include "led.h"
#include "button.h"
#include "thermometer.h"
#include "display.h"
#include "pump.h"
#include <stdint.h>
// typedefs
typedef enum { COLLECTOR, WATER, THERMOMETER_SIZE } eTemperature;
typedef enum { ENABLED, OVERHEAT, NODIFF } eCircuitState;
typedef enum { OK, HEATING } eControlState;
typedef enum { CONTROL, SETTING } eSystemState;
// defines
#define COLLECTOR_ID 0x0D0114339EA1D228
#define WATER_ID 0x8A01143392748328
#define IDLE_TIMEOUT 10000LL
#define TARGET_DEFAULT 26.0
#define DISPLAY_DEFAULT WATER
#define MAX_DOWN 45.0
#define MAX_UP 38.0
#define DIFF_UP 3.0
#define DIFF_DOWN 1.0
// static variables
static deviceaddress_t thermometers[THERMOMETER_SIZE] = { COLLECTOR_ID, WATER_ID };
static uint64_t idleCountdown = IDLE_TIMEOUT;
static float target = TARGET_DEFAULT;
static eTemperature temperatureDisplay = DISPLAY_DEFAULT;
static float temperatures[THERMOMETER_SIZE] = { 0.0 };
static eCircuitState circuit = ENABLED;
static eControlState water = OK;
static eSystemState systemState = CONTROL;
void measure() {
thermometer_getTemperaturesC(temperatures, thermometers, THERMOMETER_SIZE);
}
void act() {
if (temperatures[COLLECTOR] > MAX_DOWN)
circuit = OVERHEAT;
if ((temperatures[COLLECTOR] - temperatures[WATER]) < DIFF_DOWN)
circuit = NODIFF;
if (circuit == OVERHEAT && temperatures[COLLECTOR] < MAX_UP)
circuit = ENABLED;
if (circuit == NODIFF && (temperatures[COLLECTOR] - temperatures[WATER]) > DIFF_UP)
circuit = ENABLED;
if (water == OK && temperatures[WATER] < target)
water = HEATING;
if (water == HEATING && temperatures[WATER] >= target)
water = OK;
if (circuit == ENABLED && water == HEATING)
setPumpState(PUMP_ON);
else
setPumpState(PUMP_OFF);
}
void display() {
if (systemState == SETTING)
{
displayTemperature(target);
led_on(LED_RED);
}
else {
displayTemperature(temperatures[temperatureDisplay]);
displayHRT(water);
displayEXC(circuit == OVERHEAT);
displayANT(circuit == NODIFF);
displayTMR(circuit == ENABLED);
led_off(LED_RED);
}
}
void control() {
measure();
act();
display();
}
void idleTimeout()
{
if (systemState == SETTING)
systemState = CONTROL;
if (systemState == CONTROL)
control();
}
void onButtonRight()
{
if (systemState == CONTROL)
temperatureDisplay ^= 1;
else {
idleCountdown = IDLE_TIMEOUT * 20;
target += 0.5;
}
display();
}
void onButtonLeft()
{
if (systemState == CONTROL) {
systemState = SETTING;
}
else {
target -= 0.5;
}
idleCountdown = IDLE_TIMEOUT * 20;
display();
}
void setup()
{
boot();
led_new(LED_RED);
led_new(LED_GREEN);
button_new(BUTTON_LEFT, onButtonLeft);
button_new(BUTTON_RIGHT, onButtonRight);
control();
}
void loop()
{
if (!idleCountdown--)
{
idleTimeout();
idleCountdown = IDLE_TIMEOUT;
}
}
void test()
{
ledTest();
buttonTest();
displayTest();
deviceIdTest();
temperatureManyTest();
}
int main(void)
{
setup();
//test();
while (1)
{
loop();
}
}
/*
* DONE:
* add button driver
* add display driver
* add LED driver
* do some wiring like in the wiring image
* read ROM command is for one device only
* add OneWire and Temperature Reader
* correct temperature interpretation
* include ids of other temperature sensors (maybe label them too)
* add Control logic
* test control logic by using leds (at first)
*/