upm  1.0.2
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ds18b20.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <iostream>
28 #include <map>
29 
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 
34 #include <mraa/common.hpp>
35 #include <mraa/uart_ow.hpp>
36 
37 #define DS18B20_DEFAULT_UART 0
38 
39 namespace upm {
112  class DS18B20 {
113  public:
114 
115  // The family code for these devices. We handle all of them that
116  // are found on the bus.
117  static const uint8_t DS18B20_FAMILY_CODE = 0x28;
118 
119  // commands
120  typedef enum {
121  CMD_CONVERT = 0x44, // start a temp conversion
122  CMD_WRITE_SCRATCHPAD = 0x4e,
123  CMD_READ_SCRATCHPAD = 0xbe,
124  CMD_COPY_SCRATCHPAD = 0x48, // copy scratchpad to EEPROM
125  CMD_RECALL_EEPROM = 0xb8, // copy EEPROM to scratchpad
126  CMD_READ_POWER_SUPPLY = 0xb4 // parasitically powered?
127  } CMD_T;
128 
129  // config register (scratchpad[4])
130  typedef enum {
131  CFG_RESOLUTION_R0 = 0x20,
132  CFG_RESOLUTION_R1 = 0x40,
133  _CFG_RESOLUTION_MASK = 3,
134  _CFG_RESOLUTION_SHIFT = 5
135 
136  // all other bits reserved and non-writable
137  } CFG_BITS_T;
138 
139  typedef enum {
140  RESOLUTION_9BITS = 0, // 93.75ms (tconv/8)
141  RESOLUTION_10BITS = 1, // 187.5 (tconv/4)
142  RESOLUTION_11BITS = 2, // 375ms (tconv/2)
143  RESOLUTION_12BITS = 3 // 750ms (tconv)
144  } RESOLUTIONS_T;
145 
151  DS18B20(int uart=DS18B20_DEFAULT_UART);
152 
156  ~DS18B20();
157 
166  void init();
167 
175  void update(int index=-1);
176 
187  float getTemperature(int index, bool fahrenheit=false);
188 
197  void setResolution(int index, RESOLUTIONS_T res);
198 
205  void copyScratchPad(int index);
206 
215  void recallEEPROM(int index);
216 
224  {
225  return m_devicesFound;
226  }
227 
236  std::string getId(int index)
237  {
238  if (index < 0 || index >= m_devicesFound)
239  {
240  throw std::out_of_range(std::string(__FUNCTION__) +
241  ": device index out of range");
242  }
243  return m_deviceMap[index].id;
244  }
245 
246  protected:
247  mraa::UartOW m_uart;
248 
249  // the total number of devices found
250  int m_devicesFound;
251 
252  // this struct will generate SWIG warnings on build, but as it's not
253  // exposed outside the class, they can be safely ignored
254 
255  // data we need to store for each sensor we are dealing with
256  typedef struct {
257  std::string id; // 8-byte romcode id
258  float temperature;
259  RESOLUTIONS_T resolution;
260  } sensor_info_t;
261 
262  std::map<int, sensor_info_t> m_deviceMap;
263 
264  private:
265  // internal utility function to read temperature from a single
266  // device
267  float readSingleTemp(int index);
268  };
269 }
DS18B20(int uart=DS18B20_DEFAULT_UART)
Definition: ds18b20.cxx:41
Definition: ds18b20.hpp:256
std::string getId(int index)
Definition: ds18b20.hpp:236
float getTemperature(int index, bool fahrenheit=false)
Definition: ds18b20.cxx:234
int devicesFound()
Definition: ds18b20.hpp:223
~DS18B20()
Definition: ds18b20.cxx:56
void setResolution(int index, RESOLUTIONS_T res)
Definition: ds18b20.cxx:248
void recallEEPROM(int index)
Definition: ds18b20.cxx:288
API for the DS18B20 1-Wire Temperature Sensor.
Definition: ds18b20.hpp:112
void copyScratchPad(int index)
Definition: ds18b20.cxx:274
void init()
Definition: ds18b20.cxx:60
void update(int index=-1)
Definition: ds18b20.cxx:136