DUDS
Distributed Update of Data from Something
MCP9808.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2020 Jeff Jackowski
9  */
13 
14 namespace duds { namespace hardware { namespace devices { namespace instruments {
15 
16 MCP9808::MCP9808(std::unique_ptr<duds::hardware::interface::Smbus> &c) {
17  // attempt to confirm that this is a MCP9808
18  std::int16_t chk = c->receiveWordBe(6);
19  if (chk != 0x54) {
20  // wrong response
23  );
24  }
25  chk = c->receiveWordBe(7);
26  if ((chk & 0xFF00) != 0x400) {
27  // wrong response
30  );
31  }
32  // keep revision
33  rev = chk & 0xFF;
34  // get the resolution
35  res = c->receiveByte(8);
36  // get the current configuration
37  config = c->receiveWordBe(1);
38  // only take the communicator if no exception is thrown
39  com = std::move(c);
40 }
41 
43  suspend();
44 }
45 
47  config &= 0x6FF;
48  com->transmitWordBe(1, config);
49 }
50 
52  config |= 0x100;
53  com->transmitWordBe(1, config);
54 }
55 
57  res = r & 3;
58  com->transmitByte(8, res);
59 }
60 
62  switch (res) {
63  case Half:
64  return 0.5;
65  case Quarter:
66  return 0.25;
67  case Eighth:
68  return 0.125;
69  case Sixteenth:
70  return 0.0625;
71  }
72  // should never happen
73  return 0;
74 }
75 
77  std::int16_t samp = com->receiveWordBe(5);
78  temp = ((double)duds::general::SignExtend<13>(samp)) / 16.0 + 273.15;
79 }
80 
83 }
84 
85 } } } }
A container for a value and a unit to better describe the value.
Definition: Quantity.hpp:37
char rev
Device&#39;s revision byte.
Definition: MCP9808.hpp:51
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
char res
The raw data for the configured resolution.
Definition: MCP9808.hpp:47
std::unique_ptr< duds::hardware::interface::Smbus > com
Communication bus.
Definition: MCP9808.hpp:35
void sample()
Reads sample data from the device.
Definition: MCP9808.cpp:76
std::uint16_t config
The configuration word.
Definition: MCP9808.hpp:43
duds::data::Quantity temperature() const
Returns the last sampled temperature.
Definition: MCP9808.cpp:81
std::uint16_t receiveWordBe(std::uint8_t cmd)
Sends a command byte, then reads a big-endian word from the device.
Definition: Smbus.hpp:146
double temp
Last temperature sample.
Definition: MCP9808.hpp:39
boost::error_info< struct Info_smbusdevaddr, int > SmbusDeviceAddr
Provides the device (slave) address along with an error.
Definition: SmbusErrors.hpp:80
MCP9808(std::unique_ptr< duds::hardware::interface::Smbus > &c)
Prepares to use a MCP9808 by identifying the device and reading, but not changing, its current configuration.
Definition: MCP9808.cpp:16
Resolution resolution() const
Returns the current sampling resolution.
Definition: MCP9808.hpp:110
virtual std::uint8_t receiveByte()=0
Read a single byte from the device without sending a command/register byte first. ...
An attempt was made to use a device that seems to exist, but the responding device is not the type th...
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48
constexpr Unit Kelvin(DUDS_UNIT_VALUE(0, 0, 1, 0, 0, 0, 0, 0, 0))