From ec931fe5fe4749569c5b1e4b2882149d6d773c90 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 21 Dec 2018 11:36:55 +0100 Subject: [PATCH] Change RAM usage BMP/BME Change RAM usage BMP/BME I2C sensors --- sonoff/_changelog.ino | 1 + sonoff/xsns_09_bmp.ino | 58 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 227bb32c7..2950e4094 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,5 +1,6 @@ /* 6.4.0.1 20181217 * Add support for AZ-Instrument 7798 CO2 meter/datalogger (#4672) + * Change RAM usage BMP/BME I2C sensors * * 6.4.0 20181217 * Change GUI Configure Module by using AJAX for data fetch to cut page size (and memory use) by 40% diff --git a/sonoff/xsns_09_bmp.ino b/sonoff/xsns_09_bmp.ino index 68eef5c14..f5767a246 100755 --- a/sonoff/xsns_09_bmp.ino +++ b/sonoff/xsns_09_bmp.ino @@ -42,11 +42,8 @@ #define BMP_MAX_SENSORS 2 const char kBmpTypes[] PROGMEM = "BMP180|BMP280|BME280|BME680"; -uint8_t bmp_addresses[] = { BMP_ADDR1, BMP_ADDR2 }; -uint8_t bmp_count = 0; -uint8_t bmp_once = 1; -struct BMPSTRUCT { +typedef struct { uint8_t bmp_address; // I2C bus address char bmp_name[7]; // Sensor name - "BMPXXX" uint8_t bmp_type = 0; @@ -58,7 +55,13 @@ struct BMPSTRUCT { float bmp_temperature = 0.0; float bmp_pressure = 0.0; float bmp_humidity = 0.0; -} bmp_sensors[BMP_MAX_SENSORS]; +} bmp_sensors_t; + +uint8_t bmp_addresses[] = { BMP_ADDR1, BMP_ADDR2 }; +uint8_t bmp_count = 0; +uint8_t bmp_once = 1; + +bmp_sensors_t *bmp_sensors = NULL; /*********************************************************************************************\ * BMP085 and BME180 @@ -83,7 +86,7 @@ struct BMPSTRUCT { #define BMP180_OSS 3 -struct BMP180CALIBDATA { +typedef struct { int16_t cal_ac1; int16_t cal_ac2; int16_t cal_ac3; @@ -94,10 +97,17 @@ struct BMP180CALIBDATA { uint16_t cal_ac4; uint16_t cal_ac5; uint16_t cal_ac6; -} bmp180_cal_data[BMP_MAX_SENSORS]; +} bmp180_cal_data_t; + +bmp180_cal_data_t *bmp180_cal_data = NULL; boolean Bmp180Calibration(uint8_t bmp_idx) { + if (!bmp180_cal_data) { + bmp180_cal_data = (bmp180_cal_data_t*)malloc(BMP_MAX_SENSORS * sizeof(bmp180_cal_data_t)); + } + if (!bmp180_cal_data) { return false; } + bmp180_cal_data[bmp_idx].cal_ac1 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC1); bmp180_cal_data[bmp_idx].cal_ac2 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC2); bmp180_cal_data[bmp_idx].cal_ac3 = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_AC3); @@ -140,6 +150,8 @@ boolean Bmp180Calibration(uint8_t bmp_idx) void Bmp180Read(uint8_t bmp_idx) { + if (!bmp180_cal_data) { return; } + I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BMP180_REG_CONTROL, BMP180_TEMPERATURE); delay(5); // 5ms conversion time int ut = I2cRead16(bmp_sensors[bmp_idx].bmp_address, BMP180_REG_RESULT); @@ -211,8 +223,7 @@ void Bmp180Read(uint8_t bmp_idx) #define BME280_REGISTER_DIG_H5 0xE5 #define BME280_REGISTER_DIG_H6 0xE7 -struct BME280CALIBDATA -{ +typedef struct { uint16_t dig_T1; int16_t dig_T2; int16_t dig_T3; @@ -231,12 +242,19 @@ struct BME280CALIBDATA uint8_t dig_H1; uint8_t dig_H3; int8_t dig_H6; -} Bme280CalibrationData[BMP_MAX_SENSORS]; +} Bme280CalibrationData_t; + +Bme280CalibrationData_t *Bme280CalibrationData = NULL; boolean Bmx280Calibrate(uint8_t bmp_idx) { // if (I2cRead8(bmp_address, BMP_REGISTER_CHIPID) != BME280_CHIPID) return false; + if (!Bme280CalibrationData) { + Bme280CalibrationData = (Bme280CalibrationData_t*)malloc(BMP_MAX_SENSORS * sizeof(Bme280CalibrationData_t)); + } + if (!Bme280CalibrationData) { return false; } + Bme280CalibrationData[bmp_idx].dig_T1 = I2cRead16LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_T1); Bme280CalibrationData[bmp_idx].dig_T2 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_T2); Bme280CalibrationData[bmp_idx].dig_T3 = I2cReadS16_LE(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_DIG_T3); @@ -270,6 +288,8 @@ boolean Bmx280Calibrate(uint8_t bmp_idx) void Bme280Read(uint8_t bmp_idx) { + if (!Bme280CalibrationData) { return; } + int32_t adc_T = I2cRead24(bmp_sensors[bmp_idx].bmp_address, BME280_REGISTER_TEMPDATA); adc_T >>= 4; @@ -324,7 +344,7 @@ void Bme280Read(uint8_t bmp_idx) #include -struct bme680_dev gas_sensor[BMP_MAX_SENSORS]; +struct bme680_dev *gas_sensor = NULL; static void BmeDelayMs(uint32_t ms) { @@ -333,6 +353,11 @@ static void BmeDelayMs(uint32_t ms) boolean Bme680Init(uint8_t bmp_idx) { + if (!gas_sensor) { + gas_sensor = (bme680_dev*)malloc(BMP_MAX_SENSORS * sizeof(bme680_dev)); + } + if (!gas_sensor) { return false; } + gas_sensor[bmp_idx].dev_id = bmp_sensors[bmp_idx].bmp_address; gas_sensor[bmp_idx].intf = BME680_I2C_INTF; gas_sensor[bmp_idx].read = &I2cReadBuffer; @@ -377,6 +402,8 @@ boolean Bme680Init(uint8_t bmp_idx) void Bme680Read(uint8_t bmp_idx) { + if (!gas_sensor) { return; } + int8_t rslt = BME680_OK; if (BME680_CHIPID == bmp_sensors[bmp_idx].bmp_type) { @@ -421,6 +448,11 @@ void BmpDetect(void) { if (bmp_count) return; + if (!bmp_sensors) { + bmp_sensors = (bmp_sensors_t*)malloc(BMP_MAX_SENSORS * sizeof(bmp_sensors_t)); + } + if (!bmp_sensors) { return; } + for (byte i = 0; i < BMP_MAX_SENSORS; i++) { uint8_t bmp_type = I2cRead8(bmp_addresses[i], BMP_REGISTER_CHIPID); if (bmp_type) { @@ -458,6 +490,8 @@ void BmpDetect(void) void BmpRead(void) { + if (!bmp_sensors) { return; } + for (byte bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { switch (bmp_sensors[bmp_idx].bmp_type) { case BMP180_CHIPID: @@ -491,6 +525,8 @@ void BmpEverySecond(void) void BmpShow(boolean json) { + if (!bmp_sensors) { return; } + for (byte bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { if (bmp_sensors[bmp_idx].bmp_type) { float bmp_sealevel = 0.0;