modm API documentation for STM32F107VCT7
tcs3414.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, David Hebbeker
3  * Copyright (c) 2013-2014, Sascha Schade
4  * Copyright (c) 2013-2016, 2018, Niklas Hauser
5  *
6  * This file is part of the modm project.
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11  */
12 // ----------------------------------------------------------------------------
13 
14 /**
15  * \file tcs3414.hpp
16  * \date 02 Mar 2013
17  * \author David Hebbeker
18  */
19 
20 #ifndef MODM_TCS3414_HPP
21 #define MODM_TCS3414_HPP
22 
23 #include <stdint.h>
24 
25 #include <modm/ui/color.hpp>
26 #include <modm/architecture/interface/i2c_device.hpp>
27 
28 namespace modm
29 {
30 /**
31  * \brief Settings to configure the digital color sensor TCS3414 / TCS3413 / TCS3415 / TCS3416.
32  * \see Tcs3414
33  * \ingroup modm_driver_tcs3414
34  *
35  * Device Address
36  * TCS3413 0x29
37  * 4 0x39
38  * 5 0x49
39  * 6 0x59
40  *
41  */
42 struct tcs3414
43 {
44  /** @name Gain_Register
45  * @{
46  */
47 
48  //! \brief Analog gain control
49  enum class Gain : uint8_t
50  {
51  X1 = 0b000000, //!< x1 gain
52  X4 = 0b010000, //!< x4 gain
53  X16 = 0b100000, //!< x16 gain
54  X64 = 0b110000, //!< x64 gain
55  DEFAULT = 0 //!< default value on chip reset
56  };
57 
58  //! \brief Prescaler mode
59  enum class Prescaler : uint8_t
60  {
61  D1 = 0b000, //!< divide by 1
62  D2 = 0b001, //!< divide by 2
63  D4 = 0b010, //!< divide by 4
64  D8 = 0b011, //!< divide by 8
65  D16 = 0b100, //!< divide by 16
66  D32 = 0b101, //!< divide by 32
67  D64 = 0b110, //!< divide by 64
68  DEFAULT = 0 //!< default value on chip reset
69  };
70  //! @}
71 
72 
73  /** @name Timing register
74  * @{
75  */
76 
77  //! \brief Select mode how to choose the integration time
78  enum class IntegrationMode : uint8_t
79  {
80  INTERNAL = 0b000000, //!< integrates with the free-running mode
81  ADC_CTR = 0b010000, //!< use the ADC enable bit field in the control register to start and stop integration
82  SYNC_NOM = 0b100000, //!< use synchronize signal to integrate over nominal integration time
83  SYNC_COUNT = 0b110000, //!< integrate over pulse count pulses of the sync pin
84  DEFAULT = 0 //!< default value on chip reset
85  };
86 
87  //! \brief Integration for a fixed time
88  enum class NominalIntegrationTime : uint8_t
89  {
90  MSEC_12 = 0b0000, //!< integrate over 12 ms
91  MSEC_100 = 0b0001, //!< integrate over 100 ms
92  MSEC_400 = 0b0010, //!< integrate over 400 ms
93  DEFAULT = 0 //!< default value on chip reset
94  };
95 
96  //! \brief The number of pulses on sync pin to integrate over
97  enum class SyncPulseCount : uint8_t
98  {
99  PULSES_1 = 0b0000, //!< integrate over 1 pulses of sync pin
100  PULSES_2 = 0b0001, //!< integrate over 2 pulses of sync pin
101  PULSES_4 = 0b0010, //!< integrate over 4 pulses of sync pin
102  PULSES_8 = 0b0011, //!< integrate over 8 pulses of sync pin
103  PULSES_16 = 0b0100, //!< integrate over 16 pulses of sync pin
104  PULSES_32 = 0b0101, //!< integrate over 32 pulses of sync pin
105  PULSES_64 = 0b0110, //!< integrate over 64 pulses of sync pin
106  PULSES_128 = 0b0111, //!< integrate over 128 pulses of sync pin
107  PULSES_256 = 0b1000, //!< integrate over 256 pulses of sync pin
108  DEFAULT = 0 //!< default value on chip reset
109  };
110  //! @}
111 
112  //! \brief Register addresses
113  enum class RegisterAddress : uint8_t
114  {
115  CONTROL = 0x00, //!< Control of basic functions
116  TIMING = 0x01, //!< Integration time control @see Tcs3414::setIntegrationTime
117  INTERRUPT = 0x02, //!< Interrupt settings
118  INT_SOURCE = 0x03, //!< Interrupt source
119  ID = 0x04, //!< Part number
120  GAIN = 0x07, //!< Sensitivity settings @see Tcs3414::setGain
121  LOW_THRESH_LOW_BYTE = 0x08, //!< Low byte of low interrupt threshold
122  LOW_THRESH_HIGH_BYTE = 0x09, //!< High byte of low interrupt threshold
123  HIGH_THRESH_LOW_BYTE = 0x0A, //!< Low byte of high interrupt threshold
124  HIGH_THRESH_HIGH_BYTE = 0x0B, //!< High byte of high interrupt threshold
125  // Data registers
126  DATA1LOW = 0x10, //!< Low byte of ADC green channel
127  DATA1HIGH = 0x11, //!< High byte of ADC green channel
128  DATA2LOW = 0x12, //!< Low byte of ADC green channel
129  DATA2HIGH = 0x13, //!< High byte of ADC green channel
130  DATA3LOW = 0x14, //!< Low byte of ADC green channel
131  DATA3HIGH = 0x15, //!< High byte of ADC green channel
132  DATA4LOW = 0x16, //!< Low byte of ADC green channel
133  DATA5HIGH = 0x17 //!< High byte of ADC green channel
134  };
135 
136  typedef uint16_t UnderlyingType; //!< datatype of color values
138 
139 };
140 
141 /**
142  * \brief TCS3414 Digital Color Sensors
143  *
144  * \todo Not all features of the sensors are implemented in this driver
145  * yet.
146  *
147  * \tparam I2CMaster I2C interface which needs an \em initialized
148  * modm::i2c::Master
149  * \see tcs3414
150  * \author David Hebbeker
151  * \ingroup modm_driver_tcs3414
152  */
153 template < typename I2cMaster >
154 class Tcs3414 : public tcs3414, public modm::I2cDevice< I2cMaster, 2 >
155 {
156 public:
157  Tcs3414(uint8_t address = 0x39);
158 
159  //! \brief Power up sensor and start conversions
160  // Blocking
161  bool inline
163  {
164  return RF_CALL_BLOCKING(initialize());
165  }
166 
167  //! \brief Configures some of the most important settings for the sensor.
168  static inline bool
170  const Gain gain = Gain::DEFAULT,
171  const Prescaler prescaler = Prescaler::DEFAULT,
172  const IntegrationMode mode = IntegrationMode::DEFAULT,
173  const NominalIntegrationTime time = NominalIntegrationTime::DEFAULT)
174  {
175  return configure(gain, prescaler, mode, static_cast<uint8_t>(time));
176  }
177 
178  //! \brief Configures some of the most important settings for the sensor.
179  static inline bool
181  const Gain gain = Gain::DEFAULT,
182  const Prescaler prescaler = Prescaler::DEFAULT,
183  const IntegrationMode mode = IntegrationMode::DEFAULT,
184  const SyncPulseCount time = SyncPulseCount::DEFAULT)
185  {
186  return configure(gain, prescaler, mode, static_cast<uint8_t>(time));
187  }
188 
189  //! \brief The gain can be used to adjust the sensitivity of all ADC output channels.
192  const Gain gain = Gain::DEFAULT,
193  const Prescaler prescaler = Prescaler::DEFAULT)
194  {
195  return writeRegister(RegisterAddress::GAIN,
196  static_cast<uint8_t>(gain) | static_cast<uint8_t>(prescaler));
197  }
198 
199  //! \brief Sets the integration time for the ADCs.
202  const IntegrationMode mode = IntegrationMode::DEFAULT,
203  const NominalIntegrationTime time = NominalIntegrationTime::DEFAULT)
204  {
205  return setIntegrationTime(mode, static_cast<uint8_t>(time));
206  }
207 
208  //! \brief Sets the integration time for the ADCs.
211  const IntegrationMode mode = IntegrationMode::DEFAULT,
212  const SyncPulseCount time = SyncPulseCount::DEFAULT)
213  {
214  return setIntegrationTime(mode, static_cast<uint8_t>(time));
215  }
216 
217  /**
218  * @name Return already sampled color
219  * @{
220  */
221  inline static tcs3414::Rgb
222  getOldColors()
223  {
224  return color;
225  };
226 
227  //!@}
228 
229  /**
230  * @name Sample and return fresh color values
231  * @{
232  */
233  inline static tcs3414::Rgb
234  getNewColors()
235  {
237  return getOldColors();
238  };
239 
240  //!@}
241 
242  //! \brief Read current samples of ADC conversions for all channels.
243  // Non-blocking
246 
247  // MARK: - TASKS
249  initialize()
250  {
251  return writeRegister(RegisterAddress::CONTROL, 0b11); // control to power up and start conversion
252  };
253 
255  configure(
256  const Gain gain = Gain::DEFAULT,
257  const Prescaler prescaler = Prescaler::DEFAULT,
258  const IntegrationMode mode = IntegrationMode::DEFAULT,
259  const uint8_t time = 0);
260 
261 private:
262  //! \brief Sets the integration time for the ADCs.
265  const IntegrationMode mode = IntegrationMode::DEFAULT,
266  const uint8_t time = 0)
267  {
268  return writeRegister(
269  RegisterAddress::TIMING,
270  static_cast<uint8_t>(mode) | time );
271  }
272 
273 private:
274  uint8_t commandBuffer[4];
275  bool success;
276 
277 private:
278  //! \brief Read value of specific register.
280  readRegisters(
281  const RegisterAddress address,
282  uint8_t * const values,
283  const uint8_t count = 1);
284 
286  writeRegister(
287  const RegisterAddress address,
288  const uint8_t value);
289 
290 private:
291  class uint16_t_LOW_HIGH
292  {
293  private:
294  uint8_t low;
295  uint8_t high;
296  public:
297  uint16_t
298  get() const
299  {
300  uint16_t value = low;
301  value |= high << 8;
302  return value;
303  }
304  inline uint8_t getLSB() const { return low; }
305  inline uint8_t getMSB() const { return high; }
306  } modm_packed;
307 
308  static union Data
309  {
310  uint8_t dataBytes[2*4];
311  struct
312  {
313  uint16_t_LOW_HIGH green;
314  uint16_t_LOW_HIGH red;
315  uint16_t_LOW_HIGH blue;
316  uint16_t_LOW_HIGH clear;
317  } modm_packed;
318  } data;
319 
320  static Rgb color;
321 };
322 }
323 
324 #include "tcs3414_impl.hpp"
325 
326 #endif // MODM_TCS3414_HPP
modm::ResumableResult< bool > setIntegrationTime(const IntegrationMode mode=IntegrationMode::DEFAULT, const SyncPulseCount time=SyncPulseCount::DEFAULT)
Sets the integration time for the ADCs.
Definition: tcs3414.hpp:210
modm::ResumableResult< bool > refreshAllColors()
Read current samples of ADC conversions for all channels.
modm::ResumableResult< bool > setIntegrationTime(const IntegrationMode mode=IntegrationMode::DEFAULT, const NominalIntegrationTime time=NominalIntegrationTime::DEFAULT)
Sets the integration time for the ADCs.
Definition: tcs3414.hpp:201
static bool configure(const Gain gain=Gain::DEFAULT, const Prescaler prescaler=Prescaler::DEFAULT, const IntegrationMode mode=IntegrationMode::DEFAULT, const SyncPulseCount time=SyncPulseCount::DEFAULT)
Configures some of the most important settings for the sensor.
Definition: tcs3414.hpp:180
static bool configure(const Gain gain=Gain::DEFAULT, const Prescaler prescaler=Prescaler::DEFAULT, const IntegrationMode mode=IntegrationMode::DEFAULT, const NominalIntegrationTime time=NominalIntegrationTime::DEFAULT)
Configures some of the most important settings for the sensor.
Definition: tcs3414.hpp:169
#define RF_CALL_BLOCKING(...)
Definition: resumable/macros.hpp:128
bool initializeBlocking()
Power up sensor and start conversions.
Definition: tcs3414.hpp:162
Definition: i2c_device.hpp:44
Definition: color.hpp:32
uint16_t UnderlyingType
datatype of color values
Definition: tcs3414.hpp:136
Definition: sab/interface_test.cpp:73
High byte of ADC green channel.
Low byte of ADC green channel.
Low byte of ADC green channel.
Low byte of ADC green channel.
High byte of ADC green channel.
Low byte of ADC green channel.
#define modm_packed
Specifies that a variable or structure field should have the smallest possible alignment...
Definition: architecture/utils.hpp:67
High byte of high interrupt threshold.
Low byte of high interrupt threshold.
Low byte of low interrupt threshold.
Integration time control.
Control of basic functions.
RegisterAddress
Register addresses.
Definition: tcs3414.hpp:113
default value on chip reset
integrate over 128 pulses of sync pin
integrate over 64 pulses of sync pin
modm::ResumableResult< bool > setGain(const Gain gain=Gain::DEFAULT, const Prescaler prescaler=Prescaler::DEFAULT)
The gain can be used to adjust the sensitivity of all ADC output channels.
Definition: tcs3414.hpp:191
integrate over 32 pulses of sync pin
integrate over 16 pulses of sync pin
integrate over 8 pulses of sync pin
integrate over 4 pulses of sync pin
High byte of ADC green channel.
integrate over 2 pulses of sync pin
integrate over 1 pulses of sync pin
SyncPulseCount
The number of pulses on sync pin to integrate over.
Definition: tcs3414.hpp:97
NominalIntegrationTime
Integration for a fixed time.
Definition: tcs3414.hpp:88
default value on chip reset
High byte of ADC green channel.
integrate over pulse count pulses of the sync pin
use synchronize signal to integrate over nominal integration time
use the ADC enable bit field in the control register to start and stop integration
integrates with the free-running mode
IntegrationMode
Select mode how to choose the integration time.
Definition: tcs3414.hpp:78
High byte of low interrupt threshold.
default value on chip reset
integrate over 256 pulses of sync pin
Prescaler
Prescaler mode.
Definition: tcs3414.hpp:59
default value on chip reset
Gain
Analog gain control.
Definition: tcs3414.hpp:49
Settings to configure the digital color sensor TCS3414 / TCS3413 / TCS3415 / TCS3416...
Definition: tcs3414.hpp:42