-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPlotToSerial.cpp
More file actions
99 lines (91 loc) · 2.69 KB
/
PlotToSerial.cpp
File metadata and controls
99 lines (91 loc) · 2.69 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
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
/*
Example using M5UnitUnified for UnitCO2
*/
// #define USING_M5HAL // When using M5HAL
#include <M5Unified.h>
#include <M5UnitUnified.h>
#include <M5UnitUnifiedENV.h>
namespace {
auto& lcd = M5.Display;
m5::unit::UnitUnified Units;
m5::unit::UnitCO2 unit;
} // namespace
void setup()
{
M5.begin();
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
#if defined(USING_M5HAL)
#pragma message "Using M5HAL"
// Using M5HAL
m5::hal::bus::I2CBusConfig i2c_cfg;
i2c_cfg.pin_sda = m5::hal::gpio::getPin(pin_num_sda);
i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl);
auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
if (!Units.add(unit, i2c_bus ? i2c_bus.value() : nullptr) || !Units.begin()) {
M5_LOGE("Failed to begin");
lcd.clear(TFT_RED);
while (true) {
m5::utility::delay(10000);
}
}
#else
#pragma message "Using Wire"
// Using TwoWire
Wire.end();
Wire.begin(pin_num_sda, pin_num_scl, 400000U);
if (!Units.add(unit, Wire) || !Units.begin()) {
M5_LOGE("Failed to begin");
lcd.clear(TFT_RED);
while (true) {
m5::utility::delay(10000);
}
}
#endif
M5_LOGI("M5UnitUnified has been begun");
M5_LOGI("%s", Units.debugInfo().c_str());
{
auto ret = unit.stopPeriodicMeasurement();
float offset{};
ret &= unit.readTemperatureOffset(offset);
uint16_t altitude{};
ret &= unit.readSensorAltitude(altitude);
uint16_t pressure{};
ret &= unit.readAmbientPressure(pressure);
bool asc{};
ret &= unit.readAutomaticSelfCalibrationEnabled(asc);
uint16_t ppm{};
ret &= unit.readAutomaticSelfCalibrationTarget(ppm);
ret &= unit.startPeriodicMeasurement();
M5.Log.printf(
" temp offset:%f\n"
" sensor altitude:%u\n"
"ambient pressure:%u\n"
" ASC enabled:%u\n"
" ASC target:%u\n",
offset, altitude, pressure, asc, ppm);
if (!ret) {
lcd.clear(TFT_RED);
while (true) {
m5::utility::delay(10000);
}
}
}
lcd.clear(TFT_DARKGREEN);
}
void loop()
{
M5.update();
Units.update();
if (unit.updated()) {
// Can be checked e.g. by serial plotters
M5.Log.printf(">CO2:%u\n>Temperature:%2.2f\n>Humidity:%2.2f\n", unit.co2(), unit.temperature(),
unit.humidity());
}
}