Skip to content

Commit

Permalink
Added configuration of enabled decoders at run time (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs authored May 8, 2024
1 parent 3f186f8 commit 8fa80e2
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 80 deletions.
85 changes: 51 additions & 34 deletions examples/BresserWeatherSensorMQTTCustom/src/WeatherSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
// 20240423 Implemented setting of sensor_ids_inc/sensor_ids_exc to empty if first value in
// Preferences is 0x00000000
// 20240506 Changed sensor from array to std::vector, added getSensorsCfg() / setSensorsCfg()
// 20240507 Added configuration of enabled decoders at run time
//
// ToDo:
// -
Expand Down Expand Up @@ -129,9 +130,10 @@ void
int16_t WeatherSensor::begin(void)
{
uint8_t maxSensors;
getSensorsCfg(maxSensors, rxFlags);
getSensorsCfg(maxSensors, rxFlags, enDecoders);
log_d("max_sensors: %u", maxSensors);
log_d("rx_flags: %u", rxFlags);
log_d("en_decoders: %u", enDecoders);
sensor.resize(maxSensors);

// List of sensor IDs to be excluded - can be empty
Expand Down Expand Up @@ -358,43 +360,53 @@ DecodeStatus WeatherSensor::decodeMessage(const uint8_t *msg, uint8_t msgSize)
DecodeStatus decode_res = DECODE_INVALID;

#ifdef BRESSER_7_IN_1
decode_res = decodeBresser7In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
if (enDecoders & DECODER_7IN1) {
decode_res = decodeBresser7In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
}
}
#endif
#ifdef BRESSER_6_IN_1
decode_res = decodeBresser6In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
if (enDecoders & DECODER_6IN1) {
decode_res = decodeBresser6In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
}
}
#endif
#ifdef BRESSER_5_IN_1
decode_res = decodeBresser5In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
if (enDecoders & DECODER_5IN1) {
decode_res = decodeBresser5In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
}
}
#endif
#ifdef BRESSER_LIGHTNING
decode_res = decodeBresserLightningPayload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
if (enDecoders & DECODER_LIGHTNING) {
decode_res = decodeBresserLightningPayload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
}
}
#endif
#ifdef BRESSER_LEAKAGE
decode_res = decodeBresserLeakagePayload(msg, msgSize);
if (enDecoders & DECODER_LEAKAGE) {
decode_res = decodeBresserLeakagePayload(msg, msgSize);
}
#endif
return decode_res;
}
Expand Down Expand Up @@ -677,23 +689,28 @@ uint8_t WeatherSensor::getSensorsExc(uint8_t *payload)
}

// Set sensor configuration and store in in Preferences
void WeatherSensor::setSensorsCfg(uint8_t maxSensors, uint8_t rxFlags)
void WeatherSensor::setSensorsCfg(uint8_t max_sensors, uint8_t rx_flags, uint8_t en_decoders)
{
rxFlags = rx_flags;
enDecoders = enDecoders;
cfgPrefs.begin("BWS-CFG", false);
cfgPrefs.putUChar("maxsensors", maxSensors);
cfgPrefs.getUChar("rxflags", rxFlags);
cfgPrefs.putUChar("maxsensors", max_sensors);
cfgPrefs.putUChar("rxflags", rx_flags);
cfgPrefs.putUChar("endec", en_decoders);
cfgPrefs.end();
log_d("max_sensors: %u", maxSensors);
log_d("max_sensors: %u", max_sensors);
log_d("rx_flags: %u", rxFlags);
sensor.resize(maxSensors);
log_d("enabled_decoders: %u", enDecoders);
sensor.resize(max_sensors);
}

