diff --git a/src/cmnds/cmd_if.c b/src/cmnds/cmd_if.c index 07c2d4f8b..e91a187fc 100644 --- a/src/cmnds/cmd_if.c +++ b/src/cmnds/cmd_if.c @@ -69,7 +69,7 @@ const char *CMD_FindOperator(const char *s, const char *stop, byte *oCode) { if (*s == 0) return 0; // special case for number like -12 at the start of the string - if (s[0] == '-' && isdigit(s[1])) { + if ((s[0] == '-') && (isdigit((int)s[1]))) { s++;// "-" is a part of the digit, not an operator } if (*s == 0) @@ -122,7 +122,7 @@ const char *strCompareBound(const char *s, const char *templ, const char *stoppe } // are the chars the same? if(bAllowWildCard && *templ == '*') { - if (isdigit(*s)) { + if (isdigit((int)(*s))) { } else { diff --git a/src/cmnds/cmd_main.c b/src/cmnds/cmd_main.c index 20b4eff70..bed4f04d4 100644 --- a/src/cmnds/cmd_main.c +++ b/src/cmnds/cmd_main.c @@ -322,7 +322,7 @@ void CMD_UART_Run() { } } if (totalSize < 2) { - return 0; + return; } // skip garbage data (should not happen) for (i = 0; i < totalSize; i++) { diff --git a/src/driver/drv_bl0942.c b/src/driver/drv_bl0942.c index f3eee0434..4f00b91c0 100644 --- a/src/driver/drv_bl0942.c +++ b/src/driver/drv_bl0942.c @@ -9,6 +9,7 @@ #include "drv_local.h" #include "drv_uart.h" #include "../httpserver/new_http.h" +#include static float BL0942_PREF = 598; static float BL0942_UREF = 15188; @@ -19,7 +20,9 @@ static int raw_unscaled_current; static int raw_unscaled_power; static int raw_unscaled_freq; - +static float valid_voltage = 0.0f; +static float valid_current = 0.0f; +static float valid_power = 0.0f; #define BL0942_BAUD_RATE 4800 @@ -95,6 +98,8 @@ int BL0942_TryToGetNextBL0942Packet() { // those are not values like 230V, but unscaled addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER,"Unscaled current %d, voltage %d, power %d, freq %d\n", raw_unscaled_current, raw_unscaled_voltage,raw_unscaled_power,raw_unscaled_freq); + addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER,"HEX Current: %08lX; Voltage: %08lX; Power: %08lX;\n", (unsigned long)raw_unscaled_current, + (unsigned long)raw_unscaled_voltage, (unsigned long)raw_unscaled_power); // those are final values, like 230V { @@ -102,8 +107,20 @@ int BL0942_TryToGetNextBL0942Packet() { power = (raw_unscaled_power / BL0942_PREF); voltage = (raw_unscaled_voltage / BL0942_UREF); current = (raw_unscaled_current / BL0942_IREF); + addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER,"Real current %1.3lf, voltage %1.1lf, power %1.1lf\n", current, voltage, power); - BL_ProcessUpdate(voltage,current,power); + /* Logical check of values */ + if (abs(power) <= (voltage * current * 1.1f)) + { + valid_voltage = voltage; + valid_current = current; + valid_power = power; + addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER,"Valid current %1.3lf, voltage %1.1lf, power %1.1lf\n", valid_current, valid_voltage, valid_power); + } else { + addLogAdv(LOG_WARN, LOG_FEATURE_ENERGYMETER,"Invalid Power value: %1.3lf Expected: %1.3lf\n", abs(power), (voltage * current)); + } + + BL_ProcessUpdate(valid_voltage, valid_current, valid_power); } diff --git a/src/driver/drv_bl_shared.c b/src/driver/drv_bl_shared.c index 688df9a14..fb9f7873f 100644 --- a/src/driver/drv_bl_shared.c +++ b/src/driver/drv_bl_shared.c @@ -337,14 +337,16 @@ void BL_ProcessUpdate(float voltage, float current, float power) // I had reports that BL0942 sometimes gives // a large, negative peak of current/power - if (CFG_HasFlag(OBK_FLAG_POWER_ALLOW_NEGATIVE)==false) { - if (power < -0.1f) - return; - if (voltage < -0.1f) - return; - if (current < -0.1f) - return; + if (CFG_HasFlag(OBK_FLAG_POWER_ALLOW_NEGATIVE)==false) + { + if (power < 0.0f) + power = 0.0f; + if (voltage < 0.0f) + voltage = 0.0f; + if (current < 0.0f) + current = 0.0f; } + // those are final values, like 230V lastReadings[OBK_POWER] = power; lastReadings[OBK_VOLTAGE] = voltage; diff --git a/src/driver/drv_bridge_driver.c b/src/driver/drv_bridge_driver.c new file mode 100644 index 000000000..a5ada7974 --- /dev/null +++ b/src/driver/drv_bridge_driver.c @@ -0,0 +1,192 @@ +#include "../new_common.h" +#include "../new_pins.h" +#include "../cmnds/cmd_public.h" +#include "drv_local.h" +#include "drv_public.h" +#include "../logging/logging.h" +#include "../hal/hal_pins.h" +#include "../mqtt/new_mqtt.h" + +/* + * Bridge control structure */ +typedef struct BRIDGE_CONTROL { + int GPIO_HLW_FWD; // Forward direction control pin + int GPIO_HLW_REV; // reverse direction control pin + int channel; // assigned channel (General Config) + bool current_state; // Current state + bool new_state; // New state + int pulseCnt; // Pulse Counter + int pulseLen; // Pulse length +} BRIDGE_CONTROL; + +/* Local variables */ +static BRIDGE_CONTROL *br_ctrl = NULL; +static int ch_count = 0; + +/**************************************************************************************/ +// MosFet Bridge driver +void Bridge_driver_Init() +{ + int i; + int ch = 0; + + addLogAdv(LOG_INFO, LOG_FEATURE_DRV, "Bridge Driver Init.\n"); + + ch_count = PIN_CountPinsWithRole(IOR_BridgeForward); + if (ch_count != PIN_CountPinsWithRole(IOR_BridgeReverse)) + { + addLogAdv(LOG_WARN, LOG_FEATURE_DRV, "Bridge Driver Pins mismatched \n"); + } + + if (ch_count>0) + { + /* Brignde channel detected */ + addLogAdv(LOG_INFO, LOG_FEATURE_DRV, "Detected %i bridge channels\n", ch_count); + if (br_ctrl != NULL) + os_free(br_ctrl); + /* Allocate memeory */ + br_ctrl = (BRIDGE_CONTROL *)os_malloc(sizeof(BRIDGE_CONTROL)*ch_count); + /* Reset settings */ + for(ch=0;ch0) + { + /* Pulse timer */ + br_ctrl[ch].pulseCnt--; + if (br_ctrl[ch].pulseCnt == 0) + { + addLogAdv(LOG_INFO, LOG_FEATURE_DRV, "Bridge Driver: %lu :PULSE Complete. HOLD\n", (unsigned long)xTaskGetTickCount()); + HAL_PIN_SetOutputValue(br_ctrl[ch].GPIO_HLW_FWD, 0); + HAL_PIN_SetOutputValue(br_ctrl[ch].GPIO_HLW_REV, 0); + } + } + else if (br_ctrl[ch].current_state < br_ctrl[ch].new_state) + { + /* Detected change in state - Forward Move */ + addLogAdv(LOG_INFO, LOG_FEATURE_DRV, "Bridge Driver: %lu : FORWARD PULSE\n", (unsigned long)xTaskGetTickCount()); + br_ctrl[ch].pulseCnt = br_ctrl[ch].pulseLen; + HAL_PIN_SetOutputValue(br_ctrl[ch].GPIO_HLW_FWD, 1); + br_ctrl[ch].current_state = br_ctrl[ch].new_state; + MQTT_ChannelChangeCallback(br_ctrl[ch].channel, br_ctrl[ch].new_state); + } + else if (br_ctrl[ch].current_state > br_ctrl[ch].new_state) + { + /* Detected change in state - Reverse Move */ + addLogAdv(LOG_INFO, LOG_FEATURE_DRV, "Bridge Driver: %lu : REVERSE PULSE\n", (unsigned long)xTaskGetTickCount()); + br_ctrl[ch].current_state = br_ctrl[ch].new_state; + br_ctrl[ch].pulseCnt = br_ctrl[ch].pulseLen; + HAL_PIN_SetOutputValue(br_ctrl[ch].GPIO_HLW_REV, 1); + br_ctrl[ch].current_state = br_ctrl[ch].new_state; + MQTT_ChannelChangeCallback(br_ctrl[ch].channel, br_ctrl[ch].new_state); + } + } + } +} + +/***************************************************************************************/ +void Bridge_driver_OnChannelChanged(int ch, int value) +{ + int i; + + addLogAdv(LOG_INFO, LOG_FEATURE_DRV, "Bridge Driver OnChannelChanged: CH:%i VAL:%i\n", ch, value); + + if (br_ctrl != NULL) + { + for (i=0;ifunction send_ha_disc(){var e=new XMLHttpRequest;e.open(\"GET\",\"/ha_discovery?prefix=\"+document.getElementById(\"ha_disc_topic\").value,!1),e.onload=function(){200===e.status?alert(e.responseText):404===e.status&&alert(\"Error invoking ha_discovery\")},e.onerror=function(){alert(\"Error invoking ha_discovery\")},e.send()}"; -//region_end ha_discovery_script \ No newline at end of file +//region_end ha_discovery_script diff --git a/src/logging/logging.c b/src/logging/logging.c index f2dc2e733..51f2d2cfd 100644 --- a/src/logging/logging.c +++ b/src/logging/logging.c @@ -75,12 +75,13 @@ char* logfeaturenames[] = { "HASS:", // = 19 "IR:", // = 20 "SENSOR:", // = 21 - "ERROR",// = 22, + "DRV:" // = 22 "ERROR",// = 23, "ERROR",// = 24, "ERROR",// = 25, "ERROR",// = 26, "ERROR",// = 27, + "ERROR",// = 28, }; #define LOGGING_BUFFER_SIZE 1024 @@ -99,7 +100,6 @@ void LOG_SetRawSocketCallback(int newFD) g_extraSocketToSendLOG = newFD; } - static int http_getlog(http_request_t* request); static int http_getlograw(http_request_t* request); @@ -357,14 +357,18 @@ void addLogAdv(int level, int feature, const char* fmt, ...) #ifdef PLATFORM_BEKEN trigger_log_send(); #endif - if (log_delay) { + if (log_delay != 0) + { int timems = log_delay; // is log_delay set -ve, then calculate delay // required for the number of characters to TX // plus 2ms to be sure. - if (log_delay < 0) { + if (log_delay < 0) + { int cps = (115200 / 8); - timems = ((1000 * len) / cps) + 2; + timems = (((1000 / portTICK_RATE_MS) * len) / cps) + 2; + if (timems < 2) + timems = 2; } rtos_delay_milliseconds(timems); } diff --git a/src/logging/logging.h b/src/logging/logging.h index 20173d446..7fabced07 100644 --- a/src/logging/logging.h +++ b/src/logging/logging.h @@ -84,7 +84,8 @@ typedef enum { LOG_FEATURE_HASS = 19, LOG_FEATURE_IR = 20, LOG_FEATURE_SENSOR = 21, - LOG_FEATURE_MAX = 22, + LOG_FEATURE_DRV = 22, + LOG_FEATURE_MAX = 23, } log_features; #endif diff --git a/src/new_common.h b/src/new_common.h index 9ccfcdcc6..7dcbf9630 100644 --- a/src/new_common.h +++ b/src/new_common.h @@ -273,6 +273,9 @@ OSStatus rtos_create_thread( beken_thread_t* thread, #include #include #include +#include "lwip/sys.h" + +#define portTICK_RATE_MS ( ( portTickType ) 1000 / configTICK_RATE_HZ ) #define bk_printf printf #define os_strcpy strcpy @@ -282,8 +285,6 @@ OSStatus rtos_create_thread( beken_thread_t* thread, #define portTICK_PERIOD_MS portTICK_RATE_MS -#define portTICK_PERIOD_MS portTICK_RATE_MS - #define rtos_delay_milliseconds sys_msleep #define delay_ms sys_msleep diff --git a/src/new_pins.c b/src/new_pins.c index 440203f8e..363d6cfb4 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -466,6 +466,10 @@ void CHANNEL_SetAll(int iVal, int iFlags) { case IOR_PWM_n: CHANNEL_Set(g_cfg.pins.channels[i],iVal,iFlags); break; + case IOR_BridgeForward: + case IOR_BridgeReverse: + CHANNEL_Set(g_cfg.pins.channels[i],iVal,iFlags); + break; default: break; @@ -586,6 +590,9 @@ void PIN_SetPinRoleForPinIndex(int index, int role) { case IOR_ADC: // TODO: disable? break; + case IOR_BridgeForward: + case IOR_BridgeReverse: + break; default: break; @@ -699,6 +706,20 @@ void PIN_SetPinRoleForPinIndex(int index, int role) { } } break; + case IOR_BridgeForward: + case IOR_BridgeReverse: + { + int channelIndex; + int channelValue; + + channelIndex = PIN_GetPinChannelForPinIndex(index); + channelValue = g_channelValues[channelIndex]; + + HAL_PIN_Setup_Output(index); + HAL_PIN_SetOutputValue(index,0); + } + break; + case IOR_AlwaysHigh: { HAL_PIN_Setup_Output(index); @@ -1138,8 +1159,10 @@ int CHANNEL_GetRoleForOutputChannel(int ch){ case IOR_PWM_n: case IOR_PWM: return g_cfg.pins.roles[i]; - break; - case IOR_Button: + case IOR_BridgeForward: + case IOR_BridgeReverse: + return g_cfg.pins.roles[i]; + case IOR_Button: case IOR_Button_n: case IOR_LED_WIFI: case IOR_LED_WIFI_n: @@ -1688,6 +1711,10 @@ int h_isChannelRelay(int tg_ch) { if(role == IOR_Relay || role == IOR_Relay_n || role == IOR_LED || role == IOR_LED_n) { return true; } + if((role == IOR_BridgeForward) || (role == IOR_BridgeReverse)) + { + return true; + } } return false; } diff --git a/src/new_pins.h b/src/new_pins.h index 72f9ef2ec..8ca45b36e 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -81,6 +81,9 @@ enum IORole { IOR_SM2235_DAT, IOR_SM2235_CLK, + IOR_BridgeForward, + IOR_BridgeReverse, + IOR_Total_Options, }; diff --git a/src/user_main.c b/src/user_main.c index 3267a947d..ab9edf097 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -904,7 +904,13 @@ void Main_Init_BeforeDelay_Unsafe(bool bAutoRunScripts) { DRV_StartDriver("BL0937"); #endif } - } + if((PIN_FindPinIndexForRole(IOR_BridgeForward, -1) != -1) && (PIN_FindPinIndexForRole(IOR_BridgeReverse, -1) != -1)) + { +#ifndef OBK_DISABLE_ALL_DRIVERS + DRV_StartDriver("BridgeDRV"); +#endif + } + } g_enable_pins = 1; // this actually sets the pins, moved out so we could avoid if necessary