Skip to content

Commit 67bad23

Browse files
committed
Merge pull request #129 from roboprint/master
Non-blocking Dallas sensors temperature conversion
2 parents 2b41450 + dffec69 commit 67bad23

File tree

17 files changed

+1024
-478
lines changed

17 files changed

+1024
-478
lines changed

libraries/DallasTemperature/DallasTemperature.cpp

Lines changed: 521 additions & 379 deletions
Large diffs are not rendered by default.

libraries/DallasTemperature/DallasTemperature.h

Lines changed: 78 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef DallasTemperature_h
22
#define DallasTemperature_h
33

4+
#define DALLASTEMPLIBVERSION "3.7.2"
5+
46
// This library is free software; you can redistribute it and/or
57
// modify it under the terms of the GNU Lesser General Public
68
// License as published by the Free Software Foundation; either
@@ -20,9 +22,10 @@
2022
#include <OneWire.h>
2123

2224
// Model IDs
23-
#define DS18S20MODEL 0x10
25+
#define DS18S20MODEL 0x10 // also DS1820
2426
#define DS18B20MODEL 0x28
2527
#define DS1822MODEL 0x22
28+
#define DS1825MODEL 0x3B
2629

2730
// OneWire commands
2831
#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
@@ -51,7 +54,9 @@
5154
#define TEMP_12_BIT 0x7F // 12 bit
5255

5356
// Error Codes
54-
#define DEVICE_DISCONNECTED -127
57+
#define DEVICE_DISCONNECTED_C -127
58+
#define DEVICE_DISCONNECTED_F -196.6
59+
#define DEVICE_DISCONNECTED_RAW -7040
5560

5661
typedef uint8_t DeviceAddress[8];
5762

@@ -61,54 +66,74 @@ class DallasTemperature
6166

6267
DallasTemperature(OneWire*);
6368

64-
// initalize bus
69+
// initialise bus
6570
void begin(void);
6671

6772
// returns the number of devices found on the bus
6873
uint8_t getDeviceCount(void);
6974

7075
// returns true if address is valid
71-
bool validAddress(uint8_t*);
76+
bool validAddress(const uint8_t*);
7277

7378
// finds an address at a given index on the bus
74-
bool getAddress(uint8_t*, const uint8_t);
79+
bool getAddress(uint8_t*, uint8_t);
7580

7681
// attempt to determine if the device at the given address is connected to the bus
77-
bool isConnected(uint8_t*);
82+
bool isConnected(const uint8_t*);
7883

7984
// attempt to determine if the device at the given address is connected to the bus
8085
// also allows for updating the read scratchpad
81-
bool isConnected(uint8_t*, uint8_t*);
86+
bool isConnected(const uint8_t*, uint8_t*);
8287

8388
// read device's scratchpad
84-
void readScratchPad(uint8_t*, uint8_t*);
89+
void readScratchPad(const uint8_t*, uint8_t*);
8590

8691
// write device's scratchpad
87-
void writeScratchPad(uint8_t*, const uint8_t*);
92+
void writeScratchPad(const uint8_t*, const uint8_t*);
8893

8994
// read device's power requirements
90-
bool readPowerSupply(uint8_t*);
95+
bool readPowerSupply(const uint8_t*);
9196

92-
// returns the current resolution, 9-12
93-
uint8_t getResolution(uint8_t*);
97+
// get global resolution
98+
uint8_t getResolution();
99+
100+
// set global resolution to 9, 10, 11, or 12 bits
101+
void setResolution(uint8_t);
94102

95-
// set resolution of a device to 9, 10, 11, or 12 bits
96-
void setResolution(uint8_t*, uint8_t);
103+
// returns the device resolution: 9, 10, 11, or 12 bits
104+
uint8_t getResolution(const uint8_t*);
97105