// Get sensor configuration from Preferences
void WeatherSensor::getSensorsCfg(uint8_t &maxSensors, uint8_t &rxFlags)
void WeatherSensor::getSensorsCfg(uint8_t &max_sensors, uint8_t &rx_flags, uint8_t &en_decoders)
{
cfgPrefs.begin("BWS-CFG", false);
maxSensors = cfgPrefs.getUChar("maxsensors", MAX_SENSORS_DEFAULT);
rxFlags = cfgPrefs.getUChar("rxflags", DATA_COMPLETE);
max_sensors = cfgPrefs.getUChar("maxsensors", MAX_SENSORS_DEFAULT);
rx_flags = cfgPrefs.getUChar("rxflags", DATA_COMPLETE);
en_decoders = cfgPrefs.getUChar("endec", 0xFF);
cfgPrefs.end();
}

Expand Down
24 changes: 18 additions & 6 deletions examples/BresserWeatherSensorMQTTCustom/src/WeatherSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
// 20240409 Added radioReset()
// 20240417 Added sensor configuration at run time
// 20240506 Changed sensor from array to std::vector, added getSensorCfg() / setSensorCfg()
// 20240507 Added configuration of enabled decoders at run time
//
// ToDo:
// -
Expand Down Expand Up @@ -131,6 +132,13 @@
#define DATA_TYPE 0x2 // at least one slot with specific sensor type
#define DATA_ALL_SLOTS 0x8 // all slots completed

// Flags for checking enabled decoders
#define DECODER_5IN1 0x01
#define DECODER_6IN1 0x02
#define DECODER_7IN1 0x04
#define DECODER_LIGHTNING 0x08
#define DECODER_LEAKAGE 0x10

// Message buffer size
#define MSG_BUF_SIZE 27

Expand Down Expand Up @@ -163,6 +171,7 @@ class WeatherSensor {
Preferences cfgPrefs; //!< Preferences (stored in flash memory)
std::vector<uint32_t> sensor_ids_inc;
std::vector<uint32_t> sensor_ids_exc;
static uint8_t _dummy_en_decoders;

public:
/*!
Expand Down Expand Up @@ -320,6 +329,7 @@ class WeatherSensor {
std::vector<sensor_t> sensor; //!< sensor data array
float rssi = 0.0; //!< received signal strength indicator in dBm
uint8_t rxFlags; //!< receive flags (see getData())
uint8_t enDecoders = 0xFF; //!< enabled Decoders

/*!
\brief Generates data otherwise received and decoded from a radio message.
Expand Down Expand Up @@ -394,10 +404,11 @@ class WeatherSensor {
/*!
* Set maximum number of sensors and store it in Preferences
*
* \param maxSensors maximum number of sensors
* \param rxFlags receive flags (see getData())
* \param max_sensors maximum number of sensors
* \param rx_flags receive flags (see getData())
* \param en_decoders enabled decoders
*/
void setSensorsCfg(uint8_t maxSensors, uint8_t rxFlags);
void setSensorsCfg(uint8_t max_sensors, uint8_t rx_flags, uint8_t en_decoders = 0xFF);

/*!
* Get sensors include list from Preferences
Expand All @@ -420,10 +431,11 @@ class WeatherSensor {
/*!
* Get maximum number of sensors from Preferences
*
* \param maxSensors maximum number of sensors
* \param rxFlags receive flags (see getData())
* \param max_sensors maximum number of sensors
* \param rx_flags receive flags (see getData())
* \param en_decoders enabled decoders
*/
void getSensorsCfg(uint8_t &maxSensors, uint8_t &rxFlags);
void getSensorsCfg(uint8_t &max_sensors, uint8_t &rx_flags, uint8_t &en_decoders = _dummy_en_decoders);

private:
struct Sensor *pData; //!< pointer to slot in sensor data array
Expand Down
85 changes: 51 additions & 34 deletions src/WeatherSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
// 20240423 Implemented setting of sensor_ids_inc/sensor_ids_exc to empty if first value in
// Preferences is 0x00000000
// 20240506 Changed sensor from array to std::vector, added getSensorsCfg() / setSensorsCfg()
// 20240507 Added configuration of enabled decoders at run time
//
// ToDo:
// -
Expand Down Expand Up @@ -129,9 +130,10 @@ void
int16_t WeatherSensor::begin(void)
{
uint8_t maxSensors;
getSensorsCfg(maxSensors, rxFlags);
getSensorsCfg(maxSensors, rxFlags, enDecoders);
log_d("max_sensors: %u", maxSensors);
log_d("rx_flags: %u", rxFlags);
log_d("en_decoders: %u", enDecoders);
sensor.resize(maxSensors);

// List of sensor IDs to be excluded - can be empty
Expand Down Expand Up @@ -358,43 +360,53 @@ DecodeStatus WeatherSensor::decodeMessage(const uint8_t *msg, uint8_t msgSize)
DecodeStatus decode_res = DECODE_INVALID;

#ifdef BRESSER_7_IN_1
decode_res = decodeBresser7In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
if (enDecoders & DECODER_7IN1) {
decode_res = decodeBresser7In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
}
}
#endif
#ifdef BRESSER_6_IN_1
decode_res = decodeBresser6In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
if (enDecoders & DECODER_6IN1) {
decode_res = decodeBresser6In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
}
}
#endif
#ifdef BRESSER_5_IN_1
decode_res = decodeBresser5In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
if (enDecoders & DECODER_5IN1) {
decode_res = decodeBresser5In1Payload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
}
}
#endif
#ifdef BRESSER_LIGHTNING
decode_res = decodeBresserLightningPayload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
if (enDecoders & DECODER_LIGHTNING) {
decode_res = decodeBresserLightningPayload(msg, msgSize);
if (decode_res == DECODE_OK ||
decode_res == DECODE_FULL ||
decode_res == DECODE_SKIP)
{
return decode_res;
}
}
#endif
#ifdef BRESSER_LEAKAGE
decode_res = decodeBresserLeakagePayload(msg, msgSize);
if (enDecoders & DECODER_LEAKAGE) {
decode_res = decodeBresserLeakagePayload(msg, msgSize);
}
#endif
return decode_res;
}
Expand Down Expand Up @@ -677,23 +689,28 @@ uint8_t WeatherSensor::getSensorsExc(uint8_t *payload)
}

