-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx30_temp.ino
91 lines (76 loc) · 1.9 KB
/
x30_temp.ino
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
90
91
Task tempTask(1, TASK_FOREVER, &tempConvertCb, &scheduler, true);
typedef struct {
uint8_t magic;
float a;
float b;
} TempConfig;
OneWire tempWire(PIN_TEMP);
float tempCurrent;
float tempUnadjusted;
TempConfig tempConfig;
uint32_t tempTime;
#define TEMP_MAGIC 0x5e
void tempSetup() {
tempCurrent = 0;
tempUnadjusted = 0;
tempTime = 0;
EEPROM.get(0, tempConfig);
if (tempConfig.magic == TEMP_MAGIC) {
logInfo("Got temp config");
logValue(" a: ", tempConfig.a);
logValue(" b: ", tempConfig.b);
} else {
logInfo("Invalid temp config, resetting");
tempConfig.magic = TEMP_MAGIC;
tempConfig.a = 1;
tempConfig.b = 0;
}
}
void tempConvertCb() {
tempWire.reset();
tempWire.skip();
tempWire.write(0x44);
tempTask.setInterval(1000);
tempTask.setCallback(&tempReadCb);
}
uint32_t tempGetAge() {
return tempTime == 0 ? 0 : millis() - tempTime;
}
void tempCalcCurrent() {
tempCurrent = tempUnadjusted * tempConfig.a + tempConfig.b;
}
void tempSetConfig(float a, float b) {
tempConfig.a = a;
tempConfig.b = b;
tempConfig.magic = TEMP_MAGIC;
EEPROM.put(0, tempConfig);
EEPROM.commit();
tempCalcCurrent();
logInfo("Saved temp config");
logValue(" a: ", tempConfig.a);
logValue(" b: ", tempConfig.b);
}
// reading works only when sensor is set to 12bit precision
void tempReadCb() {
if (tempWire.reset()) {
tempWire.skip();
tempWire.write(0xBE);
uint8_t data[9];
for (uint8_t i = 0; i < 9; i++) {
data[i] = tempWire.read();
}
if (data[8] == OneWire::crc8(data, 8)) {
int16_t raw = (data[1] << 8) | data[0];
tempUnadjusted = (float)raw / 16.0;
tempCalcCurrent();
tempTime = millis();
tempTask.setInterval(30000);
logValue("Got temp: ", tempCurrent);
} else {
logInfo("CRC mismatch");
}
} else {
logInfo("Unable to reset bus");
}
tempTask.setCallback(&tempConvertCb);
}