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.

90 lines
2.5 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 (35 <= type && type <= 37) {
  23. _type = type;
  24. }
  25. }
  26. unsigned char getType() {
  27. return _type;
  28. }
  29. // ---------------------------------------------------------------------
  30. // Sensor API
  31. // ---------------------------------------------------------------------
  32. // Initialization method, must be idempotent
  33. void begin() {
  34. pinMode(0, INPUT);
  35. _ready = true;
  36. }
  37. // Descriptive name of the sensor
  38. String description() {
  39. char buffer[14];
  40. snprintf(buffer, sizeof(buffer), "TMP%d @ TOUT", _type);
  41. return String(buffer);
  42. }
  43. // Descriptive name of the slot # index
  44. String slot(unsigned char index) {
  45. return description();
  46. };
  47. // Address of the sensor (it could be the GPIO or I2C address)
  48. String address(unsigned char index) {
  49. return String("0");
  50. }
  51. // Type for slot # index
  52. unsigned char type(unsigned char index) {
  53. if (index == 0) return MAGNITUDE_TEMPERATURE;
  54. return MAGNITUDE_NONE;
  55. }
  56. // Current value for slot # index
  57. double value(unsigned char index) {
  58. if (index == 0) {
  59. double mV = 3300.0 * analogRead(0) / 1024.0;
  60. if (_type == TMP3X_TMP35) return mV / 10.0;
  61. if (_type == TMP3X_TMP36) return mV / 10.0 - 50.0;
  62. if (_type == TMP3X_TMP37) return mV / 20.0;
  63. }
  64. return 0;
  65. }
  66. private:
  67. unsigned char _type = TMP3X_TMP35;
  68. };
  69. #endif // SENSOR_SUPPORT && TMP3X_SUPPORT