Browse Source

Added event callback to sensors

softuart
Xose Pérez 6 years ago
parent
commit
dfad1d5eec
2 changed files with 17 additions and 0 deletions
  1. +11
    -0
      code/espurna/sensor.ino
  2. +6
    -0
      code/espurna/sensors/BaseSensor.h

+ 11
- 0
code/espurna/sensor.ino View File

@ -396,6 +396,12 @@ void _sensorInit() {
} }
void _sensorCallback(unsigned char i, unsigned char type, const char * payload) {
DEBUG_MSG_P(PSTR("[SENSOR] Sensor #%u callback, type %u, payload: '%s'\n"), i, type, payload);
}
void _sensorConfigure() { void _sensorConfigure() {
for (unsigned char i=0; i<_sensors.size(); i++) { for (unsigned char i=0; i<_sensors.size(); i++) {
@ -430,6 +436,11 @@ void _sensorConfigure() {
// Force sensor to reload config // Force sensor to reload config
_sensors[i]->begin(); _sensors[i]->begin();
// Hook callback
_sensors[i]->onEvent([i](unsigned char type, const char * payload) {
_sensorCallback(i, type, payload);
});
#if HLW8012_SUPPORT #if HLW8012_SUPPORT


+ 6
- 0
code/espurna/sensors/BaseSensor.h View File

@ -21,6 +21,8 @@
#define SENSOR_ERROR_I2C 6 // Wrong or locked I2C address #define SENSOR_ERROR_I2C 6 // Wrong or locked I2C address
#define SENSOR_ERROR_GPIO_USED 7 // The GPIO is already in use #define SENSOR_ERROR_GPIO_USED 7 // The GPIO is already in use
typedef std::function<void(unsigned char, const char *)> TSensorCallback;
class BaseSensor { class BaseSensor {
public: public:
@ -79,8 +81,12 @@ class BaseSensor {
// Number of available slots // Number of available slots
unsigned char count() { return _count; } unsigned char count() { return _count; }
// Hook for event callback
void onEvent(TSensorCallback fn) { _callback = fn; };
protected: protected:
TSensorCallback _callback = NULL;
unsigned char _sensor_id = 0x00; unsigned char _sensor_id = 0x00;
int _error = 0; int _error = 0;
bool _dirty = true; bool _dirty = true;


Loading…
Cancel
Save