-
Notifications
You must be signed in to change notification settings - Fork 0
/
KaramelTraktatie.ino
296 lines (269 loc) · 8.14 KB
/
KaramelTraktatie.ino
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*!
* \file KaramelTraktatie.ino
* \brief Arduino Leonardo Digital Clock with temperature sensor.
*
* For this project, lots of libraries from other people have been included.
* Thank you so much for your great work . The awesome people with their
* libraries are:
*
* LedControl by Wayoda
* http://playground.arduino.cc/Main/LedControl
* https://github.com/wayoda/LedControl/issues
*
* OneWire, maintained by Paul Stoffregen (website)
* http://playground.arduino.cc/Learning/OneWire
*
* DS1307, Updated by bricofoy from Arduino Forums
* http://forum.arduino.cc/index.php/topic,93077.0.html
*/
#include <DS1307.h>
#include <LedControl.h>
#include <OneWire.h>
#include <Wire.h>
//------------------------------------------------------------------------------
// Class: Timekeeper
// Takes care of the time.
//------------------------------------------------------------------------------
class TimeKeeper {
private:
int16_t time[7];
public:
int16_t* updateAndGetTime() {
// Get the data from DS1307 RTC.
RTC.get(static_cast<int*>(time), true);
return time;
}
void setTime(uint8_t* input_time) {
RTC.stop();
RTC.set(DS1307_SEC, input_time[6]);
RTC.set(DS1307_MIN, input_time[5]);
RTC.set(DS1307_HR, input_time[4]);
RTC.set(DS1307_DOW, input_time[3]);
RTC.set(DS1307_DATE, input_time[2]);
RTC.set(DS1307_MTH, input_time[1]);
RTC.set(DS1307_YR, input_time[0]);
RTC.start();
}
TimeKeeper() {
RTC.stop();
delay(100);
RTC.start();
}
};
//------------------------------------------------------------------------------
// Class: Displayer
// Displays the digits to the seven segment display.
//------------------------------------------------------------------------------
class Displayer {
private:
const uint8_t din;
const uint8_t clk;
const uint8_t cs;
const uint8_t numOfController;
uint8_t temp;
uint8_t dp;
LedControl* lc;
public:
Displayer() : din(4), clk(6), cs(5), numOfController(1) {
lc = new LedControl(this->din, this->clk, this->cs,
this->numOfController);
lc->shutdown(0, false);
lc->setIntensity(0, 8);
lc->clearDisplay(0);
this->temp = 0;
this->dp = 0;
}
~Displayer() { delete lc; }
void Display(int16_t* time, int16_t* temperature) {
static uint8_t conversionMatrix[] = {0x7E, 0x30, 0x6D, 0x79, 0x33,
0x5B, 0x5F, 0x70, 0x7F, 0x7B};
// Get the hours.
uint8_t hours1st = *(time + 2) / 10;
uint8_t hours2nd = *(time + 2) % 10;
// Get the minutes.
uint8_t minutes1st = *(time + 1) / 10;
uint8_t minutes2nd = *(time + 1) % 10;
// Get temporary value.
this->temp = conversionMatrix[minutes2nd];
// Print out the hours and minutes.
lc->setRow(0, 7, conversionMatrix[hours1st]);
lc->setRow(0, 6, conversionMatrix[hours2nd] | 0x80);
lc->setRow(0, 5, conversionMatrix[minutes1st]);
lc->setRow(0, 4, conversionMatrix[minutes2nd]);
// Print out the temperatures.
if (temperature != NULL) {
// Celsius.
lc->setRow(0, 0, 0x4E);
if (*temperature < 0) {
lc->setRow(0, 3, 0x01);
} else {
lc->setRow(0, 3, 0x00);
}
uint8_t temperature1st = *(temperature) / 10;
uint8_t temperature2nd = *(temperature) % 10;
lc->setRow(0, 2, conversionMatrix[temperature1st]);
lc->setRow(0, 1, conversionMatrix[temperature2nd]);
}
}
void ToggleSeconds() {
// Set the last place.
lc->setRow(0, 4, temp | dp);
// Flip dp value.
dp = (0x00 == dp) ? 0x80 : 0x00;
}
};
//------------------------------------------------------------------------------
// Class: TemperatureReader
// Reads the temperature from the sensor.
//------------------------------------------------------------------------------
class TemperatureReader {
private:
OneWire* ds;
const uint8_t din;
public:
TemperatureReader() : din(8) { ds = new OneWire(din); }
~TemperatureReader() { delete ds; }
float readBack(bool read) {
static uint8_t addr[] = {0x22, 0xF4, 0x32, 0x1A,
0x00, 0x00, 0x00, 0x1F};
float celsius = 0.0f;
if (false == read) {
ds->reset();
ds->select(addr);
ds->write(0x44, 1);
} else {
uint8_t data[12];
ds->reset();
ds->select(addr);
ds->write(0xBE);
for (int j = 0; j < 9; j++) {
data[j] = ds->read();
}
int16_t raw = (data[1] << 8) | data[0];
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) {
raw = raw & ~7;
} else if (cfg == 0x20) {
raw = raw & ~3;
} else if (cfg == 0x40) {
raw = raw & ~1;
}
celsius = (float)raw / 16.0;
}
return celsius;
}
};
//------------------------------------------------------------------------------
// Class: SerialCommandReader
// Reads commands from the Serial Port.
//------------------------------------------------------------------------------
class SerialCommandReader {
private:
char raw_commands[200];
uint8_t raw_commands_index;
uint8_t time_value[7];
public:
SerialCommandReader() {
Serial.begin(9600);
delay(1000);
Serial.println("-----------------------------------------------");
Serial.println("KaramelTraktatie: Serial Commander Initialized");
Serial.println("-----------------------------------------------");
}
uint8_t* getTimeValue() { return time_value; }
bool readSerialCommand() {
raw_commands[0] = '\0';
raw_commands_index = 0;
while (Serial.available()) {
raw_commands[raw_commands_index++] = Serial.read();
if (raw_commands_index >= 200) {
raw_commands_index = 0;
}
}
raw_commands[raw_commands_index] = '\0';
uint8_t raw_commands_length = raw_commands_index;
raw_commands_index = 0;
// Example:
// SET_YYMMDDdd_hhmmss
// SET_16030201_123001 (19 bytes)
if ((raw_commands_length == 19) and (raw_commands[0] == 'S') and
(raw_commands[1] == 'E') and (raw_commands[2] == 'T')) {
Serial.println("Received a SET TIME command.");
time_value[0] =
(raw_commands[4] - '0') * 10 + (raw_commands[5] - '0');
time_value[1] =
(raw_commands[6] - '0') * 10 + (raw_commands[7] - '0');
time_value[2] =
(raw_commands[8] - '0') * 10 + (raw_commands[9] - '0');
time_value[3] =
(raw_commands[10] - '0') * 10 + (raw_commands[11] - '0');
time_value[4] =
(raw_commands[13] - '0') * 10 + (raw_commands[14] - '0');
time_value[5] =
(raw_commands[15] - '0') * 10 + (raw_commands[16] - '0');
time_value[6] =
(raw_commands[17] - '0') * 10 + (raw_commands[18] - '0');
Serial.println("Parameters: ");
Serial.println(time_value[0]);
Serial.println(time_value[1]);
Serial.println(time_value[2]);
Serial.println(time_value[3]);
Serial.println(time_value[4]);
Serial.println(time_value[5]);
Serial.println(time_value[6]);
Serial.println("");
return true;
}
return false;
}
};
//------------------------------------------------------------------------------
// Section: Main
// The main program of the Arduino.
//------------------------------------------------------------------------------
TimeKeeper* tk = NULL;
Displayer* dpl = NULL;
TemperatureReader* tr = NULL;
SerialCommandReader* sc = NULL;
void setup() {
tk = new TimeKeeper();
dpl = new Displayer();
tr = new TemperatureReader();
sc = new SerialCommandReader();
// Initial readout
tr->readBack(false);
delay(1000);
int16_t temperature = static_cast<int>(tr->readBack(true));
int16_t* time = tk->updateAndGetTime();
dpl->Display(time, &temperature);
}
uint32_t previousMillis = 0;
const uint32_t interval = 1000;
void loop() {
uint32_t currentMillis = millis();
if (currentMillis - previousMillis > interval) {
// Read the command and update time if necessary.
if (sc->readSerialCommand() == true) {
tk->setTime(sc->getTimeValue());
}
// Obtain the time.
int16_t* time = tk->updateAndGetTime();
// Measure the temperature.
if (0x00 == time[0]) {
tr->readBack(false);
}
// Get the measurement one second after.
else if (0x01 == time[0]) {
int16_t temperature = static_cast<int>(tr->readBack(true));
dpl->Display(time, &temperature);
}
// No measurement takes place.
else {
dpl->Display(time, NULL);
}
// Toggle led that indicates seconds.
dpl->ToggleSeconds();
// Save the last time.
previousMillis = currentMillis;
}
}