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.

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