ds18b20: rewrite C++ to wrap C, add FTI, update examples

Previously, the C++ and C versions of this driver were separate.  Now
the C++ implementation wraps the C implementation.

In addition, the C++ init() function has been deprecated.  It
currently does nothing, and examples have been modified to remove it's
calls.  This function will be removed in a separate release.

The examples have been further modified to update all detected devices
and print their respective temperatures, instead of only reporting the
on the first device detected.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson 2017-01-18 13:09:51 -07:00
parent 6769d976a0
commit d3b864362d
15 changed files with 685 additions and 757 deletions

View File

@ -4,6 +4,9 @@ API Changes {#apichanges}
Here's a list of other API changes made to the library that break source/binary
compatibility between releases:
* **ds18b20** The C++ interface init() function has been deprecated.
It is still present, but currently does nothing. It will be removed
in a future release.
* **grove<name>** Starting with UPM 1.0 the Grove libraries have been renamed
from *upm-grove<name>* to simply *upm-<name>*. Class names also match this new
format, with old classes marked as deprecated throughout the documentation.

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
* Copyright (c) 2016-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -49,9 +49,6 @@ int main(int argc, char **argv)
// Instantiate an DS18B20 instance using the default values (uart 0)
upm::DS18B20 sensor;
// locate and setup our devices
sensor.init();
cout << "Found " << sensor.devicesFound() << " device(s)" << endl;
cout << endl;
@ -59,18 +56,26 @@ int main(int argc, char **argv)
if (!sensor.devicesFound())
return 1;
// update and print available values every second
// update and print available values every 2 seconds
while (shouldRun)
{
// update our values for the first sensor
sensor.update(0);
// update our values for all of the detected sensors
sensor.update(-1);
// we show both C and F for temperature for the first sensor
cout << "Temperature: " << sensor.getTemperature(0)
<< " C / " << sensor.getTemperature(0, true) << " F"
// we show both C and F for temperature for the sensors
int i;
for (i=0; i<sensor.devicesFound(); i++)
{
cout << "Device "
<< i
<< ": Temperature: "
<< sensor.getTemperature(i)
<< " C / " << sensor.getTemperature(i, true) << " F"
<< endl;
}
cout << endl;
sleep(1);
sleep(2);
}
cout << "Exiting..." << endl;

View File

@ -56,7 +56,7 @@ int main(int argc, char **argv)
printf("Found %d device(s)\n\n", ds18b20_devices_found(sensor));
// update and print available values every second
// update and print available values every 2 seconds
while (shouldRun)
{
// update our values for all sensors

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
* Copyright (c) 2016-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -33,9 +33,6 @@ console.log("Initializing...");
// Instantiate an DS18B20 instance using the default values (uart 0)
var sensor = new sensorObj.DS18B20(0);
// locate and setup our devices
sensor.init();
console.log("Found", sensor.devicesFound(), "device(s)");
console.log("");
@ -44,17 +41,21 @@ if (!sensor.devicesFound())
process.exit(1);
}
// update and print available values every second
// update and print available values every 2 seconds
setInterval(function()
{
// update our values for the first sensor
sensor.update(0);
// update our values for all of the detected sensors
sensor.update(-1);
// we show both C and F for temperature for the first sensor
console.log("Temperature:", sensor.getTemperature(0),
"C /", sensor.getTemperature(0, true), "F");
// we show both C and F for temperature for the sensors
for (var i=0; i<sensor.devicesFound(); i++)
{
console.log("Device:", i, "Temperature:", sensor.getTemperature(i),
"C /", sensor.getTemperature(i, true), "F");
}
}, 1000);
console.log();
}, 2000);
process.on('SIGINT', function()

View File

@ -1,6 +1,6 @@
#!/usr/bin/python
# Author: Jon Trulson <jtrulson@ics.com>
# Copyright (c) 2016 Intel Corporation.
# Copyright (c) 2016-2017 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@ -45,25 +45,26 @@ def main():
# Instantiate an DS18B20 instance using the default values (uart 0)
sensor = sensorObj.DS18B20(0)
# locate and setup our devices
sensor.init()
print("Found", sensor.devicesFound(), "device(s)")
print()
if (not sensor.devicesFound()):
sys.exit(1);
# update and print available values every second
# update and print available values every 2 seconds
while (1):
# update our values for the first sensor
sensor.update(0)
# update our values for all of the detected sensors
sensor.update(-1)
# we show both C and F for temperature for the first sensor
print("Temperature:", sensor.getTemperature(0), "C /", end=' ')
print(sensor.getTemperature(0, True), "F")
# we show both C and F for temperature for the sensors
for i in range(sensor.devicesFound()):
print("Device:", i, end=' ')
print("Temperature:", sensor.getTemperature(i), "C /", end=' ')
print(sensor.getTemperature(i, True), "F")
time.sleep(1)
print()
time.sleep(2)
if __name__ == '__main__':
main()

View File

@ -1,9 +1,9 @@
upm_mixed_module_init (NAME ds18b20
DESCRIPTION "Programmable resolution 1-wire digital thermometer"
C_HDR ds18b20.h
DESCRIPTION "Programmable resolution DS 1-wire digital thermometer"
C_HDR ds18b20.h ds18b20_defs.h
C_SRC ds18b20.c
CPP_HDR ds18b20.hpp
CPP_SRC ds18b20.cxx
# FTI_SRC ds18b20_fti.c
# CPP_WRAPS_C
FTI_SRC ds18b20_fti.c
CPP_WRAPS_C
REQUIRES upmc-utilities mraa)

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
* Copyright (c) 2016-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -23,22 +23,13 @@
*/
#include <assert.h>
#include <upm_utilities.h>
#include "ds18b20.h"
// I'd rather use MRAA_UART_OW_ROMCODE_SIZE defined in uart_ow.h, but
// this then can't be used to specify array sizes since it's a static
// const int, rather than a define. This should be fixed in MRAA (PR
// submitted 9/2016). Until then, work around it.
#if !defined(MRAA_UART_OW_ROMCODE)
# define ROMCODE_SIZE 8
#else
# define ROMCODE_SIZE MRAA_UART_OW_ROMCODE
#endif
// an internal struct we use to store information on the devices
// found during initialization
struct _ds18b20_info_t {
uint8_t id[ROMCODE_SIZE]; // 8-byte romcode id
uint8_t id[MRAA_UART_OW_ROMCODE_SIZE]; // 8-byte romcode id
float temperature;
DS18B20_RESOLUTIONS_T resolution;
};
@ -87,7 +78,7 @@ ds18b20_context ds18b20_init(unsigned int uart)
return NULL;
}
uint8_t id[ROMCODE_SIZE];
uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
rv = mraa_uart_ow_rom_search(dev->ow, 1, id);
if (rv == MRAA_ERROR_UART_OW_NO_DEVICES)
@ -131,7 +122,7 @@ ds18b20_context ds18b20_init(unsigned int uart)
dev->devices = dsPtr;
// copy in the romcode
memcpy(dev->devices[dev->numDevices].id, id,
ROMCODE_SIZE);
MRAA_UART_OW_ROMCODE_SIZE);
// set defaults for now
dev->devices[dev->numDevices].temperature = 0.0;
dev->devices[dev->numDevices].resolution =
@ -224,7 +215,7 @@ void ds18b20_update(const ds18b20_context dev, int index)
mraa_uart_ow_command(dev->ow, DS18B20_CMD_CONVERT, dev->devices[index].id);
// wait for conversion(s) to finish
usleep(750000); // 750ms max
upm_delay_ms(750000); // 750ms max
if (doAll)
{
@ -263,8 +254,9 @@ static float readSingleTemp(const ds18b20_context dev, unsigned int index)
if (crc != scratch[8])
{
printf("%s: crc check failed for device %d, returning previously "
"measured temperature\n", __FUNCTION__, index);
printf("%s: crc check failed for device %d. Got %02x, expected %02x."
" Returning previously measured temperature\n",
__FUNCTION__, index, scratch[8], crc);
return dev->devices[index].temperature;
}
@ -288,6 +280,10 @@ static float readSingleTemp(const ds18b20_context dev, unsigned int index)
case DS18B20_RESOLUTION_11BITS: frac &= 0x0e; break;
// use all bits for 12b
case DS18B20_RESOLUTION_12BITS: break;
default:
printf("%s: Internal error, invalid resolution %d\n",
__FUNCTION__, (int)dev->devices[index].resolution);
break;
}
// remove the fractional with extreme prejudice
@ -347,7 +343,7 @@ void ds18b20_set_resolution(const ds18b20_context dev, unsigned int index,
mraa_uart_ow_write_byte(dev->ow, scratch[i+2]);
}
void ds18b20_copy_scratch_pad(const ds18b20_context dev, unsigned int index)
void ds18b20_copy_scratchpad(const ds18b20_context dev, unsigned int index)
{
assert(dev != NULL);
@ -361,10 +357,10 @@ void ds18b20_copy_scratch_pad(const ds18b20_context dev, unsigned int index)
mraa_uart_ow_command(dev->ow, DS18B20_CMD_COPY_SCRATCHPAD,
dev->devices[index].id);
sleep(1); // to be safe...
upm_delay(1); // to be safe...
}
void ds18b20_recallEEPROM(const ds18b20_context dev, unsigned int index)
void ds18b20_recall_eeprom(const ds18b20_context dev, unsigned int index)
{
assert(dev != NULL);
@ -380,10 +376,10 @@ void ds18b20_recallEEPROM(const ds18b20_context dev, unsigned int index)
// issue read timeslots until a '1' is read back, indicating completion
while (!mraa_uart_ow_bit(dev->ow, 1))
usleep(100);
upm_delay_us(100);
}
int ds18b20_devices_found(const ds18b20_context dev)
unsigned int ds18b20_devices_found(const ds18b20_context dev)
{
assert(dev != NULL);
@ -397,8 +393,8 @@ const uint8_t *ds18b20_get_id(const ds18b20_context dev, unsigned int index)
if (index >= dev->numDevices)
return NULL;
static uint8_t id[ROMCODE_SIZE];
static uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
memcpy(id, dev->devices[index].id, ROMCODE_SIZE);
memcpy(id, dev->devices[index].id, MRAA_UART_OW_ROMCODE_SIZE);
return id;
}

