-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI2C.cpp
More file actions
226 lines (195 loc) · 5.03 KB
/
I2C.cpp
File metadata and controls
226 lines (195 loc) · 5.03 KB
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
#include "I2C.h"
I2C::I2C(NixieDisplay* nixie, RTC_DS3231* rtc, Adafruit_BME280* bme, Adafruit_VEML7700* veml, Settings* settings, HV* hv) {
_nixie = nixie;
_rtc = rtc;
_bme = bme;
_veml = veml;
_settings = settings;
_hv = hv;
}
void I2C::rtcBegin() {
Wire.begin();
// Start the DS3231 RTC
if (!_rtc->begin()) {
_settings->I2C_CODE[0] = 1;
} else {
_settings->I2C_CODE[0] = 0;
}
}
void I2C::bmeBegin() {
// Start the BME280 Environmental Sensor with address of 0x76
if (!_bme->begin(0x76)) {
_settings->I2C_CODE[1] = 1;
// Reduce BME sampling rate to prevent overheating
_bme->setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF);
} else {
_settings->I2C_CODE[1] = 0;
}
}
void I2C::vemlBegin() {
// Start the VEML7700 light sensor
if (!_veml->begin()) {
_settings->I2C_CODE[2] = 1;
} else {
_settings->I2C_CODE[2] = 0;
}
// Set the BS612 PIR pin to input
pinMode(PIN_PIR, INPUT);
}
void I2C::displayTHP() {
int t;
int h;
int p;
_bme->takeForcedMeasurement();
t = _bme->readTemperature() * 100;
h = _bme->readHumidity();
p = _bme->readPressure() / 100.0F;
// Enable and disable the right segments
_nixie->disableAllSegments();
_nixie->enableSegment(hourTens[t/1000]);
_nixie->enableSegment(hourUnits[(t%100)/100]);
_nixie->enableSegment(minuteTens[(t%100)/10]);
// Write to display
_nixie->updateDisplay();
delay(2000);
/* DEBUG *
SP("Temp");
SP(t[0]/10);
SP(t[0]%10);
SP(t[1]/10);
SPL(" ");
*/
// Display humidity
// Enable and disable the right segments
_nixie->disableSegments(hourTens, 10);
_nixie->disableSegments(hourUnits, 10);
_nixie->disableSegments(minuteTens, 10);
_nixie->disableSegments(minuteUnits, 10);
_nixie->disableSegments(secondTens, 10);
_nixie->disableSegments(secondUnits, 10);
_nixie->enableSegment(hourTens[h/10]);
_nixie->enableSegment(hourUnits[h%10]);
// Write to display
_nixie->updateDisplay();
delay(2000);
/* DEBUG *
SP("H");
SP(h/10);
SP(h%10);
SPL(" ");
*/
byte p1[4];
if (p > 999) {
p1[0] = p/1000;
p1[1] = (p/100)%10;
p1[2] = (p/10)%10;
p1[3] = p%10;
} else if (p > 99) {
p1[0] = 10;
p1[1] = (p/100)%10;
p1[2] = (p/10)%10;
p1[3] = p%10;
} else if (p > 9) {
p1[0] = 0;
p1[1] = 0;
p1[2] = (p/10)%10;
p1[3] = p%10;
} else if (p > 0) {
p1[0] = 0;
p1[1] = 0;
p1[2] = 0;
p1[3] = p%10;
} else {
p1[0] = 0;
p1[1] = 0;
p1[2] = 0;
p1[3] = 0;
}
// Display pressure
// Enable and disable the right segments
_nixie->disableSegments(hourTens, 10);
_nixie->disableSegments(hourUnits, 10);
_nixie->disableSegments(minuteTens, 10);
_nixie->disableSegments(minuteUnits, 10);
_nixie->disableSegments(secondTens, 10);
_nixie->disableSegments(secondUnits, 10);
_nixie->enableSegment(hourTens[p1[0]]);
_nixie->enableSegment(hourUnits[p1[1]]);
_nixie->enableSegment(minuteTens[p1[2]]);
_nixie->enableSegment(minuteUnits[p1[3]]);
// Write to display
_nixie->updateDisplay();
delay(2000);
/* DEBUG *
SP("P");
SP(p1[0]);
SP(p1[1]);
SP(p1[2]);
SP(p1[3]);
SPL(" ");
*/
}
// Light Sensor Routine
void I2C::readLight() {
if (_settings->I2C_CODE[2]) {
return;
}
// Enable/disable HV out
if (_veml->readLux() < _settings->flashLux) {
if (_hv->_hvon) {
_hv->switchOff();
}
} else {
if (!_hv->_hvon) {
_hv->switchOn();
}
}
}
// PIR Sensor Routine
void I2C::PIR() {
motion = digitalRead(PIN_PIR); // Read the PIR Pin Output
if (motion == HIGH) {
if (pirState == LOW) {
_hv->switchOn();
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
_hv->switchOff();
pirState = LOW;
}
}
}
// Read time from DS3231
void I2C::readTime() {
DateTime now = _rtc->now();
_settings->hour = now.hour();
_settings->minute = now.minute();
_settings->second = now.second();
}
// Read date from DS3231
void I2C::readDate() {
DateTime now = _rtc->now();
_settings->day = now.day();
_settings->month = now.month();
_settings->year = now.year() % 100;
}
// Adjust DS3231 date and time using epoch
void I2C::adjustEpoch(unsigned long epoch) {
_rtc->adjust(DateTime(epoch));
SP("Setting DS3231 Date and Time using EPOCH");
}
// Adjust DS3231 date/time using user input values
void I2C::adjustDateTime(byte dt) {
if (dt == 1) { // If dt = 1, then only set date
readTime();
} else if (dt == 2) { // If dt = 2, then only set time
readDate();
}
_rtc->adjust(DateTime(_settings->year, _settings->month, _settings->day, _settings->hour, _settings->minute, _settings->second));
SPL("Setting date/time manually updated");
}