Skip to content

Commit

Permalink
fix: Resolved merge conflicts - take one million
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles Burton committed Jan 4, 2025
1 parent 2576a3d commit 21c469e
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 183 deletions.
132 changes: 63 additions & 69 deletions DallasTemperature.cpp
Original file line number Diff line number Diff line change
@@ -1,69 +1,63 @@
#pragma once

#include <stdint.h>
#include <Arduino.h>
#ifdef __STM32F1__
#include <OneWireSTM.h>
#else
#include <OneWire.h>
#endif

namespace Dallas {

class DallasTemperature {
public:
static constexpr const char* LIB_VERSION = "4.0.0";

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

struct Request {
bool result;
unsigned long timestamp;
operator bool() const { return result; }
};

// Constructors
DallasTemperature();
explicit DallasTemperature(OneWire* oneWire);
DallasTemperature(OneWire* oneWire, uint8_t pullupPin);

// Configuration
void begin();
void setOneWire(OneWire* oneWire);
void setPullupPin(uint8_t pullupPin);

// Device Management
uint8_t getDeviceCount() const;
uint8_t getDS18Count() const;
bool isConnected(const DeviceAddress& address) const;
bool isConnected(const DeviceAddress& address, ScratchPad& scratchPad) const;

// Temperature Reading
Request requestTemperatures();
Request requestTemperaturesByAddress(const DeviceAddress& address);
Request requestTemperaturesByIndex(uint8_t index);
float getTempC(const DeviceAddress& address, uint8_t retryCount = 0) const;
float getTempF(const DeviceAddress& address, uint8_t retryCount = 0) const;

// Utility Methods
static float toFahrenheit(float celsius);
static float toCelsius(float fahrenheit);
static float rawToCelsius(int32_t raw);
static float rawToFahrenheit(int32_t raw);

private:
bool parasitePowerMode = false;
uint8_t bitResolution = 9;
OneWire* oneWire = nullptr;
uint8_t pullupPin = 0;

bool readScratchPad(const DeviceAddress& address, ScratchPad& scratchPad) const;
bool readPowerSupply(const DeviceAddress& address) const;
void activateExternalPullup() const;
void deactivateExternalPullup() const;

int32_t calculateTemperature(const DeviceAddress& address, const ScratchPad& scratchPad) const;
};

} // namespace Dallas
#include "DallasTemperature.h"

// Constructors
DallasTemperature::DallasTemperature(OneWire* oneWire) : oneWire(oneWire) {}

// Initialization
void DallasTemperature::begin() {
if (!oneWire) return;
oneWire->reset_search();
parasitePowerMode = false;
deviceCount = 0;

DeviceAddress address;
while (oneWire->search(address)) {
deviceCount++;
if (readPowerSupply(address)) {
parasitePowerMode = true;
}
}
oneWire->reset_search();
}

// Get the total number of devices
uint8_t DallasTemperature::getDeviceCount() const {
return deviceCount;
}

// Get the address of a device by index
bool DallasTemperature::getAddress(DeviceAddress deviceAddress, uint8_t index) const {
if (!oneWire) return false;

uint8_t count = 0;
oneWire->reset_search();
while (count <= index && oneWire->search(deviceAddress)) {
if (count == index) return true;
count++;
}
return false;
}

// Check if the bus is using parasite power
bool DallasTemperature::isParasitePowerMode() const {
return parasitePowerMode;
}

// Read the power supply mode of a specific device or all devices
bool DallasTemperature::readPowerSupply(const DeviceAddress deviceAddress) const {
if (!oneWire) return false;

oneWire->reset();
if (deviceAddress) {
oneWire->select(deviceAddress);
} else {
oneWire->skip(); // Skip ROM command for all devices
}
oneWire->write(0xB4); // READ POWER SUPPLY command
return (oneWire->read_bit() == 0);
}

// Reset search to start looking for devices again
void DallasTemperature::resetSearch() const {
if (oneWire) oneWire->reset_search();
}
65 changes: 18 additions & 47 deletions DallasTemperature.h
Original file line number Diff line number Diff line change
@@ -1,70 +1,41 @@
#pragma once

#include <array>
#include <cstdint>
#include <Arduino.h>
#ifdef __STM32F1__
#include <OneWireSTM.h>

// Platform-specific OneWire include
#if defined(STM32)
#include <OneWireSTM.h> // STM32-specific OneWire implementation
#else
#include <OneWire.h>
#include <OneWire.h> // Default OneWire implementation
#endif

