-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempSensors.h
More file actions
89 lines (66 loc) · 2.79 KB
/
Copy pathTempSensors.h
File metadata and controls
89 lines (66 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
TempSensors.h
---------------------------------------------------------------------------
Non-blocking DS18B20 reader.
Reports raw sensor values only. All correction happens in the collector -
see TempConfig.h for why.
USAGE
tempSensors.begin(); // once, in setup()
tempSensors.update(motorIsRunning); // every loop pass
update() does at most one 1-Wire transaction per call and never waits for a
conversion, so the main loop keeps feeding the current sampler.
Sensors are indexed in 1-Wire address order, which is stable across reboots,
so index 0 is always the same physical probe.
*/
#ifndef TEMP_SENSORS_H
#define TEMP_SENSORS_H
#include <Arduino.h>
#include "TempConfig.h"
class TempSensors {
public:
void begin();
// Call every loop pass. At most one bus transaction happens per call, and
// reads are spaced TEMP_READ_SPACING_MS apart so they land in different
// telemetry windows. motorRunning suspends bus traffic entirely when the
// TEMP_READ_DURING_RUN policy says to.
void update(bool motorRunning);
uint8_t count() const { return count_; }
bool enabled() const { return TEMP_ENABLED && count_ > 0; }
bool valid(uint8_t index) const;
float celsius(uint8_t index) const; // raw, exactly as the sensor read
unsigned long ageMs(uint8_t index) const; // since this sensor last read
const char* address(uint8_t index) const; // 16-char hex ROM code
const char* label(uint8_t index) const; // where this probe is installed
// Time the last 1-Wire transaction took, for verifying it is not starving
// the current sampler.
unsigned long lastBusMicros() const { return lastBusMicros_; }
unsigned long busyMicrosThisWindow(); // reads and clears the accumulator
bool conversionPending() const { return state_ == State::CONVERTING; }
// The hardware GPIO the bus actually ended up on, after pin remapping.
uint8_t gpio() const { return gpio_; }
unsigned long lastCycleAtMs() const { return lastCycleAtMs_; }
private:
enum class State : uint8_t { IDLE, CONVERTING, READING };
void discover();
void startConversion();
void readOne();
State state_ = State::IDLE;
uint8_t count_ = 0;
uint8_t readIndex_ = 0;
float rawC_[TEMP_MAX_SENSORS];
bool valid_[TEMP_MAX_SENSORS];
unsigned long readAtMs_[TEMP_MAX_SENSORS];
char addressHex_[TEMP_MAX_SENSORS][17];
unsigned long lastCycleAtMs_ = 0;
unsigned long conversionStartedAtMs_ = 0;
unsigned long conversionWaitMs_ = 750;
uint8_t gpio_ = 0;
unsigned long lastRescanAtMs_ = 0;
unsigned long lastReadAtMs_ = 0;
unsigned long lastBusMicros_ = 0;
unsigned long busyMicrosAccum_ = 0;
};
extern TempSensors tempSensors;
float celsiusToFahrenheit(float celsius);
float fahrenheitToCelsius(float fahrenheit);
#endif // TEMP_SENSORS_H