diff --git a/src/driver/drv_bl0937.c b/src/driver/drv_bl0937.c index e060b8536..3e8d58549 100644 --- a/src/driver/drv_bl0937.c +++ b/src/driver/drv_bl0937.c @@ -15,14 +15,17 @@ #include "sys_timer.h" #include "gw_intf.h" +// HLW8012 aka BL0937 + #define ELE_HW_TIME 1 #define HW_TIMER_ID 0 - -int GPIO_NRG_SEL = 24; // pwm4 +// Those can be set by Web page pins configurator +// The below are default values for Mycket smart socket +int GPIO_HLW_SEL = 24; // pwm4 int GPIO_HLW_CF = 7; -int GPIO_NRG_CF1 = 8; +int GPIO_HLW_CF1 = 8; bool g_sel = true; uint32_t res_v = 0; @@ -119,12 +122,12 @@ int BL0937_CurrentSet(const void *context, const char *cmd, const char *args, in } void BL0937_Init() { - HAL_PIN_Setup_Output(GPIO_NRG_SEL); - HAL_PIN_SetOutputValue(GPIO_NRG_SEL, g_sel); + HAL_PIN_Setup_Output(GPIO_HLW_SEL); + HAL_PIN_SetOutputValue(GPIO_HLW_SEL, g_sel); - HAL_PIN_Setup_Input_Pullup(GPIO_NRG_CF1); + HAL_PIN_Setup_Input_Pullup(GPIO_HLW_CF1); - gpio_int_enable(GPIO_NRG_CF1,IRQ_TRIGGER_FALLING_EDGE,HlwCf1Interrupt); + gpio_int_enable(GPIO_HLW_CF1,IRQ_TRIGGER_FALLING_EDGE,HlwCf1Interrupt); HAL_PIN_Setup_Input_Pullup(GPIO_HLW_CF); gpio_int_enable(GPIO_HLW_CF,IRQ_TRIGGER_FALLING_EDGE,HlwCfInterrupt); @@ -141,6 +144,12 @@ void BL0937_RunFrame() { float final_c; float final_p; + + // if not found, this will return the already set value + GPIO_HLW_SEL = PIN_FindPinIndexForRole(IOR_BL0937_SEL,GPIO_HLW_SEL); + GPIO_HLW_CF = PIN_FindPinIndexForRole(IOR_BL0937_CF,GPIO_HLW_CF); + GPIO_HLW_CF1 = PIN_FindPinIndexForRole(IOR_BL0937_CF1,GPIO_HLW_CF1); + if(g_sel) { res_v = g_vc_pulses; g_sel = false; @@ -148,7 +157,7 @@ void BL0937_RunFrame() { res_c = g_vc_pulses; g_sel = true; } - HAL_PIN_SetOutputValue(GPIO_NRG_SEL, g_sel); + HAL_PIN_SetOutputValue(GPIO_HLW_SEL, g_sel); g_vc_pulses = 0; res_p = g_p_pulses; @@ -159,7 +168,11 @@ void BL0937_RunFrame() { final_v = res_v * calib_v; final_c = res_c * calib_c; final_p = res_p * calib_p; - addLogAdv(LOG_INFO, LOG_FEATURE_BL0942,"Voltage %f, current %f, power %f\n", final_v, final_c, final_p); + { + char dbg[128]; + sprintf(dbg,"Voltage %f, current %f, power %f\n", final_v, final_c, final_p); + addLogAdv(LOG_INFO, LOG_FEATURE_BL0942,dbg); + } BL_ProcessUpdate(final_v,final_c,final_p); } diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index ddfc42e9b..5236e6951 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -236,6 +236,9 @@ const char *htmlPinRoleNames[] = { "ToggleChannelOnToggle", "DigitalInput_NoPullUp", "DigitalInput_NoPullUp_n", + "BL0937 SEL", + "BL0937 CF", + "BL0937 CF1", "e", "e", }; diff --git a/src/new_pins.c b/src/new_pins.c index 692f9cadb..708189895 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -85,6 +85,15 @@ int PIN_GetPinChannelForPinIndex(int index) { } return g_cfg.pins.channels[index]; } +int PIN_FindPinIndexForRole(int role, int defaultIndexToReturnIfNotFound) { + int i; + + for(i = 0; i < PLATFORM_GPIO_MAX; i++) { + if(g_cfg.pins.roles[i] == role) + return i; + } + return defaultIndexToReturnIfNotFound; +} int PIN_GetPinChannel2ForPinIndex(int index) { if(index < 0 || index >= PLATFORM_GPIO_MAX) { addLogAdv(LOG_ERROR, LOG_FEATURE_CFG, "PIN_GetPinChannel2ForPinIndex: Pin index %i out of range <0,%i).",index,PLATFORM_GPIO_MAX); diff --git a/src/new_pins.h b/src/new_pins.h index 6d43d9d82..5f0a37e7e 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -29,6 +29,10 @@ enum IORole { IOR_DigitalInput_NoPup, // as above, but inverted IOR_DigitalInput_NoPup_n, + // energy sensor + IOR_BL0937_SEL, + IOR_BL0937_CF, + IOR_BL0937_CF1, IOR_Total_Options, }; @@ -134,6 +138,7 @@ void CFG_ClearPins(); int PIN_GetPinRoleForPinIndex(int index); int PIN_GetPinChannelForPinIndex(int index); int PIN_GetPinChannel2ForPinIndex(int index); +int PIN_FindPinIndexForRole(int role, int defaultIndexToReturnIfNotFound); const char *PIN_GetPinNameAlias(int index); void PIN_SetPinRoleForPinIndex(int index, int role); void PIN_SetPinChannelForPinIndex(int index, int ch);