Skip to content

Commit

Permalink
Refactoring temp (#84)
Browse files Browse the repository at this point in the history
* Refactoring temp

* Fix refactoring temperature
  • Loading branch information
FredM67 authored Mar 2, 2024
1 parent af1fe84 commit 866f94a
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 71 deletions.
13 changes: 6 additions & 7 deletions Mk2_3phase_RFdatalog_temp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,19 @@ inline constexpr uint8_t rotationPin{ 0xff }; /**< if LOW, trigger a load prio
inline constexpr uint8_t forcePin{ 0xff }; /**< for 3-phase PCB, force pin */
inline constexpr uint8_t watchDogPin{ 0xff }; /**< watch dog LED */

inline constexpr uint8_t tempSensorPin{ 0xff }; /**< for 3-phase PCB, sensor pin */

inline constexpr relayOutput relay_Output{ relayPin, 1000, 200, 1, 1 }; /**< config for relay diversion, see class definition for defaults and advanced options */

inline constexpr uint8_t ul_OFF_PEAK_DURATION{ 8 }; /**< Duration of the off-peak period in hours */
inline constexpr pairForceLoad rg_ForceLoad[NO_OF_DUMPLOADS]{ { -3, 2 } }; /**< force config for load #1 ONLY for dual tariff */

inline constexpr int16_t iTemperatureThreshold{ 100 }; /**< the temperature threshold to stop overriding in °C */

inline constexpr DeviceAddress sensorAddrs[]{ { 0x28, 0xBE, 0x41, 0x6B, 0x09, 0x00, 0x00, 0xA4 },
{ 0x28, 0xED, 0x5B, 0x6A, 0x09, 0x00, 0x00, 0x9D },
{ 0x28, 0xDB, 0x6D, 0x6A, 0x09, 0x00, 0x00, 0xDA },
{ 0x28, 0x59, 0x1F, 0x6A, 0x09, 0x00, 0x00, 0xB0 },
{ 0x28, 0x1B, 0xD7, 0x6A, 0x09, 0x00, 0x00, 0xB7 } }; /**< list of temperature sensor Addresses */
inline constexpr TemperatureSensing temperatureSensing{ 0xff,
{ { 0x28, 0xBE, 0x41, 0x6B, 0x09, 0x00, 0x00, 0xA4 },
{ 0x28, 0xED, 0x5B, 0x6A, 0x09, 0x00, 0x00, 0x9D },
{ 0x28, 0xDB, 0x6D, 0x6A, 0x09, 0x00, 0x00, 0xDA },
{ 0x28, 0x59, 0x1F, 0x6A, 0x09, 0x00, 0x00, 0xB0 },
{ 0x28, 0x1B, 0xD7, 0x6A, 0x09, 0x00, 0x00, 0xB7 } } }; /**< list of temperature sensor Addresses */

inline constexpr uint32_t ROTATION_AFTER_CYCLES{ 8UL * 3600UL * SUPPLY_FREQUENCY }; /**< rotates load priorities after this period of inactivity */

Expand Down
6 changes: 3 additions & 3 deletions Mk2_3phase_RFdatalog_temp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ void setup()

if constexpr (TEMP_SENSOR_PRESENT)
{
initTemperatureSensors();
temperatureSensing.initTemperatureSensors();
}

DBUG(F(">>free RAM = "));
Expand Down Expand Up @@ -415,7 +415,7 @@ void loop()
for (uint8_t idx = 0; idx < size(tx_data.temperature_x100); ++idx)
{
static int16_t tmp;
tmp = readTemperature(sensorAddrs[idx]);
tmp = temperatureSensing.readTemperature(idx);

// if read temperature is 85 and the delta with previous is greater than 5, skip the value
if (8500 == tmp && (abs(tmp - tx_data.temperature_x100[idx]) > 500))
Expand All @@ -425,7 +425,7 @@ void loop()

tx_data.temperature_x100[idx] = tmp;
}
requestTemperatures(); // for use next time around
temperatureSensing.requestTemperatures(); // for use next time around
}

sendResults(bOffPeak);
Expand Down
2 changes: 1 addition & 1 deletion Mk2_3phase_RFdatalog_temp/processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ inline volatile uint16_t copyOf_sampleSetsDuringThisDatalogPeriod; /**< copy of
inline volatile uint16_t copyOf_countLoadON[NO_OF_DUMPLOADS]; /**< copy of number of cycle the load was ON (over 1 datalog period) */

#ifdef TEMP_ENABLED
inline PayloadTx_struct< NO_OF_PHASES, size(sensorAddrs) > tx_data; /**< logging data */
inline PayloadTx_struct< NO_OF_PHASES, temperatureSensing.get_size() > tx_data; /**< logging data */
#else
inline PayloadTx_struct< NO_OF_PHASES > tx_data; /**< logging data */
#endif
Expand Down
148 changes: 91 additions & 57 deletions Mk2_3phase_RFdatalog_temp/utils_temp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,117 @@

#include "constants.h"

using ScratchPad = uint8_t[9];
using DeviceAddress = uint8_t[8];

inline int16_t readTemperature(const DeviceAddress &deviceAddress);
inline void requestTemperatures();
inline void initTemperatureSensors();

#ifdef TEMP_ENABLED
#include <OneWire.h> // for temperature sensing
inline constexpr bool TEMP_SENSOR_PRESENT{ true }; /**< set it to 'true' if temperature sensing is needed */
#include <OneWire.h> // for temperature sensing
#else
inline constexpr bool TEMP_SENSOR_PRESENT{ false }; /**< set it to 'true' if temperature sensing is needed */
#endif

