Skip to content

Commit bbf4cad

Browse files
committed
📝 Review controller/hardware classes, self-review
1 parent 63385d1 commit bbf4cad

File tree

3 files changed

+40
-50
lines changed

3 files changed

+40
-50
lines changed

src/components/ds18x20/controller.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
6666
if (is_initialized) {
6767
WS_DEBUG_PRINTLN("Sensor found on OneWire bus and initialized");
6868

69-
// Set the sensor's pin name (non-logical)
69+
// Set the sensor's pin name (non-logical name)
7070
new_dsx_driver->setOneWirePinName(
7171
_DS18X20_model->GetDS18x20AddMsg()->onewire_pin);
7272

@@ -88,16 +88,18 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
8888
wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT) {
8989
new_dsx_driver->is_read_temp_f = true;
9090
} else {
91-
WS_DEBUG_PRINTLN("ERROR | DS18x20: Unknown SensorType, failed to add sensor!");
92-
return false;
91+
WS_DEBUG_PRINTLN(
92+
"ERROR | DS18x20: Unknown SensorType, failed to add sensor!");
93+
is_initialized = false;
9394
}
9495
}
9596

96-
// Add the DS18X20Hardware object to the vector of hardware objects
97-
_DS18X20_pins.push_back(std::move(new_dsx_driver));
97+
// If the sensor was successfully initialized, add it to the controller
98+
if (is_initialized == true)
99+
_DS18X20_pins.push_back(std::move(new_dsx_driver));
98100
} else {
99-
WS_DEBUG_PRINTLN(
100-
"ERROR | DS18x20: Unable to get sensor ID!");
101+
WS_DEBUG_PRINTLN("ERROR | DS18x20: Unable to get sensor ID!");
102+
is_initialized = false;
101103
}
102104

103105
// Encode and publish a Ds18x20Added message back to the broker
@@ -110,7 +112,8 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
110112

111113
if (!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_ds18x20_added_tag,
112114
_DS18X20_model->GetDS18x20AddedMsg())) {
113-
WS_DEBUG_PRINTLN("ERROR | DS18x20: Unable to publish Ds18x20Added message!");
115+
WS_DEBUG_PRINTLN(
116+
"ERROR | DS18x20: Unable to publish Ds18x20Added message!");
114117
return false;
115118
}
116119

