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.

84 lines
2.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // Abstract sensor class (other sensor classes extend this class)
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include <Arduino.h>
  7. #include <ArduinoJson.h>
  8. #define GPIO_NONE 0x99
  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. class BaseSensor {
  18. public:
  19. // Constructor
  20. BaseSensor() {}
  21. // Destructor
  22. ~BaseSensor() {}
  23. // Initialization method, must be idempotent
  24. virtual void begin() {}
  25. // Loop-like method, call it in your main loop
  26. virtual void tick() {}
  27. // Pre-read hook (usually to populate registers with up-to-date data)
  28. virtual void pre() {}
  29. // Post-read hook (usually to reset things)
  30. virtual void post() {}
  31. // Descriptive name of the sensor
  32. virtual String description() {}
  33. // Type for slot # index
  34. virtual magnitude_t type(unsigned char index) {}
  35. // Current value for slot # index
  36. virtual double value(unsigned char index) {}
  37. // Retrieve current instance configuration
  38. virtual void getConfig(JsonObject& root) {};
  39. // Save current instance configuration
  40. virtual void setConfig(JsonObject& root) {};
  41. // Load the configuration manifest
  42. static void manifest(JsonArray& root) {};
  43. // Descriptive name of the slot # index
  44. String slot(unsigned char index) { return description(); }
  45. // Sensor ID
  46. unsigned char getID() { return _sensor_id; };
  47. // Return sensor status (true for ready)
  48. bool status() { return _error == 0; }
  49. // Return sensor last internal error
  50. int error() { return _error; }
  51. // Number of available slots
  52. unsigned char count() { return _count; }
  53. protected:
  54. unsigned char _sensor_id = 0x00;
  55. int _error = 0;
  56. bool _dirty = true;
  57. unsigned char _count = 0;
  58. };