98-
// sends command for all devices on the bus to perform a temperature conversion
106+
// set resolution of a device to 9, 10, 11, or 12 bits
107+
bool setResolution(const uint8_t*, uint8_t);
108+
109+
// sets/gets the waitForConversion flag
110+
void setWaitForConversion(bool);
111+
bool getWaitForConversion(void);
112+
113+
// sets/gets the checkForConversion flag
114+
void setCheckForConversion(bool);
115+
bool getCheckForConversion(void);
116+
117+
// sends command for all devices on the bus to perform a temperature conversion
99118
void requestTemperatures(void);
100-
119+
120+
// returns time to wait for a temperature conversion based on given resolution
121+
int16_t millisToWaitForConversion(uint8_t);
122+
101123
// sends command for one device to perform a temperature conversion by address
102-
void requestTemperaturesByAddress(uint8_t*);
124+
bool requestTemperaturesByAddress(const uint8_t*);
103125

104126
// sends command for one device to perform a temperature conversion by index
105-
void requestTemperaturesByIndex(uint8_t);
127+
bool requestTemperaturesByIndex(uint8_t);
128+
129+
// returns temperature raw value (12 bit integer of 1/16 degrees C)
130+
int16_t getTemp(const uint8_t*);
106131

107132
// returns temperature in degrees C
108-
float getTempC(uint8_t*);
133+
float getTempC(const uint8_t*);
109134

110135
// returns temperature in degrees F
111-
float getTempF(uint8_t*);
136+
float getTempF(const uint8_t*);
112137

113138
// Get temperature for device index (slow)
114139
float getTempCByIndex(uint8_t);
@@ -118,26 +143,28 @@ class DallasTemperature
118143

119144
// returns true if the bus requires parasite power
120145
bool isParasitePowerMode(void);
146+
147+
bool isConversionAvailable(const uint8_t*);
121148

122149
#if REQUIRESALARMS
123150

124-
typedef void AlarmHandler(uint8_t*);
151+
typedef void AlarmHandler(const uint8_t*);
125152

126153
// sets the high alarm temperature for a device
127154
// accepts a char. valid range is -55C - 125C
128-
void setHighAlarmTemp(uint8_t*, const char);
155+
void setHighAlarmTemp(const uint8_t*, char);
129156

130157
// sets the low alarm temperature for a device
131158
// accepts a char. valid range is -55C - 125C
132-
void setLowAlarmTemp(uint8_t*, const char);
159+
void setLowAlarmTemp(const uint8_t*, char);
133160

134161
// returns a signed char with the current high alarm temperature for a device
135162
// in the range -55C - 125C
136-
char getHighAlarmTemp(uint8_t*);
163+
char getHighAlarmTemp(const uint8_t*);
137164

138165
// returns a signed char with the current low alarm temperature for a device
139166
// in the range -55C - 125C
140-
char getLowAlarmTemp(uint8_t*);
167+
char getLowAlarmTemp(const uint8_t*);
141168

142169
// resets internal variables used for the alarm search
143170
void resetAlarmSearch(void);
@@ -146,7 +173,7 @@ class DallasTemperature
146173
bool alarmSearch(uint8_t*);
147174

148175
// returns true if ia specific device has an alarm
149-
bool hasAlarm(uint8_t*);
176+
bool hasAlarm(const uint8_t*);
150177

151178
// returns true if any device is reporting an alarm on the bus
152179
bool hasAlarm(void);
@@ -155,22 +182,28 @@ class DallasTemperature
155182
void processAlarms(void);
156183

157184
// sets the alarm handler
158-
void setAlarmHandler(AlarmHandler *);
185+
void setAlarmHandler(const AlarmHandler *);
159186

160187
// The default alarm handler
161-
static void defaultAlarmHandler(uint8_t*);
188+
static void defaultAlarmHandler(const uint8_t*);
162189

163190
#endif
164191

165-
// convert from celcius to farenheit
166-
static float toFahrenheit(const float);
192+
// convert from Celsius to Fahrenheit
193+
static float toFahrenheit(float);
194+
195+
// convert from Fahrenheit to Celsius
196+
static float toCelsius(float);
167197