// Set sensor configuration and store in in Preferences
void WeatherSensor::setSensorsCfg(uint8_t maxSensors, uint8_t rxFlags)
void WeatherSensor::setSensorsCfg(uint8_t max_sensors, uint8_t rx_flags, uint8_t en_decoders)
{
rxFlags = rx_flags;
enDecoders = enDecoders;
cfgPrefs.begin("BWS-CFG", false);
cfgPrefs.putUChar("maxsensors", maxSensors);
cfgPrefs.putUChar("rxflags", rxFlags);
cfgPrefs.putUChar("maxsensors", max_sensors);
cfgPrefs.putUChar("rxflags", rx_flags);
cfgPrefs.putUChar("endec", en_decoders);
cfgPrefs.end();
log_d("max_sensors: %u", maxSensors);
log_d("max_sensors: %u", max_sensors);
log_d("rx_flags: %u", rxFlags);
sensor.resize(maxSensors);
log_d("enabled_decoders: %u", enDecoders);
sensor.resize(max_sensors);
}

// Get sensor configuration from Preferences
void WeatherSensor::getSensorsCfg(uint8_t &maxSensors, uint8_t &rxFlags)
void WeatherSensor::getSensorsCfg(uint8_t &max_sensors, uint8_t &rx_flags, uint8_t &en_decoders)
{
cfgPrefs.begin("BWS-CFG", false);
maxSensors = cfgPrefs.getUChar("maxsensors", MAX_SENSORS_DEFAULT);
rxFlags = cfgPrefs.getUChar("rxflags", DATA_COMPLETE);
max_sensors = cfgPrefs.getUChar("maxsensors", MAX_SENSORS_DEFAULT);
rx_flags = cfgPrefs.getUChar("rxflags", DATA_COMPLETE);
en_decoders = cfgPrefs.getUChar("endec", 0xFF);
cfgPrefs.end();
}

Expand Down
Loading

0 comments on commit 8fa80e2

Please sign in to comment.