Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Käppler
ampel-firmware
Commits
316d13bc
Commit
316d13bc
authored
Dec 21, 2020
by
Eric Duminil
Browse files
Refactor co2_sensor. Smaller methods
parent
e10bc6a4
Changes
3
Show whitespace changes
Inline
Side-by-side
ampel-firmware.ino
View file @
316d13bc
...
@@ -116,7 +116,7 @@ void loop() {
...
@@ -116,7 +116,7 @@ void loop() {
// Short press for night mode, Long press for calibration.
// Short press for night mode, Long press for calibration.
checkFlashButton
();
checkFlashButton
();
sensor
::
getAndSend
Data
();
sensor
::
process
Data
();
uint32_t
duration
=
millis
()
-
t0
;
uint32_t
duration
=
millis
()
-
t0
;
if
(
duration
>
max_loop_duration
)
{
if
(
duration
>
max_loop_duration
)
{
...
...
co2_sensor.cpp
View file @
316d13bc
...
@@ -97,6 +97,7 @@ namespace sensor {
...
@@ -97,6 +97,7 @@ namespace sensor {
bool
updateDataIfAvailable
()
{
bool
updateDataIfAvailable
()
{
if
(
scd30
.
dataAvailable
())
{
if
(
scd30
.
dataAvailable
())
{
// checkTimerDeviation();
// checkTimerDeviation();
timestamp
=
ntp
::
getLocalTime
();
co2
=
scd30
.
getCO2
();
co2
=
scd30
.
getCO2
();
temperature
=
scd30
.
getTemperature
();
temperature
=
scd30
.
getTemperature
();
humidity
=
scd30
.
getHumidity
();
humidity
=
scd30
.
getHumidity
();
...
@@ -130,7 +131,36 @@ namespace sensor {
...
@@ -130,7 +131,36 @@ namespace sensor {
ESP
.
restart
();
ESP
.
restart
();
}
}
void
getAndSendData
()
{
void
logToSerial
()
{
Serial
.
println
(
timestamp
);
Serial
.
print
(
F
(
"co2(ppm): "
));
Serial
.
print
(
co2
);
Serial
.
print
(
F
(
" temp(C): "
));
Serial
.
print
(
temperature
);
Serial
.
print
(
F
(
" humidity(%): "
));
Serial
.
println
(
humidity
);
}
void
displayCO2OnLedRing
()
{
if
(
co2
<
250
)
{
// Sensor should be calibrated.
LedEffects
::
showWaitingLED
(
color
::
magenta
);
return
;
}
/**
* Display data, even if it's "old" (with breathing).
* Those effects include a short delay.
*/
if
(
co2
<
2000
)
{
LedEffects
::
displayCO2color
(
co2
);
LedEffects
::
breathe
(
co2
);
}
else
{
// >= 2000: entire ring blinks red
LedEffects
::
redAlert
();
}
}
void
processData
()
{
bool
freshData
=
updateDataIfAvailable
();
bool
freshData
=
updateDataIfAvailable
();
//NOTE: Data is available, but it's sometimes erroneous: the sensor outputs zero ppm but non-zero temperature and non-zero humidity.
//NOTE: Data is available, but it's sometimes erroneous: the sensor outputs zero ppm but non-zero temperature and non-zero humidity.
...
@@ -141,22 +171,14 @@ namespace sensor {
...
@@ -141,22 +171,14 @@ namespace sensor {
}
}
/**
/**
* Fresh data.
Show
it and send it if needed.
* Fresh data.
Log
it and send it if needed.
*/
*/
if
(
freshData
)
{
if
(
freshData
)
{
if
(
should_calibrate
)
{
if
(
should_calibrate
)
{
countStableMeasurements
();
countStableMeasurements
();
}
}
timestamp
=
ntp
::
getLocalTime
();
Serial
.
println
(
timestamp
);
Serial
.
print
(
F
(
"co2(ppm): "
));
Serial
.
print
(
co2
);
Serial
.
print
(
F
(
" temp(C): "
));
Serial
.
print
(
temperature
);
Serial
.
print
(
F
(
" humidity(%): "
));
Serial
.
println
(
humidity
);
logToSerial
();
csv_writer
::
logIfTimeHasCome
(
timestamp
,
co2
,
temperature
,
humidity
);
csv_writer
::
logIfTimeHasCome
(
timestamp
,
co2
,
temperature
,
humidity
);
#ifdef MQTT
#ifdef MQTT
...
@@ -172,22 +194,6 @@ namespace sensor {
...
@@ -172,22 +194,6 @@ namespace sensor {
return
;
return
;
}
}
if
(
co2
<
250
)
{
displayCO2OnLedRing
();
// Sensor should be calibrated.
LedEffects
::
showWaitingLED
(
color
::
magenta
);
return
;
}
/**
* Display data, even if it's "old" (with breathing).
* Those effects include a short delay.
*/
if
(
co2
<
2000
)
{
LedEffects
::
displayCO2color
(
co2
);
LedEffects
::
breathe
(
co2
);
}
else
{
// >= 2000: entire ring blinks red
LedEffects
::
redAlert
();
}
}
}
}
}
co2_sensor.h
View file @
316d13bc
...
@@ -28,7 +28,7 @@ namespace sensor {
...
@@ -28,7 +28,7 @@ namespace sensor {
extern
String
timestamp
;
extern
String
timestamp
;
void
initialize
();
void
initialize
();
void
getAndSend
Data
();
void
process
Data
();
void
startCalibrationProcess
();
void
startCalibrationProcess
();
}
}
#endif
#endif
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment