Skip to content

Commit db3cc85

Browse files
committed
Cleaner color sensor Registers
1 parent 295dbc3 commit db3cc85

File tree

3 files changed

+87
-82
lines changed

3 files changed

+87
-82
lines changed

examples/arduino_nano/color/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Sensorthread : public modm::pt::Protothread
4545

4646
MODM_LOG_INFO << "TCS34725 responded" << modm::endl;
4747

48-
PT_CALL(sensor.initialize(modm::tcs3472::Enable::POWER_ON_INTERRUPT_AND_WAITTIME));
48+
PT_CALL(sensor.initialize(sensor.Enable_InterruptMode_Waittime));
4949
PT_CALL(sensor.configure(modm::tcs3472::Gain::X16, modm::tcs3472::IntegrationTime::MSEC_2_4));
5050
PT_CALL(sensor.setInterruptPersistenceFilter(modm::tcs3472::InterruptPersistence::CNT_20));
5151
// Setup WaitTime to further slow down samplerate

src/modm/driver/color/tcs3414.hpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ struct tcs3414
5454
enum class
5555
Prescaler : uint8_t
5656
{
57-
D1 = 0b000, ///< divide by 1
58-
D2 = 0b001, ///< divide by 2
59-
D4 = 0b010, ///< divide by 4
60-
D8 = 0b011, ///< divide by 8
61-
D16 = 0b100, ///< divide by 16
62-
D32 = 0b101, ///< divide by 32
63-
D64 = 0b110, ///< divide by 64
57+
D1 = 0, ///< divide by 1
58+
D2 = 1, ///< divide by 2
59+
D4 = 2, ///< divide by 4
60+
D8 = 3, ///< divide by 8
61+
D16 = 4, ///< divide by 16
62+
D32 = 5, ///< divide by 32
63+
D64 = 6, ///< divide by 64
6464
};
6565

6666
/// Select mode how to choose the integration time
@@ -77,24 +77,24 @@ struct tcs3414
7777
enum class
7878
NominalIntegrationTime : uint8_t
7979
{
80-
MSEC_12 = 0b0000, ///< integrate over 12 ms
81-
MSEC_100 = 0b0001, ///< integrate over 100 ms
82-
MSEC_400 = 0b0010, ///< integrate over 400 ms
80+
MSEC_12 = 0, ///< integrate over 12 ms
81+
MSEC_100 = 1, ///< integrate over 100 ms
82+
MSEC_400 = 2, ///< integrate over 400 ms
8383
};
8484

8585
/// The number of pulses on sync pin to integrate over
8686
enum class
8787
SyncPulseCount : uint8_t
8888
{
89-
PULSES_1 = 0b0000, ///< integrate over 1 pulses of sync pin
90-
PULSES_2 = 0b0001, ///< integrate over 2 pulses of sync pin
91-
PULSES_4 = 0b0010, ///< integrate over 4 pulses of sync pin
92-
PULSES_8 = 0b0011, ///< integrate over 8 pulses of sync pin
93-
PULSES_16 = 0b0100, ///< integrate over 16 pulses of sync pin
94-
PULSES_32 = 0b0101, ///< integrate over 32 pulses of sync pin
95-
PULSES_64 = 0b0110, ///< integrate over 64 pulses of sync pin
96-
PULSES_128 = 0b0111, ///< integrate over 128 pulses of sync pin
97-
PULSES_256 = 0b1000, ///< integrate over 256 pulses of sync pin
89+
PULSES_1 = 0, ///< integrate over 1 pulses of sync pin
90+
PULSES_2 = 1, ///< integrate over 2 pulses of sync pin
91+
PULSES_4 = 2, ///< integrate over 4 pulses of sync pin
92+
PULSES_8 = 3, ///< integrate over 8 pulses of sync pin
93+
PULSES_16 = 4, ///< integrate over 16 pulses of sync pin
94+
PULSES_32 = 5, ///< integrate over 32 pulses of sync pin
95+
PULSES_64 = 6, ///< integrate over 64 pulses of sync pin
96+
PULSES_128 = 7, ///< integrate over 128 pulses of sync pin
97+
PULSES_256 = 8, ///< integrate over 256 pulses of sync pin
9898
};
9999

100100
/// Register addresses

src/modm/driver/color/tcs3472.hpp

Lines changed: 67 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <stdint.h>
1919
#include <modm/architecture/interface/i2c_device.hpp>
20-
#include <modm/math/utils/endianness.hpp>
20+
#include <modm/math/utils.hpp>
2121
#include <modm/ui/color.hpp>
2222

