From b44147bee2dcf74f9d995092ecabda9614dd8689 Mon Sep 17 00:00:00 2001 From: openshwprojects Date: Tue, 7 Jun 2022 19:12:28 +0200 Subject: [PATCH] reorganize, allow setting start value from web panel part 1 --- src/httpserver/http_fns.c | 61 +++++++++++++++++++++++++++++++++++++++ src/httpserver/http_fns.h | 1 + src/httpserver/new_http.c | 1 + src/new_cfg.c | 20 ++++++++++++- src/new_cfg.h | 3 ++ src/new_pins.c | 38 +++++++++++++++++------- src/new_pins.h | 6 ++-- src/user_main.c | 8 ++--- 8 files changed, 120 insertions(+), 18 deletions(-) diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 6a23b9624..078ffdff3 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1175,6 +1175,7 @@ int http_fn_cfg(http_request_t *request) { poststr(request,g_header); poststr(request,"
"); poststr(request,"
"); + poststr(request,"
"); poststr(request,"
"); poststr(request,"
"); poststr(request,"
"); @@ -1372,6 +1373,66 @@ int http_fn_cfg_generic(http_request_t *request) { poststr(request, NULL); return 0; } +int http_fn_cfg_startup(http_request_t *request) { + int iChanged = 0; + int iChangedRequested = 0; + int channelIndex; + int newValue; + int i; + char tmpA[128]; + char tmpB[64]; + + http_setup(request, httpMimeTypeHTML); + poststr(request,htmlHeader); + poststr(request,g_header); + + hprintf128(request,"
Here you can set pin start values
"); + hprintf128(request,"
For relays, simply use 1 or 0
"); + //hprintf128(request,"
For 'remember last power state', use -1 as a special value
"); + hprintf128(request,"
For dimmers, range is 0 to 100
"); + hprintf128(request,"
For custom values, you can set any number you want to
"); + hprintf128(request,"
Remember that you can also use short startup command to run commands like led_baseColor #FF0000 and led_enableAll 1 etc
"); + + if( http_getArg(request->url,"idx",tmpA,sizeof(tmpA))) { + channelIndex = atoi(tmpA); + if( http_getArg(request->url,"value",tmpA,sizeof(tmpA))) { + newValue = atoi(tmpA); + + + CFG_SetChannelStartupValue(channelIndex,newValue); + + hprintf128(request,"
Setting channel %i start value to %i
",channelIndex,newValue); + + } + } + + for(i = 0; i < CHANNEL_MAX; i++) { + if(CHANNEL_IsInUse(i)) { + int startValue; + + startValue = CFG_GetChannelStartupValue(i); + + poststr(request,"
\ +
\ + "); + poststr(request,"
"); + poststr(request,"
"); + } + } + + poststr(request,htmlReturnToCfg); + HTTP_AddBuildFooter(request); + poststr(request,htmlEnd); + + poststr(request, NULL); + return 0; +} void XR809_RequestOTAHTTP(const char *s); diff --git a/src/httpserver/http_fns.h b/src/httpserver/http_fns.h index 09975db85..c415e48d1 100644 --- a/src/httpserver/http_fns.h +++ b/src/httpserver/http_fns.h @@ -30,3 +30,4 @@ int http_fn_other(http_request_t *request); int http_fn_cm(http_request_t *request); int http_fn_startup_command(http_request_t *request); int http_fn_cfg_generic(http_request_t *request); +int http_fn_cfg_startup(http_request_t *request); diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index 4bd7612fb..14ab43119 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -531,6 +531,7 @@ int HTTP_ProcessPacket(http_request_t *request) { if(http_checkUrlBase(urlStr,"config_dump_table")) return http_fn_config_dump_table(request); if(http_checkUrlBase(urlStr,"startup_command")) return http_fn_startup_command(request); if(http_checkUrlBase(urlStr,"cfg_generic")) return http_fn_cfg_generic(request); + if(http_checkUrlBase(urlStr,"cfg_startup")) return http_fn_cfg_startup(request); if(http_checkUrlBase(urlStr,"cfg_quick")) return http_fn_cfg_quick(request); if(http_checkUrlBase(urlStr,"cfg_ha")) return http_fn_cfg_ha(request); diff --git a/src/new_cfg.c b/src/new_cfg.c index 00d8239a4..c7d4fd742 100644 --- a/src/new_cfg.c +++ b/src/new_cfg.c @@ -337,6 +337,23 @@ void CFG_Save_IfThereArePendingChanges() { g_cfg_pendingChanges = 0; } } +void CFG_SetChannelStartupValue(int channelIndex,short newValue) { + if(channelIndex < 0 || channelIndex >= CHANNEL_MAX) { + addLogAdv(LOG_ERROR, LOG_FEATURE_CFG, "CFG_SetChannelStartupValue: Channel index %i out of range <0,%i).",channelIndex,CHANNEL_MAX); + return; + } + if(g_cfg.startChannelValues[channelIndex] != newValue) { + g_cfg.startChannelValues[channelIndex] = newValue; + g_cfg_pendingChanges++; + } +} +short CFG_GetChannelStartupValue(int channelIndex) { + if(channelIndex < 0 || channelIndex >= CHANNEL_MAX) { + addLogAdv(LOG_ERROR, LOG_FEATURE_CFG, "CFG_SetChannelStartupValue: Channel index %i out of range <0,%i).",channelIndex,CHANNEL_MAX); + return 0; + } + return g_cfg.startChannelValues[channelIndex]; +} void PIN_SetPinChannelForPinIndex(int index, int ch) { if(index < 0 || index >= PLATFORM_GPIO_MAX) { addLogAdv(LOG_ERROR, LOG_FEATURE_CFG, "PIN_SetPinChannelForPinIndex: Pin index %i out of range <0,%i).",index,PLATFORM_GPIO_MAX); @@ -386,4 +403,5 @@ void CFG_InitAndLoad() { addLogAdv(LOG_WARN, LOG_FEATURE_CFG, "CFG_InitAndLoad: Correct config has been loaded with %i changes count.",g_cfg.changeCounter); } g_configInitialized = 1; -} + +} \ No newline at end of file diff --git a/src/new_cfg.h b/src/new_cfg.h index 7c38be71c..446c095cf 100644 --- a/src/new_cfg.h +++ b/src/new_cfg.h @@ -45,6 +45,9 @@ void CFG_SetPingDisconnectedSecondsToRestart(int i); void CFG_SetPingIntervalSeconds(int i); void CFG_SetBootOkSeconds(int v); int CFG_GetBootOkSeconds(); +void CFG_SetChannelStartupValue(int channelIndex,short newValue); +short CFG_GetChannelStartupValue(int channelIndex); +void CFG_ApplyChannelStartValues(); diff --git a/src/new_pins.c b/src/new_pins.c index 7d7c1b4f1..088b821dd 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -65,13 +65,6 @@ static byte g_timesUp[PLATFORM_GPIO_MAX]; static byte g_lastValidState[PLATFORM_GPIO_MAX]; -void PIN_OnReboot() { - int i; - for(i = 0; i < CHANNEL_MAX; i++) { - g_channelValues[i] = 0; - } -} - void PIN_SetupPins() { int i; for(i = 0; i < PLATFORM_GPIO_MAX; i++) { @@ -487,6 +480,19 @@ static void Channel_OnChanged(int ch, int prevValue, int iFlags) { } EventHandlers_ProcessVariableChange_Integer(CMD_EVENT_CHANGE_CHANNEL0 + ch, prevValue, iVal); } +void CFG_ApplyChannelStartValues() { + int i; + for(i = 0; i < CHANNEL_MAX; i++) { + int iValue; + + iValue = g_cfg.startChannelValues[i]; + if(iValue == -1) { + + } else { + g_channelValues[i] = iValue; + } + } +} int CHANNEL_Get(int ch) { if(ch < 0 || ch >= CHANNEL_MAX) { addLogAdv(LOG_ERROR, LOG_FEATURE_GENERAL,"CHANNEL_Get: Channel index %i is out of range <0,%i)\n\r",ch,CHANNEL_MAX); @@ -652,7 +658,11 @@ void PIN_Input_Handler(int pinIndex) uint8_t read_gpio_level; handle = &g_buttons[pinIndex]; - read_gpio_level = handle->hal_button_Level(handle); + if(handle->hal_button_Level != 0) { + read_gpio_level = handle->hal_button_Level(handle); + } else { + read_gpio_level = handle->button_level; + } //ticks counter working.. if((handle->state) > 0) @@ -959,7 +969,7 @@ OS_Timer_t timer; #else beken_timer_t g_pin_timer; #endif -void PIN_Init(void) +void PIN_StartButtonScanThread(void) { int i; @@ -972,7 +982,7 @@ void PIN_Init(void) OS_TimerSetInvalid(&timer); if (OS_TimerCreate(&timer, OS_TIMER_PERIODIC, PIN_ticks, NULL, PIN_TMR_DURATION) != OS_OK) { - printf("PIN_Init timer create failed\n"); + printf("PIN_AddCommands timer create failed\n"); return; } @@ -991,8 +1001,14 @@ void PIN_Init(void) #endif + +} + +void PIN_AddCommands(void) +{ + CMD_RegisterCommand("showgpi", NULL, showgpi, "log stat of all GPIs", NULL); CMD_RegisterCommand("setChannelType", NULL, CMD_SetChannelType, "qqqqqqqq", NULL); CMD_RegisterCommand("showChannelValues", NULL,CMD_ShowChannelValues, "log channel values", NULL); -} +} \ No newline at end of file diff --git a/src/new_pins.h b/src/new_pins.h index a08ced3bc..94c43ce30 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -116,7 +116,8 @@ typedef struct mainConfig_s { char shortDeviceName[32]; char longDeviceName[64]; pinsState_t pins; - byte unusedSectorA[256]; + short startChannelValues[CHANNEL_MAX]; + byte unusedSectorA[128]; byte unusedSectorB[128]; byte unusedSectorC[55]; byte timeRequiredToMarkBootSuccessfull; @@ -133,8 +134,9 @@ extern char g_enable_pins; #define CHANNEL_SET_FLAG_FORCE 1 #define CHANNEL_SET_FLAG_SKIP_MQTT 2 -void PIN_Init(void); +void PIN_AddCommands(void); void PIN_SetupPins(); +void PIN_StartButtonScanThread(void); void PIN_OnReboot(); void CFG_ClearPins(); int PIN_GetPinRoleForPinIndex(int index); diff --git a/src/user_main.c b/src/user_main.c index f9141a82c..12d171301 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -321,7 +321,6 @@ void Main_Init() // read or initialise the boot count flash area HAL_FlashVars_IncreaseBootCount(); - PIN_OnReboot(); g_bootFailures = HAL_FlashVars_GetBootFailures(); if (g_bootFailures > RESTARTS_REQUIRED_FOR_SAFE_MODE){ @@ -359,7 +358,6 @@ void Main_Init() HTTPServer_Start(); ADDLOGF_DEBUG("Started http tcp server\r\n"); - // only initialise certain things if we are not in AP mode if (!bSafeMode){ @@ -368,7 +366,9 @@ void Main_Init() #endif RepeatingEvents_Init(); - PIN_Init(); + CFG_ApplyChannelStartValues(); + + PIN_AddCommands(); //CFG_ApplyStartChannelValues(); ADDLOGF_DEBUG("Initialised pins\r\n"); @@ -387,7 +387,6 @@ void Main_Init() // and we may add a command to empty fs just be writing first sector? init_lfs(0); #endif - // initialise rest interface init_rest(); @@ -412,6 +411,7 @@ void Main_Init() g_enable_pins = 1; // this actually sets the pins, moved out so we could avoid if necessary PIN_SetupPins(); + PIN_StartButtonScanThread(); } }