MatrixMiniR4 1.1.4
Matrix Mini R4 Arduino Library API Documentation
Loading...
Searching...
No Matches
MiniR4ColorSensorExt.cpp
Go to the documentation of this file.
1
7
9{
10 _pWire->begin();
11 i2cMUXSelect();
12 delay(50);
13 if (i2cReadData(Device_ID) == 0x43) {
14 i2cWriteData(Device_CONFIG, 0x10); // reset
15 delay(500);
16 i2cWriteData(Device_CONFIG, setting); // enable
17 return true;
18 } else {
19 return false;
20 }
21}
22
23void MatrixColor::setGamma(bool state)
24{
25 i2cMUXSelect();
26 if (state) {
27 setting |= 0b00000100;
28 } else {
29 setting &= 0b00001011;
30 }
31 i2cWriteData(Device_CONFIG, setting);
32}
33
34void MatrixColor::setLight(bool state, bool mode, uint8_t pwm)
35{
36 i2cMUXSelect();
37
38 if (state) {
39 setting |= 0b00000010;
40 } else {
41 setting &= 0b00001101;
42 }
43 if (mode) {
44 setting |= 0b00000001;
45 } else {
46 setting &= 0b00001110;
47 }
48 i2cWriteData(Device_CONFIG, setting);
49 i2cWriteData(Device_LIGHT, pwm);
50}
51
52
54{
55 i2cMUXSelect();
56 return i2cReadData(ColorRegType(color));
57}
58
59
61{
62 return i2cReadData(Device_GRAY);
63}
64
66{
67 i2cMUXSelect();
68 return i2cReadData(Device_NUM_COLOR);
69}
70
71uint8_t MatrixColor::i2cReadData(ColorRegType reg)
72{
73
74 _pWire->beginTransmission(MatrixColor_ADDR);
75 _pWire->write(reg);
76 _pWire->endTransmission(1);
77
78 delay(1);
79
80 _pWire->requestFrom(MatrixColor_ADDR, 1);
81
82 delay(1);
83
84 return _pWire->read();
85}
86
87void MatrixColor::i2cMUXSelect()
88{
89 if (_ch < 0) return; // no MUX
90 _pWire->beginTransmission(ADDR_PCA954X);
91 _pWire->write((1 << _ch));
92 _pWire->endTransmission(1);
93 delayMicroseconds(300);
94}
95
96void MatrixColor::i2cWriteData(ColorRegType reg, uint8_t data)
97{
98
99 _pWire->beginTransmission(MatrixColor_ADDR);
100
101 _pWire->write(reg);
102 _pWire->write(data);
103
104 _pWire->endTransmission(1);
105}
Matric Color Sensor functions.
#define ADDR_PCA954X
#define MatrixColor_ADDR
enum __ColorType ColorType
uint8_t getColorNumber()
Gets the number of colors supported by the sensor. Number of color as follow as below: 0:Black,...
bool begin()
Initializes the color sensor.
uint8_t getGrayscale()
Gets the grayscale value from the sensor.
uint8_t getColor(ColorType color)
Gets the value of a specific color from the sensor.
void setLight(bool state, bool mode, uint8_t pwm)
Sets the light state and mode for the sensor.
void setGamma(bool state)
Sets the gamma correction state for the sensor.