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.

150 lines
4.2 KiB

6 years ago
  1. // -----------------------------------------------------------------------------
  2. // MAX6675 Sensor
  3. // Uses MAX6675_Thermocouple library
  4. // Copyright (C) 2017-2018 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. }
  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. _count = 1;
  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