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.

59 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. }
  16. // ---------------------------------------------------------------------
  17. // Sensor API
  18. // ---------------------------------------------------------------------
  19. // Initialization method, must be idempotent
  20. void begin() {
  21. pinMode(0, INPUT);
  22. }
  23. // Descriptive name of the sensor
  24. String description() {
  25. return String("ANALOG @ GPIO0");
  26. }
  27. // Descriptive name of the slot # index
  28. String slot(unsigned char index) {
  29. return description();
  30. }
  31. // Type for slot # index
  32. magnitude_t type(unsigned char index) {
  33. _error = SENSOR_ERROR_OK;
  34. if (index == 0) return MAGNITUDE_ANALOG;
  35. _error = SENSOR_ERROR_OUT_OF_RANGE;
  36. return MAGNITUDE_NONE;
  37. }
  38. // Current value for slot # index
  39. double value(unsigned char index) {
  40. _error = SENSOR_ERROR_OK;
  41. if (index == 0) return analogRead(0);
  42. _error = SENSOR_ERROR_OUT_OF_RANGE;
  43. return 0;
  44. }
  45. };