Refactor main.cpp by extracting logic into modular components

Remove CONFIG_STDOUT_CONSOLE from configuration
This commit is contained in:
zhangyisong 2026-07-07 19:47:48 +08:00
parent e74fa5c804
commit 5c122122f0
2 changed files with 7 additions and 109 deletions

View File

@ -1,4 +1,3 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_STD_CPP23=y
CONFIG_CPP=y

View File

@ -1,116 +1,15 @@
#include <ctime>
#include <etl/vector.h>
#include <zephyr/drivers/hwinfo.h>
#include <zephyr/drivers/sensor.h>
#include "watchdog.hpp"
#include "zephyr/drivers/gpio.h"
#include "zephyr/kernel.h"
#include "zpp/fmt.hpp"
#include "zpp/timer.hpp"
#include <bitset>
#include <led_strip_indicator/led_strip_indicator.hpp>
#include <uart_com/simple_protocal.hpp>
#include <zpp/device.hpp>
#include <zpp/value.hpp>
namespace {
constexpr uart_com::SimpleProtocal::CallbackType kCbTable[] = {};
auto sensor = DEVICE_DT_GET(DT_NODELABEL(godtek));
auto &pmc =
*(uart_com::SimpleProtocal *)(DEVICE_DT_GET(DT_NODELABEL(pm_protocal)));
auto &led_strip = ZPP_DRV_GET(ledstrip::Indicator, DT_NODELABEL(indicator));
enum Command { kTemp = 0x00, kGetId, kRunningState };
enum RunningState {};
constexpr auto kDeviceIdSize = 20;
enum StatusId : uint8_t { kStandby, kRunning, kPause, kError };
gpio_dt_spec led_g = GPIO_DT_SPEC_GET(DT_NODELABEL(led_g), gpios);
k_timer led_timer;
#define GET_NTC_DRV(node) DEVICE_DT_GET(node),
const device *ntc_sensor[] = {
DT_FOREACH_CHILD(DT_NODELABEL(mcp3208), GET_NTC_DRV)};
struct Cb {
static auto GetId(uart_com::DataType data) -> void {
uint8_t buffer[kDeviceIdSize];
auto size = hwinfo_get_device_id(buffer, sizeof(buffer));
pmc.Send(kGetId, uart_com::DataType(buffer, size));
}
static auto RunningState(uart_com::DataType data) -> void {
auto state = static_cast<StatusId>(data[0]);
pmc.Send(
kRunningState,
uart_com::DataType(reinterpret_cast<uint8_t *>(&state), sizeof(state)));
led_strip.Status(state).on_error([](zpp::error_code code) {});
}
static constexpr std::pair<const uint8_t,
uart_com::SimpleProtocal::CallbackType>
kCbTable[] = {
{kGetId, GetId},
{kRunningState, RunningState},
};
};
enum SensorType {
NTC0 = 0,
NTC1,
NTC2,
NTC3,
INFRARED,
};
std::bitset<5> bs;
auto SensorTriggerCallback(const device *dev, const sensor_trigger *trig)
-> void {
if (dev == sensor) {
bs.set(NTC0);
}
}
auto InitSensors() {
static const sensor_trigger tri{.chan = SENSOR_CHAN_AMBIENT_TEMP,
.type = SENSOR_TRIG_DATA_READY};
sensor_trigger_set(sensor, &tri, SensorTriggerCallback);
}
} // namespace
#include <com.hpp>
#include <heating.hpp>
#include <infrared.hpp>
#include <ntc.hpp>
#include <watchdog.hpp>
namespace {} // namespace
auto main(void) -> int {
gpio_pin_configure_dt(&led_g, GPIO_OUTPUT_ACTIVE);
k_timer_init(
&led_timer, [](k_timer *tim) { gpio_pin_toggle_dt(&led_g); },
[](k_timer *tim) {});
k_timer_start(&led_timer, K_NO_WAIT, K_SECONDS(1));
pmc.SetRxCallbackTable(Cb::kCbTable);
auto wdt = app::WatchDogConfig{};
sensor_trigger tri{.type = SENSOR_TRIG_DATA_READY,
.chan = SENSOR_CHAN_AMBIENT_TEMP};
if (auto r = sensor_trigger_set(
sensor, &tri,
[](const device *dev, const sensor_trigger *trig) { flag = true; });
r != 0) {
return -ENODEV;
};
while (1) {
if (flag) {
sensor_value val{0, 0};
if (sensor_sample_fetch_chan(sensor, SENSOR_CHAN_AMBIENT_TEMP) == 0) {
sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &val);
const uint8_t data[2] = {static_cast<uint8_t>(val.val1),
static_cast<uint8_t>(val.val2)};
pmc.Send(kTemp, data);
}
for (auto ntc: ntc_sensor) {
if (sensor_sample_fetch_chan(ntc, SENSOR_CHAN_AMBIENT_TEMP) == 0) {
sensor_channel_get(ntc, SENSOR_CHAN_AMBIENT_TEMP, &val);
const uint8_t data[2] = {static_cast<uint8_t>(val.val1),
static_cast<uint8_t>(val.val2)};
pmc.Send(kTemp, data);
}
}
flag = false;
}
wdt.Feed();
}
return 0;