diff --git a/src/cmnds/cmd_newLEDDriver.c b/src/cmnds/cmd_newLEDDriver.c index cb2cb0b6f..862d79bf2 100644 --- a/src/cmnds/cmd_newLEDDriver.c +++ b/src/cmnds/cmd_newLEDDriver.c @@ -186,6 +186,20 @@ float led_lerpSpeedUnitsPerSecond = 200.f; float led_current_value_brightness = 0; float led_current_value_cold_or_warm = 0; +void led_Save_finalRGBCW(byte* finalRGBCW) { +#ifdef ENABLE_DRIVER_LED + if (DRV_IsRunning("SM2135")) { + SM2135_Write(finalRGBCW); + } + if (DRV_IsRunning("BP5758D")) { + BP5758D_Write(finalRGBCW); + } + if (DRV_IsRunning("BP1658CJ")) { + BP1658CJ_Write(finalRGBCW); + } +#endif +} + void LED_RunQuickColorLerp(int deltaMS) { int i; int firstChannelIndex; @@ -274,17 +288,8 @@ void LED_RunQuickColorLerp(int deltaMS) { } } } -#ifndef OBK_DISABLE_ALL_DRIVERS - if(DRV_IsRunning("SM2135")) { - SM2135_Write(finalRGBCW); - } - if(DRV_IsRunning("BP5758D")) { - BP5758D_Write(finalRGBCW); - } - if(DRV_IsRunning("BP1658CJ")) { - BP1658CJ_Write(finalRGBCW); - } -#endif + + led_Save_finalRGBCW(finalRGBCW); } #if WINDOWS int exponential_mode = 0; @@ -454,17 +459,7 @@ void apply_smart_light() { } } if(CFG_HasFlag(OBK_FLAG_LED_SMOOTH_TRANSITIONS) == false) { -#ifndef OBK_DISABLE_ALL_DRIVERS - if(DRV_IsRunning("SM2135")) { - SM2135_Write(finalRGBCW); - } - if(DRV_IsRunning("BP5758D")) { - BP5758D_Write(finalRGBCW); - } - if(DRV_IsRunning("BP1658CJ")) { - BP1658CJ_Write(finalRGBCW); - } -#endif + led_Save_finalRGBCW(finalRGBCW); } if(CFG_HasFlag(OBK_FLAG_LED_REMEMBERLASTSTATE)) { diff --git a/src/driver/drv_bl0937.c b/src/driver/drv_bl0937.c index a39f6b8d3..b211e2af8 100644 --- a/src/driver/drv_bl0937.c +++ b/src/driver/drv_bl0937.c @@ -33,6 +33,10 @@ int GPIO_HLW_SEL = 24; // pwm4 int GPIO_HLW_CF = 7; int GPIO_HLW_CF1 = 8; +//The above three actually are pin indices. For W600 the actual gpio_pins are different. +unsigned int GPIO_HLW_CF_pin; +unsigned int GPIO_HLW_CF1_pin; + bool g_sel = true; uint32_t res_v = 0; uint32_t res_c = 0; @@ -47,13 +51,29 @@ volatile uint32_t g_vc_pulses = 0; volatile uint32_t g_p_pulses = 0; static portTickType pulseStamp; +#if PLATFORM_W600 + +static void HlwCf1Interrupt(void* context) { + tls_clr_gpio_irq_status(GPIO_HLW_CF1_pin); + g_vc_pulses++; +} +static void HlwCfInterrupt(void* context) { + tls_clr_gpio_irq_status(GPIO_HLW_CF_pin); + g_p_pulses++; +} + +#else + void HlwCf1Interrupt(unsigned char pinNum) { // Service Voltage and Current g_vc_pulses++; } void HlwCfInterrupt(unsigned char pinNum) { // Service Power g_p_pulses++; } -commandResult_t BL0937_PowerSet(const void *context, const char *cmd, const char *args, int cmdFlags) { + +#endif + +commandResult_t BL0937_PowerSet(const void* context, const char* cmd, const char* args, int cmdFlags) { float realPower; if(args==0||*args==0) { @@ -179,25 +199,28 @@ commandResult_t BL0937_CurrentSet(const void *context, const char *cmd, const ch void BL0937_Shutdown_Pins() { - -#if PLATFORM_BEKEN +#if PLATFORM_W600 + tls_gpio_irq_disable(GPIO_HLW_CF1_pin); + tls_gpio_irq_disable(GPIO_HLW_CF_pin); +#elif PLATFORM_BEKEN gpio_int_disable(GPIO_HLW_CF1); -#else - -#endif -#if PLATFORM_BEKEN gpio_int_disable(GPIO_HLW_CF); -#else - #endif - } + void BL0937_Init_Pins() { // 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 PLATFORM_W600 + GPIO_HLW_CF1_pin = HAL_GetGPIOPin(GPIO_HLW_CF1); + GPIO_HLW_CF_pin = HAL_GetGPIOPin(GPIO_HLW_CF); + //printf("GPIO_HLW_CF=%d GPIO_HLW_CF1=%d\n", GPIO_HLW_CF, GPIO_HLW_CF1); + //printf("GPIO_HLW_CF1_pin=%d GPIO_HLW_CF_pin=%d\n", GPIO_HLW_CF1_pin, GPIO_HLW_CF_pin); +#endif + // UPDATE: now they are automatically saved BL0937_VREF = CFG_GetPowerMeasurementCalibrationFloat(CFG_OBK_VOLTAGE, BL0937_VREF); BL0937_PREF = CFG_GetPowerMeasurementCalibrationFloat(CFG_OBK_POWER, BL0937_PREF); @@ -209,17 +232,22 @@ void BL0937_Init_Pins() { HAL_PIN_Setup_Input_Pullup(GPIO_HLW_CF1); -#if PLATFORM_BEKEN +#if PLATFORM_W600 + tls_gpio_isr_register(GPIO_HLW_CF1_pin, HlwCf1Interrupt, NULL); + tls_gpio_irq_enable(GPIO_HLW_CF1_pin, WM_GPIO_IRQ_TRIG_FALLING_EDGE); +#elif PLATFORM_BEKEN gpio_int_enable(GPIO_HLW_CF1, IRQ_TRIGGER_FALLING_EDGE, HlwCf1Interrupt); -#else - #endif + HAL_PIN_Setup_Input_Pullup(GPIO_HLW_CF); -#if PLATFORM_BEKEN - gpio_int_enable(GPIO_HLW_CF, IRQ_TRIGGER_FALLING_EDGE, HlwCfInterrupt); -#else +#if PLATFORM_W600 + tls_gpio_isr_register(GPIO_HLW_CF_pin, HlwCfInterrupt, NULL); + tls_gpio_irq_enable(GPIO_HLW_CF_pin, WM_GPIO_IRQ_TRIG_FALLING_EDGE); +#elif PLATFORM_BEKEN + gpio_int_enable(GPIO_HLW_CF, IRQ_TRIGGER_FALLING_EDGE, HlwCfInterrupt); #endif + g_vc_pulses = 0; g_p_pulses = 0; pulseStamp = xTaskGetTickCount(); @@ -267,7 +295,7 @@ void BL0937_Init() BL0937_Init_Pins(); } -void BL0937_RunFrame() +void BL0937_RunFrame() { float final_v; float final_c; @@ -286,11 +314,11 @@ void BL0937_RunFrame() bNeedRestart = true; } - ticksElapsed = (xTaskGetTickCount() - pulseStamp); + ticksElapsed = (xTaskGetTickCount() - pulseStamp); #if PLATFORM_BEKEN - GLOBAL_INT_DECLARATION(); - GLOBAL_INT_DISABLE(); + GLOBAL_INT_DECLARATION(); + GLOBAL_INT_DISABLE(); #else #endif @@ -329,20 +357,20 @@ void BL0937_RunFrame() #endif - pulseStamp = xTaskGetTickCount(); + pulseStamp = xTaskGetTickCount(); //addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER,"Voltage pulses %i, current %i, power %i\n", res_v, res_c, res_p); final_v = res_v * BL0937_VREF; - final_v *= (float)ticksElapsed; - final_v /= (1000.0f / (float)portTICK_PERIOD_MS); - - final_c = res_c * BL0937_CREF; - final_c *= (float)ticksElapsed; - final_c /= (1000.0f / (float)portTICK_PERIOD_MS); - - final_p = res_p * BL0937_PREF; - final_p *= (float)ticksElapsed; - final_p /= (1000.0f / (float)portTICK_PERIOD_MS); + final_v *= (float)ticksElapsed; + final_v /= (1000.0f / (float)portTICK_PERIOD_MS); + + final_c = res_c * BL0937_CREF; + final_c *= (float)ticksElapsed; + final_c /= (1000.0f / (float)portTICK_PERIOD_MS); + + final_p = res_p * BL0937_PREF; + final_p *= (float)ticksElapsed; + final_p /= (1000.0f / (float)portTICK_PERIOD_MS); /* patch to limit max power reading, filter random reading errors */ if (final_p > BL0937_PMAX) diff --git a/src/driver/drv_main.c b/src/driver/drv_main.c index 73be0c0f6..b68d2b044 100644 --- a/src/driver/drv_main.c +++ b/src/driver/drv_main.c @@ -62,23 +62,37 @@ typedef struct driver_s { bool bLoaded; } driver_t; + // startDriver BL0937 static driver_t g_drivers[] = { - { "TuyaMCU", TuyaMCU_Init, TuyaMCU_RunFrame, NULL, NULL, NULL, NULL, false }, - { "NTP", NTP_Init, NTP_OnEverySecond, NTP_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, -#if DRV_ENABLE_I2C - { "I2C", DRV_I2C_Init, DRV_I2C_EverySecond, NULL, NULL, NULL, NULL, false }, -#endif - //These 4 measure power - { "BL0942", BL0942_Init, BL0942_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, - { "BL0937", BL0937_Init, BL0937_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, - { "CSE7766", CSE7766_Init, CSE7766_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, - //Test drivers +#ifdef ENABLE_DRIVER_TUYAMCU + { "TuyaMCU", TuyaMCU_Init, TuyaMCU_RunFrame, NULL, NULL, NULL, NULL, false }, + { "tmSensor", TuyaMCU_Sensor_Init, TuyaMCU_Sensor_RunFrame, NULL, NULL, NULL, NULL, false }, +#endif + +#ifdef ENABLE_BASIC_DRIVERS + { "NTP", NTP_Init, NTP_OnEverySecond, NTP_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, { "TESTPOWER", Test_Power_Init, Test_Power_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, { "TESTLED", Test_LED_Driver_Init, Test_LED_Driver_RunFrame, NULL, NULL, NULL, Test_LED_Driver_OnChannelChanged, false }, { "HTTPButtons", DRV_InitHTTPButtons, NULL, NULL, NULL, NULL, NULL, false }, +#endif +#if ENABLE_I2C + { "I2C", DRV_I2C_Init, DRV_I2C_EverySecond, NULL, NULL, NULL, NULL, false }, +#endif + +#ifdef ENABLE_DRIVER_BL0942 + { "BL0942", BL0942_Init, BL0942_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, +#endif + +#ifdef ENABLE_DRIVER_BL0937 + { "BL0937", BL0937_Init, BL0937_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, +#endif + +#ifdef ENABLE_DRIVER_CSE7766 + { "CSE7766", CSE7766_Init, CSE7766_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, +#endif #if PLATFORM_BEKEN { "SM16703P", SM16703P_Init, NULL, NULL, NULL, NULL, NULL, false }, @@ -90,10 +104,13 @@ static driver_t g_drivers[] = { { "PWMToggler", DRV_InitPWMToggler, NULL, DRV_Toggler_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, { "DGR", DRV_DGR_Init, DRV_DGR_RunEverySecond, NULL, DRV_DGR_RunQuickTick, DRV_DGR_Shutdown, DRV_DGR_OnChannelChanged, false }, #endif + +#ifdef ENABLE_DRIVER_LED { "SM2135", SM2135_Init, SM2135_RunFrame, NULL, NULL, NULL, SM2135_OnChannelChanged, false }, { "BP5758D", BP5758D_Init, BP5758D_RunFrame, NULL, NULL, NULL, BP5758D_OnChannelChanged, false }, { "BP1658CJ", BP1658CJ_Init, BP1658CJ_RunFrame, NULL, NULL, NULL, BP1658CJ_OnChannelChanged, false }, - { "tmSensor", TuyaMCU_Sensor_Init, TuyaMCU_Sensor_RunFrame, NULL, NULL, NULL, NULL, false } +#endif + }; static const int g_numDrivers = sizeof(g_drivers) / sizeof(g_drivers[0]); diff --git a/src/hal/hal_pins.h b/src/hal/hal_pins.h index 3c00cdb07..9e4ee5f90 100644 --- a/src/hal/hal_pins.h +++ b/src/hal/hal_pins.h @@ -9,9 +9,5 @@ void HAL_PIN_PWM_Start(int index); // Value range is 0 to 100, value is clamped void HAL_PIN_PWM_Update(int index, int value); int HAL_PIN_CanThisPinBePWM(int index); -const char *HAL_PIN_GetPinNameAlias(int index); - - - - - +const char* HAL_PIN_GetPinNameAlias(int index); +unsigned int HAL_GetGPIOPin(int index); diff --git a/src/hal/w800/hal_pins_w800.c b/src/hal/w800/hal_pins_w800.c index 5a2685909..df25063c0 100644 --- a/src/hal/w800/hal_pins_w800.c +++ b/src/hal/w800/hal_pins_w800.c @@ -185,5 +185,7 @@ void HAL_PIN_PWM_Update(int index, int value) { tls_pwm_duty_set(channel, value * 2.55f); } - +unsigned int HAL_GetGPIOPin(int index) { + return g_pins[index].code; +} #endif diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 635d03287..94d8f71e1 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -824,7 +824,7 @@ int http_fn_index(http_request_t* request) { hprintf255(request, "