modm API documentation for STM32F107VCT7
tcs3472.hpp
Go to the documentation of this file.
1 // coding: utf-8
2 /*
3  * Copyright (c) 2017, Arjun Sarin
4  * Copyright (c) 2017-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 tcs3472.hpp
16  * \date 04 Feb 2017
17  * \author David Hebbeker, Arjun Sarin
18  */
19 
20 #ifndef MODM_TCS3472_HPP
21 #define MODM_TCS3472_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 ams digital color sensor family tcs34721, -13, -15, 17
32  * \see tcs3472
33  * \ingroup modm_driver_tcs3472
34  *
35  * Device Address
36  * tcs34721 0x39 (with IR filter)
37  * 3 0x39 (without IR filter)
38  * 5 0x29 (with IR filter)
39  * 7 0x29 (without IR filter)
40  *
41  */
42 struct tcs3472
43 {
44  /** @name Gain_Register
45  * @{
46  */
47 
48  //! \brief Analog rgbc gain control
49  enum class Gain : uint8_t
50  {
51  X1 = 0b00, //!< x1 gain
52  X4 = 0b01, //!< x4 gain
53  X16 = 0b10, //!< x16 gain
54  X64 = 0b11, //!< x60 gain
55  DEFAULT = 0 //!< default value on chip reset
56  };
57  //! @}
58 
59 
60  //! \brief Integration for a fixed time
61  enum class IntegrationTime : uint8_t
62  {
63  MSEC_2 = 0xFF, //!< integrate over 2.4 ms
64  MSEC_24 = 0xF6, //!< integrate over 100 ms
65  MSEC_101 = 0xD5, //!< integrate over 101 ms
66  MSEC_154 = 0xC0, //!< integrate over 154 ms
67  MSEC_700 = 0x00, //!< integrate over 700 ms
68  DEFAULT = 0xFF //!< default value on chip reset
69  };
70  //! @}
71 
72 
73  //! \brief Register addresses
74  enum class RegisterAddress : uint8_t
75  {
76  ENABLE = 0x00, //!< Primarily to power up the device
77  TIMING = 0x01, //!< Integration time control @see tcs3472::setIntegrationTime
78  //INTERRUPT = 0x02, //!< Interrupt settings
79  //INT_SOURCE = 0x03, //!< Interrupt source
80  ID = 0x12, //!< Part number
81  GAIN = 0x0F, //!< Sensitivity settings @see Tcs3414::setGain
82  // Interrupt threshold registers
83  LOW_THRESH_LOW_BYTE = 0x04, //!< Low byte of low interrupt threshold
84  LOW_THRESH_HIGH_BYTE = 0x05, //!< High byte of low interrupt threshold
85  HIGH_THRESH_LOW_BYTE = 0x06, //!< Low byte of high interrupt threshold
86  HIGH_THRESH_HIGH_BYTE = 0x07, //!< High byte of high interrupt threshold
87  // Data registers
88  CDATALOW = 0x14, //!< Low byte of ADC clear channel
89  CDATAHIGH = 0x15, //!< High byte of ADC clear channel
90  RDATALOW = 0x16, //!< Low byte of ADC red channel
91  RDATAHIGH = 0x17, //!< High byte of ADC red channel
92  GDATALOW = 0x18, //!< Low byte of ADC green channel
93  GDATAHIGH = 0x19, //!< High byte of ADC green channel
94  BDATALOW = 0x1A, //!< Low byte of ADC blue channel
95  BDATAHIGH = 0x1B //!< High byte of ADC blue channel
96  };
97 
98  typedef uint16_t UnderlyingType; //!< datatype of color values
100 
101 };
102 
103 /**
104  * \brief Tcs3472X Digital Color Sensors
105  *
106  * \todo Not all features of the sensors are implemented in this driver
107  * yet.
108  *
109  * \tparam I2CMaster I2C interface which needs an \em initialized
110  * modm::i2c::Master
111  * \see tcs3472
112  * \author David Hebbeker, Arjun Sarin
113  * \ingroup modm_driver_tcs3472
114  */
115 template < typename I2cMaster >
116 class Tcs3472 : public tcs3472, public modm::I2cDevice< I2cMaster, 2 >
117 {
118 public:
119  Tcs3472(uint8_t address = 0x29);
120 
121  //! \brief Power up sensor and start conversions
122  // Blocking
123  bool inline
125  {
126  return RF_CALL_BLOCKING(initialize());
127  }
128 
129  //! \brief The gain can be used to adjust the sensitivity of all ADC output channels.
132  const Gain gain = Gain::DEFAULT)
133  {
134  return writeRegister(RegisterAddress::GAIN,
135  static_cast<uint8_t>(gain));
136  }
137 
138  /**
139  * @name Return already sampled color
140  * @{
141  */
142  inline static Tcs3472::Rgb
143  getOldColors()
144  {
145  return color;
146  };
147 
148  //!@}
149 
150  /**
151  * @name Sample and return fresh color values
152  * @{
153  */
154  inline static Tcs3472::Rgb
155  getNewColors()
156  {
158  return getOldColors();
159  };
160 
161  //!@}
162 
163  //! \brief Read current samples of ADC conversions for all channels.
164  // Non-blocking
167 
168  // MARK: - TASKS
170  initialize()
171  {
172  return writeRegister(RegisterAddress::ENABLE, 0b11); // control to power up and start conversion
173  // note: adafruits driver waits 3ms before writing AEN bit (0b10). we don't??
174  };
175 
177  configure(
178  const Gain gain = Gain::DEFAULT,
179  const uint8_t int_time = IntegrationTime::DEFAULT);
180 
181 private:
182  //! \brief Sets the integration time for the ADCs.
184  setIntegrationTime(const uint8_t int_time = 0)
185  {
186  return writeRegister(
187  RegisterAddress::TIMING,
188  static_cast<uint8_t>(int_time));
189  }
190 
191 private:
192  uint8_t commandBuffer[4];
193  bool success;
194 
195 private:
196  //! \brief Read value of specific register.
198  readRegisters(
199  const RegisterAddress address,
200  uint8_t * const values,
201  const uint8_t count = 1);
202 
204  writeRegister(
205  const RegisterAddress address,
206  const uint8_t value);
207 
208 private:
209  class uint16_t_LOW_HIGH
210  {
211  private:
212  uint8_t low;
213  uint8_t high;
214  public:
215  uint16_t
216  get() const
217  {
218  uint16_t value = low;
219  value |= high << 8;
220  return value;
221  }
222  inline uint8_t getLSB() const { return low; }
223  inline uint8_t getMSB() const { return high; }
224  } modm_packed;
225 
226  static union Data
227  {
228  uint8_t dataBytes[2*4];
229  struct
230  {
231  uint16_t_LOW_HIGH clear;
232  uint16_t_LOW_HIGH red;
233  uint16_t_LOW_HIGH green;
234  uint16_t_LOW_HIGH blue;
235  } modm_packed;
236  } data;
237 
238  static Rgb color;
239 };
240 }
241 
242 #include "tcs3472_impl.hpp"
243 
244 #endif // MODM_tcs3472_HPP
#define modm_packed
Specifies that a variable or structure field should have the smallest possible alignment...
Definition: architecture/utils.hpp:67
Definition: sab/interface_test.cpp:73
modm::ResumableResult< bool > refreshAllColors()
Read current samples of ADC conversions for all channels.
modm::ResumableResult< bool > setGain(const Gain gain=Gain::DEFAULT)
The gain can be used to adjust the sensitivity of all ADC output channels.
Definition: tcs3472.hpp:131
bool initializeBlocking()
Power up sensor and start conversions.
Definition: tcs3472.hpp:124
Definition: color.hpp:32
uint16_t UnderlyingType
datatype of color values
Definition: tcs3472.hpp:98
High byte of ADC blue channel.
Low byte of ADC blue channel.
High byte of ADC green channel.
Low byte of ADC green channel.
High byte of ADC red channel.
Low byte of ADC red channel.
High byte of ADC clear channel.
Low byte of ADC clear channel.
High byte of high interrupt threshold.
Low byte of high interrupt threshold.
High byte of low interrupt threshold.
Low byte of low interrupt threshold.
Integration time control.
Primarily to power up the device.
RegisterAddress
Register addresses.
Definition: tcs3472.hpp:74
#define RF_CALL_BLOCKING(...)
Definition: resumable/macros.hpp:128
default value on chip reset
IntegrationTime
Integration for a fixed time.
Definition: tcs3472.hpp:61
default value on chip reset
Definition: i2c_device.hpp:44
Gain
Analog rgbc gain control.
Definition: tcs3472.hpp:49
Settings to configure the ams digital color sensor family tcs34721, -13, -15, 17...
Definition: tcs3472.hpp:42