From a360ac4ef6a4c6e1fbfa1f800c50810dbde64d0b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 27 Nov 2018 11:55:54 +0100 Subject: [PATCH] Add dynamic buffer space Make serial buffer space reservation dynamic --- sonoff/xdrv_16_tuyadimmer.ino | 19 +++++++++++-------- sonoff/xdrv_19_ps16dz_dimmer.ino | 22 ++++++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 1322da33a..15db69747 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -53,7 +53,7 @@ uint8_t tuya_cmd_checksum = 0; // Checksum of tuya command uint8_t tuya_data_len = 0; // Data lenght of command int8_t tuya_wifi_state = -2; // Keep MCU wifi-status in sync with WifiState() -char tuya_buffer[TUYA_BUFFER_SIZE]; // Serial receive buffer +char *tuya_buffer = NULL; // Serial receive buffer int tuya_byte_counter = 0; // Index in serial receive buffer /*********************************************************************************************\ @@ -285,14 +285,17 @@ void TuyaInit(void) if (!Settings.param[P_TUYA_DIMMER_ID]) { Settings.param[P_TUYA_DIMMER_ID] = TUYA_DIMMER_ID; } - TuyaSerial = new TasmotaSerial(pin[GPIO_TUYA_RX], pin[GPIO_TUYA_TX], 2); - if (TuyaSerial->begin(9600)) { - if (TuyaSerial->hardwareSerial()) { ClaimSerial(); } - // Get MCU Configuration - snprintf_P(log_data, sizeof(log_data), "TYA: Request MCU configuration"); - AddLog(LOG_LEVEL_DEBUG); + tuya_buffer = reinterpret_cast(malloc(TUYA_BUFFER_SIZE)); + if (tuya_buffer != NULL) { + TuyaSerial = new TasmotaSerial(pin[GPIO_TUYA_RX], pin[GPIO_TUYA_TX], 2); + if (TuyaSerial->begin(9600)) { + if (TuyaSerial->hardwareSerial()) { ClaimSerial(); } + // Get MCU Configuration + snprintf_P(log_data, sizeof(log_data), "TYA: Request MCU configuration"); + AddLog(LOG_LEVEL_DEBUG); - TuyaSendCmd(TUYA_CMD_MCU_CONF); + TuyaSendCmd(TUYA_CMD_MCU_CONF); + } } } diff --git a/sonoff/xdrv_19_ps16dz_dimmer.ino b/sonoff/xdrv_19_ps16dz_dimmer.ino index aa6336bd6..f4780f78e 100644 --- a/sonoff/xdrv_19_ps16dz_dimmer.ino +++ b/sonoff/xdrv_19_ps16dz_dimmer.ino @@ -33,8 +33,8 @@ boolean ps16dz_power = false; uint8_t ps16dz_bright = 0; //uint64_t ps16dz_seq = 0; -char ps16dz_tx_buffer[PS16DZ_BUFFER_SIZE]; // Serial transmit buffer -char ps16dz_rx_buffer[PS16DZ_BUFFER_SIZE]; // Serial receive buffer +char *ps16dz_tx_buffer = NULL; // Serial transmit buffer +char *ps16dz_rx_buffer = NULL; // Serial receive buffer int ps16dz_byte_counter = 0; /*********************************************************************************************\ @@ -60,7 +60,7 @@ boolean PS16DZSetPower(void) snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s\",\"switch\":\"%s\""), ps16dz_tx_buffer, rpower?"on":"off"); snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); AddLog(LOG_LEVEL_DEBUG); - + PS16DZSerial->print(ps16dz_tx_buffer); PS16DZSerial->write(0x1B); PS16DZSerial->flush(); @@ -82,7 +82,7 @@ void PS16DZSerialDuty(uint8_t duty) snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s\",\"bright\":%d"), ps16dz_tx_buffer, round(duty * (100. / 255.))); snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer ); AddLog(LOG_LEVEL_DEBUG); - + PS16DZSerial->print(ps16dz_tx_buffer); PS16DZSerial->write(0x1B); PS16DZSerial->flush(); @@ -117,9 +117,15 @@ boolean PS16DZModuleSelected(void) void PS16DZInit(void) { - PS16DZSerial = new TasmotaSerial(pin[GPIO_RXD], pin[GPIO_TXD], 2); - if (PS16DZSerial->begin(19200)) { - if (PS16DZSerial->hardwareSerial()) { ClaimSerial(); } + ps16dz_tx_buffer = reinterpret_cast(malloc(PS16DZ_BUFFER_SIZE)); + if (ps16dz_tx_buffer != NULL) { + ps16dz_rx_buffer = reinterpret_cast(malloc(PS16DZ_BUFFER_SIZE)); + if (ps16dz_rx_buffer != NULL) { + PS16DZSerial = new TasmotaSerial(pin[GPIO_RXD], pin[GPIO_TXD], 2); + if (PS16DZSerial->begin(19200)) { + if (PS16DZSerial->hardwareSerial()) { ClaimSerial(); } + } + } } } @@ -174,7 +180,7 @@ void PS16DZSerialInput(void) //ps16dz_seq = strtoull(token3+1, NULL, 10); snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: sequence received: %s"), token3); AddLog(LOG_LEVEL_DEBUG); - } + } token = strtok_r(NULL, ",", &end_str); } }