DUDS
Distributed Update of Data from Something
TSL2591.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  */
13 
14 namespace duds { namespace hardware { namespace devices { namespace instruments {
15 
21 namespace TSL2591_internal {
22 
26 enum Regs {
28  RegControl, // called config in the doc's list of regs, but control in detail
29  RegDeviceId = 0x12,
32  RegCh1 = 0x16
33 };
34 
38 enum CmdFlags {
39  Cmd = 0x80, // used for all command bytes
40  TransNorm = 0x20,
41  TransSpec = 0x60,
42  AddrMask = 0x1F
43 };
44 
49  OscOn = 1,
50  Sample = 2,
51  IntEnable = 0x10,
52  SleepOnInt = 0x40,
53  NonPersistantInterrutEnable = 0x80 // really don't know what to call it
54 };
55 
62  GainShift = 4,
63  GainMask = 0x30,
64  Reset = 0x80
65 };
66 
70 static std::uint16_t gainSettings[4] = {
71  1,
72  25,
73  428,
74  9876
75 };
76 
77 }
78 
79 using namespace TSL2591_internal;
80 
81 TSL2591::TSL2591(std::unique_ptr<duds::hardware::interface::I2c> &c) :
82 com(std::move(c)), scale(0) {
83  try {
85  // verify ID
86  firstcon.addOutputVector() << (std::int8_t)(Cmd|TransNorm|RegDeviceId);
87  firstcon.addInputVector(1);
88  com->converse(firstcon);
90  std::int8_t id;
91  ex >> id;
92  if (id != 0x50) {
95  );
96  }
97  // attempt reset
98  firstcon.clear();
99  firstcon.addOutputVector() << (std::int8_t)(Cmd|TransNorm|RegControl) <<
100  (std::int8_t)(Reset);
101  try {
102  // the reset causes the device to not ack the message
103  com->converse(firstcon);
105  // bother; ignore it
106  } catch (...) {
107  // could be a real issue, but should be unlikely to occur on the second
108  // conversation with the device
109  throw;
110  }
111  // make conversation to read input so it can be reused
112  // address -- green
113  input.addOutputVector() << (std::int8_t)(Cmd|TransNorm|RegCh0);
114  // read in 4 bytes
116  } catch (...) {
117  // move the I2C communicator back
118  c = std::move(com);
119  throw;
120  }
121 }
122 
124  suspend();
125 }
126 
127 void TSL2591::init(int gain, int integration) {
128  if ((gain < 0) || (gain > 3)) {
130  }
131  if (integration >= 100) {
132  integration = (integration / 100) - 1;
133  }
134  if ((integration < 0) || (integration > 5)) {
136  }
137  // remake the initialization conversation
138  initialize.clear();
140  // command to start writing at enable register
141  (std::int8_t)(Cmd|TransNorm|RegEnable) <<
142  // make it go
143  (std::int8_t)(OscOn|Sample) <<
144  // set gain and integration time
145  (std::int8_t)(integration | (gain << GainShift));
146  com->converse(initialize);
147  // datasheet says the values are for the 100ms integration period
148  scale = (double)(integration + 1);
149  // datasheet says the values are for maximum gain
150  if (gain < 3) {
151  scale *= (double)gainSettings[gain] / (double)gainSettings[3];
152  }
153 }
154 
157  conv.addOutputVector() <<
158  // command to start writing at enable register
159  (std::int8_t)(Cmd|TransNorm|RegEnable) <<
160  // make it stop
161  (std::int8_t)(0);
162  com->converse(conv);
163 }
164 
166  if (initialize.empty()) {
168  }
169  com->converse(initialize);
170 }
171 
173  // get input
174  com->converse(input);
175  // parse input
177  ex >> broad >> ir;
178 }
179 
181  // datasheet uses uW/cm2, which is (0.01)W/m2
182  // this channel has 6024 counts per uW/cm2 for white light
183  return duds::data::Quantity(
184  (double)broad * scale * 60.24,
187  )
188  );
189 }
190 
192  // datasheet uses uW/cm2, which is (0.01)W/m2
193  // this channel has 3474 counts per uW/cm2 for IR light
194  return duds::data::Quantity(
195  (double)ir * scale * 34.74,
198  )
199  );
200 }
201 
202 } } } }
A container for a value and a unit to better describe the value.
Definition: Quantity.hpp:37
TSL2591(std::unique_ptr< duds::hardware::interface::I2c > &c)
Attempts to identify the device, then performs a reset.
Definition: TSL2591.cpp:81
constexpr Unit Meter(DUDS_UNIT_VALUE(0, 0, 0, 0, 1, 0, 0, 0, 0))
void clear()
Makes the conversation empty.
void sample()
The device will update samples after it has completed integration.
Definition: TSL2591.cpp:172
An invalid integration time was specified.
Definition: TSL2591.hpp:31
void resume()
Resumes operation after a call to suspend().
Definition: TSL2591.cpp:165
boost::error_info< struct Info_i2cdevaddr, int > I2cDeviceAddr
Provides the device (slave) address along with an error.
Definition: I2cErrors.hpp:83
std::uint16_t broad
The values supplied by the device.
Definition: TSL2591.hpp:65
void suspend()
Suspends operation by putting the device into a low-power mode.
Definition: TSL2591.cpp:155
ConversationVector & addInputVector(std::size_t len)
Creates a new ConversationVector for fixed length input and initializes it with the given length...
Header for ConversationExtractor; includes all other conversation related header files.
EnableFlags
Flags used to enable various features.
Definition: TSL2591.cpp:48
STL namespace.
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
The device did not respond to its address (NACK).
Definition: I2cErrors.hpp:47
static std::uint16_t gainSettings[4]
The set of selectable gain factors.
Definition: TSL2591.cpp:70
duds::hardware::interface::Conversation input
The conversation used to query the brightness values.
Definition: TSL2591.hpp:54
An attempt was made to use a device prior to running a required initialization step.
An invalid gain value was specified.
Definition: TSL2591.hpp:27
CmdFlags
The flags refered to as commands in the documentation.
Definition: TSL2591.cpp:38
std::unique_ptr< duds::hardware::interface::I2c > com
The I2C communication interface.
Definition: TSL2591.hpp:44
static std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > conv
String converter; UTF-8 to/from UTF-32.
Definition: BppFont.cpp:165
constexpr Unit Watt(DUDS_UNIT_VALUE(0, 0, 0, 1, 2, 0, -3, 0, 0))
duds::data::Quantity brightnessIr() const
Brightness mostly in infrared.
Definition: TSL2591.cpp:191
double scale
A scalar value used to partially convert the counts supplied by the device into a value in Watts per ...
Definition: TSL2591.hpp:61
void init(int gain, int integration)
Configures the device.
Definition: TSL2591.cpp:127
An attempt was made to use a device that seems to exist, but the responding device is not the type th...
ConversationVector & addOutputVector()
Creates a new ConversationVector for output and returns it for modification.
Extracts data from a Conversation without modifying the Conversation or copying from it...
bool empty() const
Returns true if the conversation has no parts.
#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
duds::data::Quantity brightness() const
Broad spectrum brightness.
Definition: TSL2591.cpp:180
duds::hardware::interface::Conversation initialize
The conversation used to initialize the device.
Definition: TSL2591.hpp:49
Represents a two-way conversation with a device.