This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_function_display.ino
244 lines (208 loc) · 4.98 KB
/
multi_function_display.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
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include "Buffer.h"
#include "Button.h"
//#define DEBUG_MODE
DeviceAddress temp1Addr = {
0x28, 0x29, 0x5F, 0x37, 0x04, 0x00, 0x00, 0x94 };
DeviceAddress temp2Addr = {
0x28, 0x18, 0xC3, 0xAD, 0x03, 0x00, 0x00, 0x58 };
DeviceAddress temp3Addr = {
0x10, 0x5E, 0x98, 0x74, 0x02, 0x08, 0x00, 0xD0 };
const float R1 = 3000.0;
const float R2 = 1000.0;
#define TEMP1_PIN 6
#define VOLTAGE1_PIN A0
#define BUTTON_PIN 7
#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
#define OLED_RESET 13
const float voltageFactor = 5.0 * (R1 + R2) / R2 / 1023.0;
long lastMeasureTime = 0;
class Measurements {
public:
Measurements() :
_temp1(), _temp2(), _voltage(), _ds(TEMP1_PIN), _sensors(&_ds) {
_sensors.begin();
}
void update() {
updateTemp();
updateVoltage();
}
float getTemp1() {
return constrain(_temp1.getValue(), -99.0, 99.0);
}
float getTemp2() {
return constrain(_temp2.getValue(), -99.0, 99.0);
}
float getVoltage() {
int reading = _voltage.getValue();
return reading * voltageFactor;
}
private:
Buffer <float> _temp1;
Buffer <float> _temp2;
Buffer <int> _voltage;
OneWire _ds;
DallasTemperature _sensors;
void updateTemp() {
_sensors.requestTemperatures();
_temp1.addValue(_sensors.getTempC(temp1Addr));
_temp2.addValue(_sensors.getTempC(temp2Addr));
}
void updateVoltage() {
_voltage.addValue(analogRead(VOLTAGE1_PIN));
}
};
class Display {
public:
Display(Measurements* m, Adafruit_SSD1306* oled, RTC_DS1307* rtc)
{
_m = m;
_oled = oled;
_rtc = rtc;
_currentPage = 0;
Wire.begin();
_rtc->begin();
// if (!_rtc->isrunning()) {
// // following line sets the RTC to the date & time this sketch was compiled
// _rtc->adjust(DateTime(__DATE__, __TIME__));
// }
}
void switchPage() {
_currentPage = (_currentPage + 1) % PAGES;
render();
}
void switchToPage(int page) {
_currentPage = page;
render();
}
void clear() {
_oled->clearDisplay();
_oled->setCursor(0, 0);
}
void render() {
clear();
if (_currentPage == 0) {
char label[] = "T1";
char c[8];
formatTemperature(_m->getTemp1(), c);
show(label, c);
}
else if (_currentPage == 1) {
char label[] = "T2";
char c[8];
formatTemperature(_m->getTemp2(), c);
show(label, c);
}
else if (_currentPage == 2) {
char label[] = "U1";
char c[8];
formatVoltage(_m->getVoltage(), c);
show(label, c);
}
#ifdef DEBUG_MODE
else if (_currentPage == 3) {
char label[] = "M";
char c[5];
show(label, itoa(freeRam(), c, 10));
}
#endif
// Show bottom bar
_oled->setTextColor(WHITE);
_oled->setTextSize(2);
_oled->drawLine(0, 34, 127, 34, WHITE);
_oled->setCursor(0, 38);
_now = _rtc->now();
_oled->print(_now.hour(), DEC);
_oled->print(':');
if (_now.minute() < 10) {
_oled->print("0");
}
_oled->print(_now.minute(), DEC);
_oled->display();
}
private:
#ifdef DEBUG_MODE
static const int PAGES = 4;
#else
static const int PAGES = 3;
#endif
Adafruit_SSD1306* _oled;
Measurements* _m;
RTC_DS1307* _rtc;
int _currentPage;
DateTime _now;
void formatTemperature(const float input, char* s) {
float t1 = round(input * 10) / 10.0;
dtostrf(round(t1 * 2) / 2.0, 5, 1, s);
s[5] = char(247);
s[6] = 'C';
s[7] = '\0';
}
void formatVoltage(const float input, char* s) {
float t1 = round(input * 10) / 10.0;
dtostrf(t1, 5, 1, s);
s[5] = ' ';
s[6] = 'V';
s[7] = '\0';
}
void show(char* label, char* value) {
_oled->setTextColor(WHITE);
_oled->setTextSize(2);
_oled->setCursor(0, 8);
_oled->println(label);
_oled->setCursor(30, 8);
_oled->print(value);
#ifdef DEBUG_MODE
Serial.print(label);
Serial.print(" ");
Serial.println(value);
#endif
}
#ifdef DEBUG_MODE
int freeRam () {
// http://playground.arduino.cc/Code/AvailableMemory
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
#endif
};
Button button(BUTTON_PIN);
Measurements m;
Adafruit_SSD1306 oled(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
RTC_DS1307 rtc;
Display display(&m, &oled, &rtc);
void setup() {
delay(2000);
oled.begin(SSD1306_SWITCHCAPVCC);
oled.clearDisplay();
oled.display();
pinMode(VOLTAGE1_PIN, INPUT);
m.update();
display.render();
#ifdef DEBUG_MODE
Serial.begin(9600);
#endif
}
void loop() {
if (button.released()) {
display.switchPage();
}
// Update measurements every 15 seconds
if ((millis() - lastMeasureTime) >= 15000) {
m.update();
display.render();
lastMeasureTime = millis();
}
// if (m.getTemp() > 25.0) {
// display.switchToPage(0);
// }
}