test_led_strip/src/main.cpp

57 lines
1.9 KiB
C++

#include "watchdog.h"
#include "zpp/fmt.hpp"
#include <etl/vector.h>
#include <led_strip_indicator/led_strip_indicator.hpp>
#include <uart_com/protocal.hpp>
#include <zephyr/drivers/hwinfo.h>
#include <zephyr/drivers/sensor.h>
#include <zpp/device.hpp>
namespace {
auto sensor = DEVICE_DT_GET(DT_NODELABEL(godtek));
auto &pmc = ZPP_DRV_GET(uart_com::Protocal, 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 { kRunning, kPause, kError, kStandby };
} // namespace
volatile bool flag{false};
auto main(void) -> int {
pmc.SetRxCallback(kGetId, [](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));
});
pmc.SetRxCallback(kRunningState, [](uart_com::DataType data) -> void {
auto state = static_cast<StatusId>(data[0]);
led_strip.Status(state).on_error([](zpp::error_code code) {
zpp::println("Status Error: {}", zpp::error_str(code));
});
});
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("temp", data);
}
flag = false;
}
}
return 0;
}