Skip to content

Commit acab0f1

Browse files
committed
🐛 WIP, dsx - Fix bug where two sensors are initd but only the first is read
1 parent 33a7244 commit acab0f1

File tree

2 files changed

+36
-49
lines changed

2 files changed

+36
-49
lines changed

src/components/ds18x20/controller.cpp

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,11 @@ DS18X20Controller::~DS18X20Controller() { delete _DS18X20_model; }
4343
*/
4444
/***********************************************************************/
4545
bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
46-
unsigned long total_start_time = millis();
47-
4846
// Attempt to decode the incoming message into a Ds18x20Add message
49-
unsigned long decode_start_time = millis();
5047
if (!_DS18X20_model->DecodeDS18x20Add(stream)) {
5148
WS_DEBUG_PRINTLN("ERROR: Unable to decode Ds18x20Add message");
5249
return false;
5350
}
54-
unsigned long decode_end_time = millis();
55-
WS_DEBUG_PRINT("Decode time: ");
56-
WS_DEBUG_PRINTLN(decode_end_time - decode_start_time);
5751

5852
// If we receive no sensor types to configure, bail out
5953
if (_DS18X20_model->GetDS18x20AddMsg()->sensor_types_count == 0) {
@@ -65,13 +59,9 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
6559
uint8_t pin_name = atoi(_DS18X20_model->GetDS18x20AddMsg()->onewire_pin + 1);
6660

6761
// Initialize the DS18X20Hardware object
68-
unsigned long init_start_time = millis();
6962
auto new_dsx_driver = std::make_unique<DS18X20Hardware>(pin_name);
7063
// Attempt to get the sensor's ID on the OneWire bus to show it's been init'd
7164
bool is_initialized = new_dsx_driver->GetSensor();
72-
unsigned long init_end_time = millis();
73-
WS_DEBUG_PRINT("Initialization time: ");
74-
WS_DEBUG_PRINTLN(init_end_time - init_start_time);
7565

7666
if (is_initialized) {
7767
WS_DEBUG_PRINTLN("Sensor found on OneWire bus and initialized");
@@ -84,7 +74,6 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
8474
new_dsx_driver->SetPeriod(_DS18X20_model->GetDS18x20AddMsg()->period);
8575

8676
// Configure the types of sensor reads to perform
87-
unsigned long config_types_start_time = millis();
8877
for (int i = 0; i < _DS18X20_model->GetDS18x20AddMsg()->sensor_types_count;
8978
i++) {
9079
if (_DS18X20_model->GetDS18x20AddMsg()->sensor_types[i] ==
@@ -111,29 +100,16 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
111100
WS_DEBUG_PRINTLN("ERROR: Unable to encode Ds18x20Added message");
112101
return false;
113102
}
114-
unsigned long encode_end_time = millis();
115-
WS_DEBUG_PRINT("Encode message time: ");
116-
WS_DEBUG_PRINTLN(encode_end_time - encode_start_time);
117103

118-
// Publish the AnalogIO message to the broker
119-
unsigned long publish_start_time = millis();
120104
if (!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_ds18x20_added_tag,
121105
_DS18X20_model->GetDS18x20AddedMsg())) {
122106
WS_DEBUG_PRINTLN("ERROR: Unable to publish Ds18x20Added message");
123107
return false;
124108
}
125-
unsigned long publish_end_time = millis();
126-
WS_DEBUG_PRINT("Publish message time: ");
127-
WS_DEBUG_PRINTLN(publish_end_time - publish_start_time);
128-
129-
unsigned long total_end_time = millis();
130-
WS_DEBUG_PRINT("Total function execution time: ");
131-
WS_DEBUG_PRINTLN(total_end_time - total_start_time);
132109

133110
return true;
134111
}
135112

136-
137113
/***********************************************************************/
138114
/*!
139115
@brief Handles a Ds18x20Remove message from the broker. Attempts to
@@ -174,15 +150,19 @@ bool DS18X20Controller::Handle_Ds18x20Remove(pb_istream_t *stream) {
174150
*/
175151
/***********************************************************************/
176152
void DS18X20Controller::update() {
153+
#ifdef DEBUG_PROFILE
177154
unsigned long total_start_time = millis();
155+
#endif
178156

179157
// Bail out if there are no OneWire pins to poll
180158
if (_DS18X20_pins.empty())
181159
return;
182160

183161
// Iterate through the vector
184162
for (uint8_t i = 0; i < _DS18X20_pins.size(); i++) {
163+
#ifdef DEBUG_PROFILE
185164
unsigned long sensor_start_time = millis();
165+
#endif
186166

187167
// Create a reference to the DS18X20Hardware object
188168
DS18X20Hardware &temp_dsx_driver = *(_DS18X20_pins[i]);
@@ -192,21 +172,24 @@ void DS18X20Controller::update() {
192172
continue;
193173
}
194174

195-
// Attempt to read the temperature in Celsius
196-
#ifdef DEBUG_PROFILE
197-
unsigned long temp_c_start_time = millis();
198-
#endif
199-
if (!temp_dsx_driver.ReadTemperatureC()) {
200-
WS_DEBUG_PRINTLN("ERROR: Unable to read temperature in Celsius");
201-
continue;
202-
}
203-
#ifdef DEBUG_PROFILE
204-
unsigned long temp_c_end_time = millis();
205-
WS_DEBUG_PRINT("Read temperature Celsius time: ");
206-
WS_DEBUG_PRINTLN(temp_c_end_time - temp_c_start_time);
207-
#endif
175+
// Attempt to read the temperature in Celsius
176+
#ifdef DEBUG_PROFILE
177+
unsigned long temp_c_start_time = millis();
178+
#endif
208179

209-
// We got a temperature value from the hardware, let's create a new sensor_event
180+
if (!temp_dsx_driver.ReadTemperatureC()) {
181+
WS_DEBUG_PRINTLN("ERROR: Unable to read temperature in Celsius");
182+
continue;
183+
}
184+
185+
#ifdef DEBUG_PROFILE
186+
unsigned long temp_c_end_time = millis();
187+
WS_DEBUG_PRINT("Read temperature Celsius time: ");
188+
WS_DEBUG_PRINTLN(temp_c_end_time - temp_c_start_time);
189+
#endif
190+
191+
// We got a temperature value from the hardware, let's create a new
192+
// sensor_event
210193
_DS18X20_model->InitDS18x20EventMsg();
211194

212195
// Are we reading the temperature in Celsius, Fahrenheit, or both?
@@ -224,10 +207,10 @@ void DS18X20Controller::update() {
224207
}
225208

226209
// Get the Ds18x20Event message
227-
wippersnapper_ds18x20_Ds18x20Event* event_msg = _DS18X20_model->GetDS18x20EventMsg();
210+
wippersnapper_ds18x20_Ds18x20Event *event_msg =
211+
_DS18X20_model->GetDS18x20EventMsg();
228212
pb_size_t event_count = event_msg->sensor_events_count;
229213

230-
231214
// Print the message's content out for debugging
232215
WS_DEBUG_PRINT("Sensor OneWire bus pin: ");
233216
WS_DEBUG_PRINT(temp_dsx_driver.GetOneWirePin());
@@ -255,19 +238,18 @@ void DS18X20Controller::update() {
255238
}
256239
WS_DEBUG_PRINTLN("Published!");
257240

258-
#ifdef DEBUG_PROFILE
241+
#ifdef DEBUG_PROFILE
259242
unsigned long sensor_end_time = millis();
260243
WS_DEBUG_PRINT("Total sensor processing time: ");
261244
WS_DEBUG_PRINTLN(sensor_end_time - sensor_start_time);
262-
#endif
245+
#endif
263246
}
264247

265-
#ifdef DEBUG_PROFILE
248+
#ifdef DEBUG_PROFILE
266249
unsigned long total_end_time = millis();
267250
if (total_end_time - total_start_time != 0) {
268251
WS_DEBUG_PRINT("Total update() execution time: ");
269252
WS_DEBUG_PRINTLN(total_end_time - total_start_time);
270253
}
271-
#endif
272-
254+
#endif
273255
}

src/components/ds18x20/hardware.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ float DS18X20Hardware::GetTemperatureC() { return _temp_c; }
161161
@returns The temperature in Fahrenheit.
162162
*/
163163
/*************************************************************************/
164-
float DS18X20Hardware::GetTemperatureF() {
165-
_temp_f = _temp_c * 9.0 / 5.0 + 32.0;
166-
return _temp_f;
164+
float DS18X20Hardware::GetTemperatureF() {
165+
_temp_f = _temp_c * 9.0 / 5.0 + 32.0;
166+
return _temp_f;
167167
}
168168

169169
/*************************************************************************/
@@ -178,14 +178,19 @@ bool DS18X20Hardware::ReadTemperatureC() {
178178
// bus
179179
OneWireNg::ErrorCode ec =
180180
_drv_therm.convertTemp(_sensorId, DSTherm::MAX_CONV_TIME, false);
181+
182+
// TODO: this is extra debug prints, delete all prints and just return for
183+
// final ver.
184+
WS_DEBUG_PRINT("Error Code: ");
185+
WS_DEBUG_PRINTLN(ec);
181186
if (ec != OneWireNg::EC_SUCCESS)
182187
return false;
183188

184189
// Scratchpad placeholder is static to allow reuse of the associated
185190
// sensor id while reissuing readScratchpadSingle() calls.
186191
// Note, due to its storage class the placeholder is zero initialized.
187192
static Placeholder<DSTherm::Scratchpad> scrpd;
188-
ec = _drv_therm.readScratchpadSingle(scrpd);
193+
ec = _drv_therm.readScratchpad(_sensorId, scrpd);
189194
if (ec != OneWireNg::EC_SUCCESS)
190195
return false;
191196

0 commit comments

Comments
 (0)