@@ -24,6 +24,13 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
24
24
WS_DEBUG_PRINTLN (" ERROR: Unable to decode Ds18x20Add message" );
25
25
return false ;
26
26
}
27
+
28
+ // If we receive no sensor types to configure, bail out
29
+ if (_DS18X20_model->GetDS18x20AddMsg ()->sensor_types_count == 0 ) {
30
+ WS_DEBUG_PRINTLN (" ERROR: No ds18x sensor types provided!" );
31
+ return false ;
32
+ }
33
+
27
34
// Extract the OneWire pin from the message
28
35
uint8_t pin_name = atoi (_DS18X20_model->GetDS18x20AddMsg ()->onewire_pin + 1 );
29
36
@@ -34,13 +41,27 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
34
41
if (!is_initialized) {
35
42
WS_DEBUG_PRINTLN (" Sensor found on OneWire bus and initialized" );
36
43
// Set the sensor's resolution
37
- new_dsx_driver.setResolution (
44
+ new_dsx_driver.SetResolution (
38
45
_DS18X20_model->GetDS18x20AddMsg ()->sensor_resolution );
39
-
46
+ // Set the sensor's period
47
+ new_dsx_driver.SetPeriod (_DS18X20_model->GetDS18x20AddMsg ()->period );
48
+ // Configure the types of sensor reads to perform
49
+ for (int i = 0 ; i < _DS18X20_model->GetDS18x20AddMsg ()->sensor_types_count ;
50
+ i++) {
51
+ if (_DS18X20_model->GetDS18x20AddMsg ()->sensor_types [i] ==
52
+ wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE) {
53
+ new_dsx_driver.is_read_temp_c = true ;
54
+ } else if (
55
+ _DS18X20_model->GetDS18x20AddMsg ()->sensor_types [i] ==
56
+ wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE_FAHRENHEIT) {
57
+ new_dsx_driver.is_read_temp_f = true ;
58
+ }
59
+ }
40
60
// Add the DS18X20Hardware object to the vector of hardware objects
41
61
_DS18X20_pins.push_back (new_dsx_driver);
42
62
} else {
43
- WS_DEBUG_PRINTLN (" ERROR: Unable to get sensor ID, sensor not initialized" );
63
+ WS_DEBUG_PRINTLN (
64
+ " ERROR: Unable to get ds18x sensor ID, ds18x sensor not initialized" );
44
65
}
45
66
46
67
// Encode and publish a Ds18x20Added message back to the broker
@@ -62,17 +83,21 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
62
83
63
84
void DS18X20Controller::update () {
64
85
// Bail out if there are no OneWire pins to poll
65
- if (_DS18X20_pins.size () == 0 )
86
+ if (_DS18X20_pins.empty () )
66
87
return ;
67
88
68
89
// Iterate through the vector
69
90
for (uint8_t i = 0 ; i < _DS18X20_pins.size (); i++) {
70
91
// Create a temporary DS18X20Hardware driver
71
- DS18X20Hardware *temp_dsx_driver = &_DS18X20_pins[i];
72
- // Check if the driver's timer has expired
73
- if (temp_dsx_driver->IsTimerExpired ())
74
- return ;
75
- // TODO: the update() method should check sensor type(s)
76
- // before polling ReadTemperatureX methods!
92
+ DS18X20Hardware &temp_dsx_driver = _DS18X20_pins[i];
93
+ // Check if the driver's timer has not expired yet
94
+ if (!temp_dsx_driver.IsTimerExpired ())
95
+ continue ;
96
+ // Are we reading the temperature in Celsius, Fahrenheit, or both?
97
+ if (temp_dsx_driver.is_read_temp_c )
98
+ temp_dsx_driver.GetTemperatureC ();
99
+ if (temp_dsx_driver.is_read_temp_f )
100
+ temp_dsx_driver.GetTemperatureF ();
101
+ // wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE
77
102
}
78
103
}
0 commit comments