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.

125 lines
3.5 KiB

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. // Set ADC to TOUT pin
  8. #undef ADC_MODE_VALUE
  9. #define ADC_MODE_VALUE ADC_TOUT
  10. #include "Arduino.h"
  11. #include "AnalogSensor.h"
  12. extern "C" {
  13. #include "../libs/fs_math.h"
  14. }
  15. class NTCSensor : public AnalogSensor {
  16. public:
  17. // ---------------------------------------------------------------------
  18. // Public
  19. // ---------------------------------------------------------------------
  20. NTCSensor(): AnalogSensor() {
  21. _count = 1;
  22. _sensor_id = SENSOR_NTC_ID;
  23. }
  24. void setBeta(unsigned long beta) {
  25. if (beta > 0) _beta = beta;
  26. }
  27. void setUpstreamResistor(unsigned long resistance) {
  28. _resistance_up = resistance;
  29. if (_resistance_up > 0) _resistance_down = 0;
  30. }
  31. void setDownstreamResistor(unsigned long resistance) {
  32. _resistance_down = resistance;
  33. if (_resistance_down > 0) _resistance_up = 0;
  34. }
  35. void setR0(unsigned long resistance) {
  36. if (resistance > 0) _R0 = resistance;
  37. }
  38. void setT0(double temperature) {
  39. if (temperature > 0) _T0 = temperature;
  40. }
  41. // ---------------------------------------------------------------------
  42. // ---------------------------------------------------------------------
  43. // Sensor API
  44. // ---------------------------------------------------------------------
  45. // Descriptive name of the sensor
  46. String description() {
  47. return String("NTC @ TOUT");
  48. }
  49. // Descriptive name of the slot # index
  50. String slot(unsigned char index) {
  51. return description();
  52. };
  53. // Address of the sensor (it could be the GPIO or I2C address)
  54. String address(unsigned char index) {
  55. return String("0");
  56. }
  57. // Type for slot # index
  58. unsigned char type(unsigned char index) {
  59. if (index == 0) return MAGNITUDE_TEMPERATURE;
  60. return MAGNITUDE_NONE;
  61. }
  62. // Current value for slot # index
  63. double value(unsigned char index) {
  64. double temperature = 0;
  65. if (index == 0) {
  66. // sampled reading
  67. double read = _read();
  68. // Ru = (1023/c - 1) * Rd
  69. double resistance;
  70. double alpha = (1023.0 / read) - 1;
  71. if (_resistance_down > 0) {
  72. resistance = _resistance_down * alpha;
  73. } else if (0 == alpha) {
  74. resistance = _R0;
  75. } else {
  76. resistance = _resistance_up / alpha;
  77. }
  78. // 1/T = 1/T0 + 1/B * ln(R/R0)
  79. temperature = fs_log(resistance / _R0);
  80. temperature = (1.0 / _T0) + (temperature / _beta);
  81. temperature = 1.0 / temperature - 273.15;
  82. }
  83. return temperature;
  84. }
  85. protected:
  86. unsigned long _beta = NTC_BETA;
  87. unsigned long _resistance_up = NTC_R_UP;
  88. unsigned long _resistance_down = NTC_R_DOWN;
  89. unsigned long _R0 = NTC_R0;
  90. double _T0 = NTC_T0;
  91. };
  92. #endif // SENSOR_SUPPORT && NTC_SUPPORT