forked from vitotai/BrewPiLess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneWireTempSensor.cpp
More file actions
120 lines (104 loc) · 3.89 KB
/
Copy pathOneWireTempSensor.cpp
File metadata and controls
120 lines (104 loc) · 3.89 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright 2012-2013 BrewPi/Elco Jacobs.
* Copyright 2013 Matthew McGowan.
*
* This file is part of BrewPi.
*
* BrewPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BrewPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Brewpi.h"
#include "OneWireTempSensor.h"
#include "DallasTemperature.h"
#include "OneWire.h"
#include "OneWireDevices.h"
#include "PiLink.h"
#include "Ticks.h"
#include "TemperatureFormats.h"
OneWireTempSensor::~OneWireTempSensor(){
delete sensor;
};
/**
* Initializes the temperature sensor.
* This method is called when the sensor is first created and also any time the sensor reports it's disconnected.
* If the result is TEMP_SENSOR_DISCONNECTED then subsequent calls to read() will also return TEMP_SENSOR_DISCONNECTED.
* Clients should attempt to re-initialize the sensor by calling init() again.
*/
bool OneWireTempSensor::init(){
// save address and pinNr for log messages
char addressString[17];
printBytes(sensorAddress, 8, addressString);
DEBUG_ONLY(uint8_t pinNr = oneWirePin);
bool success = false;
if (sensor==NULL) {
sensor = new DallasTemperature(oneWire);
if (sensor==NULL) {
logErrorString(ERROR_SRAM_SENSOR, addressString);
}
}
logDebug("init onewire sensor");
// This quickly tests if the sensor is connected and initializes the reset detection.
// During the main TempControl loop, we don't want to spend many seconds
// scanning each sensor since this brings things to a halt.
if (sensor && sensor->initConnection(sensorAddress) && requestConversion()) {
logDebug("init onewire sensor - wait for conversion");
waitForConversion();
temperature temp = readAndConstrainTemp();
DEBUG_ONLY(logInfoIntStringTemp(INFO_TEMP_SENSOR_INITIALIZED, pinNr, addressString, temp));
success = temp!=DEVICE_DISCONNECTED && requestConversion();
}
setConnected(success);
logDebug("init onewire sensor complete %d", success);
return success;
}
bool OneWireTempSensor::requestConversion()
{
bool ok = sensor->requestTemperaturesByAddress(sensorAddress);
setConnected(ok);
return ok;
}
void OneWireTempSensor::setConnected(bool connected) {
if (this->connected==connected)
return; // state is stays the same
char addressString[17];
printBytes(sensorAddress, 8, addressString);
this->connected = connected;
if(connected){
// TODO - fix the following to use the defined OneWire pin
// logInfoIntString(INFO_TEMP_SENSOR_CONNECTED, 0, addressString);
// logInfoIntString(INFO_TEMP_SENSOR_CONNECTED, this->oneWire->pinNr(), addressString);
}
else{
// TODO - fix the following to use the defined OneWire pin
// logWarningIntString(WARNING_TEMP_SENSOR_DISCONNECTED, 0, addressString);
// logWarningIntString(WARNING_TEMP_SENSOR_DISCONNECTED, this->oneWire->pinNr(), addressString);
}
}
temperature OneWireTempSensor::read(){
if (!connected)
return TEMP_SENSOR_DISCONNECTED;
temperature temp = readAndConstrainTemp();
requestConversion();
return temp;
}
temperature OneWireTempSensor::readAndConstrainTemp()
{
temperature temp = sensor->getTempRaw(sensorAddress);
if(temp == DEVICE_DISCONNECTED){
setConnected(false);
return TEMP_SENSOR_DISCONNECTED;
}
const uint8_t shift = TEMP_FIXED_POINT_BITS-ONEWIRE_TEMP_SENSOR_PRECISION; // difference in precision between DS18B20 format and temperature adt
temp = constrainTemp(temp+calibrationOffset+(C_OFFSET>>shift), ((int) MIN_TEMP)>>shift, ((int) MAX_TEMP)>>shift)<<shift;
return temp;
}