From 9d32418aedcf96cee173576529e7fbd845f21ede Mon Sep 17 00:00:00 2001 From: zhaohe Date: Wed, 13 Nov 2024 13:34:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84config=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .settings/language.settings.xml | 4 +- .vscode/c_cpp_properties.json | 1 - uappbase/base.hpp | 2 - uappbase/service/config_index.cpp | 156 -------------- uappbase/service/config_index.hpp | 30 --- uappbase/service/config_service.cpp | 204 ------------------- uappbase/service/config_service.hpp | 65 ------ uappbase/service/config_service_utils.cpp | 223 --------------------- uappbase/service/config_service_utils.hpp | 21 -- usrc/config/config.hpp | 4 + usrc/config/config_index.cpp | 156 ++++++++++++++ usrc/config/config_index.hpp | 26 +++ usrc/config/config_service.cpp | 204 +++++++++++++++++++ usrc/config/config_service.hpp | 63 ++++++ usrc/config/config_service_utils.cpp | 223 +++++++++++++++++++++ usrc/config/config_service_utils.hpp | 21 ++ usrc/service/app_core.hpp | 2 +- usrc/service/front_end_controler.hpp | 2 +- usrc/service/page/page_processer.hpp | 2 +- usrc/service/page/utils/page_bak_router.hpp | 2 +- usrc/service/pump_ctrl_service.hpp | 1 + usrc/service/remote_controler.hpp | 1 + usrc/service/remote_controler_event_processer.hpp | 1 + .../remote_controler_state_sync_service.hpp | 1 + usrc/service/valve_state_ctrl_service.hpp | 2 +- 25 files changed, 708 insertions(+), 709 deletions(-) delete mode 100644 uappbase/service/config_index.cpp delete mode 100644 uappbase/service/config_index.hpp delete mode 100644 uappbase/service/config_service.cpp delete mode 100644 uappbase/service/config_service.hpp delete mode 100644 uappbase/service/config_service_utils.cpp delete mode 100644 uappbase/service/config_service_utils.hpp create mode 100644 usrc/config/config.hpp create mode 100644 usrc/config/config_index.cpp create mode 100644 usrc/config/config_index.hpp create mode 100644 usrc/config/config_service.cpp create mode 100644 usrc/config/config_service.hpp create mode 100644 usrc/config/config_service_utils.cpp create mode 100644 usrc/config/config_service_utils.hpp diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 5a79ae1..1eb30e0 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 8909387..1a9f159 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -12,7 +12,6 @@ "./Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F", "./Drivers/CMSIS/Device/ST/STM32F4xx/Include", "./Drivers/CMSIS/Include", - "C:/ST/STM32CubeIDE_1.13.0/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.11.3.rel1.win32_1.1.0.202305231506/tools/arm-none-eabi/include", "./usrc/", "./" ], diff --git a/uappbase/base.hpp b/uappbase/base.hpp index b60d096..47cbb07 100644 --- a/uappbase/base.hpp +++ b/uappbase/base.hpp @@ -6,7 +6,5 @@ #include "service/app_core.hpp" #include "service/app_event_bus.hpp" #include "service/gstate_mgr.hpp" -#include "service/config_service.hpp" -#include "service/config_service_utils.hpp" // #include "apphal/apphal.hpp" diff --git a/uappbase/service/config_index.cpp b/uappbase/service/config_index.cpp deleted file mode 100644 index bbb5047..0000000 --- a/uappbase/service/config_index.cpp +++ /dev/null @@ -1,156 +0,0 @@ -#include "config_index.hpp" -namespace iflytop { - -#define CFG_ITERM(index, type, initval) {index, #index, type, initval} - -static cfg_iterm_info_t cfg_iterm_infos[] = { - - /*********************************************************************************************************************** - * 用户管理 * - ***********************************************************************************************************************/ - CFG_ITERM(kusr_name0, kcfgt_str, USER0_DEFAULT_NAME), // - CFG_ITERM(kusr_enable0, kcfgt_bool, "true"), // 1:使能,0失能 - CFG_ITERM(kusr_passwd0, kcfgt_str, USER_DEFAULT_PWD), // - - CFG_ITERM(kusr_name1, kcfgt_str, USER1_DEFAULT_NAME), // - CFG_ITERM(kusr_enable1, kcfgt_bool, "true"), // - CFG_ITERM(kusr_passwd1, kcfgt_str, USER_DEFAULT_PWD), // - - CFG_ITERM(kusr_name2, kcfgt_str, USER2_DEFAULT_NAME), // - CFG_ITERM(kusr_enable2, kcfgt_bool, "true"), // - CFG_ITERM(kusr_passwd2, kcfgt_str, USER_DEFAULT_PWD), // - - CFG_ITERM(kusr_name3, kcfgt_str, USER3_DEFAULT_NAME), // - CFG_ITERM(kusr_enable3, kcfgt_bool, "false"), // - CFG_ITERM(kusr_passwd3, kcfgt_str, USER_DEFAULT_PWD), // - - CFG_ITERM(kusr_name4, kcfgt_str, USER4_DEFAULT_NAME), // - CFG_ITERM(kusr_enable4, kcfgt_bool, "false"), // - CFG_ITERM(kusr_passwd4, kcfgt_str, USER_DEFAULT_PWD), // - - CFG_ITERM(kusr_name5, kcfgt_str, USER5_DEFAULT_NAME), // - CFG_ITERM(kusr_enable5, kcfgt_bool, "false"), // - CFG_ITERM(kusr_passwd5, kcfgt_str, USER_DEFAULT_PWD), // - - /*********************************************************************************************************************** - * 酸液预设名称 * - ***********************************************************************************************************************/ - CFG_ITERM(kcfg_acidName1, kcfgt_str, ACID_DEFAULT_NAME1), // - CFG_ITERM(kcfg_acidName2, kcfgt_str, ACID_DEFAULT_NAME2), // - CFG_ITERM(kcfg_acidName3, kcfgt_str, ACID_DEFAULT_NAME3), // - CFG_ITERM(kcfg_acidName4, kcfgt_str, ACID_DEFAULT_NAME4), // - CFG_ITERM(kcfg_acidName5, kcfgt_str, ACID_DEFAULT_NAME5), // - CFG_ITERM(kcfg_acidName6, kcfgt_str, ACID_DEFAULT_NAME6), // - CFG_ITERM(kcfg_acidName7, kcfgt_str, ACID_DEFAULT_NAME7), // - CFG_ITERM(kcfg_acidName8, kcfgt_str, ACID_DEFAULT_NAME8), // - CFG_ITERM(kcfg_acidName9, kcfgt_str, ACID_DEFAULT_NAME9), // - CFG_ITERM(kcfg_acidName10, kcfgt_str, ACID_DEFAULT_NAME10), // - CFG_ITERM(kcfg_acidName11, kcfgt_str, ACID_DEFAULT_NAME11), // - CFG_ITERM(kcfg_acidName12, kcfgt_str, ACID_DEFAULT_NAME12), // - CFG_ITERM(kcfg_acidName13, kcfgt_str, ACID_DEFAULT_NAME13), // - CFG_ITERM(kcfg_acidName14, kcfgt_str, ACID_DEFAULT_NAME14), // - CFG_ITERM(kcfg_acidName15, kcfgt_str, ACID_DEFAULT_NAME15), // - - /*********************************************************************************************************************** - * 酸液通道选择名称 * - ***********************************************************************************************************************/ - CFG_ITERM(kcfg_acidChooseName0, kcfgt_str, "未设置"), // - CFG_ITERM(kcfg_acidChooseName1, kcfgt_str, "未设置"), // - CFG_ITERM(kcfg_acidChooseName2, kcfgt_str, "未设置"), // - CFG_ITERM(kcfg_acidChooseName3, kcfgt_str, "未设置"), // - - /*********************************************************************************************************************** - * 酸液通道每次分配的数值 * - ***********************************************************************************************************************/ - CFG_ITERM(kcfg_acideval0, kcfgt_float, "10.0"), // - CFG_ITERM(kcfg_acideval1, kcfgt_float, "10.0"), // - CFG_ITERM(kcfg_acideval2, kcfgt_float, "10.0"), // - CFG_ITERM(kcfg_acideval3, kcfgt_float, "10.0"), // - - /*********************************************************************************************************************** - * 电机默认配置 * - ***********************************************************************************************************************/ - /* CFG_ITERM(kcfg_mLPR0, kcfgt_float, "0.6900"), // - CFG_ITERM(kcfg_mLPR1, kcfgt_float, "0.6900"), // - CFG_ITERM(kcfg_mLPR2, kcfgt_float, "0.1350"), // - CFG_ITERM(kcfg_mLPR3, kcfgt_float, "0.1350"), // */ - CFG_ITERM(kcfg_mLPR1, kcfgt_float, "1.0"), // - CFG_ITERM(kcfg_mLPR0, kcfgt_float, "1.0"), // - CFG_ITERM(kcfg_mLPR2, kcfgt_float, "1.0"), // - CFG_ITERM(kcfg_mLPR3, kcfgt_float, "1.0"), // - - /*********************************************************************************************************************** - * 其他配置 * - ***********************************************************************************************************************/ - CFG_ITERM(kcfg_lockAcidName, kcfgt_bool, "false"), // - CFG_ITERM(kcfg_echDitrUpLi, kcfgt_int32, "99"), // 每次分配上限设定值 each distri up-limit - CFG_ITERM(kcfg_distrInterval, kcfgt_float, "1.5"), // 每次分配间隔时间 - // CFG_ITERM(kcfg_pumpDefVel, kcfgt_int32, PUMPDEFVEL), // - - CFG_ITERM(kcfg_pumpDefVel0, kcfgt_int32, PUMPDEFVEL), // - CFG_ITERM(kcfg_pumpDefVel1, kcfgt_int32, PUMPDEFVEL), // - CFG_ITERM(kcfg_pumpDefVel2, kcfgt_int32, PUMPDEFVEL), // - CFG_ITERM(kcfg_pumpDefVel3, kcfgt_int32, PUMPDEFVEL), // - - /*********************************************************************************************************************** - * 回流参数配置 * - ***********************************************************************************************************************/ - /* CFG_ITERM(kcfg_pipeLengthML0, kcfgt_float, "14"), // - CFG_ITERM(kcfg_pipeLengthML1, kcfgt_float, "14"), // - CFG_ITERM(kcfg_pipeLengthML2, kcfgt_float, "7"), // - CFG_ITERM(kcfg_pipeLengthML3, kcfgt_float, "7"), // */ - - CFG_ITERM(kcfg_pipeLengthML0, kcfgt_float, "35.5"), // - CFG_ITERM(kcfg_pipeLengthML1, kcfgt_float, "35.5"), // - CFG_ITERM(kcfg_pipeLengthML2, kcfgt_float, "35.5"), // - CFG_ITERM(kcfg_pipeLengthML3, kcfgt_float, "35.5"), // - // - - CFG_ITERM(kcfg_m0irun, kcfgt_int32, "22"), // - CFG_ITERM(kcfg_m1irun, kcfgt_int32, "22"), // - CFG_ITERM(kcfg_m2irun, kcfgt_int32, "22"), // - CFG_ITERM(kcfg_m3irun, kcfgt_int32, "22"), // - - CFG_ITERM(kcfg_bleClientName, kcfgt_str, " "), - -}; - -const char *cfgName(int32_t index) { - for (int i = 0; i < CFG_MAX_INDEX; i++) { - if (cfg_iterm_infos[i].index == index) { - return cfg_iterm_infos[i].itermName; - } - } - ZASSERT(0); - return nullptr; -} -cfg_iterm_type_t cfgType(int32_t index) { - for (int i = 0; i < CFG_MAX_INDEX; i++) { - if (cfg_iterm_infos[i].index == index) { - return cfg_iterm_infos[i].type; - } - } - return kcfgt_float; -} -const char *cfgInitVal(int32_t index) { - for (int i = 0; i < CFG_MAX_INDEX; i++) { - if (cfg_iterm_infos[i].index == index) { - return cfg_iterm_infos[i].initval; - } - } - ZASSERT(0); - - return nullptr; -} - -cfg_iterm_info_t *cfgInfo(int32_t index) { - for (int i = 0; i < CFG_MAX_INDEX; i++) { - if (cfg_iterm_infos[i].index == index) { - return &cfg_iterm_infos[i]; - } - } - ZASSERT(0); - return nullptr; -} - -} // namespace iflytop \ No newline at end of file diff --git a/uappbase/service/config_index.hpp b/uappbase/service/config_index.hpp deleted file mode 100644 index fc82402..0000000 --- a/uappbase/service/config_index.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include "../appdep.hpp" -#include "uappbase/appcfg/appcfg.hpp" -#include "uappbase/bean/bean.hpp" -#include "uappbase/bean/config_index_enum.hpp" - -namespace iflytop { -typedef enum { - kcfgt_float, - kcfgt_int32, - kcfgt_str, - kcfgt_bool, -} cfg_iterm_type_t; - -typedef struct { - int32_t index; - const char *itermName; - cfg_iterm_type_t type; - const char *initval; -} cfg_iterm_info_t; - -#define CFG_MAX_INDEX 61 - -const char *cfgName(int32_t index); -cfg_iterm_type_t cfgType(int32_t index); -const char *cfgInitVal(int32_t index); - -cfg_iterm_info_t *cfgInfo(int32_t index); - -} // namespace iflytop \ No newline at end of file diff --git a/uappbase/service/config_service.cpp b/uappbase/service/config_service.cpp deleted file mode 100644 index 02c74b0..0000000 --- a/uappbase/service/config_service.cpp +++ /dev/null @@ -1,204 +0,0 @@ -#include "config_service.hpp" - -#include "uappbase\service\app_event_bus.hpp" -// - -#define CFG_HEADER 0x12345670 -/** - * @brief CFG内存使用 - * - * 4byte begin - * - * cfg - * cfg - * cfg - * cfg - * - * 4byte checksum - * 4byte end - * - */ - -using namespace iflytop; -#define TAG "ConfigService" - -#pragma pack(1) - -typedef struct { - union { - uint8_t str[CONTENT_MAX_LEN]; // 必须4字节对齐 - uint32_t u32s[CONTENT_MAX_LEN / 4]; - }; -} cfg_content_t; - -typedef struct { - int32_t begin; - int32_t checksum; - cfg_content_t cfg[CFG_MAX_INDEX]; -} config_data_t; - -#pragma pack() -#define TO_EEPROM_ADD(index) (CONFIG_EEPROM_ADD + 4 + 4 + sizeof(cfg_content_t) * index) -#define SET_CFG_INT(index, value) cfgcache.cfg[index].int32 = value -#define SET_CFG_STR(index, value) memcpy(cfgcache.cfg[index].str, value, strlen(value)) -#define CHECKSUM() checksum((uint8_t *)&(cfgcache.cfg[0].str[0]), sizeof(cfgcache) - 8) - -static config_data_t cfgcache; - -static int32_t checksum(uint8_t *data, int32_t n) { - int32_t sum = 0; - for (int i = 0; i < n; i++) { - sum += data[i]; - } - return sum; -} - -bool ConfigService::initialize() { - mutex.init(); - - AppHal::MX_I2C1_Init(); - eeprom.initialize(&hi2c1); - if (!eeprom.isOnline()) { - return false; - } - - static_assert(kcfg_max == CFG_MAX_INDEX, ""); - if (!checkcfg()) { - ZLOGI(TAG, "cfg check fail,init cfg"); - - cfgcache.begin = CFG_HEADER; - - for (int i = 0; i < kcfg_max; i++) { - cfg_iterm_info_t *info = cfgInfo((config_index_t)i); - _setcfg(i, info->initval); - } - - cfgcache.checksum = CHECKSUM(); - eeprom.write(CONFIG_EEPROM_ADD, (uint8_t *)&cfgcache, sizeof(cfgcache)); - flashall(); - } - bool suc = checkcfg(); - dumpcfgs(); - ZLOGI(TAG, "cfg init %s", suc ? "ok" : "fail"); - - return suc; -} - -void ConfigService::setcfgAndFlush(config_index_t index, const char *val) { - zlock_guard lg(mutex); - _setcfgAndFlush(index, val); -} -void ConfigService::setcfgAndFlush(config_index_t index, int32_t val) { - zlock_guard lg(mutex); - char buf[CONTENT_MAX_LEN]; - snprintf(buf, CONTENT_MAX_LEN, "%d", val); - - _setcfgAndFlush(index, buf); -} -void ConfigService::setcfgAndFlush(config_index_t index, float val) { - zlock_guard lg(mutex); - char buf[CONTENT_MAX_LEN]; - snprintf(buf, CONTENT_MAX_LEN, "%.3f", val); - - _setcfgAndFlush(index, buf); -} -void ConfigService::setcfgAndFlush(config_index_t index, bool val) { - zlock_guard lg(mutex); - _setcfgAndFlush(index, val ? "true" : "false"); -} - -void ConfigService::setcfg(config_index_t index, const char *val) { - zlock_guard lg(mutex); - _setcfg(index, val); -} -void ConfigService::setcfg(config_index_t index, int32_t val) { - zlock_guard lg(mutex); - char buf[CONTENT_MAX_LEN]; - snprintf(buf, CONTENT_MAX_LEN, "%d", val); - - _setcfg(index, buf); -} -void ConfigService::setcfg(config_index_t index, float val) { - zlock_guard lg(mutex); - char buf[CONTENT_MAX_LEN]; - snprintf(buf, CONTENT_MAX_LEN, "%.2f", val); - - _setcfg(index, buf); -} -void ConfigService::setcfg(config_index_t index, bool val) { - zlock_guard lg(mutex); - _setcfg(index, val ? "true" : "false"); -} - -const char *ConfigService::getStr(config_index_t index, CfgItermCache *cache) { - zlock_guard lg(mutex); - return _getcfg(index, cache); -} -int32_t ConfigService::getInt(config_index_t index) { - zlock_guard lg(mutex); - return atoi(_getcfg(index, nullptr)); -} -float ConfigService::getFloat(config_index_t index) { - zlock_guard lg(mutex); - return atof(_getcfg(index, nullptr)); -} -bool ConfigService::getBool(config_index_t index) { - zlock_guard lg(mutex); - return strcmp(_getcfg(index, nullptr), "true") == 0; -} - -/*********************************************************************************************************************** - * BASIC * - ***********************************************************************************************************************/ -void ConfigService::dumpcfgs() { - for (int i = 0; i < kcfg_max; i++) { - ZLOGI(TAG, "cfg %s:%s", cfgName((config_index_t)i), _getcfg((config_index_t)i, nullptr)); - } -} - -void ConfigService::_setcfg(int32_t index, const char *value) { - ZLOGI(TAG, "setcfg %s:%s", cfgName(index), value); - - memset(cfgcache.cfg[index].str, 0, 32); - strncpy((char *)cfgcache.cfg[index].str, value, 31); - cfgcache.checksum = CHECKSUM(); - - AppEventBus::ins()->pushEvent(createConfigChangeEvent((config_index_t)index)); - return; -} -void ConfigService::_setcfgAndFlush(int32_t index, const char *value) { - _setcfg(index, value); - flashIndex(index); - return; -} -const char *ConfigService::_getcfg(config_index_t index, CfgItermCache *cache) { - static CfgItermCache _cache; - if (cache == nullptr) { - cache = &_cache; - } - memset(cache->buf, 0, CONTENT_MAX_LEN); - strncpy(cache->buf, (const char *)cfgcache.cfg[index].str, CONTENT_MAX_LEN - 1); - return cache->buf; -} - -bool ConfigService::checkcfg() { - eeprom.read(CONFIG_EEPROM_ADD, (uint8_t *)&cfgcache, sizeof(cfgcache)); - - int32_t checksumval = CHECKSUM(); - - if (cfgcache.begin != CFG_HEADER || cfgcache.checksum != checksumval) { - ZLOGI(TAG, "cfg check fail,b:0x%08x,c:0x%08x!=0x%08x", cfgcache.begin, cfgcache.checksum, checksumval); - return false; - } - return true; -} -bool ConfigService::flashIndex(int32_t index) { - // for (int i = 0; i < sizeof(cfg_content_t); i += 4) { - // uint32_t wdata = cfgcache.cfg[index].u32s[i / 4]; - // eeprom.write32(TO_EEPROM_ADD(index) + i, wdata); - // } - eeprom.write(TO_EEPROM_ADD(index), (uint8_t *)&cfgcache.cfg[index], sizeof(cfg_content_t)); - eeprom.write32(CONFIG_EEPROM_ADD + 4 * 1, (uint32_t)cfgcache.checksum); - return true; -} -bool ConfigService::flashall() { eeprom.write(CONFIG_EEPROM_ADD, (uint8_t *)&cfgcache, sizeof(cfgcache)); } diff --git a/uappbase/service/config_service.hpp b/uappbase/service/config_service.hpp deleted file mode 100644 index 84a87c8..0000000 --- a/uappbase/service/config_service.hpp +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once -#include "../appdep.hpp" -#include "config_index.hpp" -#include "uappbase/appcfg/appcfg.hpp" -#include "uappbase/apphal/apphal.hpp" -#include "uappbase/bean/bean.hpp" -#include "ucomponents/eeprom/m24m02_i2c_eeprom.hpp" - -namespace iflytop { -using namespace std; -#define CS ConfigService::ins() - -#define CONFIG_EEPROM_ADD 0 -#define CONTENT_MAX_LEN 32 - -struct CfgItermCache { - char buf[CONTENT_MAX_LEN]; -}; - -class ConfigService { - private: - /* data */ - zmutex mutex = {"ConfigServiceMutex"}; - M24M02_I2C_EEPROM eeprom; - - public: - static ConfigService* ins() { - static ConfigService instance; - return &instance; - } - - bool initialize(); - - void setcfgAndFlush(config_index_t index, const char* val); - void setcfgAndFlush(config_index_t index, int32_t val); - void setcfgAndFlush(config_index_t index, float val); - void setcfgAndFlush(config_index_t index, bool val); - - void setcfg(config_index_t index, const char* val); - void setcfg(config_index_t index, int32_t val); - void setcfg(config_index_t index, float val); - void setcfg(config_index_t index, bool val); - - const char* getStr(config_index_t index, CfgItermCache* cache); - int32_t getInt(config_index_t index); - float getFloat(config_index_t index); - bool getBool(config_index_t index); - - private: - void _setcfg(int32_t index, const char* value); - void _setcfgAndFlush(int32_t index, const char* value); - const char* _getcfg(config_index_t index, CfgItermCache* cache); - - bool checkcfg(); - bool flashIndex(int32_t index); - bool flashall(); - void dumpcfgs(); -}; - -#define getCfgStr(index) CS->getStr(index, &cfgcache) -#define getCfgInt(index) CS->getInt(index) -#define getCfgFloat(index) CS->getFloat(index) -#define getCfgBool(index) CS->getBool(index) - -} // namespace iflytop \ No newline at end of file diff --git a/uappbase/service/config_service_utils.cpp b/uappbase/service/config_service_utils.cpp deleted file mode 100644 index 4b82597..0000000 --- a/uappbase/service/config_service_utils.cpp +++ /dev/null @@ -1,223 +0,0 @@ -#include "config_service_utils.hpp" -namespace iflytop { - -float getPumpAppendMl(int chid) { - switch (chid) { - case 0: - return CH0APPENDML; // - case 1: - return CH1APPENDML; - case 2: - return CH2APPENDML; - case 3: - return CH3APPENDML; - default: - ZASSERT(0); - return 0; - } -} - -float getCfgPipeLengthML(int chid) { - switch (chid) { - case 0: - return getCfgFloat(kcfg_pipeLengthML0); - case 1: - return getCfgFloat(kcfg_pipeLengthML1); - case 2: - return getCfgFloat(kcfg_pipeLengthML2); - case 3: - return getCfgFloat(kcfg_pipeLengthML3); - default: - ZASSERT(0); - return 0; - } -} -void setCfgPipeLengthML(int chid, float ml) { - switch (chid) { - case 0: - CS->setcfgAndFlush(kcfg_pipeLengthML0, ml); - break; - case 1: - CS->setcfgAndFlush(kcfg_pipeLengthML1, ml); - break; - case 2: - CS->setcfgAndFlush(kcfg_pipeLengthML2, ml); - break; - case 3: - CS->setcfgAndFlush(kcfg_pipeLengthML3, ml); - break; - default: - ZASSERT(0); - } -} - -int getMxRunRPM(int32_t mid) { // - float rpm = 0; - if (mid == 0) { - rpm = getCfgFloat(kcfg_pumpDefVel0); - } else if (mid == 1) { - rpm = getCfgFloat(kcfg_pumpDefVel1); - } else if (mid == 2) { - rpm = getCfgFloat(kcfg_pumpDefVel2); - } else if (mid == 3) { - rpm = getCfgFloat(kcfg_pumpDefVel3); - } - if (rpm < 10) { - rpm = 10; - } - return rpm; -} - -int setMxRunRPM(int32_t mid, int32_t val) { - switch (mid) { - case 0: - CS->setcfgAndFlush(kcfg_pumpDefVel0, val); - break; - case 1: - CS->setcfgAndFlush(kcfg_pumpDefVel1, val); - break; - case 2: - CS->setcfgAndFlush(kcfg_pumpDefVel2, val); - break; - case 3: - CS->setcfgAndFlush(kcfg_pumpDefVel3, val); - break; - default: - ZASSERT(0); - return 0; - } - return 0; -} - -float getMotorMLPR(int32_t motorid) { - switch (motorid) { - case 0: - return getCfgFloat(kcfg_mLPR0); - case 1: - return getCfgFloat(kcfg_mLPR1); - case 2: - return getCfgFloat(kcfg_mLPR2); - case 3: - return getCfgFloat(kcfg_mLPR3); - default: - ZASSERT(0); - return 0; - } -} -void setMotorMLPR(int32_t motorid, float val) { - // - switch (motorid) { - case 0: - CS->setcfgAndFlush(kcfg_mLPR0, val); - break; - case 1: - CS->setcfgAndFlush(kcfg_mLPR1, val); - break; - case 2: - CS->setcfgAndFlush(kcfg_mLPR2, val); - break; - case 3: - CS->setcfgAndFlush(kcfg_mLPR3, val); - break; - default: - ZASSERT(0); - } -} - -int32_t getMxIRun(int motorid) { - switch (motorid) { - case 0: - return getCfgInt(kcfg_m0irun); - case 1: - return getCfgInt(kcfg_m1irun); - case 2: - return getCfgInt(kcfg_m2irun); - case 3: - return getCfgInt(kcfg_m3irun); - default: - ZASSERT(0); - return 0; - } -} - -int32_t setMxIRun(int motorid, int32_t val) { - switch (motorid) { - case 0: - CS->setcfgAndFlush(kcfg_m0irun, val); - break; - case 1: - CS->setcfgAndFlush(kcfg_m1irun, val); - break; - case 2: - CS->setcfgAndFlush(kcfg_m2irun, val); - break; - case 3: - CS->setcfgAndFlush(kcfg_m3irun, val); - break; - default: - ZASSERT(0); - return 0; - } - return 0; -} - -const char* getPasswd(const char* usrname, CfgItermCache* cache) { - static CfgItermCache cfgcache; - memset(cache, 0, sizeof(CfgItermCache)); - if (strcmp(usrname, getCfgStr(kusr_name0)) == 0) { - getCfgStr(kusr_passwd0); - memcpy(cache, &cfgcache, sizeof(CfgItermCache)); - return cache->buf; - } - if (strcmp(usrname, getCfgStr(kusr_name1)) == 0) { - getCfgStr(kusr_passwd1); - memcpy(cache, &cfgcache, sizeof(CfgItermCache)); - return cache->buf; - } - if (strcmp(usrname, getCfgStr(kusr_name2)) == 0) { - getCfgStr(kusr_passwd2); - memcpy(cache, &cfgcache, sizeof(CfgItermCache)); - return cache->buf; - } - if (strcmp(usrname, getCfgStr(kusr_name3)) == 0) { - getCfgStr(kusr_passwd3); - memcpy(cache, &cfgcache, sizeof(CfgItermCache)); - return cache->buf; - } - if (strcmp(usrname, getCfgStr(kusr_name4)) == 0) { - getCfgStr(kusr_passwd4); - memcpy(cache, &cfgcache, sizeof(CfgItermCache)); - return cache->buf; - } - if (strcmp(usrname, getCfgStr(kusr_name5)) == 0) { - getCfgStr(kusr_passwd5); - memcpy(cache, &cfgcache, sizeof(CfgItermCache)); - return cache->buf; - } - - return NULL; -} - -const void setPasswd(const char* usrname, const char* passwd) { - static CfgItermCache cfgcache; - if (strcmp(usrname, getCfgStr(kusr_name0)) == 0) { - CS->setcfgAndFlush(kusr_passwd0, passwd); - } - if (strcmp(usrname, getCfgStr(kusr_name1)) == 0) { - CS->setcfgAndFlush(kusr_passwd1, passwd); - } - if (strcmp(usrname, getCfgStr(kusr_name2)) == 0) { - CS->setcfgAndFlush(kusr_passwd2, passwd); - } - if (strcmp(usrname, getCfgStr(kusr_name3)) == 0) { - CS->setcfgAndFlush(kusr_passwd3, passwd); - } - if (strcmp(usrname, getCfgStr(kusr_name4)) == 0) { - CS->setcfgAndFlush(kusr_passwd4, passwd); - } - if (strcmp(usrname, getCfgStr(kusr_name5)) == 0) { - CS->setcfgAndFlush(kusr_passwd5, passwd); - } -} - -} // namespace iflytop diff --git a/uappbase/service/config_service_utils.hpp b/uappbase/service/config_service_utils.hpp deleted file mode 100644 index e9f69b4..0000000 --- a/uappbase/service/config_service_utils.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "config_service.hpp" -namespace iflytop { -float getMotorMLPR(int32_t motorid); // 泵机转和ml转换系数 -void setMotorMLPR(int32_t motorid, float val); - -int getMxRunRPM(int32_t mid); // 泵机运行时转速 -int setMxRunRPM(int32_t mid, int32_t val); // 泵机运行时转速 - -int32_t setMxIRun(int motorid, int32_t val); // IRUN -int32_t getMxIRun(int motorid); - -const char* getPasswd(const char* usrname, CfgItermCache* cache); -const void setPasswd(const char* usrname, const char* passwd); - -float getCfgPipeLengthML(int chid); -void setCfgPipeLengthML(int chid, float ml); - -float getPumpAppendMl(int chid); - -} // namespace iflytop diff --git a/usrc/config/config.hpp b/usrc/config/config.hpp new file mode 100644 index 0000000..d653bcd --- /dev/null +++ b/usrc/config/config.hpp @@ -0,0 +1,4 @@ +#pragma once +#include "config_index.hpp" +#include "config_service.hpp" +#include "config_service_utils.hpp" diff --git a/usrc/config/config_index.cpp b/usrc/config/config_index.cpp new file mode 100644 index 0000000..bbb5047 --- /dev/null +++ b/usrc/config/config_index.cpp @@ -0,0 +1,156 @@ +#include "config_index.hpp" +namespace iflytop { + +#define CFG_ITERM(index, type, initval) {index, #index, type, initval} + +static cfg_iterm_info_t cfg_iterm_infos[] = { + + /*********************************************************************************************************************** + * 用户管理 * + ***********************************************************************************************************************/ + CFG_ITERM(kusr_name0, kcfgt_str, USER0_DEFAULT_NAME), // + CFG_ITERM(kusr_enable0, kcfgt_bool, "true"), // 1:使能,0失能 + CFG_ITERM(kusr_passwd0, kcfgt_str, USER_DEFAULT_PWD), // + + CFG_ITERM(kusr_name1, kcfgt_str, USER1_DEFAULT_NAME), // + CFG_ITERM(kusr_enable1, kcfgt_bool, "true"), // + CFG_ITERM(kusr_passwd1, kcfgt_str, USER_DEFAULT_PWD), // + + CFG_ITERM(kusr_name2, kcfgt_str, USER2_DEFAULT_NAME), // + CFG_ITERM(kusr_enable2, kcfgt_bool, "true"), // + CFG_ITERM(kusr_passwd2, kcfgt_str, USER_DEFAULT_PWD), // + + CFG_ITERM(kusr_name3, kcfgt_str, USER3_DEFAULT_NAME), // + CFG_ITERM(kusr_enable3, kcfgt_bool, "false"), // + CFG_ITERM(kusr_passwd3, kcfgt_str, USER_DEFAULT_PWD), // + + CFG_ITERM(kusr_name4, kcfgt_str, USER4_DEFAULT_NAME), // + CFG_ITERM(kusr_enable4, kcfgt_bool, "false"), // + CFG_ITERM(kusr_passwd4, kcfgt_str, USER_DEFAULT_PWD), // + + CFG_ITERM(kusr_name5, kcfgt_str, USER5_DEFAULT_NAME), // + CFG_ITERM(kusr_enable5, kcfgt_bool, "false"), // + CFG_ITERM(kusr_passwd5, kcfgt_str, USER_DEFAULT_PWD), // + + /*********************************************************************************************************************** + * 酸液预设名称 * + ***********************************************************************************************************************/ + CFG_ITERM(kcfg_acidName1, kcfgt_str, ACID_DEFAULT_NAME1), // + CFG_ITERM(kcfg_acidName2, kcfgt_str, ACID_DEFAULT_NAME2), // + CFG_ITERM(kcfg_acidName3, kcfgt_str, ACID_DEFAULT_NAME3), // + CFG_ITERM(kcfg_acidName4, kcfgt_str, ACID_DEFAULT_NAME4), // + CFG_ITERM(kcfg_acidName5, kcfgt_str, ACID_DEFAULT_NAME5), // + CFG_ITERM(kcfg_acidName6, kcfgt_str, ACID_DEFAULT_NAME6), // + CFG_ITERM(kcfg_acidName7, kcfgt_str, ACID_DEFAULT_NAME7), // + CFG_ITERM(kcfg_acidName8, kcfgt_str, ACID_DEFAULT_NAME8), // + CFG_ITERM(kcfg_acidName9, kcfgt_str, ACID_DEFAULT_NAME9), // + CFG_ITERM(kcfg_acidName10, kcfgt_str, ACID_DEFAULT_NAME10), // + CFG_ITERM(kcfg_acidName11, kcfgt_str, ACID_DEFAULT_NAME11), // + CFG_ITERM(kcfg_acidName12, kcfgt_str, ACID_DEFAULT_NAME12), // + CFG_ITERM(kcfg_acidName13, kcfgt_str, ACID_DEFAULT_NAME13), // + CFG_ITERM(kcfg_acidName14, kcfgt_str, ACID_DEFAULT_NAME14), // + CFG_ITERM(kcfg_acidName15, kcfgt_str, ACID_DEFAULT_NAME15), // + + /*********************************************************************************************************************** + * 酸液通道选择名称 * + ***********************************************************************************************************************/ + CFG_ITERM(kcfg_acidChooseName0, kcfgt_str, "未设置"), // + CFG_ITERM(kcfg_acidChooseName1, kcfgt_str, "未设置"), // + CFG_ITERM(kcfg_acidChooseName2, kcfgt_str, "未设置"), // + CFG_ITERM(kcfg_acidChooseName3, kcfgt_str, "未设置"), // + + /*********************************************************************************************************************** + * 酸液通道每次分配的数值 * + ***********************************************************************************************************************/ + CFG_ITERM(kcfg_acideval0, kcfgt_float, "10.0"), // + CFG_ITERM(kcfg_acideval1, kcfgt_float, "10.0"), // + CFG_ITERM(kcfg_acideval2, kcfgt_float, "10.0"), // + CFG_ITERM(kcfg_acideval3, kcfgt_float, "10.0"), // + + /*********************************************************************************************************************** + * 电机默认配置 * + ***********************************************************************************************************************/ + /* CFG_ITERM(kcfg_mLPR0, kcfgt_float, "0.6900"), // + CFG_ITERM(kcfg_mLPR1, kcfgt_float, "0.6900"), // + CFG_ITERM(kcfg_mLPR2, kcfgt_float, "0.1350"), // + CFG_ITERM(kcfg_mLPR3, kcfgt_float, "0.1350"), // */ + CFG_ITERM(kcfg_mLPR1, kcfgt_float, "1.0"), // + CFG_ITERM(kcfg_mLPR0, kcfgt_float, "1.0"), // + CFG_ITERM(kcfg_mLPR2, kcfgt_float, "1.0"), // + CFG_ITERM(kcfg_mLPR3, kcfgt_float, "1.0"), // + + /*********************************************************************************************************************** + * 其他配置 * + ***********************************************************************************************************************/ + CFG_ITERM(kcfg_lockAcidName, kcfgt_bool, "false"), // + CFG_ITERM(kcfg_echDitrUpLi, kcfgt_int32, "99"), // 每次分配上限设定值 each distri up-limit + CFG_ITERM(kcfg_distrInterval, kcfgt_float, "1.5"), // 每次分配间隔时间 + // CFG_ITERM(kcfg_pumpDefVel, kcfgt_int32, PUMPDEFVEL), // + + CFG_ITERM(kcfg_pumpDefVel0, kcfgt_int32, PUMPDEFVEL), // + CFG_ITERM(kcfg_pumpDefVel1, kcfgt_int32, PUMPDEFVEL), // + CFG_ITERM(kcfg_pumpDefVel2, kcfgt_int32, PUMPDEFVEL), // + CFG_ITERM(kcfg_pumpDefVel3, kcfgt_int32, PUMPDEFVEL), // + + /*********************************************************************************************************************** + * 回流参数配置 * + ***********************************************************************************************************************/ + /* CFG_ITERM(kcfg_pipeLengthML0, kcfgt_float, "14"), // + CFG_ITERM(kcfg_pipeLengthML1, kcfgt_float, "14"), // + CFG_ITERM(kcfg_pipeLengthML2, kcfgt_float, "7"), // + CFG_ITERM(kcfg_pipeLengthML3, kcfgt_float, "7"), // */ + + CFG_ITERM(kcfg_pipeLengthML0, kcfgt_float, "35.5"), // + CFG_ITERM(kcfg_pipeLengthML1, kcfgt_float, "35.5"), // + CFG_ITERM(kcfg_pipeLengthML2, kcfgt_float, "35.5"), // + CFG_ITERM(kcfg_pipeLengthML3, kcfgt_float, "35.5"), // + // + + CFG_ITERM(kcfg_m0irun, kcfgt_int32, "22"), // + CFG_ITERM(kcfg_m1irun, kcfgt_int32, "22"), // + CFG_ITERM(kcfg_m2irun, kcfgt_int32, "22"), // + CFG_ITERM(kcfg_m3irun, kcfgt_int32, "22"), // + + CFG_ITERM(kcfg_bleClientName, kcfgt_str, " "), + +}; + +const char *cfgName(int32_t index) { + for (int i = 0; i < CFG_MAX_INDEX; i++) { + if (cfg_iterm_infos[i].index == index) { + return cfg_iterm_infos[i].itermName; + } + } + ZASSERT(0); + return nullptr; +} +cfg_iterm_type_t cfgType(int32_t index) { + for (int i = 0; i < CFG_MAX_INDEX; i++) { + if (cfg_iterm_infos[i].index == index) { + return cfg_iterm_infos[i].type; + } + } + return kcfgt_float; +} +const char *cfgInitVal(int32_t index) { + for (int i = 0; i < CFG_MAX_INDEX; i++) { + if (cfg_iterm_infos[i].index == index) { + return cfg_iterm_infos[i].initval; + } + } + ZASSERT(0); + + return nullptr; +} + +cfg_iterm_info_t *cfgInfo(int32_t index) { + for (int i = 0; i < CFG_MAX_INDEX; i++) { + if (cfg_iterm_infos[i].index == index) { + return &cfg_iterm_infos[i]; + } + } + ZASSERT(0); + return nullptr; +} + +} // namespace iflytop \ No newline at end of file diff --git a/usrc/config/config_index.hpp b/usrc/config/config_index.hpp new file mode 100644 index 0000000..8e920b9 --- /dev/null +++ b/usrc/config/config_index.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "uappbase/base.hpp" +namespace iflytop { +typedef enum { + kcfgt_float, + kcfgt_int32, + kcfgt_str, + kcfgt_bool, +} cfg_iterm_type_t; + +typedef struct { + int32_t index; + const char *itermName; + cfg_iterm_type_t type; + const char *initval; +} cfg_iterm_info_t; + +#define CFG_MAX_INDEX 61 + +const char *cfgName(int32_t index); +cfg_iterm_type_t cfgType(int32_t index); +const char *cfgInitVal(int32_t index); + +cfg_iterm_info_t *cfgInfo(int32_t index); + +} // namespace iflytop \ No newline at end of file diff --git a/usrc/config/config_service.cpp b/usrc/config/config_service.cpp new file mode 100644 index 0000000..02c74b0 --- /dev/null +++ b/usrc/config/config_service.cpp @@ -0,0 +1,204 @@ +#include "config_service.hpp" + +#include "uappbase\service\app_event_bus.hpp" +// + +#define CFG_HEADER 0x12345670 +/** + * @brief CFG内存使用 + * + * 4byte begin + * + * cfg + * cfg + * cfg + * cfg + * + * 4byte checksum + * 4byte end + * + */ + +using namespace iflytop; +#define TAG "ConfigService" + +#pragma pack(1) + +typedef struct { + union { + uint8_t str[CONTENT_MAX_LEN]; // 必须4字节对齐 + uint32_t u32s[CONTENT_MAX_LEN / 4]; + }; +} cfg_content_t; + +typedef struct { + int32_t begin; + int32_t checksum; + cfg_content_t cfg[CFG_MAX_INDEX]; +} config_data_t; + +#pragma pack() +#define TO_EEPROM_ADD(index) (CONFIG_EEPROM_ADD + 4 + 4 + sizeof(cfg_content_t) * index) +#define SET_CFG_INT(index, value) cfgcache.cfg[index].int32 = value +#define SET_CFG_STR(index, value) memcpy(cfgcache.cfg[index].str, value, strlen(value)) +#define CHECKSUM() checksum((uint8_t *)&(cfgcache.cfg[0].str[0]), sizeof(cfgcache) - 8) + +static config_data_t cfgcache; + +static int32_t checksum(uint8_t *data, int32_t n) { + int32_t sum = 0; + for (int i = 0; i < n; i++) { + sum += data[i]; + } + return sum; +} + +bool ConfigService::initialize() { + mutex.init(); + + AppHal::MX_I2C1_Init(); + eeprom.initialize(&hi2c1); + if (!eeprom.isOnline()) { + return false; + } + + static_assert(kcfg_max == CFG_MAX_INDEX, ""); + if (!checkcfg()) { + ZLOGI(TAG, "cfg check fail,init cfg"); + + cfgcache.begin = CFG_HEADER; + + for (int i = 0; i < kcfg_max; i++) { + cfg_iterm_info_t *info = cfgInfo((config_index_t)i); + _setcfg(i, info->initval); + } + + cfgcache.checksum = CHECKSUM(); + eeprom.write(CONFIG_EEPROM_ADD, (uint8_t *)&cfgcache, sizeof(cfgcache)); + flashall(); + } + bool suc = checkcfg(); + dumpcfgs(); + ZLOGI(TAG, "cfg init %s", suc ? "ok" : "fail"); + + return suc; +} + +void ConfigService::setcfgAndFlush(config_index_t index, const char *val) { + zlock_guard lg(mutex); + _setcfgAndFlush(index, val); +} +void ConfigService::setcfgAndFlush(config_index_t index, int32_t val) { + zlock_guard lg(mutex); + char buf[CONTENT_MAX_LEN]; + snprintf(buf, CONTENT_MAX_LEN, "%d", val); + + _setcfgAndFlush(index, buf); +} +void ConfigService::setcfgAndFlush(config_index_t index, float val) { + zlock_guard lg(mutex); + char buf[CONTENT_MAX_LEN]; + snprintf(buf, CONTENT_MAX_LEN, "%.3f", val); + + _setcfgAndFlush(index, buf); +} +void ConfigService::setcfgAndFlush(config_index_t index, bool val) { + zlock_guard lg(mutex); + _setcfgAndFlush(index, val ? "true" : "false"); +} + +void ConfigService::setcfg(config_index_t index, const char *val) { + zlock_guard lg(mutex); + _setcfg(index, val); +} +void ConfigService::setcfg(config_index_t index, int32_t val) { + zlock_guard lg(mutex); + char buf[CONTENT_MAX_LEN]; + snprintf(buf, CONTENT_MAX_LEN, "%d", val); + + _setcfg(index, buf); +} +void ConfigService::setcfg(config_index_t index, float val) { + zlock_guard lg(mutex); + char buf[CONTENT_MAX_LEN]; + snprintf(buf, CONTENT_MAX_LEN, "%.2f", val); + + _setcfg(index, buf); +} +void ConfigService::setcfg(config_index_t index, bool val) { + zlock_guard lg(mutex); + _setcfg(index, val ? "true" : "false"); +} + +const char *ConfigService::getStr(config_index_t index, CfgItermCache *cache) { + zlock_guard lg(mutex); + return _getcfg(index, cache); +} +int32_t ConfigService::getInt(config_index_t index) { + zlock_guard lg(mutex); + return atoi(_getcfg(index, nullptr)); +} +float ConfigService::getFloat(config_index_t index) { + zlock_guard lg(mutex); + return atof(_getcfg(index, nullptr)); +} +bool ConfigService::getBool(config_index_t index) { + zlock_guard lg(mutex); + return strcmp(_getcfg(index, nullptr), "true") == 0; +} + +/*********************************************************************************************************************** + * BASIC * + ***********************************************************************************************************************/ +void ConfigService::dumpcfgs() { + for (int i = 0; i < kcfg_max; i++) { + ZLOGI(TAG, "cfg %s:%s", cfgName((config_index_t)i), _getcfg((config_index_t)i, nullptr)); + } +} + +void ConfigService::_setcfg(int32_t index, const char *value) { + ZLOGI(TAG, "setcfg %s:%s", cfgName(index), value); + + memset(cfgcache.cfg[index].str, 0, 32); + strncpy((char *)cfgcache.cfg[index].str, value, 31); + cfgcache.checksum = CHECKSUM(); + + AppEventBus::ins()->pushEvent(createConfigChangeEvent((config_index_t)index)); + return; +} +void ConfigService::_setcfgAndFlush(int32_t index, const char *value) { + _setcfg(index, value); + flashIndex(index); + return; +} +const char *ConfigService::_getcfg(config_index_t index, CfgItermCache *cache) { + static CfgItermCache _cache; + if (cache == nullptr) { + cache = &_cache; + } + memset(cache->buf, 0, CONTENT_MAX_LEN); + strncpy(cache->buf, (const char *)cfgcache.cfg[index].str, CONTENT_MAX_LEN - 1); + return cache->buf; +} + +bool ConfigService::checkcfg() { + eeprom.read(CONFIG_EEPROM_ADD, (uint8_t *)&cfgcache, sizeof(cfgcache)); + + int32_t checksumval = CHECKSUM(); + + if (cfgcache.begin != CFG_HEADER || cfgcache.checksum != checksumval) { + ZLOGI(TAG, "cfg check fail,b:0x%08x,c:0x%08x!=0x%08x", cfgcache.begin, cfgcache.checksum, checksumval); + return false; + } + return true; +} +bool ConfigService::flashIndex(int32_t index) { + // for (int i = 0; i < sizeof(cfg_content_t); i += 4) { + // uint32_t wdata = cfgcache.cfg[index].u32s[i / 4]; + // eeprom.write32(TO_EEPROM_ADD(index) + i, wdata); + // } + eeprom.write(TO_EEPROM_ADD(index), (uint8_t *)&cfgcache.cfg[index], sizeof(cfg_content_t)); + eeprom.write32(CONFIG_EEPROM_ADD + 4 * 1, (uint32_t)cfgcache.checksum); + return true; +} +bool ConfigService::flashall() { eeprom.write(CONFIG_EEPROM_ADD, (uint8_t *)&cfgcache, sizeof(cfgcache)); } diff --git a/usrc/config/config_service.hpp b/usrc/config/config_service.hpp new file mode 100644 index 0000000..3b05e9f --- /dev/null +++ b/usrc/config/config_service.hpp @@ -0,0 +1,63 @@ +#pragma once +#include "uappbase/base.hpp" + +#include "config_index.hpp" +#include "ucomponents/eeprom/m24m02_i2c_eeprom.hpp" + +namespace iflytop { +using namespace std; +#define CS ConfigService::ins() + +#define CONFIG_EEPROM_ADD 0 +#define CONTENT_MAX_LEN 32 + +struct CfgItermCache { + char buf[CONTENT_MAX_LEN]; +}; + +class ConfigService { + private: + /* data */ + zmutex mutex = {"ConfigServiceMutex"}; + M24M02_I2C_EEPROM eeprom; + + public: + static ConfigService* ins() { + static ConfigService instance; + return &instance; + } + + bool initialize(); + + void setcfgAndFlush(config_index_t index, const char* val); + void setcfgAndFlush(config_index_t index, int32_t val); + void setcfgAndFlush(config_index_t index, float val); + void setcfgAndFlush(config_index_t index, bool val); + + void setcfg(config_index_t index, const char* val); + void setcfg(config_index_t index, int32_t val); + void setcfg(config_index_t index, float val); + void setcfg(config_index_t index, bool val); + + const char* getStr(config_index_t index, CfgItermCache* cache); + int32_t getInt(config_index_t index); + float getFloat(config_index_t index); + bool getBool(config_index_t index); + + private: + void _setcfg(int32_t index, const char* value); + void _setcfgAndFlush(int32_t index, const char* value); + const char* _getcfg(config_index_t index, CfgItermCache* cache); + + bool checkcfg(); + bool flashIndex(int32_t index); + bool flashall(); + void dumpcfgs(); +}; + +#define getCfgStr(index) CS->getStr(index, &cfgcache) +#define getCfgInt(index) CS->getInt(index) +#define getCfgFloat(index) CS->getFloat(index) +#define getCfgBool(index) CS->getBool(index) + +} // namespace iflytop \ No newline at end of file diff --git a/usrc/config/config_service_utils.cpp b/usrc/config/config_service_utils.cpp new file mode 100644 index 0000000..4b82597 --- /dev/null +++ b/usrc/config/config_service_utils.cpp @@ -0,0 +1,223 @@ +#include "config_service_utils.hpp" +namespace iflytop { + +float getPumpAppendMl(int chid) { + switch (chid) { + case 0: + return CH0APPENDML; // + case 1: + return CH1APPENDML; + case 2: + return CH2APPENDML; + case 3: + return CH3APPENDML; + default: + ZASSERT(0); + return 0; + } +} + +float getCfgPipeLengthML(int chid) { + switch (chid) { + case 0: + return getCfgFloat(kcfg_pipeLengthML0); + case 1: + return getCfgFloat(kcfg_pipeLengthML1); + case 2: + return getCfgFloat(kcfg_pipeLengthML2); + case 3: + return getCfgFloat(kcfg_pipeLengthML3); + default: + ZASSERT(0); + return 0; + } +} +void setCfgPipeLengthML(int chid, float ml) { + switch (chid) { + case 0: + CS->setcfgAndFlush(kcfg_pipeLengthML0, ml); + break; + case 1: + CS->setcfgAndFlush(kcfg_pipeLengthML1, ml); + break; + case 2: + CS->setcfgAndFlush(kcfg_pipeLengthML2, ml); + break; + case 3: + CS->setcfgAndFlush(kcfg_pipeLengthML3, ml); + break; + default: + ZASSERT(0); + } +} + +int getMxRunRPM(int32_t mid) { // + float rpm = 0; + if (mid == 0) { + rpm = getCfgFloat(kcfg_pumpDefVel0); + } else if (mid == 1) { + rpm = getCfgFloat(kcfg_pumpDefVel1); + } else if (mid == 2) { + rpm = getCfgFloat(kcfg_pumpDefVel2); + } else if (mid == 3) { + rpm = getCfgFloat(kcfg_pumpDefVel3); + } + if (rpm < 10) { + rpm = 10; + } + return rpm; +} + +int setMxRunRPM(int32_t mid, int32_t val) { + switch (mid) { + case 0: + CS->setcfgAndFlush(kcfg_pumpDefVel0, val); + break; + case 1: + CS->setcfgAndFlush(kcfg_pumpDefVel1, val); + break; + case 2: + CS->setcfgAndFlush(kcfg_pumpDefVel2, val); + break; + case 3: + CS->setcfgAndFlush(kcfg_pumpDefVel3, val); + break; + default: + ZASSERT(0); + return 0; + } + return 0; +} + +float getMotorMLPR(int32_t motorid) { + switch (motorid) { + case 0: + return getCfgFloat(kcfg_mLPR0); + case 1: + return getCfgFloat(kcfg_mLPR1); + case 2: + return getCfgFloat(kcfg_mLPR2); + case 3: + return getCfgFloat(kcfg_mLPR3); + default: + ZASSERT(0); + return 0; + } +} +void setMotorMLPR(int32_t motorid, float val) { + // + switch (motorid) { + case 0: + CS->setcfgAndFlush(kcfg_mLPR0, val); + break; + case 1: + CS->setcfgAndFlush(kcfg_mLPR1, val); + break; + case 2: + CS->setcfgAndFlush(kcfg_mLPR2, val); + break; + case 3: + CS->setcfgAndFlush(kcfg_mLPR3, val); + break; + default: + ZASSERT(0); + } +} + +int32_t getMxIRun(int motorid) { + switch (motorid) { + case 0: + return getCfgInt(kcfg_m0irun); + case 1: + return getCfgInt(kcfg_m1irun); + case 2: + return getCfgInt(kcfg_m2irun); + case 3: + return getCfgInt(kcfg_m3irun); + default: + ZASSERT(0); + return 0; + } +} + +int32_t setMxIRun(int motorid, int32_t val) { + switch (motorid) { + case 0: + CS->setcfgAndFlush(kcfg_m0irun, val); + break; + case 1: + CS->setcfgAndFlush(kcfg_m1irun, val); + break; + case 2: + CS->setcfgAndFlush(kcfg_m2irun, val); + break; + case 3: + CS->setcfgAndFlush(kcfg_m3irun, val); + break; + default: + ZASSERT(0); + return 0; + } + return 0; +} + +const char* getPasswd(const char* usrname, CfgItermCache* cache) { + static CfgItermCache cfgcache; + memset(cache, 0, sizeof(CfgItermCache)); + if (strcmp(usrname, getCfgStr(kusr_name0)) == 0) { + getCfgStr(kusr_passwd0); + memcpy(cache, &cfgcache, sizeof(CfgItermCache)); + return cache->buf; + } + if (strcmp(usrname, getCfgStr(kusr_name1)) == 0) { + getCfgStr(kusr_passwd1); + memcpy(cache, &cfgcache, sizeof(CfgItermCache)); + return cache->buf; + } + if (strcmp(usrname, getCfgStr(kusr_name2)) == 0) { + getCfgStr(kusr_passwd2); + memcpy(cache, &cfgcache, sizeof(CfgItermCache)); + return cache->buf; + } + if (strcmp(usrname, getCfgStr(kusr_name3)) == 0) { + getCfgStr(kusr_passwd3); + memcpy(cache, &cfgcache, sizeof(CfgItermCache)); + return cache->buf; + } + if (strcmp(usrname, getCfgStr(kusr_name4)) == 0) { + getCfgStr(kusr_passwd4); + memcpy(cache, &cfgcache, sizeof(CfgItermCache)); + return cache->buf; + } + if (strcmp(usrname, getCfgStr(kusr_name5)) == 0) { + getCfgStr(kusr_passwd5); + memcpy(cache, &cfgcache, sizeof(CfgItermCache)); + return cache->buf; + } + + return NULL; +} + +const void setPasswd(const char* usrname, const char* passwd) { + static CfgItermCache cfgcache; + if (strcmp(usrname, getCfgStr(kusr_name0)) == 0) { + CS->setcfgAndFlush(kusr_passwd0, passwd); + } + if (strcmp(usrname, getCfgStr(kusr_name1)) == 0) { + CS->setcfgAndFlush(kusr_passwd1, passwd); + } + if (strcmp(usrname, getCfgStr(kusr_name2)) == 0) { + CS->setcfgAndFlush(kusr_passwd2, passwd); + } + if (strcmp(usrname, getCfgStr(kusr_name3)) == 0) { + CS->setcfgAndFlush(kusr_passwd3, passwd); + } + if (strcmp(usrname, getCfgStr(kusr_name4)) == 0) { + CS->setcfgAndFlush(kusr_passwd4, passwd); + } + if (strcmp(usrname, getCfgStr(kusr_name5)) == 0) { + CS->setcfgAndFlush(kusr_passwd5, passwd); + } +} + +} // namespace iflytop diff --git a/usrc/config/config_service_utils.hpp b/usrc/config/config_service_utils.hpp new file mode 100644 index 0000000..e9f69b4 --- /dev/null +++ b/usrc/config/config_service_utils.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "config_service.hpp" +namespace iflytop { +float getMotorMLPR(int32_t motorid); // 泵机转和ml转换系数 +void setMotorMLPR(int32_t motorid, float val); + +int getMxRunRPM(int32_t mid); // 泵机运行时转速 +int setMxRunRPM(int32_t mid, int32_t val); // 泵机运行时转速 + +int32_t setMxIRun(int motorid, int32_t val); // IRUN +int32_t getMxIRun(int motorid); + +const char* getPasswd(const char* usrname, CfgItermCache* cache); +const void setPasswd(const char* usrname, const char* passwd); + +float getCfgPipeLengthML(int chid); +void setCfgPipeLengthML(int chid, float ml); + +float getPumpAppendMl(int chid); + +} // namespace iflytop diff --git a/usrc/service/app_core.hpp b/usrc/service/app_core.hpp index 2b7e6e2..e9594f5 100644 --- a/usrc/service/app_core.hpp +++ b/usrc/service/app_core.hpp @@ -2,7 +2,7 @@ #include "apphardware/apphardware.hpp" #include "uappbase/base.hpp" - +#include "config/config.hpp" namespace iflytop { class AppCore { private: diff --git a/usrc/service/front_end_controler.hpp b/usrc/service/front_end_controler.hpp index 3f1a3c3..0936213 100644 --- a/usrc/service/front_end_controler.hpp +++ b/usrc/service/front_end_controler.hpp @@ -1,7 +1,7 @@ #pragma once #include "apphardware/apphardware.hpp" #include "uappbase/base.hpp" - +#include "config/config.hpp" // #include "tjc/tjc.hpp" diff --git a/usrc/service/page/page_processer.hpp b/usrc/service/page/page_processer.hpp index 58592d6..3aae729 100644 --- a/usrc/service/page/page_processer.hpp +++ b/usrc/service/page/page_processer.hpp @@ -4,7 +4,7 @@ #include "uappbase/base.hpp" #include "service\front_end_controler.hpp" #include "utils/page_bak_router.hpp" - +#include "config/config.hpp" #include "ui/ui.h" namespace iflytop { using namespace std; diff --git a/usrc/service/page/utils/page_bak_router.hpp b/usrc/service/page/utils/page_bak_router.hpp index 0de399b..bdfa0a3 100644 --- a/usrc/service/page/utils/page_bak_router.hpp +++ b/usrc/service/page/utils/page_bak_router.hpp @@ -2,7 +2,7 @@ #include "apphardware/apphardware.hpp" #include "uappbase/base.hpp" #include "service\front_end_controler.hpp" - +#include "config/config.hpp" namespace iflytop { int getBakPage(int nowPage); } \ No newline at end of file diff --git a/usrc/service/pump_ctrl_service.hpp b/usrc/service/pump_ctrl_service.hpp index 688da3b..fba66f4 100644 --- a/usrc/service/pump_ctrl_service.hpp +++ b/usrc/service/pump_ctrl_service.hpp @@ -1,6 +1,7 @@ #pragma once #include "apphardware/apphardware.hpp" #include "uappbase/base.hpp" +#include "config/config.hpp" namespace iflytop { using namespace std; #define PUMPCS PumpCtrlService::ins() diff --git a/usrc/service/remote_controler.hpp b/usrc/service/remote_controler.hpp index 98e4858..7138085 100644 --- a/usrc/service/remote_controler.hpp +++ b/usrc/service/remote_controler.hpp @@ -1,6 +1,7 @@ #pragma once #include "apphardware/apphardware.hpp" #include "uappbase/base.hpp" +#include "config/config.hpp" namespace iflytop { using namespace std; #define RCTRL RemoteControlerUpper::ins() diff --git a/usrc/service/remote_controler_event_processer.hpp b/usrc/service/remote_controler_event_processer.hpp index 1cec6ec..12475d2 100644 --- a/usrc/service/remote_controler_event_processer.hpp +++ b/usrc/service/remote_controler_event_processer.hpp @@ -2,6 +2,7 @@ #include "apphardware/apphardware.hpp" #include "uappbase/base.hpp" +#include "config/config.hpp" namespace iflytop { using namespace std; /** diff --git a/usrc/service/remote_controler_state_sync_service.hpp b/usrc/service/remote_controler_state_sync_service.hpp index bcc4314..9648bac 100644 --- a/usrc/service/remote_controler_state_sync_service.hpp +++ b/usrc/service/remote_controler_state_sync_service.hpp @@ -2,6 +2,7 @@ #include "apphardware/apphardware.hpp" #include "uappbase/base.hpp" +#include "config/config.hpp" namespace iflytop { using namespace std; class RemoteControlerStateSyncService { diff --git a/usrc/service/valve_state_ctrl_service.hpp b/usrc/service/valve_state_ctrl_service.hpp index 0e38878..bec146a 100644 --- a/usrc/service/valve_state_ctrl_service.hpp +++ b/usrc/service/valve_state_ctrl_service.hpp @@ -3,7 +3,7 @@ #include "apphardware/apphardware.hpp" #include "uappbase/base.hpp" #include "ucomponents/zcan/zcan.hpp" - +#include "config/config.hpp" namespace iflytop { using namespace std; class ValveStateSyncService {