@@ -185,7 +188,8 @@ void DS18X20Controller::update() {
185188
#endif
186189

187190
if (!temp_dsx_driver.ReadTemperatureC()) {
188-
WS_DEBUG_PRINTLN("ERROR | DS18x20: Unable to read temperature in Celsius");
191+
WS_DEBUG_PRINTLN(
192+
"ERROR | DS18x20: Unable to read temperature in Celsius");
189193
continue;
190194
}
191195

@@ -219,28 +223,27 @@ void DS18X20Controller::update() {
219223
pb_size_t event_count = event_msg->sensor_events_count;
220224

221225
// Print the message's content out for debugging
222-
WS_DEBUG_PRINT("Sensor OneWire bus pin: ");
226+
WS_DEBUG_PRINT("DS18x20: OneWire pin: ");
223227
WS_DEBUG_PRINT(temp_dsx_driver.GetOneWirePin());
224-
WS_DEBUG_PRINT(" got ");
225-
WS_DEBUG_PRINT(event_count);
226-
WS_DEBUG_PRINTLN(" sensor events");
228+
WS_DEBUG_PRINT(" got value(s): ");
227229
for (int i = 0; i < event_count; i++) {
228-
WS_DEBUG_PRINT("Sensor value: ");
229230
WS_DEBUG_PRINT(event_msg->sensor_events[i].value.float_value);
230231
}
231232

232233
// Encode the Ds18x20Event message
233234
if (!_DS18X20_model->EncodeDs18x20Event()) {
234-
WS_DEBUG_PRINTLN("ERROR | DS18x20: Failed to encode Ds18x20Event message");
235+
WS_DEBUG_PRINTLN(
236+
"ERROR | DS18x20: Failed to encode Ds18x20Event message");
235237
continue;
236238
}
237239

238240
// Publish the Ds18x20Event message to the broker
239-
WS_DEBUG_PRINT("Publishing Ds18x20Event message to broker...");
241+
WS_DEBUG_PRINT("DS18x20: Publishing event to broker...");
240242
if (!WsV2.PublishSignal(
241243
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag,
242244
_DS18X20_model->GetDS18x20EventMsg())) {
243-
WS_DEBUG_PRINTLN("ERROR | DS18x20: Failed to publish Ds18x20Event message");
245+
WS_DEBUG_PRINTLN(
246+
"ERROR | DS18x20: Failed to publish Ds18x20Event message");
244247
continue;
245248
}
246249
WS_DEBUG_PRINTLN("Published!");

src/components/ds18x20/hardware.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,6 @@ DS18X20Hardware::~DS18X20Hardware() {
3939
INPUT); // Set the pin to hi-z and release it for other uses
4040
}
4141

42-
void DS18X20Hardware::printErrorCode(OneWireNg::ErrorCode ec) {
43-
switch (ec) {
44-
case OneWireNg::EC_SUCCESS:
45-
WS_DEBUG_PRINTLN("EC_SUCCESS");
46-
break;
47-
case OneWireNg::EC_NO_DEVS:
48-
WS_DEBUG_PRINTLN("EC_NO_DEVSNo slave devices; search process finished");
49-
break;
50-
case OneWireNg::EC_BUS_ERROR:
51-
WS_DEBUG_PRINTLN("EC_BUS_ERROR-wire bus error");
52-
break;
53-
case OneWireNg::EC_CRC_ERROR:
54-
WS_DEBUG_PRINTLN("EC_CRC_ERRORCRC error");
55-
break;
56-
case OneWireNg::EC_UNSUPPORED:
57-
WS_DEBUG_PRINTLN("EC_UNSUPPOREDService is not supported by the platform");
58-
break;
59-
case OneWireNg::EC_FULL:
60-
WS_DEBUG_PRINTLN("EC_FULLNo space (e.g. filters table is full)");
61-
break;
62-
default:
63-
WS_DEBUG_PRINTLN("EC ?!?1 Unknown error");
64-
break;
65-
}
66-
}
67-
6842
/***********************************************************************/
6943
/*!
7044
@brief Get the sensor's ID
@@ -73,7 +47,6 @@ void DS18X20Hardware::printErrorCode(OneWireNg::ErrorCode ec) {
7347
/***********************************************************************/
7448
bool DS18X20Hardware::GetSensor() {
7549
OneWireNg::ErrorCode ec = _ow->readSingleId(_sensorId);
76-
printErrorCode(ec);
7750
return ec == OneWireNg::EC_SUCCESS;
7851
}
7952

@@ -85,12 +58,26 @@ bool DS18X20Hardware::GetSensor() {
8558
/***********************************************************************/
8659
uint8_t DS18X20Hardware::GetOneWirePin() { return _onewire_pin; }
8760

61+
/***********************************************************************/
62+
/*!
63+
@brief Sets the name of the OneWire bus pin.
64+
@param prettyOWPinName
65+
The name of the OneWire bus pin (non-logical pin name,
66+
includes the "D" or "A" prefix).
67+
*/
68+
/***********************************************************************/
8869
void DS18X20Hardware::setOneWirePinName(const char *prettyOWPinName) {
8970
strncpy(_onewire_pin_name, prettyOWPinName, sizeof(_onewire_pin_name));
9071
_onewire_pin_name[sizeof(_onewire_pin_name) - 1] = '\0';
9172
}
9273

93-
// Return _onewire_pin_name
74+
/***********************************************************************/
75+
/*!
76+
@brief Gets the name of the OneWire bus pin.
77+
@returns The name of the OneWire bus pin (non-logical pin name,
78+
includes the "D" or "A" prefix).
79+
*/
80+
/***********************************************************************/
9481
const char *DS18X20Hardware::getOneWirePinName() { return _onewire_pin_name; }
9582

9683
/*************************************************************************/
@@ -141,7 +128,8 @@ void DS18X20Hardware::SetResolution(int resolution) {
141128
/*************************************************************************/
142129
void DS18X20Hardware::SetPeriod(float period) {
143130
_period = period * 1000; // Convert to milliseconds
144-
_prv_period = 0; // Also reset the previous period here
131+
_prv_period = 0; // Also reset the previous period whenever we set a
132+
// new period
145133
}
146134

147135
/*************************************************************************/

src/components/ds18x20/hardware.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ class DS18X20Hardware {
3030
public:
3131
DS18X20Hardware(uint8_t onewire_pin);
3232
~DS18X20Hardware();
33-
void printErrorCode(OneWireNg::ErrorCode ec);
34-
void setOneWirePinName(const char *prettyOWPinName);
33+
uint8_t GetOneWirePin();
3534
void SetResolution(int resolution);
3635
void SetPeriod(float period);
36+
void setOneWirePinName(const char *prettyOWPinName);
37+
const char *getOneWirePinName();
3738
bool IsTimerExpired();
3839
bool GetSensor();
39-
uint8_t GetOneWirePin();
40-
const char *getOneWirePinName();
4140
bool ReadTemperatureC();
4241
float GetTemperatureC();
4342
float GetTemperatureF();

0 commit comments

Comments
 (0)