/*
* Copyright (c) 2019-2022 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
* see accompanying file LICENSE for details.
*
* This software is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the License for more information.
*/
/**
* Dallas family thermometers access example (Arduino).
*
* Required configuration:
* - @c CONFIG_SEARCH_ENABLED for non single sensor setup,
* - @c CONFIG_PWR_CTRL_ENABLED if @c PWR_CTRL_PIN is configured.
*/
#include "OneWireNg_CurrentPlatform.h"
#include "drivers/DSTherm.h"
#include "utils/Placeholder.h"
#define OW_PIN 13
static Placeholder<OneWireNg_CurrentPlatform> ow;
/* returns false if not supported */
static bool printId(const OneWireNg::Id& id)
{
const char *name = DSTherm::getFamilyName(id);
Serial.print(id[0], HEX);
for (size_t i = 1; i < sizeof(OneWireNg::Id); i++) {
Serial.print(':');
Serial.print(id[i], HEX);
}
if (name) {
Serial.print(" -> ");
Serial.print(name);
}
Serial.println();
return (name != NULL);
}
static void printScratchpad(const DSTherm::Scratchpad& scrpd)
{
const uint8_t *scrpd_raw = scrpd.getRaw();
Serial.print(" Scratchpad:");
for (size_t i = 0; i < DSTherm::Scratchpad::LENGTH; i++) {
Serial.print(!i ? ' ' : ':');
Serial.print(scrpd_raw[i], HEX);
}
Serial.print("; Th:");
Serial.print(scrpd.getTh());
Serial.print("; Tl:");
Serial.print(scrpd.getTl());
Serial.print("; Resolution:");
Serial.print(9 + (int)(scrpd.getResolution() - DSTherm::RES_9_BIT));
long temp = scrpd.getTemp();
Serial.print("; Temp:");
if (temp < 0) {
temp = -temp;
Serial.print('-');
}
Serial.print(temp / 1000);
Serial.print('.');
Serial.print(temp % 1000);
Serial.print(" C");
Serial.println();
}
void setup()
{
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, false);
DSTherm drv(ow);
Serial.begin(115200);
drv.writeScratchpadAll(0, 0, DSTherm::RES_12_BIT);
drv.copyScratchpadAll(false);
//DSTherm drv(ow);
}
void loop()
{
DSTherm drv(ow);
/* convert temperature on all sensors connected... */
drv.convertTempAll(DSTherm::MAX_CONV_TIME, false);
static Placeholder<DSTherm::Scratchpad> scrpd;
OneWireNg::ErrorCode ec = drv.readScratchpadSingle(scrpd);
if (ec == OneWireNg::EC_SUCCESS) {
printId(scrpd->getId());
printScratchpad(scrpd);
} else if (ec == OneWireNg::EC_CRC_ERROR)
Serial.println(" CRC error.");
Serial.println("----------");
delay(1000);
}