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.

120 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. // -----------------------------------------------------------------------------
  2. // NTC Sensor (maps to a NTCSensor)
  3. // Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && NTC_SUPPORT
  6. #pragma once
  7. #include "AnalogSensor.h"
  8. extern "C" {
  9. #include "../libs/fs_math.h"
  10. }
  11. class NTCSensor : public AnalogSensor {
  12. public:
  13. // ---------------------------------------------------------------------
  14. // Public
  15. // ---------------------------------------------------------------------
  16. NTCSensor() {
  17. _count = 1;
  18. _sensor_id = SENSOR_NTC_ID;
  19. }
  20. void setBeta(unsigned long beta) {
  21. if (beta > 0) _beta = beta;
  22. }
  23. void setUpstreamResistor(unsigned long resistance) {
  24. _resistance_up = resistance;
  25. if (_resistance_up > 0) _resistance_down = 0;
  26. }
  27. void setDownstreamResistor(unsigned long resistance) {
  28. _resistance_down = resistance;
  29. if (_resistance_down > 0) _resistance_up = 0;
  30. }
  31. void setR0(unsigned long resistance) {
  32. if (resistance > 0) _R0 = resistance;
  33. }
  34. void setT0(double temperature) {
  35. if (temperature > 0) _T0 = temperature;
  36. }
  37. // ---------------------------------------------------------------------
  38. // ---------------------------------------------------------------------
  39. // Sensor API
  40. // ---------------------------------------------------------------------
  41. // Descriptive name of the sensor
  42. String description() {
  43. return String("NTC @ TOUT");
  44. }
  45. // Descriptive name of the slot # index
  46. String description(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. double temperature = 0;
  61. if (index == 0) {
  62. // sampled reading
  63. double read = _read();
  64. // Ru = (1023/c - 1) * Rd
  65. double resistance;
  66. double alpha = (1023.0 / read) - 1;
  67. if (_resistance_down > 0) {
  68. resistance = _resistance_down * alpha;
  69. } else if (0 == alpha) {
  70. resistance = _R0;
  71. } else {
  72. resistance = _resistance_up / alpha;
  73. }
  74. // 1/T = 1/T0 + 1/B * ln(R/R0)
  75. temperature = fs_log(resistance / _R0);
  76. temperature = (1.0 / _T0) + (temperature / _beta);
  77. temperature = 1.0 / temperature - 273.15;
  78. }
  79. return temperature;
  80. }
  81. protected:
  82. unsigned long _beta = NTC_BETA;
  83. unsigned long _resistance_up = NTC_R_UP;
  84. unsigned long _resistance_down = NTC_R_DOWN;
  85. unsigned long _R0 = NTC_R0;
  86. double _T0 = NTC_T0;
  87. };
  88. #endif // SENSOR_SUPPORT && NTC_SUPPORT