Skip to content

Commit 6050407

Browse files
committed
🚧 WIP, dsx - Implement readF, getters
1 parent 936f276 commit 6050407

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/components/ds18x20/controller.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void DS18X20Controller::update() {
7272
// Check if the driver's timer has expired
7373
if (temp_dsx_driver->IsTimerExpired())
7474
return;
75-
// TODO: Poll the sensor
75+
// TODO: the update() method should check sensor type(s)
76+
// before polling ReadTemperatureX methods!
7677
}
7778
}

src/components/ds18x20/hardware.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,39 @@ bool DS18X20Hardware::IsTimerExpired() {
6464
return millis() - _prv_period > _period;
6565
}
6666

67-
OneWireNg::ErrorCode DS18X20Hardware::ReadTemperatureC() {
67+
float DS18X20Hardware::GetTemperatureC() { return _temp_c; }
68+
69+
float DS18X20Hardware::GetTemperatureF() { return _temp_f; }
70+
71+
bool DS18X20Hardware::ReadTemperatureF() {
72+
bool is_success = ReadTemperatureC();
73+
// Did we read the temperature successfully?
74+
if (!is_success)
75+
return false;
76+
// We have the temperature in Celsius, convert to Fahrenheit
77+
_temp_f = _temp_c * 9.0 / 5.0 + 32.0;
78+
return true;
79+
}
80+
81+
bool DS18X20Hardware::ReadTemperatureC() {
6882
// Start temperature conversion for the first identified sensor on the OneWire
6983
// bus
7084
OneWireNg::ErrorCode ec =
7185
_drv_therm.convertTemp(_sensorId, DSTherm::MAX_CONV_TIME, false);
7286
if (ec != OneWireNg::EC_SUCCESS)
73-
return ec;
87+
return false
7488

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;
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;
7994
ec = _drv_therm.readScratchpadSingle(scrpd);
8095
if (ec != OneWireNg::EC_SUCCESS)
81-
return ec;
96+
return false;
8297

8398
// Read the temperature from the sensor
8499
long temp = scrpd->getTemp2();
85100
_temp_c = temp / 16.0; // Convert from 16-bit int to float
86-
87-
return ec;
101+
return true;
88102
}

src/components/ds18x20/hardware.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ class DS18X20Hardware {
3131
DS18X20Hardware(uint8_t onewire_pin);
3232
~DS18X20Hardware();
3333
bool GetSensor();
34-
OneWireNg::ErrorCode DS18X20Hardware::ReadTemperatureC();
3534
void setResolution(int resolution);
3635
bool IsTimerExpired();
36+
bool ReadTemperatureC();
37+
bool ReadTemperatureF();
38+
float GetTemperatureC();
39+
float GetTemperatureF();
3740

3841
private:
39-
// NOTE: We are going to try definining a vector of DS18X20Hardware objects
40-
// iwthin the controller so instead of a struct, these are all assigned to
41-
// this class
4242
Placeholder<OneWireNg_CurrentPlatform> _ow; ///< OneWire bus object
4343
OneWireNg::Id _sensorId;
4444
DSTherm _drv_therm; ///< DS18X20 driver object
4545
DSTherm::Resolution _resolution; ///< Resolution of the DS18X20 sensor
46-
float _temp_c;
47-
float _temp_f;
46+
float _temp_c; ///< Temperature in Celsius
47+
float _temp_f; ///< Temperature in Fahrenheit
4848
// From the PB model
4949
uint8_t onewire_pin; ///< Pin utilized by the OneWire bus, used for addressing
5050
float _period; ///< The desired period to read the sensor, in seconds

0 commit comments

Comments
 (0)