#ifndef __THER_COM_HPP__ #define __THER_COM_HPP__ #include "led.hpp" #include #include #include #include #include #include #include #include #ifdef CONFIG_APP_DFU #include #endif namespace ther { class Com { public: enum HostStatus : uint8_t { STANDBY, RUNNING, PAUSE, ERROR }; using RunningStateSignal = etl::signal; static auto Init() -> zpp::error { s_proto->SetRxCallbackTable(kRxCallbackTable); auto size = hwinfo_get_device_id(buff, sizeof(buff)); s_id_buff = etl::span(buff, size); return zpp::ok(); } static auto SendTemp(sensor_value val) -> void { const uint8_t temp_data[] = {(uint8_t)val.val1, (uint8_t)val.val2}; s_proto->Send(R_TEMP, temp_data); } static auto AddRunningState(RunningStateSignal::slot_type slot) -> bool { return s_running_state_sig.connect(slot); } private: enum Addr : uint8_t { R_TEMP = 0, R_GET_ID, W_RUNNING_STATE, }; inline static RunningStateSignal s_running_state_sig{}; static auto RunningState(uart_com::DataType data) -> void { if (data.size() != 1) { return; } s_running_state_sig(HostStatus(data[0])); } static auto GetId(uart_com::DataType data) -> void { printk("handle get id"); s_proto->Send(R_GET_ID, uart_com::DataType(s_id_buff)); }; #ifdef CONFIG_APP_DFU /// 升级命令(0xFF):data[0]=0x01 进入升级模式,data[0]=0x02 查询状态 static auto DfuCommand(uart_com::DataType data) -> void { if (data.size() < 1) return; switch (data[0]) { case 0x01: printk("[com] entering DFU mode, rebooting...\n"); k_msleep(100); sys_reboot(SYS_REBOOT_COLD); break; case 0x02: { uint8_t status = 0x00; s_proto->Send(0xFF, uart_com::DataType(&status, 1)); break; } } } #endif constexpr static std::pair kRxCallbackTable[] = { {R_GET_ID, GetId}, {W_RUNNING_STATE, RunningState}, #ifdef CONFIG_APP_DFU {0xFF, DfuCommand}, #endif }; inline static auto s_proto = (uart_com::SimpleProtocal *)DEVICE_DT_GET( DT_COMPAT_GET_ANY_STATUS_OKAY(uart_com_simple_protocal)); inline static uint8_t buff[20]; inline static auto s_heating_state = false; inline static etl::span s_id_buff; }; } // namespace ther #endif