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.

92 lines
2.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // TMP3X Temperature Analog Sensor
  3. // Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && TMP3X_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #define TMP3X_TMP35 35
  10. #define TMP3X_TMP36 36
  11. #define TMP3X_TMP37 37
  12. class TMP3XSensor : public BaseSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. TMP3XSensor(): BaseSensor() {
  18. _count = 1;
  19. _sensor_id = SENSOR_TMP3X_ID;
  20. }
  21. void setType(unsigned char type) {
  22. if (type == _type) return;
  23. if (35 <= type && type <= 37) {
  24. _type = type;
  25. _dirty = true;
  26. }
  27. }
  28. unsigned char getType() {
  29. return _type;
  30. }
  31. // ---------------------------------------------------------------------
  32. // Sensor API
  33. // ---------------------------------------------------------------------
  34. // Initialization method, must be idempotent
  35. void begin() {
  36. pinMode(0, INPUT);
  37. _ready = true;
  38. }
  39. // Descriptive name of the sensor
  40. String description() {
  41. char buffer[14];
  42. snprintf(buffer, sizeof(buffer), "TMP%d @ TOUT", _type);
  43. return String(buffer);
  44. }
  45. // Descriptive name of the slot # index
  46. String slot(unsigned char index) {
  47. return description();
  48. };
  49. // Address of the sensor (it could be the GPIO or I2C address)
  50. String address(unsigned char index) {
  51. return String("0");
  52. }
  53. // Type for slot # index
  54. unsigned char type(unsigned char index) {
  55. if (index == 0) return MAGNITUDE_TEMPERATURE;
  56. return MAGNITUDE_NONE;
  57. }
  58. // Current value for slot # index
  59. double value(unsigned char index) {
  60. if (index == 0) {
  61. double mV = 3300.0 * analogRead(0) / 1024.0;
  62. if (_type == TMP3X_TMP35) return mV / 10.0;
  63. if (_type == TMP3X_TMP36) return mV / 10.0 - 50.0;
  64. if (_type == TMP3X_TMP37) return mV / 20.0;
  65. }
  66. return 0;
  67. }
  68. private:
  69. unsigned char _type = TMP3X_TMP35;
  70. };
  71. #endif // SENSOR_SUPPORT && TMP3X_SUPPORT