Skip to content

Commit 922f0a6

Browse files
committed
🚧 WIP, dsx - Add to hardware, updat eloop
1 parent 6050407 commit 922f0a6

File tree

3 files changed

+70
-31
lines changed

3 files changed

+70
-31
lines changed

src/components/ds18x20/controller.cpp

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
2424
WS_DEBUG_PRINTLN("ERROR: Unable to decode Ds18x20Add message");
2525
return false;
2626
}
27+
28+
// If we receive no sensor types to configure, bail out
29+
if (_DS18X20_model->GetDS18x20AddMsg()->sensor_types_count == 0) {
30+
WS_DEBUG_PRINTLN("ERROR: No ds18x sensor types provided!");
31+
return false;
32+
}
33+
2734
// Extract the OneWire pin from the message
2835
uint8_t pin_name = atoi(_DS18X20_model->GetDS18x20AddMsg()->onewire_pin + 1);
2936

@@ -34,13 +41,27 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
3441
if (!is_initialized) {
3542
WS_DEBUG_PRINTLN("Sensor found on OneWire bus and initialized");
3643
// Set the sensor's resolution
37-
new_dsx_driver.setResolution(
44+
new_dsx_driver.SetResolution(
3845
_DS18X20_model->GetDS18x20AddMsg()->sensor_resolution);
39-
46+
// Set the sensor's period
47+
new_dsx_driver.SetPeriod(_DS18X20_model->GetDS18x20AddMsg()->period);
48+
// Configure the types of sensor reads to perform
49+
for (int i = 0; i < _DS18X20_model->GetDS18x20AddMsg()->sensor_types_count;
50+
i++) {
51+
if (_DS18X20_model->GetDS18x20AddMsg()->sensor_types[i] ==
52+
wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE) {
53+
new_dsx_driver.is_read_temp_c = true;
54+
} else if (
55+
_DS18X20_model->GetDS18x20AddMsg()->sensor_types[i] ==
56+
wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE_FAHRENHEIT) {
57+
new_dsx_driver.is_read_temp_f = true;
58+
}
59+
}
4060
// Add the DS18X20Hardware object to the vector of hardware objects
4161
_DS18X20_pins.push_back(new_dsx_driver);
4262
} else {
43-
WS_DEBUG_PRINTLN("ERROR: Unable to get sensor ID, sensor not initialized");
63+
WS_DEBUG_PRINTLN(
64+
"ERROR: Unable to get ds18x sensor ID, ds18x sensor not initialized");
4465
}
4566

4667
// Encode and publish a Ds18x20Added message back to the broker
@@ -62,17 +83,21 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
6283

6384
void DS18X20Controller::update() {
6485
// Bail out if there are no OneWire pins to poll
65-
if (_DS18X20_pins.size() == 0)
86+
if (_DS18X20_pins.empty())
6687
return;
6788

6889
// Iterate through the vector
6990
for (uint8_t i = 0; i < _DS18X20_pins.size(); i++) {
7091
// 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: the update() method should check sensor type(s)
76-
// before polling ReadTemperatureX methods!
92+
DS18X20Hardware &temp_dsx_driver = _DS18X20_pins[i];
93+
// Check if the driver's timer has not expired yet
94+
if (!temp_dsx_driver.IsTimerExpired())
95+
continue;
96+
// Are we reading the temperature in Celsius, Fahrenheit, or both?
97+
if (temp_dsx_driver.is_read_temp_c)
98+
temp_dsx_driver.GetTemperatureC();
99+
if (temp_dsx_driver.is_read_temp_f)
100+
temp_dsx_driver.GetTemperatureF();
101+
// wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE
77102
}
78103
}

src/components/ds18x20/hardware.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,25 @@
1515
#include "hardware.h"
1616

1717
DS18X20Hardware::DS18X20Hardware(uint8_t onewire_pin) : _drv_therm(_ow) {
18+
is_read_temp_c = false;
19+
is_read_temp_f = false;
1820
// Initialize the OneWire bus object
21+
_onewire_pin = onewire_pin;
1922
new (&_ow) OneWireNg_CurrentPlatform(onewire_pin, false);
2023
}
2124

22-
DS18X20Hardware::~DS18X20Hardware() { delete &_ow; }
25+
DS18X20Hardware::~DS18X20Hardware() {
26+
pinMode(_onewire_pin,
27+
INPUT); // Set the pin to hi-z and release it for other uses
28+
delete &_ow;
29+
}
2330

2431
bool DS18X20Hardware::GetSensor() {
2532
OneWireNg::ErrorCode ec = _ow->readSingleId(_sensorId);
2633
return ec == OneWireNg::EC_SUCCESS;
2734
}
2835

29-
void DS18X20Hardware::setResolution(int resolution) {
36+
void DS18X20Hardware::SetResolution(int resolution) {
3037
// Set the resolution of the DS18X20 sensor driver
3138
switch (resolution) {
3239
case 9:
@@ -57,10 +64,13 @@ void DS18X20Hardware::setResolution(int resolution) {
5764
_drv_therm.copyScratchpadAll(false);
5865
}
5966

67+
void DS18X20Hardware::SetPeriod(float period) {
68+
_period = period * 1000; // Convert to milliseconds
69+
}
70+
71+
// Get the current time in milliseconds and compare it to the last time
72+
// the sensor was polled
6073
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();
6474
return millis() - _prv_period > _period;
6575
}
6676

@@ -73,8 +83,11 @@ bool DS18X20Hardware::ReadTemperatureF() {
7383
// Did we read the temperature successfully?
7484
if (!is_success)
7585
return false;
76-
// We have the temperature in Celsius, convert to Fahrenheit
86+
// We now have the temperature but it's in in Celsius. Let's convert it to
87+
// Fahrenheit
7788
_temp_f = _temp_c * 9.0 / 5.0 + 32.0;
89+
90+
_prv_period = millis(); // Update the last time the sensor was polled
7891
return true;
7992
}
8093

@@ -84,19 +97,20 @@ bool DS18X20Hardware::ReadTemperatureC() {
8497
OneWireNg::ErrorCode ec =
8598
_drv_therm.convertTemp(_sensorId, DSTherm::MAX_CONV_TIME, false);
8699
if (ec != OneWireNg::EC_SUCCESS)
87-
return false
100+
return false;
88101

89-
// Scratchpad placeholder is static to allow reuse of the associated
90-
// sensor id while reissuing readScratchpadSingle() calls.
91-
// Note, due to its storage class the placeholder is zero initialized.
92-
static Placeholder<DSTherm::Scratchpad>
93-
scrpd;
102+
// Scratchpad placeholder is static to allow reuse of the associated
103+
// sensor id while reissuing readScratchpadSingle() calls.
104+
// Note, due to its storage class the placeholder is zero initialized.
105+
static Placeholder<DSTherm::Scratchpad> scrpd;
94106
ec = _drv_therm.readScratchpadSingle(scrpd);
95107
if (ec != OneWireNg::EC_SUCCESS)
96108
return false;
97109

98110
// Read the temperature from the sensor
99111
long temp = scrpd->getTemp2();
100112
_temp_c = temp / 16.0; // Convert from 16-bit int to float
113+
114+
_prv_period = millis(); // Update the last time the sensor was polled
101115
return true;
102116
}

src/components/ds18x20/hardware.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ class DS18X20Hardware {
3030
public:
3131
DS18X20Hardware(uint8_t onewire_pin);
3232
~DS18X20Hardware();
33-
bool GetSensor();
34-
void setResolution(int resolution);
33+
void SetResolution(int resolution);
34+
void SetPeriod(float period);
3535
bool IsTimerExpired();
36+
bool GetSensor();
3637
bool ReadTemperatureC();
3738
bool ReadTemperatureF();
3839
float GetTemperatureC();
3940
float GetTemperatureF();
41+
bool is_read_temp_c;
42+
bool is_read_temp_f;
4043

4144
private:
4245
Placeholder<OneWireNg_CurrentPlatform> _ow; ///< OneWire bus object
@@ -46,12 +49,9 @@ class DS18X20Hardware {
4649
float _temp_c; ///< Temperature in Celsius
4750
float _temp_f; ///< Temperature in Fahrenheit
4851
// From the PB model
49-
uint8_t onewire_pin; ///< Pin utilized by the OneWire bus, used for addressing
50-
float _period; ///< The desired period to read the sensor, in seconds
51-
float _prv_period; ///< Last time the sensor was polled, in seconds
52-
pb_size_t
53-
_sensor_types_count; ///< Number of sensor types to read from the sensor
54-
wippersnapper_sensor_SensorType
55-
_sensor_types[2]; ///< DS sensor type(s) to read from the sensor
52+
uint8_t
53+
_onewire_pin; ///< Pin utilized by the OneWire bus, used for addressing
54+
float _period; ///< The desired period to read the sensor, in seconds
55+
float _prv_period; ///< Last time the sensor was polled, in seconds
5656
};
5757
#endif // WS_DS18X20_HARDWARE_H

0 commit comments

Comments
 (0)