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.

60 lines
1.7 KiB

  1. // -----------------------------------------------------------------------------
  2. // Analog Sensor (maps to an analogRead)
  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 "BaseSensor.h"
  8. class AnalogSensor : public BaseSensor {
  9. public:
  10. // ---------------------------------------------------------------------
  11. // Public
  12. // ---------------------------------------------------------------------
  13. AnalogSensor(): BaseSensor() {
  14. _count = 1;
  15. _sensor_id = SENSOR_ANALOG_ID;
  16. }
  17. // ---------------------------------------------------------------------
  18. // Sensor API
  19. // ---------------------------------------------------------------------
  20. // Initialization method, must be idempotent
  21. void begin() {
  22. pinMode(0, INPUT);
  23. }
  24. // Descriptive name of the sensor
  25. String description() {
  26. return String("ANALOG @ GPIO0");
  27. }
  28. // Descriptive name of the slot # index
  29. String slot(unsigned char index) {
  30. return description();
  31. }
  32. // Type for slot # index
  33. magnitude_t type(unsigned char index) {
  34. _error = SENSOR_ERROR_OK;
  35. if (index == 0) return MAGNITUDE_ANALOG;
  36. _error = SENSOR_ERROR_OUT_OF_RANGE;
  37. return MAGNITUDE_NONE;
  38. }
  39. // Current value for slot # index
  40. double value(unsigned char index) {
  41. _error = SENSOR_ERROR_OK;
  42. if (index == 0) return analogRead(0);
  43. _error = SENSOR_ERROR_OUT_OF_RANGE;
  44. return 0;
  45. }
  46. };