Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // Abstract sensor class (other sensor classes extend this class)
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT
  6. #pragma once
  7. #include <Arduino.h>
  8. #include <ArduinoJson.h>
  9. #define GPIO_NONE 0x99
  10. #define SENSOR_ERROR_OK 0 // No error
  11. #define SENSOR_ERROR_OUT_OF_RANGE 1 // Result out of sensor range
  12. #define SENSOR_ERROR_WARM_UP 2 // Sensor is warming-up
  13. #define SENSOR_ERROR_TIMEOUT 3 // Response from sensor timed out
  14. #define SENSOR_ERROR_UNKNOWN_ID 4 // Sensor did not report a known ID
  15. #define SENSOR_ERROR_CRC 5 // Sensor data corrupted
  16. #define SENSOR_ERROR_I2C 6 // Wrong or locked I2C address
  17. #define SENSOR_ERROR_GPIO_USED 7 // The GPIO is already in use
  18. typedef std::function<void(unsigned char, const char *)> TSensorCallback;
  19. class BaseSensor {
  20. public:
  21. // Constructor
  22. BaseSensor() {}
  23. // Destructor
  24. ~BaseSensor() {}
  25. // Initialization method, must be idempotent
  26. virtual void begin() {}
  27. // Loop-like method, call it in your main loop
  28. virtual void tick() {}
  29. // Pre-read hook (usually to populate registers with up-to-date data)
  30. virtual void pre() {}
  31. // Post-read hook (usually to reset things)
  32. virtual void post() {}
  33. // Descriptive name of the sensor
  34. virtual String description() {}
  35. // Address of the sensor (it could be the GPIO or I2C address)
  36. virtual String address(unsigned char index) {}
  37. // Descriptive name of the slot # index
  38. virtual String slot(unsigned char index) {};
  39. // Type for slot # index
  40. virtual unsigned char type(unsigned char index) {}
  41. // Current value for slot # index
  42. virtual double value(unsigned char index) {}
  43. // Retrieve current instance configuration
  44. virtual void getConfig(JsonObject& root) {};
  45. // Save current instance configuration
  46. virtual void setConfig(JsonObject& root) {};
  47. // Load the configuration manifest
  48. static void manifest(JsonArray& root) {};
  49. // Sensor ID
  50. unsigned char getID() { return _sensor_id; };
  51. // Return sensor status (true for ready)
  52. bool status() { return _error == 0; }
  53. // Return sensor last internal error
  54. int error() { return _error; }
  55. // Number of available slots
  56. unsigned char count() { return _count; }
  57. // Hook for event callback
  58. void onEvent(TSensorCallback fn) { _callback = fn; };
  59. protected:
  60. TSensorCallback _callback = NULL;
  61. unsigned char _sensor_id = 0x00;
  62. int _error = 0;
  63. bool _dirty = true;
  64. unsigned char _count = 0;
  65. };
  66. #endif