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.

144 lines
4.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. // -----------------------------------------------------------------------------
  2. // MAX6675 Sensor
  3. // Uses MAX6675_Thermocouple library
  4. // Copyright (C) 2017-2019 by Xose Pérez <andrade dot luciano at gmail dot com>
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && MAX6675_SUPPORT
  7. #pragma once
  8. #include <MAX6675.h>
  9. #include <vector>
  10. #include "BaseSensor.h"
  11. #define MAX6675_READ_INTERVAL 3000
  12. class MAX6675Sensor : public BaseSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. MAX6675Sensor() {
  18. _sensor_id = SENSOR_MAX6675_ID;
  19. _count = 1;
  20. }
  21. ~MAX6675Sensor() {
  22. }
  23. // ---------------------------------------------------------------------
  24. // ---------------------------------------------------------------------
  25. void setCS(unsigned char pin_cs) {
  26. if (_pin_cs == pin_cs) return;
  27. _pin_cs = pin_cs;
  28. _dirty = true;
  29. }
  30. void setSO(unsigned char pin_so) {
  31. if (_pin_so == pin_so) return;
  32. _pin_so = pin_so;
  33. _dirty = true;
  34. }
  35. void setSCK(unsigned char pin_sck) {
  36. if (_pin_sck == pin_sck) return;
  37. _pin_sck = pin_sck;
  38. _dirty = true;
  39. }
  40. // ---------------------------------------------------------------------
  41. // Sensor API
  42. // ---------------------------------------------------------------------
  43. // Initialization method, must be idempotent
  44. void begin() {
  45. if (!_dirty) return;
  46. //// MAX6675
  47. int units = 1; // Units to readout temp (0 = raw, 1 = ˚C, 2 = ˚F)
  48. if (_max) delete _max;
  49. _max = new MAX6675(_pin_cs, _pin_so, _pin_sck, units);
  50. _ready = true;
  51. _dirty = false;
  52. }
  53. // Loop-like method, call it in your main loop
  54. void tick() {
  55. static unsigned long last = 0;
  56. if (millis() - last < MAX6675_READ_INTERVAL) return;
  57. last = millis();
  58. last_read = _max->read_temp();
  59. }
  60. // Descriptive name of the sensor
  61. String description() {
  62. char buffer[20];
  63. //snprintf(buffer, sizeof(buffer), "MAX6675 @ CS %d", _gpio);
  64. snprintf(buffer, sizeof(buffer), "MAX6675");
  65. return String(buffer);
  66. }
  67. String address(unsigned char index) {
  68. return String("@ address");
  69. }
  70. // Address of the device
  71. // Descriptive name of the slot # index
  72. String description(unsigned char index) {
  73. if (index < _count) {
  74. // char buffer[40];
  75. // uint8_t * address = _devices[index].address;
  76. // snprintf(buffer, sizeof(buffer), "%s (%02X%02X%02X%02X%02X%02X%02X%02X) @ GPIO%d",
  77. // chipAsString(index).c_str(),
  78. // address[0], address[1], address[2], address[3],
  79. // address[4], address[5], address[6], address[7],
  80. // _gpio
  81. // );
  82. return description();
  83. }
  84. return String();
  85. }
  86. // Type for slot # index
  87. unsigned char type(unsigned char index) {
  88. if (index < _count) return MAGNITUDE_TEMPERATURE;
  89. return MAGNITUDE_NONE;
  90. }
  91. // Pre-read hook (usually to populate registers with up-to-date data)
  92. void pre() {
  93. _error = SENSOR_ERROR_OK;
  94. }
  95. // Current value for slot # index
  96. double value(unsigned char index) {
  97. return last_read;
  98. }
  99. protected:
  100. // ---------------------------------------------------------------------
  101. // Protected
  102. // ---------------------------------------------------------------------
  103. unsigned int _pin_cs = MAX6675_CS_PIN;
  104. unsigned int _pin_so = MAX6675_SO_PIN;
  105. unsigned int _pin_sck = MAX6675_SCK_PIN;
  106. bool _busy = false;
  107. double last_read = 0;
  108. MAX6675 * _max = NULL;
  109. };
  110. #endif // SENSOR_SUPPORT && MAX6675_SUPPORT