@@ -59,30 +59,31 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
59
59
uint8_t pin_name = atoi (_DS18X20_model->GetDS18x20AddMsg ()->onewire_pin + 1 );
60
60
61
61
// Initialize the DS18X20Hardware object
62
- DS18X20Hardware new_dsx_driver (pin_name);
62
+ auto new_dsx_driver = std::make_unique<DS18X20Hardware> (pin_name);
63
63
// Attempt to get the sensor's ID on the OneWire bus to show it's been init'd
64
- bool is_initialized = new_dsx_driver. GetSensor ();
65
- if (! is_initialized) {
64
+ bool is_initialized = new_dsx_driver-> GetSensor ();
65
+ if (is_initialized) {
66
66
WS_DEBUG_PRINTLN (" Sensor found on OneWire bus and initialized" );
67
67
// Set the sensor's resolution
68
- new_dsx_driver. SetResolution (
68
+ new_dsx_driver-> SetResolution (
69
69
_DS18X20_model->GetDS18x20AddMsg ()->sensor_resolution );
70
70
// Set the sensor's period
71
- new_dsx_driver. SetPeriod (_DS18X20_model->GetDS18x20AddMsg ()->period );
71
+ new_dsx_driver-> SetPeriod (_DS18X20_model->GetDS18x20AddMsg ()->period );
72
72
// Configure the types of sensor reads to perform
73
73
for (int i = 0 ; i < _DS18X20_model->GetDS18x20AddMsg ()->sensor_types_count ;
74
74
i++) {
75
75
if (_DS18X20_model->GetDS18x20AddMsg ()->sensor_types [i] ==
76
- wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE ) {
77
- new_dsx_driver. is_read_temp_c = true ;
76
+ wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE ) {
77
+ new_dsx_driver-> is_read_temp_c = true ;
78
78
} else if (
79
79
_DS18X20_model->GetDS18x20AddMsg ()->sensor_types [i] ==
80
- wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE_FAHRENHEIT ) {
81
- new_dsx_driver. is_read_temp_f = true ;
80
+ wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT ) {
81
+ new_dsx_driver-> is_read_temp_f = true ;
82
82
}
83
83
}
84
84
// Add the DS18X20Hardware object to the vector of hardware objects
85
- _DS18X20_pins.push_back (new_dsx_driver);
85
+ // _DS18X20_pins.push_back(new_dsx_driver);
86
+ _DS18X20_pins.push_back (std::move (new_dsx_driver));
86
87
} else {
87
88
WS_DEBUG_PRINTLN (
88
89
" ERROR: Unable to get ds18x sensor ID, ds18x sensor not initialized" );
@@ -126,12 +127,12 @@ bool DS18X20Controller::Handle_Ds18x20Remove(pb_istream_t *stream) {
126
127
wippersnapper_ds18x20_Ds18x20Remove *msg_remove =
127
128
_DS18X20_model->GetDS18x20RemoveMsg ();
128
129
uint8_t pin_name = atoi (msg_remove->onewire_pin + 1 );
129
- // Destroy the DS18X20Hardware object, remove it from the vector, and release
130
- // it for other uses
131
- for (uint8_t i = 0 ; i < _DS18X20_pins.size (); i++ ) {
132
- if (_DS18X20_pins[i]. GetOneWirePin () == pin_name) {
130
+
131
+ // Find the driver/bus in the vector and remove it
132
+ for (size_t i = 0 ; i < _DS18X20_pins.size (); ++i ) {
133
+ if (_DS18X20_pins[i]-> GetOneWirePin () == pin_name) {
133
134
_DS18X20_pins.erase (_DS18X20_pins.begin () + i);
134
- break ;
135
+ return true ;
135
136
}
136
137
}
137
138
WS_DEBUG_PRINT (" Removed OneWire Pin: " );
@@ -152,7 +153,7 @@ void DS18X20Controller::update() {
152
153
// Iterate through the vector
153
154
for (uint8_t i = 0 ; i < _DS18X20_pins.size (); i++) {
154
155
// Create a temporary DS18X20Hardware driver
155
- DS18X20Hardware &temp_dsx_driver = _DS18X20_pins[i];
156
+ DS18X20Hardware &temp_dsx_driver = *( _DS18X20_pins[i]) ;
156
157
// Check if the driver's timer has not expired yet
157
158
if (!temp_dsx_driver.IsTimerExpired ()) {
158
159
continue ;
@@ -162,17 +163,27 @@ void DS18X20Controller::update() {
162
163
// Are we reading the temperature in Celsius, Fahrenheit, or both?
163
164
if (temp_dsx_driver.is_read_temp_c ) {
164
165
WS_DEBUG_PRINTLN (" Reading temperature in Celsius" ); // TODO: Debug remove
166
+ // Attempt to read the temperature in Celsius
167
+ if (!temp_dsx_driver.ReadTemperatureC ()) {
168
+ WS_DEBUG_PRINTLN (" ERROR: Unable to read temperature in Celsius" );
169
+ continue ;
170
+ }
165
171
float temp_c = temp_dsx_driver.GetTemperatureC ();
166
172
_DS18X20_model->addSensorEvent (
167
- wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE ,
173
+ wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE ,
168
174
temp_c);
169
175
}
170
176
if (temp_dsx_driver.is_read_temp_f ) {
171
177
WS_DEBUG_PRINTLN (
172
178
" Reading temperature in Fahrenheit" ); // TODO: Debug remove
179
+ // Attempt to read the temperature in Fahrenheit
180
+ if (!temp_dsx_driver.ReadTemperatureF ()) {
181
+ WS_DEBUG_PRINTLN (" ERROR: Unable to read temperature in Fahrenheit" );
182
+ continue ;
183
+ }
173
184
float temp_f = temp_dsx_driver.GetTemperatureF ();
174
185
_DS18X20_model->addSensorEvent (
175
- wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE_FAHRENHEIT ,
186
+ wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT ,
176
187
temp_f);
177
188
}
178
189
// Print out the SensorEvent message's contents for debugging
0 commit comments