Firmware SDK
twr_ds18b20.h
1 #ifndef _TWR_DS18B20_H
2 #define _TWR_DS18B20_H
3 
4 #include <twr_tick.h>
5 #include <twr_module_sensor.h>
6 #include <twr_scheduler.h>
7 #include <twr_onewire.h>
8 
12 
14 
15 typedef enum
16 {
19 
22 
24 
26 
27 typedef struct twr_ds18b20_t twr_ds18b20_t;
28 typedef struct twr_ds18b20_sensor_t twr_ds18b20_sensor_t;
29 
31 
32 typedef enum
33 {
34  TWR_DS18B20_STATE_ERROR = -1,
35  TWR_DS18B20_STATE_PREINITIALIZE = 0,
36  TWR_DS18B20_STATE_INITIALIZE = 1,
37  TWR_DS18B20_STATE_READY = 2,
38  TWR_DS18B20_STATE_MEASURE = 3,
39  TWR_DS18B20_STATE_READ = 4,
40  TWR_DS18B20_STATE_RESULTS = 5
41 
42 } twr_ds18b20_state_t;
43 
44 typedef enum
45 {
46  TWR_DS18B20_RESOLUTION_BITS_9 = 0,
47  TWR_DS18B20_RESOLUTION_BITS_10 = 1,
48  TWR_DS18B20_RESOLUTION_BITS_11 = 2,
49  TWR_DS18B20_RESOLUTION_BITS_12 = 3
50 
51 } twr_ds18b20_resolution_bits_t;
52 
53 struct twr_ds18b20_sensor_t
54 {
55  int16_t _temperature_raw;
56  uint64_t _device_address;
57  bool _temperature_valid;
58 };
59 
60 struct twr_ds18b20_t
61 {
62  twr_scheduler_task_id_t _task_id_interval;
63  twr_scheduler_task_id_t _task_id_measure;
64  void (*_event_handler)(twr_ds18b20_t *, uint64_t _device_address, twr_ds18b20_event_t, void *);
65  void *_event_param;
66  bool _measurement_active;
67  twr_tick_t _update_interval;
68  twr_ds18b20_state_t _state;
69 
70  twr_ds18b20_sensor_t *_sensor;
71  int _sensor_count;
72  int _sensor_found;
73 
74  twr_ds18b20_resolution_bits_t _resolution;
75 
76  bool _power;
77  bool _power_dynamic;
78  twr_onewire_t *_onewire;
79 };
80 
82 
83 
87 
88 void twr_ds18b20_init_single(twr_ds18b20_t *self, twr_ds18b20_resolution_bits_t resolution);
89 
95 
96 void twr_ds18b20_init_multiple(twr_ds18b20_t *self, twr_ds18b20_sensor_t *sensors, int sensor_count, twr_ds18b20_resolution_bits_t resolution);
97 
104 
105 void twr_ds18b20_init(twr_ds18b20_t *self, twr_onewire_t *onewire, twr_ds18b20_sensor_t *sensors, int sensor_count, twr_ds18b20_resolution_bits_t resolution);
106 
111 
112 void twr_ds18b20_set_event_handler(twr_ds18b20_t *self, void (*event_handler)(twr_ds18b20_t *, uint64_t _device_address, twr_ds18b20_event_t, void *), void *event_param);
113 
117 
119 
124 
126 
133 
134 bool twr_ds18b20_get_temperature_raw(twr_ds18b20_t *self, uint64_t device_address, int16_t *raw);
135 
142 
143 bool twr_ds18b20_get_temperature_celsius(twr_ds18b20_t *self, uint64_t _device_address, float *celsius);
144 
148 
149 int twr_ds18b20_get_index_by_device_address(twr_ds18b20_t *self, uint64_t device_address);
150 
155 
156 uint64_t twr_ds182b0_get_short_address(twr_ds18b20_t *self, uint8_t index);
157 
160 
162 
164 
166 
170 
171 void twr_ds18b20_set_power_dynamic(twr_ds18b20_t *self, bool on);
172 
174 
175 #endif // _TWR_DS18B20_H
int twr_ds18b20_get_sensor_found(twr_ds18b20_t *self)
Get number of found sensor.
Definition: twr_ds18b20.c:120
void twr_ds18b20_set_event_handler(twr_ds18b20_t *self, void(*event_handler)(twr_ds18b20_t *, uint64_t _device_address, twr_ds18b20_event_t, void *), void *event_param)
Set callback function.
Definition: twr_ds18b20.c:56
void twr_ds18b20_init_single(twr_ds18b20_t *self, twr_ds18b20_resolution_bits_t resolution)
Initialize single ds18b20 over channel B on Sensor Module.
Definition: twr_ds18b20.c:23
void twr_ds18b20_set_update_interval(twr_ds18b20_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_ds18b20.c:63
void twr_ds18b20_set_power_dynamic(twr_ds18b20_t *self, bool on)
Set power dynamic, Turns VDD on and pull 4k7 only for measurement.
Definition: twr_ds18b20.c:142
void twr_ds18b20_rescan(twr_ds18b20_t *self)
Request to rescan the bus.
Definition: twr_ds18b20.c:125
bool twr_ds18b20_get_temperature_celsius(twr_ds18b20_t *self, uint64_t _device_address, float *celsius)
Get measured temperature in degrees of Celsius.
Definition: twr_ds18b20.c:166
void twr_ds18b20_init(twr_ds18b20_t *self, twr_onewire_t *onewire, twr_ds18b20_sensor_t *sensors, int sensor_count, twr_ds18b20_resolution_bits_t resolution)
Initialize ds18b20.
Definition: twr_ds18b20.c:36
twr_ds18b20_event_t
Callback events.
Definition: twr_ds18b20.h:16
int twr_ds18b20_get_index_by_device_address(twr_ds18b20_t *self, uint64_t device_address)
Get device index by its device address.
Definition: twr_ds18b20.c:93
struct twr_ds18b20_t twr_ds18b20_t
BigClown ds18b20 instance.
Definition: twr_ds18b20.h:27
bool twr_ds18b20_get_temperature_raw(twr_ds18b20_t *self, uint64_t device_address, int16_t *raw)
Get measured temperature in degrees of Celsius.
Definition: twr_ds18b20.c:147
bool twr_ds18b20_measure(twr_ds18b20_t *self)
Start measurement manually.
Definition: twr_ds18b20.c:79
uint64_t twr_ds182b0_get_short_address(twr_ds18b20_t *self, uint8_t index)
Get device index by its device address.
Definition: twr_ds18b20.c:106
void twr_ds18b20_init_multiple(twr_ds18b20_t *self, twr_ds18b20_sensor_t *sensors, int sensor_count, twr_ds18b20_resolution_bits_t resolution)
Initialize multiple ds18b20 over channel B on Sensor Module.
Definition: twr_ds18b20.c:30
@ TWR_DS18B20_EVENT_UPDATE
Update event.
Definition: twr_ds18b20.h:21
@ TWR_DS18B20_EVENT_ERROR
Error event.
Definition: twr_ds18b20.h:18
struct twr_onewire_t twr_onewire_t
1-Wire instance
Definition: twr_onewire.h:14
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: twr_scheduler.h:22
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16