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.

94 lines
2.6 KiB

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