namespace Dallas {
// Define DeviceAddress globally for compatibility
typedef uint8_t DeviceAddress[8];

class DallasTemperature {
public:
static constexpr const char* LIB_VERSION = "4.0.0";

using DeviceAddress = std::array<uint8_t, 8>;
using ScratchPad = std::array<uint8_t, 9>;

struct Request {
bool result;
unsigned long timestamp;
operator bool() const { return result; }
};
// Library version
static constexpr const char* LIB_VERSION = "4.0.1";

// Constructors
DallasTemperature();
explicit DallasTemperature(OneWire* oneWire);
DallasTemperature(OneWire* oneWire, uint8_t pullupPin);

// Configuration
// Initialization
void begin();
void setOneWire(OneWire* oneWire);
void setPullupPin(uint8_t pullupPin);

// Device Management
uint8_t getDeviceCount() const;
uint8_t getDS18Count() const;
bool isConnected(const DeviceAddress& address) const;
bool isConnected(const DeviceAddress& address, ScratchPad& scratchPad) const;

// Temperature Reading
Request requestTemperatures();
Request requestTemperaturesByAddress(const DeviceAddress& address);
Request requestTemperaturesByIndex(uint8_t index);
float getTempC(const DeviceAddress& address, uint8_t retryCount = 0) const;
float getTempF(const DeviceAddress& address, uint8_t retryCount = 0) const;
bool getAddress(DeviceAddress deviceAddress, uint8_t index) const;
bool isParasitePowerMode() const;

// Utility Methods
static float toFahrenheit(float celsius);
static float toCelsius(float fahrenheit);
static float rawToCelsius(int32_t raw);
static float rawToFahrenheit(int32_t raw);
// Scratchpad Operations
bool readPowerSupply(const DeviceAddress deviceAddress = nullptr) const;

private:
bool parasitePowerMode = false;
uint8_t bitResolution = 9;
OneWire* oneWire = nullptr;
uint8_t pullupPin = 0;

bool readScratchPad(const DeviceAddress& address, ScratchPad& scratchPad) const;
bool readPowerSupply(const DeviceAddress& address) const;
void activateExternalPullup() const;
void deactivateExternalPullup() const;
bool parasitePowerMode = false;
uint8_t deviceCount = 0;

int32_t calculateTemperature(const DeviceAddress& address, const ScratchPad& scratchPad) const;
// Helper Methods
void resetSearch() const;
};

} // namespace Dallas
113 changes: 46 additions & 67 deletions examples/readPowerSupply/readPowerSupply.ino
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// PURPOSE: demo
// DATE: 2025-01-04
//
// Released to the public domain
//

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

Expand All @@ -17,68 +8,56 @@
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
Dallas::DallasTemperature sensors(&oneWire);
DallasTemperature sensors(&oneWire);

// Arrays to hold device addresses
Dallas::DeviceAddress insideThermometer, outsideThermometer;

// Assign address manually. The addresses below will need to be changed
// to valid device addresses on your bus. Device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
// Example:
// Dallas::DeviceAddress insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
// Dallas::DeviceAddress outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };

int devCount = 0;

/*
* The setup function. We only start the sensors here
*/
void setup(void) {
Serial.begin(115200);
Serial.println("Arduino Temperature Control Library Demo - readPowerSupply");

sensors.begin();

devCount = sensors.getDeviceCount();
Serial.print("#devices: ");
Serial.println(devCount);

// Report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON"); // Check parasite mode globally
else Serial.println("OFF");

// Search for devices on the bus and assign based on an index
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");

// Show the addresses we found on the bus
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
Serial.print("Power = parasite: ");
Serial.println(sensors.readPowerSupply(insideThermometer));
Serial.println();

Serial.print("Device 1 Address: ");
printAddress(outsideThermometer);
Serial.println();
Serial.print("Power = parasite: ");
Serial.println(sensors.readPowerSupply(outsideThermometer));
Serial.println();
DeviceAddress insideThermometer, outsideThermometer;

void setup() {
Serial.begin(115200);
Serial.println("Arduino Temperature Control Library Demo - readPowerSupply");

sensors.begin();

// Count devices
int deviceCount = sensors.getDeviceCount();
Serial.print("Device count: ");
Serial.println(deviceCount);

// Check parasite power
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");

// Get device addresses
if (!sensors.getAddress(insideThermometer, 0)) {
Serial.println("Unable to find address for Device 0");
}
if (!sensors.getAddress(outsideThermometer, 1)) {
Serial.println("Unable to find address for Device 1");
}

// Print addresses
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
Serial.print("Power = parasite: ");
Serial.println(sensors.readPowerSupply(insideThermometer));

Serial.print("Device 1 Address: ");
printAddress(outsideThermometer);
Serial.println();
Serial.print("Power = parasite: ");
Serial.println(sensors.readPowerSupply(outsideThermometer));
}

// Function to print a device address
void printAddress(Dallas::DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
// Zero pad the address if necessary
if (deviceAddress[i] < 0x10) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
void loop() {
// Empty
}

// Empty on purpose
void loop(void) {
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 0x10) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}

0 comments on commit 21c469e

Please sign in to comment.