From dfad1d5eec755b04a2a47ae6e21b24c326cc1942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 6 Feb 2018 20:59:45 +0100 Subject: [PATCH] Added event callback to sensors --- code/espurna/sensor.ino | 11 +++++++++++ code/espurna/sensors/BaseSensor.h | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 9ffa7ceb..9b4b4bf8 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -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() { for (unsigned char i=0; i<_sensors.size(); i++) { @@ -430,6 +436,11 @@ void _sensorConfigure() { // Force sensor to reload config _sensors[i]->begin(); + // Hook callback + _sensors[i]->onEvent([i](unsigned char type, const char * payload) { + _sensorCallback(i, type, payload); + }); + #if HLW8012_SUPPORT diff --git a/code/espurna/sensors/BaseSensor.h b/code/espurna/sensors/BaseSensor.h index 1daf41f3..89ba11c1 100644 --- a/code/espurna/sensors/BaseSensor.h +++ b/code/espurna/sensors/BaseSensor.h @@ -21,6 +21,8 @@ #define SENSOR_ERROR_I2C 6 // Wrong or locked I2C address #define SENSOR_ERROR_GPIO_USED 7 // The GPIO is already in use +typedef std::function TSensorCallback; + class BaseSensor { public: @@ -79,8 +81,12 @@ class BaseSensor { // Number of available slots unsigned char count() { return _count; } + // Hook for event callback + void onEvent(TSensorCallback fn) { _callback = fn; }; + protected: + TSensorCallback _callback = NULL; unsigned char _sensor_id = 0x00; int _error = 0; bool _dirty = true;