168-
// convert from farenheit to celsius
169-
static float toCelsius(const float);
198+
// convert from raw to Celsius
199+
static float rawToCelsius(int16_t);
200+
201+
// convert from raw to Fahrenheit
202+
static float rawToFahrenheit(int16_t);
170203

171204
#if REQUIRESNEW
172205

173-
// initalize memory area
206+
// initialize memory area
174207
void* operator new (unsigned int);
175208

176209
// delete memory reference
@@ -186,16 +219,24 @@ class DallasTemperature
186219

187220
// used to determine the delay amount needed to allow for the
188221
// temperature conversion to take place
189-
int conversionDelay;
190-
222+
uint8_t bitResolution;
223+
224+
// used to requestTemperature with or without delay
225+
bool waitForConversion;
226+
227+
// used to requestTemperature to dynamically check if a conversion is complete
228+
bool checkForConversion;
229+
191230
// count of devices on the bus
192231
uint8_t devices;
193232

194233
// Take a pointer to one wire instance
195234
OneWire* _wire;
196235

197-
// reads scratchpad and returns the temperature in degrees C
198-
float calculateTemperature(uint8_t*, uint8_t*);
236+
// reads scratchpad and returns the raw temperature
237+
int16_t calculateTemperature(const uint8_t*, uint8_t*);
238+
239+
void blockTillConversionComplete(uint8_t, const uint8_t*);
199240

200241
#if REQUIRESALARMS
201242

libraries/DallasTemperature/README renamed to libraries/DallasTemperature/README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,33 @@ Arduino Library for Dallas Temperature ICs
44
Usage
55
-----
66

7-
This library supports the following devices:
8-
DS18B20
9-
DS18S20 - Please note there appears to be an issue with this series.
10-
DS1822
7+
This library supports the following devices :
8+
9+
10+
* DS18B20
11+
* DS18S20 - Please note there appears to be an issue with this series.
12+
* DS1822
13+
* DS1820
14+
1115

1216
You will need a pull-up resistor of about 5 KOhm between the 1-Wire data line
1317
and your 5V power. If you are using the DS18B20, ground pins 1 and 3. The
1418
centre pin is the data line '1-wire'.
1519

1620
We have included a "REQUIRESNEW" and "REQUIRESALARMS" definition. If you
1721
want to slim down the code feel free to use either of these by including
18-
#define REQUIRESNEW or #define REQUIRESALARMS a the top of DallasTemperature.h
22+
23+
24+
25+
#define REQUIRESNEW
26+
27+
or
28+
29+
#define REQUIRESALARMS
30+
31+
32+
at the top of DallasTemperature.h
33+
1934

2035
Credits
2136
-------
@@ -26,6 +41,8 @@ Miles Burton <[email protected]> originally developed this library.
2641
Tim Newsome <[email protected]> added support for multiple sensors on
2742
the same bus.
2843
Guil Barros [[email protected]] added getTempByAddress (v3.5)
44+
Rob Tillaart [[email protected]] added async modus (v3.7.0)
45+
2946