2323
namespace modm
@@ -30,28 +30,52 @@ class Tcs3472;
3030
/// @ingroup modm_driver_tcs3472
3131
struct tcs3472
3232
{
33-
/** Device Address depends on version TCS3472x
34-
*
35-
* - 1 = 0x39 (with IR filter)
36-
* - 3 = 0x39 (without IR filter)
37-
* - 5 = 0x29 (with IR filter)
38-
* - 7 = 0x29 (without IR filter)
39-
*/
40-
static constexpr uint8_t
41-
addr(uint8_t version=5)
42-
{ return version < 5 ? 0x39 : 0x29; }
33+
/// Register addresses
34+
enum class
35+
RegisterAddress : uint8_t
36+
{
37+
ENABLE = 0x00, ///< Power up the device, Enable features
38+
ID = 0x12, ///< Read part-ID
39+
40+
// Shutter configuration
41+
INTEGRATION_TIME = 0x01, ///< Shutter integration time
42+
GAIN = 0x0F, ///< Shutter gain control
43+
44+
// Waittime configuration
45+
WAIT_TIME = 0x03, ///< Waittime between samples. Conceived for power savings
46+
CONFIGURATION = 0x0D, ///< Setting WAITLONG multiplies WAIT_TIME by 12
47+
48+
// Interrupt configuration
49+
LOW_THRESH_LOW_BYTE = 0x04, ///< Low byte of low interrupt threshold
50+
LOW_THRESH_HIGH_BYTE = 0x05, ///< High byte of low interrupt threshold
51+
HIGH_THRESH_LOW_BYTE = 0x06, ///< Low byte of high interrupt threshold
52+
HIGH_THRESH_HIGH_BYTE = 0x07, ///< High byte of high interrupt threshold
53+
INTERRUPT_PERSIST_FILTER = 0x0C,///< Set how much threshold-passing cycles are required, to trigger the interrupt
54+
RELOAD_INTERRUPT = 0x66, ///< Reset Interrupt special command
55+
56+
// Data results
57+
CDATALOW = 0x14, ///< Low byte of ADC clear channel
58+
CDATAHIGH = 0x15, ///< High byte of ADC clear channel
59+
RDATALOW = 0x16, ///< Low byte of ADC red channel
60+
RDATAHIGH = 0x17, ///< High byte of ADC red channel
61+
GDATALOW = 0x18, ///< Low byte of ADC green channel
62+
GDATAHIGH = 0x19, ///< High byte of ADC green channel
63+
BDATALOW = 0x1A, ///< Low byte of ADC blue channel
64+
BDATAHIGH = 0x1B ///< High byte of ADC blue channel
65+
};
4366

67+
public:
4468
/// Analog rgbc gain control
4569
enum class
4670
Gain : uint8_t
4771
{
48-
X1 = 0b00, ///< x1 gain
49-
X4 = 0b01, ///< x4 gain
50-
X16 = 0b10, ///< x16 gain
51-
X60 = 0b11, ///< x60 gain
72+
X1 = 0, ///< x1 gain
73+
X4 = 1, ///< x4 gain
74+
X16 = 2, ///< x16 gain
75+
X60 = 3, ///< x60 gain
5276
};
5377

54-
/// Integration for a fixed time
78+
/// Light integration time in ms
5579
enum class
5680
IntegrationTime : uint8_t
5781
{
@@ -62,6 +86,7 @@ struct tcs3472
6286
MSEC_700 = 0x00, ///< integrate over 700 ms
6387
};
6488

89+
/// Time to wait between samples in ms
6590
enum class
6691
WaitTime : uint8_t
6792
{
@@ -74,10 +99,10 @@ struct tcs3472
7499
enum class
75100
InterruptPersistence : uint8_t
76101
{
77-
EVERY = 0, // Interrupt for every new sample independent of thresholds
78-
CNT_1 = 1, // Interrupt after 1 cycle being below or above thresholds
79-
CNT_2 = 2, // Interrupt after 2 cycles being below or above thresholds
80-
CNT_3 = 3, // ...
102+
EVERY = 0, ///< Interrupt for every new sample independent of thresholds
103+
CNT_1 = 1, ///< Interrupt after 1 cycle being below or above thresholds
104+
CNT_2 = 2, ///< Interrupt after 2 cycles being below or above thresholds
105+
CNT_3 = 3, ///< ...
81106
CNT_5 = 4,
82107
CNT_10 = 5,
83108
CNT_15 = 6,
@@ -96,48 +121,23 @@ struct tcs3472
96121
enum class
97122
Enable : uint8_t
98123
{
99-
POWER_ON = 1,
100-
ADC_ENABLE = 1 << 1,
101-
WAIT_ENABLE = 1 << 3,
102-
INTERRUPT_ENABLE = 1 << 4,
103-
POWER_ON_POLLING = POWER_ON | ADC_ENABLE,
104-
POWER_ON_INTERRUPT = POWER_ON | ADC_ENABLE | INTERRUPT_ENABLE,
105-
POWER_ON_INTERRUPT_AND_WAITTIME = POWER_ON | ADC_ENABLE | WAIT_ENABLE | INTERRUPT_ENABLE
124+
POWER_ON = Bit0,
125+
ADC_ENABLE = Bit1,
126+
WAIT_ENABLE = Bit3,
127+
INTERRUPT_ENABLE = Bit4
106128
};
129+
MODM_FLAGS8(Enable);
107130

108-
/// Register addresses
109-
enum class
110-
RegisterAddress : uint8_t
111-
{
112-
ENABLE = 0x00, ///< Power up the device, Enable features
113-
ID = 0x12, ///< Read part-ID
114-
115-
// Shutter configuration
116-
INTEGRATION_TIME = 0x01, ///< Shutter integration time
117-
GAIN = 0x0F, ///< Shutter gain control
118-
119-
// Waittime configuration
120-
WAIT_TIME = 0x03, ///< Waittime between samples. Conceived for power savings
121-
CONFIGURATION = 0x0D, ///< Setting WAITLONG multiplies WAIT_TIME by 12
122-
123-
// Interrupt configuration
124-
LOW_THRESH_LOW_BYTE = 0x04, ///< Low byte of low interrupt threshold
125-
LOW_THRESH_HIGH_BYTE = 0x05, ///< High byte of low interrupt threshold
126-
HIGH_THRESH_LOW_BYTE = 0x06, ///< Low byte of high interrupt threshold
127-
HIGH_THRESH_HIGH_BYTE = 0x07, ///< High byte of high interrupt threshold
128-
INTERRUPT_PERSIST_FILTER = 0x0C,///< Set how much threshold-passing cycles are required, to trigger the interrupt
129-
RELOAD_INTERRUPT = 0x66, ///< Reset Interrupt special command
130-
131-
// Data results
132-
CDATALOW = 0x14, ///< Low byte of ADC clear channel
133-
CDATAHIGH = 0x15, ///< High byte of ADC clear channel
134-
RDATALOW = 0x16, ///< Low byte of ADC red channel
135-
RDATAHIGH = 0x17, ///< High byte of ADC red channel
136-
GDATALOW = 0x18, ///< Low byte of ADC green channel
137-
GDATAHIGH = 0x19, ///< High byte of ADC green channel
138-
BDATALOW = 0x1A, ///< Low byte of ADC blue channel
139-
BDATAHIGH = 0x1B ///< High byte of ADC blue channel
140-
};
131+
/** Device Address depends on version TCS3472x
132+
*
133+
* - 1 = 0x39 (with IR filter)
134+
* - 3 = 0x39 (without IR filter)
135+
* - 5 = 0x29 (with IR filter)
136+
* - 7 = 0x29 (without IR filter)
137+
*/
138+
static constexpr uint8_t
139+
addr(uint8_t version=5)
140+
{ return version < 5 ? 0x39 : 0x29; }
141141

142142
using Rgb = color::RgbT<uint16_t>;
143143

@@ -205,10 +205,15 @@ class Tcs3472 : public tcs3472, public modm::I2cDevice<I2cMaster, 2>
205205
public:
206206
Tcs3472(Data &data, uint8_t address = addr());
207207

208+
// Typical Runmodes
209+
static constexpr Enable_t Enable_PollingMode = Enable::POWER_ON | Enable::ADC_ENABLE;
210+
static constexpr Enable_t Enable_InterruptMode = Enable_PollingMode | Enable::INTERRUPT_ENABLE;
211+
static constexpr Enable_t Enable_InterruptMode_Waittime = Enable_InterruptMode | Enable::INTERRUPT_ENABLE;
212+
208213
/// Power up sensor and start conversions
209214
modm::ResumableResult<bool>
210-
initialize(Enable flags = Enable::POWER_ON_POLLING)
211-
{ return writeRegister(RegisterAddress::ENABLE, uint8_t(flags)); }
215+
initialize(Enable_t flags = Enable_PollingMode)
216+
{ return writeRegister(RegisterAddress::ENABLE, flags.value); }
212217

213218
modm::ResumableResult<bool>
214219
configure(Gain gain = Gain::X1,

0 commit comments

Comments
 (0)