inline constexpr bool TEMP_SENSOR_PRESENT{ true };
template< uint8_t N >
class TemperatureSensing
{
using ScratchPad = uint8_t[9];

inline OneWire oneWire(tempSensorPin); /**< For temperature sensing */
struct DeviceAddress
{
uint8_t addr[8];
};

/**
* @brief Read temperature of a specific device
*
* @param deviceAddress The address of the device
* @return int16_t Temperature * 100
*/
int16_t readTemperature(const DeviceAddress &deviceAddress)
{
static ScratchPad buf;
public:
constexpr TemperatureSensing(uint8_t pin, const DeviceAddress (&ref)[N])
: sensorPin{ pin }, sensorAddrs(ref)
{
}

if (!oneWire.reset())
/**
* @brief Request temperature for all sensors
*
*/
void requestTemperatures()
{
return DEVICE_DISCONNECTED_RAW;
#ifdef TEMP_ENABLED
oneWire.reset();
oneWire.skip();
oneWire.write(CONVERT_TEMPERATURE);
#endif
}
oneWire.select(deviceAddress);
oneWire.write(READ_SCRATCHPAD);

for (auto &buf_elem : buf)
/**
* @brief Initialize the Dallas sensors
*
*/
void initTemperatureSensors()
{
buf_elem = oneWire.read();
#ifdef TEMP_ENABLED
oneWire.begin(sensorPin);
requestTemperatures();
#endif
}

if (!oneWire.reset())
constexpr auto get_size() const
{
return DEVICE_DISCONNECTED_RAW;
return N;
}
if (oneWire.crc8(buf, 8) != buf[8])

constexpr auto get_pin() const
{
return DEVICE_DISCONNECTED_RAW;
return sensorPin;
}

// result is temperature x16, multiply by 6.25 to convert to temperature x100
int16_t result = (buf[1] << 8) | buf[0];
result = (result * 6) + (result >> 2);
if (result <= TEMP_RANGE_LOW || result >= TEMP_RANGE_HIGH)
/**
* @brief Read temperature of a specific device
*
* @param deviceAddress The address of the device
* @return int16_t Temperature * 100
*/
int16_t readTemperature(const uint8_t idx)
{
return OUTOFRANGE_TEMPERATURE; // return value ('Out of range')
static ScratchPad buf;

#ifdef TEMP_ENABLED
if (!oneWire.reset())
{
return DEVICE_DISCONNECTED_RAW;
}
oneWire.select(sensorAddrs[idx].addr);
oneWire.write(READ_SCRATCHPAD);

for (auto &buf_elem : buf)
{
buf_elem = oneWire.read();
}

if (!oneWire.reset())
{
return DEVICE_DISCONNECTED_RAW;
}
if (oneWire.crc8(buf, 8) != buf[8])
{
return DEVICE_DISCONNECTED_RAW;
}

#endif

// result is temperature x16, multiply by 6.25 to convert to temperature x100
int16_t result = (buf[1] << 8) | buf[0];
result = (result * 6) + (result >> 2);
if (result <= TEMP_RANGE_LOW || result >= TEMP_RANGE_HIGH)
{
return OUTOFRANGE_TEMPERATURE; // return value ('Out of range')
}

return result;
}

return result;
}
private:
const uint8_t sensorPin;

/**
* @brief Request temperature for all sensors
*
*/
void requestTemperatures()
{
oneWire.reset();
oneWire.skip();
oneWire.write(CONVERT_TEMPERATURE);
}
const DeviceAddress sensorAddrs[N];

/**
* @brief Initialize the Dallas sensors
*
*/
void initTemperatureSensors()
{
requestTemperatures();
}
#else
inline constexpr bool TEMP_SENSOR_PRESENT{ false };
#endif // TEMP_SENSOR_PRESENT
#ifdef TEMP_ENABLED
static inline OneWire oneWire; /**< For temperature sensing */
#endif
};

#endif // _UTILS_TEMP_H
9 changes: 6 additions & 3 deletions Mk2_3phase_RFdatalog_temp/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

static_assert(DATALOG_PERIOD_IN_SECONDS <= 40, "**** Data log duration is too long and will lead to overflow ! ****");

static_assert(TEMP_SENSOR_PRESENT ^ (tempSensorPin == 0xff), "******** Wrong pin value for temperature sensor(s). Please check your config.h ! ********");
static_assert(TEMP_SENSOR_PRESENT ^ (temperatureSensing.get_pin() == 0xff), "******** Wrong pin value for temperature sensor(s). Please check your config.h ! ********");
static_assert(DIVERSION_PIN_PRESENT ^ (diversionPin == 0xff), "******** Wrong pin value for diversion command. Please check your config.h ! ********");
static_assert((PRIORITY_ROTATION == RotationModes::PIN) ^ (rotationPin == 0xff), "******** Wrong pin value for rotation command. Please check your config.h ! ********");
static_assert(OVERRIDE_PIN_PRESENT ^ (forcePin == 0xff), "******** Wrong pin value for override command. Please check your config.h ! ********");
Expand All @@ -49,8 +49,11 @@ check_pins()
{
uint16_t used_pins{ 0 };

if (tempSensorPin != 0xff)
bit_set(used_pins, tempSensorPin);
if constexpr (TEMP_SENSOR_PRESENT)
{
if (temperatureSensing.get_pin() != 0xff)
bit_set(used_pins, temperatureSensing.get_pin());
}

if (diversionPin != 0xff)
{
Expand Down

0 comments on commit 866f94a

Please sign in to comment.