DUDS
Distributed Update of Data from Something
AMG88xx.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) 2018 Jeff Jackowski
9  */
15 #include <thread>
16 
17 namespace duds { namespace hardware { namespace devices { namespace instruments {
18 
19 AMG88xx::AMG88xx(std::unique_ptr<duds::hardware::interface::I2c> &c) :
20 com(std::move(c)), mode(Sleep), fps1Not10(0), misid(0) {
21  try {
23  // normal operating mode
24  reset.addOutputVector() << (std::int16_t)0;
25  com->converse(reset);
26  std::this_thread::sleep_for(std::chrono::milliseconds(50));
27  reset.clear();
28  reset.addOutputVector() << (std::int8_t)0;
29  reset.addInputVector(1);
30  com->converse(reset);
32  std::int8_t mode;
33  ex >> mode;
34  if (mode != 0) {
36  }
37  reset.clear();
38  // reset device
39  reset.addOutputVector() << (std::int8_t)1 << (std::int8_t)0x3F;
40  com->converse(reset);
41  std::this_thread::sleep_for(std::chrono::milliseconds(50));
42  reset.clear();
43  // sleep
44  reset.addOutputVector() << (std::int8_t)0 << (std::int8_t)0x10;
45  com->converse(reset);
46  std::this_thread::sleep_for(std::chrono::milliseconds(50));
47  } catch (...) {
48  c = std::move(com);
49  throw;
50  }
51  read.addOutputVector() << (std::int8_t)0x0E;
53  (read.addOutputVector() << (std::int8_t)0x80).breakBefore();
54  read.addInputVector(128);
55 }
56 
58  if (!misid) {
59  suspend();
60  }
61 }
62 
63 void AMG88xx::configure(bool fps1) {
64  // only act on a change
65  if (fps1 != (fps1Not10 > 0)) {
66  // value actually a bit, so this will toggle
67  ++fps1Not10;
68  // if device is not sleeping . . .
69  if (mode != Sleep) {
70  // change the frame rate now
72  frate.addOutputVector() << (std::int8_t)2 << (std::int8_t)fps1Not10;
73  com->converse(frate);
74  }
75  }
76 }
77 
80  // normal operating mode
81  go.addOutputVector() << (std::int16_t)0;
82  com->converse(go);
83  std::this_thread::sleep_for(std::chrono::milliseconds(50));
84  go.clear();
85  go.addOutputVector() << (std::int8_t)0;
86  go.addInputVector(1);
87  try {
88  com->converse(go);
90  std::int8_t mode;
91  ex >> mode;
92  if (mode != 0) {
93  misid = 1;
95  }
96  } catch (...) {
97  misid = 1;
98  throw;
99  }
100  misid = 0;
101  go.clear();
102  // configure frame rate
103  go.addOutputVector() << (std::int8_t)2 << (std::int8_t)fps1Not10;
104  com->converse(go);
105  //std::this_thread::sleep_for(std::chrono::milliseconds(50));
106  mode = Normal;
107 }
108 
111  stop.addOutputVector() << (std::int8_t)0 << (std::int8_t)0x10;
112  com->converse(stop);
113  mode = Sleep;
114  std::this_thread::sleep_for(std::chrono::milliseconds(50));
115 }
116 
118  com->converse(read);
120  std::int16_t t;
121  ex >> t;
122  temp = duds::general::SignedMagnitudeToTwosComplement<12>(t);
123  double *pixel = &(img[0][0]);
124  for (int c = 64; c > 0; --c, ++pixel) {
125  std::int16_t val;
126  ex >> val;
127  val = duds::general::SignExtend<12>(val);
128  *pixel = (double)val / 4.0 + 273.15;
129  }
130 }
131 
133  return duds::data::Quantity(
134  (double)temp / 16.0 + 273.15,
136  );
137 }
138 
139 } } } }
A container for a value and a unit to better describe the value.
Definition: Quantity.hpp:37
void clear()
Makes the conversation empty.
ConversationPart & breakBefore()
Flags the conversation part to have a break before this part is sent.
void configure(bool fps1)
Configures the device.
Definition: AMG88xx.cpp:63
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.
~AMG88xx()
Suspends the device operation (sleep mode) if a DeviceMisidentified error did not occur on the last s...
Definition: AMG88xx.cpp:57
duds::data::Quantity temperature() const
Returns the temperature of the device as reported by its thermistor.
Definition: AMG88xx.cpp:132
void suspend()
Transitions the device to sleep mode.
Definition: AMG88xx.cpp:109
STL namespace.
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
unsigned int mode
Current operating mode.
Definition: AMG88xx.hpp:90
void sample()
Reads a sample from the device.
Definition: AMG88xx.cpp:117
void start()
Transitions the device to normal operating mode so that it begins sampling.
Definition: AMG88xx.cpp:78
unsigned int fps1Not10
Frame rate setting.
Definition: AMG88xx.hpp:94
unsigned int misid
True when the last read of the operating mode failed on error, or provided a result that did not matc...
Definition: AMG88xx.hpp:106
Regular sampling at 1Hz or 10Hz.
Definition: AMG88xx.hpp:70
std::unique_ptr< duds::hardware::interface::I2c > com
The I2C communication interface.
Definition: AMG88xx.hpp:50
AMG88xx(std::unique_ptr< duds::hardware::interface::I2c > &c)
Attempts to reset the device and put it into sleep mode.
Definition: AMG88xx.cpp:19
An attempt was made to use a device that seems to exist, but the responding device is not the type th...
std::int16_t temp
Thermistor temperature.
Definition: AMG88xx.hpp:62
No sampling ; reduced power usage.
Definition: AMG88xx.hpp:74
ConversationVector & addOutputVector()
Creates a new ConversationVector for output and returns it for modification.
duds::hardware::interface::Conversation read
Used to read in sampled data from the device.
Definition: AMG88xx.hpp:54
Extracts data from a Conversation without modifying the Conversation or copying from it...
#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
double img[8][8]
Temperature image.
Definition: AMG88xx.hpp:58
Represents a two-way conversation with a device.
constexpr Unit Kelvin(DUDS_UNIT_VALUE(0, 0, 1, 0, 0, 0, 0, 0, 0))