Skip to content

Commit 33a7244

Browse files
committed
🚧 WIP, dsx - Add debug profile, remove redundant code within update() call
1 parent 6fcd6d6 commit 33a7244

File tree

6 files changed

+68
-69
lines changed

6 files changed

+68
-69
lines changed

‎src/Wippersnapper_V2.cpp‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,15 @@ void Wippersnapper_V2::haltErrorV2(String error,
869869
*/
870870
/**************************************************************************/
871871
bool Wippersnapper_V2::PublishSignal(pb_size_t which_payload, void *payload) {
872+
873+
#ifdef DEBUG_PROFILE
874+
unsigned long total_start_time = micros();
875+
#endif
876+
872877
size_t szMessageBuf;
873878
wippersnapper_signal_DeviceToBroker MsgSignal =
874879
wippersnapper_signal_DeviceToBroker_init_default;
880+
875881
// Fill generic signal payload with the payload from the args.
876882
WS_DEBUG_PRINT("Signal Payload Type: ");
877883
switch (which_payload) {
@@ -900,13 +906,15 @@ bool Wippersnapper_V2::PublishSignal(pb_size_t which_payload, void *payload) {
900906
WS_DEBUG_PRINTLN("DS18X20Added");
901907
MsgSignal.which_payload =
902908
wippersnapper_signal_DeviceToBroker_ds18x20_added_tag;
903-
MsgSignal.payload.ds18x20_added = *(wippersnapper_ds18x20_Ds18x20Added *)payload;
909+
MsgSignal.payload.ds18x20_added =
910+
*(wippersnapper_ds18x20_Ds18x20Added *)payload;
904911
break;
905912
case wippersnapper_signal_DeviceToBroker_ds18x20_event_tag:
906913
WS_DEBUG_PRINTLN("DS18X20Event");
907914
MsgSignal.which_payload =
908915
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag;
909-
MsgSignal.payload.ds18x20_event = *(wippersnapper_ds18x20_Ds18x20Event *)payload;
916+
MsgSignal.payload.ds18x20_event =
917+
*(wippersnapper_ds18x20_Ds18x20Event *)payload;
910918
break;
911919
default:
912920
WS_DEBUG_PRINTLN("ERROR: Invalid signal payload type, bailing out!");
@@ -934,20 +942,37 @@ bool Wippersnapper_V2::PublishSignal(pb_size_t which_payload, void *payload) {
934942
"ERROR: Unable to encode d2b signal message, bailing out!");
935943
return false;
936944
}
937-
WS_DEBUG_PRINTLN("Encoded!")
945+
WS_DEBUG_PRINTLN("Encoded!");
938946

939-
//. Check that we are still connected
947+
// Check that we are still connected
940948
runNetFSMV2();
941949
WsV2.feedWDTV2();
942950

951+
#ifdef DEBUG_PROFILE
952+
unsigned long publish_start_time = micros();
953+
#endif
954+
943955
// Attempt to publish the signal message to the broker
944956
WS_DEBUG_PRINT("Publishing signal message to broker...");
957+
#ifdef DEBUG_PROFILE
958+
WS_DEBUG_PRINT("Message buffer size: ");
959+
WS_DEBUG_PRINTLN(szMessageBuf);
960+
#endif
945961
if (!WsV2._mqttV2->publish(WsV2._topicD2b, msgBuf, szMessageBuf, 1)) {
946962
WS_DEBUG_PRINTLN("ERROR: Failed to publish signal message to broker!");
947963
return false;
948964
}
949965
WS_DEBUG_PRINTLN("Published!");
950966

967+
#ifdef DEBUG_PROFILE
968+
unsigned long publish_end_time = micros();
969+
WS_DEBUG_PRINT("Publishing time: ");
970+
WS_DEBUG_PRINTLN(publish_end_time - publish_start_time);
971+
unsigned long total_end_time = micros();
972+
WS_DEBUG_PRINT("Total PublishSignal() execution time: ");
973+
WS_DEBUG_PRINTLN(total_end_time - total_start_time);
974+
#endif
975+
951976
return true;
952977
}
953978

‎src/Wippersnapper_V2.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@
7272
#include "display/ws_display_ui_helper.h"
7373
#endif
7474

75-
76-
7775
#include "provisioning/ConfigJson.h"
7876
#if defined(USE_TINYUSB)
7977
#include "provisioning/tinyusb/Wippersnapper_FS_V2.h"
@@ -82,6 +80,10 @@
8280
#include "provisioning/littlefs/WipperSnapper_LittleFS.h"
8381
#endif
8482

83+
// Debug print macros
84+
// Uncomment the following to enable debug output for function profiling
85+
// #DEBUG_PROFILE 1
86+
8587
#define WS_VERSION \
8688
"2.0.0-alpha.1" ///< WipperSnapper app. version (semver-formatted)
8789

@@ -91,9 +93,6 @@
9193
#define WS_KEEPALIVE_INTERVAL_MS \
9294
5000 ///< Session keepalive interval time, in milliseconds
9395

94-
#define WS_MQTT_MAX_PAYLOAD_SIZE \
95-
512 ///< MAXIMUM expected payload size, in bytes
96-
9796
// Forward declarations (API v1)
9897
class Wippersnapper_AnalogIO;
9998
class Wippersnapper_FS_V2;

‎src/components/ds18x20/controller.cpp‎

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -192,74 +192,60 @@ void DS18X20Controller::update() {
192192
continue;
193193
}
194194

195-
// Create a new sensor_event
196-
_DS18X20_model->InitDS18x20EventMsg();
197-
198-
// Are we reading the temperature in Celsius, Fahrenheit, or both?
199-
if (temp_dsx_driver.is_read_temp_c) {
200-
unsigned long temp_c_start_time = millis();
201-
202-
WS_DEBUG_PRINTLN("Reading temperature in Celsius"); // TODO: Debug remove
203195
// Attempt to read the temperature in Celsius
196+
#ifdef DEBUG_PROFILE
197+
unsigned long temp_c_start_time = millis();
198+
#endif
204199
if (!temp_dsx_driver.ReadTemperatureC()) {
205200
WS_DEBUG_PRINTLN("ERROR: Unable to read temperature in Celsius");
206201
continue;
207202
}
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
208+
209+
// We got a temperature value from the hardware, let's create a new sensor_event
210+
_DS18X20_model->InitDS18x20EventMsg();
211+
212+
// Are we reading the temperature in Celsius, Fahrenheit, or both?
213+
if (temp_dsx_driver.is_read_temp_c) {
208214
float temp_c = temp_dsx_driver.GetTemperatureC();
209215
_DS18X20_model->addSensorEvent(
210216
wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE,
211217
temp_c);
212-
213-
unsigned long temp_c_end_time = millis();
214-
WS_DEBUG_PRINT("Read temperature Celsius time: ");
215-
WS_DEBUG_PRINTLN(temp_c_end_time - temp_c_start_time);
216218
}
217219
if (temp_dsx_driver.is_read_temp_f) {
218-
unsigned long temp_f_start_time = millis();
219-
220-
WS_DEBUG_PRINTLN("Reading temperature in Fahrenheit"); // TODO: Debug remove
221-
// Attempt to read the temperature in Fahrenheit
222-
if (!temp_dsx_driver.ReadTemperatureF()) {
223-
WS_DEBUG_PRINTLN("ERROR: Unable to read temperature in Fahrenheit");
224-
continue;
225-
}
226220
float temp_f = temp_dsx_driver.GetTemperatureF();
227221
_DS18X20_model->addSensorEvent(
228222
wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT,
229223
temp_f);
230-
231-
unsigned long temp_f_end_time = millis();
232-
WS_DEBUG_PRINT("Read temperature Fahrenheit time: ");
233-
WS_DEBUG_PRINTLN(temp_f_end_time - temp_f_start_time);
234224
}
235225

236-
// Get and print out the SensorEvent message's contents
237-
wippersnapper_ds18x20_Ds18x20Event event_msg =
238-
*_DS18X20_model->GetDS18x20EventMsg();
239-
pb_size_t event_count = event_msg.sensor_events_count;
226+
// Get the Ds18x20Event message
227+
wippersnapper_ds18x20_Ds18x20Event* event_msg = _DS18X20_model->GetDS18x20EventMsg();
228+
pb_size_t event_count = event_msg->sensor_events_count;
229+
240230

231+
// Print the message's content out for debugging
241232
WS_DEBUG_PRINT("Sensor OneWire bus pin: ");
242233
WS_DEBUG_PRINT(temp_dsx_driver.GetOneWirePin());
243234
WS_DEBUG_PRINT(" got ");
244235
WS_DEBUG_PRINT(event_count);
245236
WS_DEBUG_PRINTLN(" sensor events");
246237
for (int i = 0; i < event_count; i++) {
247238
WS_DEBUG_PRINT("Sensor value: ");
248-
WS_DEBUG_PRINTLN(event_msg.sensor_events[i].value.float_value);
239+
WS_DEBUG_PRINT(event_msg->sensor_events[i].value.float_value);
249240
}
250241

251242
// Encode the Ds18x20Event message
252-
unsigned long encode_start_time = millis();
253243
if (!_DS18X20_model->EncodeDs18x20Event()) {
254244
WS_DEBUG_PRINTLN("ERROR: Failed to encode Ds18x20Event message");
255245
continue;
256246
}
257-
unsigned long encode_end_time = millis();
258-
WS_DEBUG_PRINT("Encode event message time: ");
259-
WS_DEBUG_PRINTLN(encode_end_time - encode_start_time);
260247

261248
// Publish the Ds18x20Event message to the broker
262-
unsigned long publish_start_time = millis();
263249
WS_DEBUG_PRINT("Publishing Ds18x20Event message to broker...");
264250
if (!WsV2.PublishSignal(
265251
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag,
@@ -268,16 +254,20 @@ void DS18X20Controller::update() {
268254
continue;
269255
}
270256
WS_DEBUG_PRINTLN("Published!");
271-
unsigned long publish_end_time = millis();
272-
WS_DEBUG_PRINT("Publish event message time: ");
273-
WS_DEBUG_PRINTLN(publish_end_time - publish_start_time);
274257

258+
#ifdef DEBUG_PROFILE
275259
unsigned long sensor_end_time = millis();
276260
WS_DEBUG_PRINT("Total sensor processing time: ");
277261
WS_DEBUG_PRINTLN(sensor_end_time - sensor_start_time);
262+
#endif
278263
}
279264

265+
#ifdef DEBUG_PROFILE
280266
unsigned long total_end_time = millis();
281-
WS_DEBUG_PRINT("Total update() execution time: ");
282-
WS_DEBUG_PRINTLN(total_end_time - total_start_time);
267+
if (total_end_time - total_start_time != 0) {
268+
WS_DEBUG_PRINT("Total update() execution time: ");
269+
WS_DEBUG_PRINTLN(total_end_time - total_start_time);
270+
}
271+
#endif
272+
283273
}

‎src/components/ds18x20/hardware.cpp‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,9 @@ float DS18X20Hardware::GetTemperatureC() { return _temp_c; }
161161
@returns The temperature in Fahrenheit.
162162
*/
163163
/*************************************************************************/
164-
float DS18X20Hardware::GetTemperatureF() { return _temp_f; }
165-
166-
/*************************************************************************/
167-
/*!
168-
@brief Attempts to obtain the temperature from the sensor, in
169-
degrees Fahrenheit.
170-
@returns True if the temperature was successfully read, False otherwise.
171-
*/
172-
/*************************************************************************/
173-
bool DS18X20Hardware::ReadTemperatureF() {
174-
bool is_success = ReadTemperatureC();
175-
// Did we read the temperature successfully?
176-
if (!is_success)
177-
return false;
178-
// We now have the temperature but it's in in Celsius. Let's convert it to
179-
// Fahrenheit
180-
_temp_f = _temp_c * 9.0 / 5.0 + 32.0;
181-
182-
_prv_period = millis(); // Update the last time the sensor was polled
183-
return true;
164+
float DS18X20Hardware::GetTemperatureF() {
165+
_temp_f = _temp_c * 9.0 / 5.0 + 32.0;
166+
return _temp_f;
184167
}
185168

186169
/*************************************************************************/

‎src/components/ds18x20/hardware.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class DS18X20Hardware {
3737
bool GetSensor();
3838
uint8_t GetOneWirePin();
3939
bool ReadTemperatureC();
40-
bool ReadTemperatureF();
4140
float GetTemperatureC();
4241
float GetTemperatureF();
4342
bool is_read_temp_c; ///< Flag telling the controller to read the temperature

‎src/components/ds18x20/model.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ void DS18X20Model::addSensorEvent(wippersnapper_sensor_SensorType sensor_type,
182182
sensor_type;
183183
_msg_DS18x20Event.sensor_events[_msg_DS18x20Event.sensor_events_count]
184184
.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
185+
186+
// Convert the float to 2 decimal places
187+
sensor_value = roundf(sensor_value * 100) / 100;
185188
_msg_DS18x20Event.sensor_events[_msg_DS18x20Event.sensor_events_count]
186189
.value.float_value = sensor_value;
187190
_msg_DS18x20Event.sensor_events_count++;

0 commit comments

Comments
 (0)