diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index cf9e0be..ab6f426 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/app_protocols b/app_protocols index 94d89e0..38674ec 160000 --- a/app_protocols +++ b/app_protocols @@ -1 +1 @@ -Subproject commit 94d89e0e6d4a76e4eb434557974187e73dced518 +Subproject commit 38674eceb0655cfa98679886e95bd1e4f9dfc89d diff --git a/stm32basic b/stm32basic index a3f7743..b2fbccd 160000 --- a/stm32basic +++ b/stm32basic @@ -1 +1 @@ -Subproject commit a3f7743a964044334b57b0ca786b69f76f6e4af6 +Subproject commit b2fbccdc9ddd701095912425f8f6f3eaaa47166a diff --git a/ucomponents/zcan/zcan.cpp b/ucomponents/zcan/zcan.cpp index c5f75f0..3a53da7 100644 --- a/ucomponents/zcan/zcan.cpp +++ b/ucomponents/zcan/zcan.cpp @@ -125,6 +125,9 @@ HAL_StatusTypeDef ZCAN1::activateRxIT() { } void ZCAN1::init() { + if (inited) return; + + inited = true; can1_init(); HAL_StatusTypeDef hal_status; m_lock.init(); @@ -192,16 +195,19 @@ bool ZCAN1::txMsg(const uint32_t extid, const uint8_t txdata[], uint32_t txdatal ZLOGE(TAG, "HAL_CAN_AddTxMessage fail"); return false; } - + int i = 1; while (HAL_CAN_IsTxMessagePending(&hcan1, txMailBox)) { if (zos_haspassedms(enterticket) > (uint32_t)overtime) { ZLOGE(TAG, "HAL_CAN_IsTxMessagePending overtime"); HAL_CAN_AbortTxRequest(&hcan1, txMailBox); return false; } + osDelay(i); + i++; } return true; } +void ZCAN1::regOnCanMessage(function onmessage) { onZcanRx = onmessage; } bool ZCAN1::getRxMsg(zcanrx_t *rx) { /** @@ -223,7 +229,6 @@ bool ZCAN1::getRxMsg(zcanrx_t *rx) { } return false; } -void ZCAN1::regOnCanMessagePending(function fn) {} static inline const char *zhex2str(uint8_t *data, uint32_t len) { static char str[256]; @@ -249,6 +254,5 @@ void ZCAN1::STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *canHandle) { break; } } - } void ZCAN1::STM32_HAL_onCAN_Error(CAN_HandleTypeDef *canHandle) { ZLOGE(TAG, "onCAN_Error\r\n"); } diff --git a/ucomponents/zcan/zcan.hpp b/ucomponents/zcan/zcan.hpp index c484423..f79afd8 100644 --- a/ucomponents/zcan/zcan.hpp +++ b/ucomponents/zcan/zcan.hpp @@ -19,7 +19,8 @@ class ZCAN1 { ZQueue rxQueue; - function onZcanRx; + function onZcanRx; + bool inited = false; public: ZCAN1() {} @@ -32,7 +33,7 @@ class ZCAN1 { void init(); bool txMsg(const uint32_t extid, const uint8_t txdata[], uint32_t txdatalen, int32_t overtime); bool getRxMsg(zcanrx_t *rx); - void regOnCanMessagePending(function fn); + void regOnCanMessage(function onmessage); public: void STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *can); diff --git a/usrc/service/app_core.cpp b/usrc/service/app_core.cpp index 6728257..ee22cb0 100644 --- a/usrc/service/app_core.cpp +++ b/usrc/service/app_core.cpp @@ -7,6 +7,7 @@ #include "service/pump_ctrl_service.hpp" #include "service/remote_controler.hpp" #include "service/remote_controler_state_sync_service.hpp" +#include "valve_state_ctrl_service.hpp" // #include "service/page/page.hpp" #include "ucomponents/zcan/zcan.hpp" @@ -126,11 +127,13 @@ void AppCore::appsetup() { ***********************************************************************************************************************/ AppEventBus::ins()->initialize(); ConfigService::ins()->initialize(); + ZCAN1::ins()->init(); // hardInit AppHardware::ins()->initialize(); // 基础硬件初始化 RemoteControlerUpper::ins()->initialize(); // 遥控器初始化 FrontEndControler::ins()->initialize(); // 前端控制器,对屏幕的消息进行解析,发送消息给屏幕 + ValveStateSyncService::ins()->initialize(ZCAN1::ins()); // BaseServiceInit PUMPCS->initialize(); @@ -164,12 +167,11 @@ void AppCore::appsetup() { FrontEndControler::ins()->startSchedule(); RCTRL->startSchedule(); RemoteControlerStateSyncService::ins()->startSync(); + ValveStateSyncService::ins()->startSync(); // dim UIS->chpage(pg_login); - ZCAN1::ins()->init(); - osDelay(100); for (size_t i = 0;; i++) { if (i != 0) ZLOGI(TAG, "re start scan %s %d", "AciDisp00000", i); @@ -204,13 +206,6 @@ void AppCore::initialize() { osDelay(30); debugLightLoop(); - - valve_ctrl_protocol_t valve_ctrl_msg; - valve_ctrl_msg.valvestate0 = 1; - valve_ctrl_msg.valvestate1 = 2; - valve_ctrl_msg.valvestate2 = 3; - valve_ctrl_msg.valvestate3 = 4; - ZCAN1::ins()->txMsg(kvalve_ctrl_msg, (uint8_t*)&valve_ctrl_msg, sizeof(valve_ctrl_msg), 30); // HAL_IWDG_Refresh(&hiwdg); } } diff --git a/usrc/service/valve_state_ctrl_service.cpp b/usrc/service/valve_state_ctrl_service.cpp new file mode 100644 index 0000000..822512b --- /dev/null +++ b/usrc/service/valve_state_ctrl_service.cpp @@ -0,0 +1,62 @@ +#include "valve_state_ctrl_service.hpp" + +#include "app_protocols\app_protocols.h" + +using namespace iflytop; +#define TAG "ValveStateSyncService" +void ValveStateSyncService::initialize(ZCAN1* can) { + m_thread.init("ValveStateSyncService-Thread"); + this->can = can; + ZCAN1::ins()->init(); + lock.init(); +} + +void ValveStateSyncService::startSync() { + m_thread.start([this]() { + while (true) { + m_thread.sleep(300); + { + zlock_guard l(lock); + + valve_ctrl_msg_t msg = {0}; + msg.output0 = output0state; + msg.output1 = output1state; + msg.rgbw = rgbwstate; + ZCAN1::ins()->txMsg(kvalve_ctrl_msg, (uint8_t*)&msg, sizeof(msg), 30); + } + } + }); +} + +void ValveStateSyncService::setValveState(int valveIndex, bool state) { + zlock_guard l(lock); + + if (valveIndex >= 4) { + ZLOGW(TAG, "valveIndex %d out of range", valveIndex); + return; + } + output0state = (output0state & ~(1 << valveIndex)) | (state << valveIndex); + forceupdate = true; + m_thread.wake(); +} + +#define SETBIT(byte, off, valve) (byte = (byte & ~(1 << off)) | (valve << off)) + +void ValveStateSyncService::setRGBState(bool r, bool g, bool b) { + zlock_guard l(lock); + // bit0:r + // bit1:g + // bit2:b + SETBIT(rgbwstate, 0, r); + SETBIT(rgbwstate, 1, g); + SETBIT(rgbwstate, 2, b); + forceupdate = true; + m_thread.wake(); +} +void ValveStateSyncService::setWarningState(bool warning) { + zlock_guard l(lock); + + SETBIT(rgbwstate, 3, warning); + forceupdate = true; + m_thread.wake(); +} diff --git a/usrc/service/valve_state_ctrl_service.hpp b/usrc/service/valve_state_ctrl_service.hpp new file mode 100644 index 0000000..e281116 --- /dev/null +++ b/usrc/service/valve_state_ctrl_service.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "apphardware/apphardware.hpp" +#include "uappbase/base.hpp" +#include "ucomponents/zcan/zcan.hpp" + +namespace iflytop { +using namespace std; +class ValveStateSyncService { + private: + /* data */ + ZThread m_thread; + ZCAN1* can; + + uint8_t output0state = 0; + uint8_t output1state = 0; + uint8_t rgbwstate = 0; + + bool forceupdate = false; + zmutex lock = {"ValveStateSyncServiceLock"}; + + public: + static ValveStateSyncService* ins() { + static ValveStateSyncService instance; + return &instance; + } + + void initialize(ZCAN1* can); + void startSync(); + + void setValveState(int valveIndex, bool state); + void setRGBState(bool r, bool g, bool b); + void setWarningState(bool warning); + + public: +}; +} // namespace iflytop \ No newline at end of file