Skip to content

Commit 36226cc

Browse files
committed
🚧 WIP, dsx - Read temperature and properly reset all counters
1 parent 60238ca commit 36226cc

File tree

7 files changed

+73
-22
lines changed

7 files changed

+73
-22
lines changed

platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ extends = common:esp32
179179
board = adafruit_feather_esp32s3
180180
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3 -DBOARD_HAS_PSRAM
181181
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
182-
board_build.partitions = tinyuf2-partitions-4MB.csv
182+
; board_build.partitions = tinyuf2-partitions-4MB.csv
183+
board_build.partitions = tinyuf2-partitions-4MB-noota.csv
183184
extra_scripts = pre:rename_usb_config.py
184185

185186
; Adafruit Feather ESP32-S3 NO PSRAM

src/Wippersnapper_V2.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ bool cbDecodeBrokerToDevice(pb_istream_t *stream, const pb_field_t *field,
372372
return false;
373373
}
374374
break;
375+
case wippersnapper_signal_BrokerToDevice_ds18x20_add_tag:
376+
WS_DEBUG_PRINTLN("-> DS18X20 Add Message Type");
377+
if (!WsV2._ds18x20_controller->Handle_Ds18x20Add(stream)) {
378+
WS_DEBUG_PRINTLN("ERROR: Unable to add DS18X20 sensor!");
379+
return false;
380+
}
381+
break;
375382
default:
376383
WS_DEBUG_PRINTLN("ERROR: BrokerToDevice message type not found!");
377384
return false;
@@ -1171,6 +1178,9 @@ ws_status_t Wippersnapper_V2::runV2() {
11711178
// Process all analog inputs
11721179
WsV2.analogio_controller->update();
11731180

1181+
// Process all DS18x20 sensor events
1182+
WsV2._ds18x20_controller->update();
1183+
11741184
// TODO: Process I2C sensor events
11751185

11761186
// TODO: Process DS18x20 sensor events

src/components/ds18x20/controller.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,31 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
5959
uint8_t pin_name = atoi(_DS18X20_model->GetDS18x20AddMsg()->onewire_pin + 1);
6060

6161
// Initialize the DS18X20Hardware object
62-
DS18X20Hardware new_dsx_driver(pin_name);
62+
auto new_dsx_driver = std::make_unique<DS18X20Hardware>(pin_name);
6363
// 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) {
6666
WS_DEBUG_PRINTLN("Sensor found on OneWire bus and initialized");
6767
// Set the sensor's resolution
68-
new_dsx_driver.SetResolution(
68+
new_dsx_driver->SetResolution(
6969
_DS18X20_model->GetDS18x20AddMsg()->sensor_resolution);
7070
// Set the sensor's period
71-
new_dsx_driver.SetPeriod(_DS18X20_model->GetDS18x20AddMsg()->period);
71+
new_dsx_driver->SetPeriod(_DS18X20_model->GetDS18x20AddMsg()->period);
7272
// Configure the types of sensor reads to perform
7373
for (int i = 0; i < _DS18X20_model->GetDS18x20AddMsg()->sensor_types_count;
7474
i++) {
7575
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;
7878
} else if (
7979
_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;
8282
}
8383
}
8484
// 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));
8687
} else {
8788
WS_DEBUG_PRINTLN(
8889
"ERROR: Unable to get ds18x sensor ID, ds18x sensor not initialized");
@@ -126,12 +127,12 @@ bool DS18X20Controller::Handle_Ds18x20Remove(pb_istream_t *stream) {
126127
wippersnapper_ds18x20_Ds18x20Remove *msg_remove =
127128
_DS18X20_model->GetDS18x20RemoveMsg();
128129
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) {
133134
_DS18X20_pins.erase(_DS18X20_pins.begin() + i);
134-
break;
135+
return true;
135136
}
136137
}
137138
WS_DEBUG_PRINT("Removed OneWire Pin: ");
@@ -152,7 +153,7 @@ void DS18X20Controller::update() {
152153
// Iterate through the vector
153154
for (uint8_t i = 0; i < _DS18X20_pins.size(); i++) {
154155
// Create a temporary DS18X20Hardware driver
155-
DS18X20Hardware &temp_dsx_driver = _DS18X20_pins[i];
156+
DS18X20Hardware &temp_dsx_driver = *(_DS18X20_pins[i]);
156157
// Check if the driver's timer has not expired yet
157158
if (!temp_dsx_driver.IsTimerExpired()) {
158159
continue;
@@ -162,17 +163,27 @@ void DS18X20Controller::update() {
162163
// Are we reading the temperature in Celsius, Fahrenheit, or both?
163164
if (temp_dsx_driver.is_read_temp_c) {
164165
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+
}
165171
float temp_c = temp_dsx_driver.GetTemperatureC();
166172
_DS18X20_model->addSensorEvent(
167-
wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE,
173+
wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE,
168174
temp_c);
169175
}
170176
if (temp_dsx_driver.is_read_temp_f) {
171177
WS_DEBUG_PRINTLN(
172178
"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+
}
173184
float temp_f = temp_dsx_driver.GetTemperatureF();
174185
_DS18X20_model->addSensorEvent(
175-
wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE_FAHRENHEIT,
186+
wippersnapper_sensor_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE_FAHRENHEIT,
176187
temp_f);
177188
}
178189
// Print out the SensorEvent message's contents for debugging

src/components/ds18x20/controller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Wippersnapper_V2.h"
1818
#include "hardware.h"
1919
#include "model.h"
20+
#include <memory>
2021

2122
class Wippersnapper_V2; ///< Forward declaration
2223
class DS18X20Model; ///< Forward declaration
@@ -39,8 +40,7 @@ class DS18X20Controller {
3940

4041
private:
4142
DS18X20Model *_DS18X20_model; ///< ds18x20 model
42-
std::vector<DS18X20Hardware>
43-
_DS18X20_pins; ///< Vector containing multiple DS18X20Hardware objects
43+
std::vector<std::unique_ptr<DS18X20Hardware>> _DS18X20_pins;
4444
};
4545
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
4646
#endif // WS_DS18X20_CONTROLLER_H

src/components/ds18x20/hardware.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,32 @@ DS18X20Hardware::DS18X20Hardware(uint8_t onewire_pin) : _drv_therm(_ow) {
3737
DS18X20Hardware::~DS18X20Hardware() {
3838
pinMode(_onewire_pin,
3939
INPUT); // Set the pin to hi-z and release it for other uses
40-
delete &_ow;
40+
}
41+
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+
}
4166
}
4267

4368
/***********************************************************************/
@@ -48,6 +73,7 @@ DS18X20Hardware::~DS18X20Hardware() {
4873
/***********************************************************************/
4974
bool DS18X20Hardware::GetSensor() {
5075
OneWireNg::ErrorCode ec = _ow->readSingleId(_sensorId);
76+
printErrorCode(ec);
5177
return ec == OneWireNg::EC_SUCCESS;
5278
}
5379

@@ -107,6 +133,7 @@ void DS18X20Hardware::SetResolution(int resolution) {
107133
/*************************************************************************/
108134
void DS18X20Hardware::SetPeriod(float period) {
109135
_period = period * 1000; // Convert to milliseconds
136+
_prv_period = 0; // Also reset the previous period here
110137
}
111138

112139
/*************************************************************************/

src/components/ds18x20/hardware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class DS18X20Hardware {
3030
public:
3131
DS18X20Hardware(uint8_t onewire_pin);
3232
~DS18X20Hardware();
33+
void printErrorCode(OneWireNg::ErrorCode ec);
3334
void SetResolution(int resolution);
3435
void SetPeriod(float period);
3536
bool IsTimerExpired();

src/components/ds18x20/model.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,5 @@ void DS18X20Model::addSensorEvent(wippersnapper_sensor_SensorType sensor_type,
193193
.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
194194
_msg_DS18x20Event.sensor_events[_msg_DS18x20Event.sensor_events_count]
195195
.value.float_value = sensor_value;
196+
_msg_DS18x20Event.sensor_events_count++;
196197
}

0 commit comments

Comments
 (0)