3047
Website
3148
-------
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
This file contains the change history of the Dallas Temperature Control Library.
3+
4+
VERSION 3.7.2 BETA
5+
===================
6+
DATE: 6 DEC 2011
7+
8+
- Jordan Hochenbaum [[email protected]] updated library for compatibility with Arduino 1.0.
9+
10+
VERSION 3.7.0 BETA
11+
===================
12+
DATE: 11 JAN 2011
13+
14+
- Rob Tillaart [[email protected]] added async modus (v3.7.0)
15+
The library is backwards compatible with version 3.6.0
16+
17+
MAJOR: async modus
18+
------------------
19+
- Added - private bool waitForConversion.
20+
This boolean is default set to true in the Constructor to keep the library backwards compatible. If this flag is true calls to requestTemperatures(), requestTemperaturesByAddress() et al, will be blocking with the appropiate time specified (in datasheet) for the resolution used. If the flag is set to false, requestTemperatures() et al, will return immediately after the conversion command is send over the 1-wire interface. The programmer is responsible to wait long enough before reading the temperature values. This enables the application to do other things while waiting for a new reading, like calculations, update LCD, read/write other IO lines etc. See examples.
21+
22+
- Added - void setWaitForConversion(bool);
23+
To set the flag to true or false, depending on the modus needed.
24+
25+
- Added - bool getWaitForConversion(void);
26+
To get the current value of the flag.
27+
28+
- Changed - void requestTemperatures(void);
29+
Added a test (false == waitForConversion) to return immediately after the conversion command instead of waiting until the conversion is ready.
30+
31+
- Changed - bool requestTemperaturesByAddress(uint8_t*);
32+
Added a test (false == waitForConversion) to return immediately after the conversion command instead of waiting until the conversion is ready.
33+
34+
35+
MINOR version number
36+
--------------------
37+
- Added - #define DALLASTEMPLIBVERSION "3.7.0"
38+
To indicate the version number in .h file
39+
40+
41+
MINOR internal var bitResolution
42+
----------------------------
43+
- Changed - private int conversionDelay - is renamed to - private int bitResolution
44+
As this variable holds the resolution. The delay for the conversion is derived from it.
45+
46+
- Changed - uint8_t getResolution(uint8_t* deviceAddress);
47+
If the device is not connected, it returns 0, otherwise it returns the resolution of the device.
48+
49+
- Changed - bool setResolution(uint8_t* deviceAddress, uint8_t newResolution);
50+
If the device is not connected, it returns FALSE (fail), otherwise it returns TRUE (succes).
51+
52+
- Added - uint8_t getResolution();
53+
Returns bitResolution.
54+
55+
- Added - void setResolution(uint8_t newResolution)
56+
Sets the internal variable bitResolution, and all devices to this value
57+
58+
59+
MINOR check connected state
60+
----------------------------
61+
- Changed - bool requestTemperaturesByIndex(deviceIndex)
62+
Changed return type from void to bool. The function returns false if the device identified with [deviceIndex] is not found on the bus and true otherwise.
63+
64+
- Changed - bool requestTemperaturesByAddress(deviceAddress)
65+
Changed return type from void to bool. The function returns false if the device identified with [deviceAddress] is not found on the bus and true otherwise.
66+
Added code to handle the DS18S20 which has a 9 bit resolution separately.
67+
Changed code so the blocking delay matches the bitResolution set in the device with deviceAddress.
68+
69+
- Changed - bool requestTemperaturesByIndex(uint8_t deviceIndex)
70+
Changed return type from void to bool. The function returns false if the device identified with [deviceIndex] is not found on the bus and true otherwise.
71+
72+
73+
74+
VERSION 3.6.0
75+
==============
76+
DATE: 2010-10-10
77+
78+
- no detailed change history known except:
79+
80+
- The OneWire code has been derived from
81+
http://www.arduino.cc/playground/Learning/OneWire.
82+
- Miles Burton <[email protected]> originally developed this library.
83+
- Tim Newsome <[email protected]> added support for multiple sensors on
84+
the same bus.
85+
- Guil Barros [[email protected]] added getTempByAddress (v3.5)

libraries/DallasTemperature/examples/Alarm/Alarm.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <DallasTemperature.h>
33

44
// Data wire is plugged into port 2 on the Arduino
5-
#define ONE_WIRE_BUS 3
5+
#define ONE_WIRE_BUS 2
66

77
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
88
OneWire oneWire(ONE_WIRE_BUS);

libraries/DallasTemperature/examples/AlarmHandler/AlarmHandler.pde

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
#include <DallasTemperature.h>
33

44
// Data wire is plugged into port 2 on the Arduino
5-
#define ONE_WIRE_BUS 3
6-
#define TEMPERATURE_PRECISION 9
5+
#define ONE_WIRE_BUS 2
76

87
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
98
OneWire oneWire(ONE_WIRE_BUS);

0 commit comments

Comments
 (0)