esp32-warm-water
esp32 based project for the control of a heating element based on temperature
ds18b20.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2017 David Antliff
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
34 #ifndef DS18B20_H
35 #define DS18B20_H
36 
37 #include "owb.h"
38 
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43 
47  typedef enum
48  {
50  DS18B20_OK = 0,
55  } DS18B20_ERROR;
56 
60  typedef enum
61  {
68 
73  typedef struct
74  {
75  bool init;
76  bool solo;
77  bool use_crc;
78  const OneWireBus *bus;
81  } DS18B20_Info;
82 
89 
95  void ds18b20_free(DS18B20_Info **ds18b20_info);
96 
103  void ds18b20_init(DS18B20_Info *ds18b20_info, const OneWireBus *bus, OneWireBus_ROMCode rom_code);
104 
116  void ds18b20_init_solo(DS18B20_Info *ds18b20_info, const OneWireBus *bus);
117 
123  void ds18b20_use_crc(DS18B20_Info *ds18b20_info, bool use_crc);
124 
135  bool ds18b20_set_resolution(DS18B20_Info *ds18b20_info, DS18B20_RESOLUTION resolution);
136 
143 
150 
155  bool ds18b20_convert(const DS18B20_Info *ds18b20_info);
156 
164  void ds18b20_convert_all(const OneWireBus *bus);
165 
173  float ds18b20_wait_for_conversion(const DS18B20_Info *ds18b20_info);
174 
184  DS18B20_ERROR ds18b20_read_temp(const DS18B20_Info *ds18b20_info, float *value);
185 
192  DS18B20_ERROR ds18b20_convert_and_read_temp(const DS18B20_Info *ds18b20_info, float *value);
193 
201  DS18B20_ERROR ds18b20_check_for_parasite_power(const OneWireBus *bus, bool *present);
202 
203 #ifdef __cplusplus
204 }
205 #endif
206 
207 #endif // DS18B20_H
OneWireBus_ROMCode ds18b20_read_rom(DS18B20_Info *ds18b20_info)
Read 64-bit ROM code from device - only works when there is a single device on the bus.
void ds18b20_init_solo(DS18B20_Info *ds18b20_info, const OneWireBus *bus)
Initialise a device info instance as a solo device on the bus.
Definition: ds18b20.c:378
bool ds18b20_set_resolution(DS18B20_Info *ds18b20_info, DS18B20_RESOLUTION resolution)
Set temperature measurement resolution.
Definition: ds18b20.c:404
void ds18b20_init(DS18B20_Info *ds18b20_info, const OneWireBus *bus, OneWireBus_ROMCode rom_code)
Initialise a device info instance with the specified GPIO.
Definition: ds18b20.c:362
void ds18b20_free(DS18B20_Info **ds18b20_info)
Delete an existing device info instance.
Definition: ds18b20.c:352
DS18B20_RESOLUTION ds18b20_read_resolution(DS18B20_Info *ds18b20_info)
Update and return the current temperature measurement resolution from the device.
Definition: ds18b20.c:443
float ds18b20_wait_for_conversion(const DS18B20_Info *ds18b20_info)
Wait for the maximum conversion time according to the current resolution of the device....
Definition: ds18b20.c:503
DS18B20_ERROR ds18b20_read_temp(const DS18B20_Info *ds18b20_info, float *value)
Read last temperature measurement from device.
Definition: ds18b20.c:523
DS18B20_RESOLUTION
Symbols for the supported temperature resolution of the device.
Definition: ds18b20.h:61
@ DS18B20_RESOLUTION_10_BIT
10-bit resolution, LSB bits 1,0 undefined
Definition: ds18b20.h:64
@ DS18B20_RESOLUTION_12_BIT
12-bit resolution (default)
Definition: ds18b20.h:66
@ DS18B20_RESOLUTION_11_BIT
11-bit resolution, LSB bit 0 undefined
Definition: ds18b20.h:65
@ DS18B20_RESOLUTION_9_BIT
9-bit resolution, LSB bits 2,1,0 undefined
Definition: ds18b20.h:63
@ DS18B20_RESOLUTION_INVALID
Invalid resolution.
Definition: ds18b20.h:62
DS18B20_ERROR
Success and error codes.
Definition: ds18b20.h:48
@ DS18B20_OK
Success.
Definition: ds18b20.h:50
@ DS18B20_ERROR_UNKNOWN
An unknown error occurred, or the value was not set.
Definition: ds18b20.h:49
@ DS18B20_ERROR_NULL
A parameter or value is NULL.
Definition: ds18b20.h:54
@ DS18B20_ERROR_DEVICE
A device error occurred.
Definition: ds18b20.h:51
@ DS18B20_ERROR_CRC
A CRC error occurred.
Definition: ds18b20.h:52
@ DS18B20_ERROR_OWB
A One Wire Bus error occurred.
Definition: ds18b20.h:53
DS18B20_ERROR ds18b20_check_for_parasite_power(const OneWireBus *bus, bool *present)
Check OneWire bus for presence of parasitic-powered devices.
Definition: ds18b20.c:579
void ds18b20_convert_all(const OneWireBus *bus)
Start temperature conversion on all connected devices.
Definition: ds18b20.c:487
void ds18b20_use_crc(DS18B20_Info *ds18b20_info, bool use_crc)
Enable or disable use of CRC checks on device communications.
Definition: ds18b20.c:395
DS18B20_ERROR ds18b20_convert_and_read_temp(const DS18B20_Info *ds18b20_info, float *value)
Convert, wait and read current temperature from device.
Definition: ds18b20.c:555
bool ds18b20_convert(const DS18B20_Info *ds18b20_info)
Start a temperature measurement conversion on a single device.
Definition: ds18b20.c:467
DS18B20_Info * ds18b20_malloc(void)
Construct a new device info instance. New instance should be initialised before calling other functio...
Definition: ds18b20.c:336
Interface definitions for the 1-Wire bus component.
Structure containing information related to a single DS18B20 device connected via a 1-Wire bus.
Definition: ds18b20.h:74
DS18B20_RESOLUTION resolution
Temperature measurement resolution per reading.
Definition: ds18b20.h:80
bool init
True if struct has been initialised, otherwise false.
Definition: ds18b20.h:75
const OneWireBus * bus
Pointer to 1-Wire bus information relevant to this device.
Definition: ds18b20.h:78
OneWireBus_ROMCode rom_code
The ROM code used to address this device on the bus.
Definition: ds18b20.h:79
bool use_crc
True if CRC checks are to be used when retrieving information from a device on the bus.
Definition: ds18b20.h:77
bool solo
True if device is intended to be the only one connected to the bus, otherwise false.
Definition: ds18b20.h:76
Structure containing 1-Wire bus information relevant to a single instance.
Definition: owb.h:69
Represents a 1-Wire ROM Code. This is a sequence of eight bytes, where the first byte is the family n...
Definition: owb.h:83