DUDS
Distributed Update of Data from Something
INA219.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) 2017 Jeff Jackowski
9  */
11 
12 namespace duds { namespace hardware { namespace devices { namespace instruments {
13 
15  std::unique_ptr<duds::hardware::interface::Smbus> &c,
16  double sr
17 ) : shunt(sr) {
18  // reset device
19  c->transmitWordBe(0, 0x8000);
20  c->transmitWordBe(0, 0x1FFF); // 128 samples, 16v bus
21  // only take the communicator if no exception is thrown
22  com = std::move(c);
23 }
24 
26  if (com) {
27  // put device to sleep; set widest ranges in case that is good
28  com->transmitWordBe(0, 0x3998);
29  }
30 }
31 
34 }
35 
38 }
39 
41  return duds::data::Quantity(((double)shuntV) * 1e-5, duds::data::units::Volt);
42 }
43 
45  return duds::data::Quantity(((double)busV) * 4e-3, duds::data::units::Volt);
46 }
47 
49  return shuntVoltage() / shuntResistance();
50 }
51 
53  return busVoltage() * busCurrent();
54 }
55 
57  shuntV = com->receiveWordBe(1);
58  busV = (std::int16_t)(com->receiveWordBe(2)) >> 3;
59 }
60 
61 } } } }
A container for a value and a unit to better describe the value.
Definition: Quantity.hpp:37
duds::data::Quantity shuntVoltage() const
Returns the sampled shunt voltage.
Definition: INA219.cpp:40
duds::data::Quantity busPower() const
Returns the computed bus power using the sampled bus voltage and the computed bus current...
Definition: INA219.cpp:52
constexpr Unit Volt(DUDS_UNIT_VALUE(-1, 0, 0, 1, 2, 0, -3, 0, 0))
void transmitWordBe(std::uint8_t cmd, std::uint16_t word)
Sends a command byte and a big-endian data word to the device.
Definition: Smbus.hpp:281
constexpr Unit Ohm(DUDS_UNIT_VALUE(-2, 0, 0, 1, 2, 0, -3, 0, 0))
std::unique_ptr< duds::hardware::interface::Smbus > com
Communication bus.
Definition: INA219.hpp:35
duds::data::Quantity maxCurrent() const
Returns the maximum current that can be measured by the device.
Definition: INA219.cpp:32
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
duds::data::Quantity shuntResistance() const
Returns the shunt resistance.
Definition: INA219.cpp:36
std::int16_t busV
The raw data for the sampled bus voltage.
Definition: INA219.hpp:47
std::int16_t shuntV
The raw data for the sampled shunt voltage.
Definition: INA219.hpp:43
duds::data::Quantity busVoltage() const
Returns the sampled bus voltage.
Definition: INA219.cpp:44
constexpr Unit Ampere(DUDS_UNIT_VALUE(1, 0, 0, 0, 0, 0, 0, 0, 0))
INA219(std::unique_ptr< duds::hardware::interface::Smbus > &c, double sr)
Definition: INA219.cpp:14
double shunt
Shunt resistance; used to compute current.
Definition: INA219.hpp:39
void sample()
Reads sample data from the device.
Definition: INA219.cpp:56
duds::data::Quantity busCurrent() const
Returns the computed bus current using the sampled shunt voltage and the shunt resistance.
Definition: INA219.cpp:48