View File

@ -23,9 +23,7 @@
*/
#include <iostream>
#include <time.h>
#include <stdexcept>
#include <syslog.h>
#include "ds18b20.hpp"
@ -39,264 +37,84 @@ static float c2f(float c)
}
DS18B20::DS18B20(int uart) :
m_uart(uart)
m_ds18b20(ds18b20_init(uart))
{
m_devicesFound = 0;
// check basic access to the 1-wire bus (presence detect)
mraa::Result rv;
if ((rv = m_uart.reset()) != mraa::SUCCESS)
{
throw std::runtime_error(std::string(__FUNCTION__) +
": reset() failed, no devices on bus?");
}
if (!m_ds18b20)
throw std::runtime_error(string(__FUNCTION__)
+ ": ds18b20_init() failed");
}
DS18B20::~DS18B20()
{
ds18b20_close(m_ds18b20);
}
void DS18B20::init()
{
// iterate through the bus and build up a list of detected DS18B20
// devices (only)
// empty the map, in case this method has already been run once
// before
m_devicesFound = 0;
m_deviceMap.clear();
sensor_info_t sinfo;
// defaults
sinfo.temperature = 0.0;
sinfo.resolution = RESOLUTION_12BITS;
// start the search from scratch
string id = m_uart.search(true);
if (id.empty())
{
throw std::runtime_error(std::string(__FUNCTION__) +
": no devices detected on bus");
}
while (!id.empty())
{
// The first byte (id[0]]) is the device type (family) code. We
// are only interested in the family code for these devices.
if ((uint8_t)id[0] == DS18B20_FAMILY_CODE)
{
// we have a winner, add it to our map and continue searching
sinfo.id = id;
m_deviceMap[m_devicesFound] = sinfo;
m_devicesFound++;
}
// continue search
id = m_uart.search(false);
}
if (!m_devicesFound)
{
throw std::runtime_error(std::string(__FUNCTION__) +
": no DS18B20 devices found on bus");
}
// iterate through the found devices and query their resolutions
for (int i=0; i<m_devicesFound; i++)
{
// read only the first 5 bytes of the scratchpad
static const int numScratch = 5;
uint8_t scratch[numScratch];
m_uart.command(CMD_READ_SCRATCHPAD, m_deviceMap[i].id);
for (int j=0; j<numScratch; j++)
scratch[j] = m_uart.readByte();
// config byte, shift the resolution to bit 0
scratch[4] >>= _CFG_RESOLUTION_SHIFT;
switch (scratch[4] & _CFG_RESOLUTION_MASK)
{
case 0: m_deviceMap[i].resolution = RESOLUTION_9BITS; break;
case 1: m_deviceMap[i].resolution = RESOLUTION_10BITS; break;
case 2: m_deviceMap[i].resolution = RESOLUTION_11BITS; break;
case 3: m_deviceMap[i].resolution = RESOLUTION_12BITS; break;
}
// reset the bus
m_uart.reset();
}
// deprecated function. Handled in ctor now.
}
void DS18B20::update(int index)
{
if (index >= m_devicesFound)
{
throw std::out_of_range(std::string(__FUNCTION__) +
": device index out of range");
}
if (index >= (int)ds18b20_devices_found(m_ds18b20))
throw std::out_of_range(string(__FUNCTION__)
+ ": Invalid index");
// should we update all of them?
bool doAll = (index < 0) ? true : false;
if (doAll)
{
// if we want to update all of them, we will first send the
// convert command to all of them, then wait. This will be
// faster, timey-wimey wise, then converting, sleeping, and
// reading each individual sensor.
for (int i=0; i<m_devicesFound; i++)
m_uart.command(CMD_CONVERT, m_deviceMap[i].id);
}
else
m_uart.command(CMD_CONVERT, m_deviceMap[index].id);
// wait for conversion(s) to finish
usleep(750000); // 750ms max
if (doAll)
{
for (int i=0; i<m_devicesFound; i++)
m_deviceMap[i].temperature = readSingleTemp(i);
}
else
m_deviceMap[index].temperature = readSingleTemp(index);
ds18b20_update(m_ds18b20, index);
}
// utility function to read temp data from a single sensor
float DS18B20::readSingleTemp(int index)
float DS18B20::getTemperature(unsigned int index, bool fahrenheit)
{
if (index < 0 || index >= m_devicesFound)
{
throw std::out_of_range(std::string(__FUNCTION__) +
": device index out of range");
}
static const int numScratch = 9;
uint8_t scratch[numScratch];
// read the 9-byte scratchpad
m_uart.command(CMD_READ_SCRATCHPAD, m_deviceMap[index].id);
for (int i=0; i<numScratch; i++)
scratch[i] = m_uart.readByte();
// validate cksum -- if we get an error, we will warn and simply
// return the current (previously read) temperature
uint8_t crc = m_uart.crc8(scratch, 8);
if (crc != scratch[8])
{
cerr << __FUNCTION__ << ": crc check failed for device "
<< index << ", returning previously measured temperature" << endl;
return m_deviceMap[index].temperature;
}
// check the sign bit(s)
bool negative = (scratch[1] & 0x80) ? true : false;
// shift everything into position
int16_t temp = (scratch[1] << 8) | scratch[0];
// grab the fractional
uint8_t frac = temp & 0x0f;
// depending on the resolution, some frac bits should be ignored, so
// we mask them off. For 12bits, all bits are valid so we leve them
// alone.
switch (m_deviceMap[index].resolution)
{
case RESOLUTION_9BITS: frac &= 0x08; break;
case RESOLUTION_10BITS: frac &= 0x0c; break;
case RESOLUTION_11BITS: frac &= 0x0e; break;
default:
syslog(LOG_WARNING, "%s: switch case not defined",
std::string(__FUNCTION__).c_str());
}
// remove the fractional with extreme prejudice
temp >>= 4;
// compensate for sign
if (negative)
temp -= 65536; // 2^^16
// convert
return ( float(temp) + (float(frac) * 0.0625) );
}
float DS18B20::getTemperature(int index, bool fahrenheit)
{
if (index < 0 || index >= m_devicesFound)
{
throw std::out_of_range(std::string(__FUNCTION__) +
": device index out of range");
}
if (index >= ds18b20_devices_found(m_ds18b20))
throw std::out_of_range(string(__FUNCTION__)
+ ": Invalid index");
if (fahrenheit)
return c2f(m_deviceMap[index].temperature);
return c2f(ds18b20_get_temperature(m_ds18b20, index));
else
return m_deviceMap[index].temperature;
return ds18b20_get_temperature(m_ds18b20, index);
}
void DS18B20::setResolution(int index, RESOLUTIONS_T res)
void DS18B20::setResolution(unsigned int index, DS18B20_RESOLUTIONS_T res)
{
if (index < 0 || index >= m_devicesFound)
if (index >= ds18b20_devices_found(m_ds18b20))
{
throw std::out_of_range(std::string(__FUNCTION__) +
": device index out of range");
}
static const int numScratch = 9;
uint8_t scratch[numScratch];
// read the 9-byte scratchpad
m_uart.command(CMD_READ_SCRATCHPAD, m_deviceMap[index].id);
for (int i=0; i<numScratch; i++)
scratch[i] = m_uart.readByte();
// resolution is stored in byte 4
scratch[4] = ((scratch[4] & ~(_CFG_RESOLUTION_MASK << _CFG_RESOLUTION_SHIFT))
| (res << _CFG_RESOLUTION_SHIFT));
// now, write back, we only write 3 bytes (2-4), no cksum.
m_uart.command(CMD_WRITE_SCRATCHPAD, m_deviceMap[index].id);
for (int i=0; i<3; i++)
m_uart.writeByte(scratch[i+2]);
ds18b20_set_resolution(m_ds18b20, index, res);
}
void DS18B20::copyScratchPad(int index)
void DS18B20::copyScratchPad(unsigned int index)
{
if (index < 0 || index >= m_devicesFound)
if (index >= ds18b20_devices_found(m_ds18b20))
{
throw std::out_of_range(std::string(__FUNCTION__) +
": device index out of range");
}
// issue the command
m_uart.command(CMD_COPY_SCRATCHPAD, m_deviceMap[index].id);
sleep(1); // to be safe...
ds18b20_copy_scratchpad(m_ds18b20, index);
}
void DS18B20::recallEEPROM(int index)
void DS18B20::recallEEPROM(unsigned int index)
{
if (index < 0 || index >= m_devicesFound)
if (index >= ds18b20_devices_found(m_ds18b20))
{
throw std::out_of_range(std::string(__FUNCTION__) +
": device index out of range");
}
// issue the command
m_uart.command(CMD_RECALL_EEPROM, m_deviceMap[index].id);
// issue read timeslots until a '1' is read back, indicating completion
while (!m_uart.writeBit(1))
usleep(100);
ds18b20_recall_eeprom(m_ds18b20, index);
}
string DS18B20::getId(unsigned int index)
{
if (index >= ds18b20_devices_found(m_ds18b20))
{
throw std::out_of_range(std::string(__FUNCTION__) +
": device index out of range");
}
return string((char *)ds18b20_get_id(m_ds18b20, index),
MRAA_UART_OW_ROMCODE_SIZE);
}

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
* Copyright (c) 2016-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -28,17 +28,13 @@
#include <string.h>
#include <mraa/uart_ow.h>
#include "upm.h"
#include <upm.h>
#include "ds18b20_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
// The family code for these devices. We handle all of them that
// are found on the bus.
#define DS18B20_FAMILY_CODE 0x28
/**
* @file ds18b20.h
* @library ds18b20
@ -63,33 +59,6 @@ extern "C" {
ds18b20_info_t *devices;
} *ds18b20_context;
// commands
typedef enum {
DS18B20_CMD_CONVERT = 0x44, // start a temp conversion
DS18B20_CMD_WRITE_SCRATCHPAD = 0x4e,
DS18B20_CMD_READ_SCRATCHPAD = 0xbe,
DS18B20_CMD_COPY_SCRATCHPAD = 0x48, // copy scratchpad to EEPROM
DS18B20_CMD_RECALL_EEPROM = 0xb8, // copy EEPROM to scratchpad
DS18B20_CMD_READ_POWER_SUPPLY = 0xb4 // parasitically powered?
} DS18B20_CMD_T;
// config register (scratchpad[4])
typedef enum {
DS18B20_CFG_RESOLUTION_R0 = 0x20,
DS18B20_CFG_RESOLUTION_R1 = 0x40,
_DS18B20_CFG_RESOLUTION_MASK = 3,
_DS18B20_CFG_RESOLUTION_SHIFT = 5
// all other bits reserved and non-writable
} DS18B20_CFG_BITS_T;
typedef enum {
DS18B20_RESOLUTION_9BITS = 0, // 93.75ms (tconv/8)
DS18B20_RESOLUTION_10BITS = 1, // 187.5 (tconv/4)
DS18B20_RESOLUTION_11BITS = 2, // 375ms (tconv/2)
DS18B20_RESOLUTION_12BITS = 3 // 750ms (tconv)
} DS18B20_RESOLUTIONS_T;
/**
* This function will initilaize and search the 1-wire bus and store
* information on each DS18B20 device detected on the bus. If no
@ -160,7 +129,7 @@ extern "C" {
*
* @return number of DS18B20's that were found on the bus
*/
int ds18b20_devices_found(const ds18b20_context dev);
unsigned int ds18b20_devices_found(const ds18b20_context dev);
/**
* Return an 8 byte string representing the unique device ID (1-wire
@ -171,7 +140,8 @@ extern "C" {
* @return pointer to 8 byte DS18B20_ROMCODE_T representing the 1-wire
* device's unique romcode, or NULL on error.
*/
const uint8_t *get_id(const ds18b20_context dev, unsigned int index);
const uint8_t *ds18b20_get_id(const ds18b20_context dev,
unsigned int index);
#ifdef __cplusplus
}

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
* Copyright (c) 2016-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -34,6 +34,8 @@
#include <mraa/common.hpp>
#include <mraa/uart_ow.hpp>
#include <ds18b20.h>
#define DS18B20_DEFAULT_UART 0
namespace upm {
@ -112,40 +114,15 @@ namespace upm {
class DS18B20 {
public:
// The family code for these devices. We handle all of them that
// are found on the bus.
static const uint8_t DS18B20_FAMILY_CODE = 0x28;
// commands
typedef enum {
CMD_CONVERT = 0x44, // start a temp conversion
CMD_WRITE_SCRATCHPAD = 0x4e,
CMD_READ_SCRATCHPAD = 0xbe,
CMD_COPY_SCRATCHPAD = 0x48, // copy scratchpad to EEPROM
CMD_RECALL_EEPROM = 0xb8, // copy EEPROM to scratchpad
CMD_READ_POWER_SUPPLY = 0xb4 // parasitically powered?
} CMD_T;
// config register (scratchpad[4])
typedef enum {
CFG_RESOLUTION_R0 = 0x20,
CFG_RESOLUTION_R1 = 0x40,
_CFG_RESOLUTION_MASK = 3,
_CFG_RESOLUTION_SHIFT = 5
// all other bits reserved and non-writable
} CFG_BITS_T;
typedef enum {
RESOLUTION_9BITS = 0, // 93.75ms (tconv/8)
RESOLUTION_10BITS = 1, // 187.5 (tconv/4)
RESOLUTION_11BITS = 2, // 375ms (tconv/2)
RESOLUTION_12BITS = 3 // 750ms (tconv)
} RESOLUTIONS_T;
/**
* DS18B20 object constructor
*
* This method will search the 1-wire bus and store information on
* each device detected on the bus. If no devices are found, an
* exception is thrown. Once this function completes
* successfully, you can use devicesFound() to determine how many
* devices were detected.
*
* @param uart Default UART to use (0 or 1). Default is 0.
*/
DS18B20(int uart=DS18B20_DEFAULT_UART);
@ -156,12 +133,8 @@ namespace upm {
~DS18B20();
/**
* This method will search the 1-wire bus and store information on
* each device detected on the bus. If no devices are found, an
* exception is thrown. Once this function completes
* successfully, you can use devicesFound() to determine how many
* devices were detected. This method must be executed first
* before any others below.
* @deprecated This method is deprecated. It's functionality is
* handled in the constructor now.
*/
void init();
@ -184,7 +157,7 @@ namespace upm {
* The default is false (degrees Celsius).
* @return The last temperature reading in Celsius or Fahrenheit
*/
float getTemperature(int index, bool fahrenheit=false);
float getTemperature(unsigned int index, bool fahrenheit=false);
/**
* Set the device resolution for a device. These devices support
@ -194,7 +167,7 @@ namespace upm {
* @param index The device index to access (starts at 0).
* @param res One of the RESOLUTIONS_T values
*/
void setResolution(int index, RESOLUTIONS_T res);
void setResolution(unsigned int index, DS18B20_RESOLUTIONS_T res);
/**
* Copy the device's scratchpad memory to the EEPROM. This
@ -202,7 +175,7 @@ namespace upm {
*
* @param index The device index to access (starts at 0).
*/
void copyScratchPad(int index);
void copyScratchPad(unsigned int index);
/**
* Copy the device's EEPROM memory to the scratchpad. This method
@ -212,7 +185,7 @@ namespace upm {
*
* @param index The device index to access (starts at 0).
*/
void recallEEPROM(int index);
void recallEEPROM(unsigned int index);
/**
* This method will return the number of DS18B20 devices that were
@ -222,7 +195,7 @@ namespace upm {
*/
int devicesFound()
{
return m_devicesFound;
return ds18b20_devices_found(m_ds18b20);
}
/**
@ -233,37 +206,11 @@ namespace upm {
* @return 8 byte string representing the 1-wire device's unique
* romcode.
*/
std::string getId(int index)
{
if (index < 0 || index >= m_devicesFound)
{
throw std::out_of_range(std::string(__FUNCTION__) +
": device index out of range");
}
return m_deviceMap[index].id;
}
std::string getId(unsigned int index);
protected:
mraa::UartOW m_uart;
// the total number of devices found
int m_devicesFound;
// this struct will generate SWIG warnings on build, but as it's not
// exposed outside the class, they can be safely ignored
// data we need to store for each sensor we are dealing with
typedef struct {
std::string id; // 8-byte romcode id
float temperature;
RESOLUTIONS_T resolution;
} sensor_info_t;
std::map<int, sensor_info_t> m_deviceMap;
ds18b20_context m_ds18b20;
private:
// internal utility function to read temperature from a single
// device
float readSingleTemp(int index);
};
}

View File

@ -0,0 +1,63 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
// The family code for these devices. We handle all of them that
// are found on the bus.
#define DS18B20_FAMILY_CODE 0x28
// commands
typedef enum {
DS18B20_CMD_CONVERT = 0x44, // start a temp conversion
DS18B20_CMD_WRITE_SCRATCHPAD = 0x4e,
DS18B20_CMD_READ_SCRATCHPAD = 0xbe,
DS18B20_CMD_COPY_SCRATCHPAD = 0x48, // copy scratchpad to EEPROM
DS18B20_CMD_RECALL_EEPROM = 0xb8, // copy EEPROM to scratchpad
DS18B20_CMD_READ_POWER_SUPPLY = 0xb4 // parasitically powered?
} DS18B20_CMD_T;
// config register (scratchpad[4])
typedef enum {
DS18B20_CFG_RESOLUTION_R0 = 0x20,
DS18B20_CFG_RESOLUTION_R1 = 0x40,
_DS18B20_CFG_RESOLUTION_MASK = 3,
_DS18B20_CFG_RESOLUTION_SHIFT = 5
// all other bits reserved and non-writable
} DS18B20_CFG_BITS_T;
typedef enum {
DS18B20_RESOLUTION_9BITS = 0, // 93.75ms (tconv/8)
DS18B20_RESOLUTION_10BITS = 1, // 187.5 (tconv/4)
DS18B20_RESOLUTION_11BITS = 2, // 375ms (tconv/2)
DS18B20_RESOLUTION_12BITS = 3 // 750ms (tconv)
} DS18B20_RESOLUTIONS_T;
#ifdef __cplusplus
}
#endif

120
src/ds18b20/ds18b20_fti.c Normal file
View File

@ -0,0 +1,120 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ds18b20.h"
#include <stdio.h>
#include <stdlib.h>
#include "upm_fti.h"
#include "upm_sensor.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_ds18b20_name[] = "DS18B20";
const char upm_ds18b20_description[] = "DS18B20 DS 1-wire Temperature Sensor";
const upm_protocol_t upm_ds18b20_protocol[] = {UPM_UART};
const upm_sensor_t upm_ds18b20_category[] = {UPM_TEMPERATURE};
// forward declarations
const upm_sensor_descriptor_t upm_ds18b20_get_descriptor();
const void* upm_ds18b20_get_ft(upm_sensor_t sensor_type);
upm_result_t upm_ds18b20_get_value(void* dev, float* tempval,
upm_temperature_u unit);
void* upm_ds18b20_init_name();
void upm_ds18b20_close(void* dev);
const upm_sensor_descriptor_t upm_ds18b20_get_descriptor()
{
upm_sensor_descriptor_t usd;
usd.name = upm_ds18b20_name;
usd.description = upm_ds18b20_description;
usd.protocol_size = 1;
usd.protocol = upm_ds18b20_protocol;
usd.category_size = 1;
usd.category = upm_ds18b20_category;
return usd;
}
static const upm_sensor_ft ft =
{
.upm_sensor_init_name = &upm_ds18b20_init_name,
.upm_sensor_close = &upm_ds18b20_close,
.upm_sensor_get_descriptor = &upm_ds18b20_get_descriptor
};
static const upm_temperature_ft tft =
{
.upm_temperature_get_value = &upm_ds18b20_get_value,
};
const void* upm_ds18b20_get_ft(upm_sensor_t sensor_type)
{
if (sensor_type == UPM_SENSOR)
return &ft;
if (sensor_type == UPM_TEMPERATURE)
return &tft;
return NULL;
}
void* upm_ds18b20_init_name()
{
return NULL;
}
void upm_ds18b20_close(void* dev)
{
ds18b20_close((ds18b20_context)dev);
}
// FTI/temp has no way to indicate devices on a bus, so we always use
// index 0 (the first detected device) for all queries.
upm_result_t upm_ds18b20_get_value(void* dev, float* tempval,
upm_temperature_u unit)
{
ds18b20_update((ds18b20_context)dev, 0);
// always returned in C
float temp = ds18b20_get_temperature((ds18b20_context)dev, 0);
switch (unit)
{
case CELSIUS:
*tempval = temp;
return UPM_SUCCESS;
case KELVIN:
*tempval = temp + 273.15;
return UPM_SUCCESS;
case FAHRENHEIT:
*tempval = temp * (9.0/5.0) + 32.0;
return UPM_SUCCESS;
}
return UPM_SUCCESS;
}

View File

@ -3,11 +3,12 @@
%include "carrays.i"
%include "std_string.i"
%include "ds18b20_defs.h"
%include "ds18b20.hpp"
%{
#include "ds18b20.hpp"
%}
%include "ds18b20.hpp"
%array_class(char, charArray);
%pragma(java) jniclasscode=%{

View File

@ -3,9 +3,10 @@
%include "carrays.i"
%include "std_string.i"
%include "ds18b20_defs.h"
%include "ds18b20.hpp"
%{
#include "ds18b20.hpp"
%}
%include "ds18b20.hpp"
%array_class(char, charArray);

View File

@ -8,6 +8,8 @@
%feature("autodoc", "3");
%include "ds18b20_defs.h"
%include "ds18b20.hpp"
%{
#include "ds18b20.hpp"
%}