upm  0.8.0
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 {
85  class DS18B20 {
86  public:
87 
88  // The family code for these devices. We handle all of them that
89  // are found on the bus.
90  static const uint8_t DS18B20_FAMILY_CODE = 0x28;
91 
92  // commands
93  typedef enum {
94  CMD_CONVERT = 0x44, // start a temp conversion
95  CMD_WRITE_SCRATCHPAD = 0x4e,
96  CMD_READ_SCRATCHPAD = 0xbe,
97  CMD_COPY_SCRATCHPAD = 0x48, // copy scratchpad to EEPROM
98  CMD_RECALL_EEPROM = 0xb8, // copy EEPROM to scratchpad
99  CMD_READ_POWER_SUPPLY = 0xb4 // parasitically powered?
100  } CMD_T;
101 
102  // config register (scratchpad[4])
103  typedef enum {
104  CFG_RESOLUTION_R0 = 0x20,
105  CFG_RESOLUTION_R1 = 0x40,
106  _CFG_RESOLUTION_MASK = 3,
107  _CFG_RESOLUTION_SHIFT = 5
108 
109  // all other bits reserved and non-writable
110  } CFG_BITS_T;
111 
112  typedef enum {
113  RESOLUTION_9BITS = 0, // 93.75ms (tconv/8)
114  RESOLUTION_10BITS = 1, // 187.5 (tconv/4)
115  RESOLUTION_11BITS = 2, // 375ms (tconv/2)
116  RESOLUTION_12BITS = 3 // 750ms (tconv)
117  } RESOLUTIONS_T;
118 
124  DS18B20(int uart=DS18B20_DEFAULT_UART);
125 
129  ~DS18B20();
130 
139  void init();
140 
148  void update(int index=-1);
149 
160  float getTemperature(int index, bool fahrenheit=false);
161 
170  void setResolution(int index, RESOLUTIONS_T res);
171 
178  void copyScratchPad(int index);
179 
188  void recallEEPROM(int index);
189 
197  {
198  return m_devicesFound;
199  }
200 
209  std::string getId(int index)
210  {
211  if (index < 0 || index >= m_devicesFound)
212  {
213  throw std::out_of_range(std::string(__FUNCTION__) +
214  ": device index out of range");
215  }
216  return m_deviceMap[index].id;
217  }
218 
219  protected:
220  mraa::UartOW m_uart;
221 
222  // the total number of devices found
223  int m_devicesFound;
224 
225  // this struct will generate SWIG warnings on build, but as it's not
226  // exposed outside the class, they can be safely ignored
227 
228  // data we need to store for each sensor we are dealing with
229  typedef struct {
230  std::string id; // 8-byte romcode id
231  float temperature;
232  RESOLUTIONS_T resolution;
233  } sensor_info_t;
234 
235  std::map<int, sensor_info_t> m_deviceMap;
236 
237  private:
238  // internal utility function to read temperature from a single
239  // device
240  float readSingleTemp(int index);
241  };
242 }
DS18B20(int uart=DS18B20_DEFAULT_UART)
Definition: ds18b20.cxx:40
Definition: ds18b20.hpp:229
std::string getId(int index)
Definition: ds18b20.hpp:209
float getTemperature(int index, bool fahrenheit=false)
Definition: ds18b20.cxx:230
int devicesFound()
Definition: ds18b20.hpp:196
~DS18B20()
Definition: ds18b20.cxx:55
void setResolution(int index, RESOLUTIONS_T res)
Definition: ds18b20.cxx:244
void recallEEPROM(int index)
Definition: ds18b20.cxx:284
API for the DS18B20 1-Wire Temperature Sensor.
Definition: ds18b20.hpp:85
void copyScratchPad(int index)
Definition: ds18b20.cxx:270
void init()
Definition: ds18b20.cxx:59
void update(int index=-1)
Definition: ds18b20.cxx:135