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.

104 lines
3.3 KiB

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