#ifndef __THER_COM_HPP__ #define __THER_COM_HPP__ #include "heating.hpp" #include "led.hpp" #include "ntc.hpp" #include #include #include #include #include #include namespace ther { class Com { public: static auto Init() -> zpp::error { s_proto->SetRxCallbackTable(kRxCallbackTable); return zpp::ok(); } static auto Send(sensor_value val) -> void { const uint8_t temp_data[] = {(uint8_t)val.val1, (uint8_t)val.val2}; s_proto->Send(TEMP, temp_data); } private: enum HostStatus : uint8_t { STANDBY, RUNNING, PAUSE, ERROR }; enum Addr : uint8_t { TEMP = 0, GET_ID, RUNNING_STATE, W_HEATING_STATE, R_HEATING_STATE }; enum DevAddr : uint8_t { R_TEMP_POLE_NTC = 100, R_TEMP_HETING_PAD_NTC = 101, }; using CbTableValueType = std::pair; struct Cb { static auto GetId(uart_com::DataType data) -> void { printk("handle get id"); uint8_t buff[20]; auto size = hwinfo_get_device_id(buff, sizeof(buff)); s_proto->Send(GET_ID, uart_com::DataType(buff, size)); } static auto RunningState(uart_com::DataType data) -> void { if (data.size() != 1) { return; } if (data[0] == RUNNING) { InfLed::On(); } else { InfLed::Off(); } printk("set ledsrtip to %d", data[0]); const uint8_t id = data[0]; s_led_strip_indicator->Status(id).on_error([](zpp::error_code err) { printk("err code: %s", zpp::error_str(err)); }); } static auto WHeatingState(uart_com::DataType data) -> void { if (data.size() != 1) { return; } const bool state = (data[0] > 0); if (state) { HeatingPad::Start(sensor_value{data[0], 0}); } else { HeatingPad::Stop(); } } static auto RHeatingState(uart_com::DataType data) -> void { if (data.size() != 0) { return; } const sensor_value temp = HeatingPad::CurrentTemp(); const uint8_t state[] = {s_heating_state, (uint8_t)temp.val1, (uint8_t)temp.val2}; s_proto->Send(R_HEATING_STATE, state); } /// Read one pole NTC by index (0~7). static auto ReadPoleNtcTemperature(uart_com::DataType data) -> void { if (data.size() != 1) { return; } const sensor_value temp = NtcGroup::GetSensorValue(data[0]); const uint8_t payload[] = {(uint8_t)temp.val1, (uint8_t)temp.val2}; s_proto->Send(R_TEMP_POLE_NTC, payload); } /// Read heating pad NTC (onboard ADC). static auto ReadHeatingPadNtcTemperature(uart_com::DataType data) -> void { const sensor_value temp = HeatingPad::CurrentTemp(); const uint8_t payload[] = {(uint8_t)temp.val1, (uint8_t)temp.val2}; s_proto->Send(R_TEMP_HETING_PAD_NTC, payload); } }; constexpr static std::pair kRxCallbackTable[] = { {GET_ID, Cb::GetId}, {RUNNING_STATE, Cb::RunningState}, {W_HEATING_STATE, Cb::WHeatingState}, {R_HEATING_STATE, Cb::RHeatingState}, // Pole NTC (MCP3208) {R_TEMP_POLE_NTC, Cb::ReadPoleNtcTemperature}, // Heating pad NTC (ADC1) {R_TEMP_HETING_PAD_NTC, Cb::ReadHeatingPadNtcTemperature}, }; inline static auto s_led_strip_indicator = ZPP_DRV_GET_P(ledstrip::Indicator, DT_NODELABEL(indicator)); inline static auto s_proto = (uart_com::SimpleProtocal *)(DEVICE_DT_GET(DT_NODELABEL(pm_protocal))); inline static auto s_heating_state = false; using InfLed = ther::Led; }; } // namespace ther #endif