Skip to content

Commit 936f276

Browse files
committed
🚧 WIP, dsx - Add read and sensor id funcs
1 parent dc7c54e commit 936f276

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

src/components/ds18x20/controller.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,23 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
2929

3030
// Initialize the DS18X20Hardware object
3131
DS18X20Hardware new_dsx_driver(pin_name);
32-
new_dsx_driver.setResolution(
33-
_DS18X20_model->GetDS18x20AddMsg()->sensor_resolution);
32+
// Attempt to get the sensor's ID on the OneWire bus to show it's been init'd
33+
bool is_initialized = new_dsx_driver.GetSensor();
34+
if (!is_initialized) {
35+
WS_DEBUG_PRINTLN("Sensor found on OneWire bus and initialized");
36+
// Set the sensor's resolution
37+
new_dsx_driver.setResolution(
38+
_DS18X20_model->GetDS18x20AddMsg()->sensor_resolution);
3439

35-
// Add the DS18X20Hardware object to the vector of hardware objects
36-
_DS18X20_pins.push_back(new_dsx_driver);
40+
// Add the DS18X20Hardware object to the vector of hardware objects
41+
_DS18X20_pins.push_back(new_dsx_driver);
42+
} else {
43+
WS_DEBUG_PRINTLN("ERROR: Unable to get sensor ID, sensor not initialized");
44+
}
3745

3846
// Encode and publish a Ds18x20Added message back to the broker
3947
if (!_DS18X20_model->EncodeDS18x20Added(
40-
_DS18X20_model->GetDS18x20AddMsg()->onewire_pin, true)) {
48+
_DS18X20_model->GetDS18x20AddMsg()->onewire_pin, is_initialized)) {
4149
WS_DEBUG_PRINTLN("ERROR: Unable to encode Ds18x20Added message");
4250
return false;
4351
}
@@ -51,3 +59,19 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
5159

5260
return true;
5361
}
62+
63+
void DS18X20Controller::update() {
64+
// Bail out if there are no OneWire pins to poll
65+
if (_DS18X20_pins.size() == 0)
66+
return;
67+
68+
// Iterate through the vector
69+
for (uint8_t i = 0; i < _DS18X20_pins.size(); i++) {
70+
// Create a temporary DS18X20Hardware driver
71+
DS18X20Hardware *temp_dsx_driver = &_DS18X20_pins[i];
72+
// Check if the driver's timer has expired
73+
if (temp_dsx_driver->IsTimerExpired())
74+
return;
75+
// TODO: Poll the sensor
76+
}
77+
}

src/components/ds18x20/controller.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class DS18X20Controller {
3333
~DS18X20Controller();
3434
// Routing
3535
bool Handle_Ds18x20Add(pb_istream_t *stream);
36+
// Polling
37+
void update();
3638

3739
private:
3840
DS18X20Model *_DS18X20_model; ///< ds18x20 model

src/components/ds18x20/hardware.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@
1515
#include "hardware.h"
1616

1717
DS18X20Hardware::DS18X20Hardware(uint8_t onewire_pin) : _drv_therm(_ow) {
18+
// Initialize the OneWire bus object
1819
new (&_ow) OneWireNg_CurrentPlatform(onewire_pin, false);
1920
}
2021

2122
DS18X20Hardware::~DS18X20Hardware() { delete &_ow; }
2223

24+
bool DS18X20Hardware::GetSensor() {
25+
OneWireNg::ErrorCode ec = _ow->readSingleId(_sensorId);
26+
return ec == OneWireNg::EC_SUCCESS;
27+
}
28+
2329
void DS18X20Hardware::setResolution(int resolution) {
2430
// Set the resolution of the DS18X20 sensor driver
2531
switch (resolution) {
@@ -49,4 +55,34 @@ void DS18X20Hardware::setResolution(int resolution) {
4955
// memory and will be lost after power unplug. Therefore store the
5056
// configuration permanently in sensors EEPROM.
5157
_drv_therm.copyScratchpadAll(false);
58+
}
59+
60+
bool DS18X20Hardware::IsTimerExpired() {
61+
// Get the current time in milliseconds and compare it to the last time
62+
// the sensor was polled
63+
ulong cur_time = millis();
64+
return millis() - _prv_period > _period;
65+
}
66+
67+
OneWireNg::ErrorCode DS18X20Hardware::ReadTemperatureC() {
68+
// Start temperature conversion for the first identified sensor on the OneWire
69+
// bus
70+
OneWireNg::ErrorCode ec =
71+
_drv_therm.convertTemp(_sensorId, DSTherm::MAX_CONV_TIME, false);
72+
if (ec != OneWireNg::EC_SUCCESS)
73+
return ec;
74+
75+
// Scratchpad placeholder is static to allow reuse of the associated
76+
// sensor id while reissuing readScratchpadSingle() calls.
77+
// Note, due to its storage class the placeholder is zero initialized.
78+
static Placeholder<DSTherm::Scratchpad> scrpd;
79+
ec = _drv_therm.readScratchpadSingle(scrpd);
80+
if (ec != OneWireNg::EC_SUCCESS)
81+
return ec;
82+
83+
// Read the temperature from the sensor
84+
long temp = scrpd->getTemp2();
85+
_temp_c = temp / 16.0; // Convert from 16-bit int to float
86+
87+
return ec;
5288
}

src/components/ds18x20/hardware.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,21 @@ class DS18X20Hardware {
3030
public:
3131
DS18X20Hardware(uint8_t onewire_pin);
3232
~DS18X20Hardware();
33+
bool GetSensor();
34+
OneWireNg::ErrorCode DS18X20Hardware::ReadTemperatureC();
3335
void setResolution(int resolution);
36+
bool IsTimerExpired();
3437

3538
private:
3639
// NOTE: We are going to try definining a vector of DS18X20Hardware objects
3740
// iwthin the controller so instead of a struct, these are all assigned to
3841
// this class
3942
Placeholder<OneWireNg_CurrentPlatform> _ow; ///< OneWire bus object
40-
DSTherm _drv_therm; ///< DS18X20 driver object
43+
OneWireNg::Id _sensorId;
44+
DSTherm _drv_therm; ///< DS18X20 driver object
4145
DSTherm::Resolution _resolution; ///< Resolution of the DS18X20 sensor
46+
float _temp_c;
47+
float _temp_f;
4248
// From the PB model
4349
uint8_t onewire_pin; ///< Pin utilized by the OneWire bus, used for addressing
4450
float _period; ///< The desired period to read the sensor, in seconds

0 commit comments

Comments
 (0)