/
test_adafruit_color_sensor_lisandro.cpp
152 lines (113 loc) · 3.46 KB
/
test_adafruit_color_sensor_lisandro.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Compile with:
// g++ test_adafruit_color_sensor_lisandro.cpp -o test_adafruit_color_sensor_lisandro -lmraa
// Controls carousel depending on color of block.
#include "mraa.hpp"
#include <cassert>
#include <csignal>
#include <iostream>
#define SensorAddress 0x29 //
#define EnableAddress 0xa0 // register address + command bits
#define ATimeAddress 0xa1 // register address + command bits
#define ControlAddress 0xaf // register address + command bits
#define IDAddress 0xb2 // register address + command bits
#define ColorAddress 0xb4 // register address + command bits
/*Individual Color Addresses
#define RedLowAddress 0x16
#define RedHighddress 0x17
#define BlueLowAddress 0x1A
#define BlueHighAddress 0x1B
#define GreenLowAddress 0x18
#define GreenHighAddress 0x19
*/
int running = 1;
#define MS 1000
//Initialize Registers
uint8_t timee[2];
uint8_t gain[2];
uint8_t enable[2];
uint8_t colors[2];
/*
uint8_t red_low[2];
uint8_t red_high[2];
uint8_t blue_low[2];
uint8_t blue_high[2];
uint8_t green_low[2];
uint8_t green_high[2];
*/
void sig_handler(int signo) {
if (signo == SIGINT) {
printf("closing spi nicely\n");
running = 0;
}
}
void init_TCS34725(mraa::I2c *i2c) {
//Pass in Addresses and Data to Registers
timee[0] = ATimeAddress;
timee[1] = 0xf6;
gain[0] = ControlAddress;
gain[1] = 0x00;
enable[0] = EnableAddress;
enable[1] = 0x03;
assert(0 == i2c->address(SensorAddress));
i2c->write(timee, 2);
i2c->write(gain, 2);
i2c->write(enable, 2);
/*mraa::printError(i2c->write(timee, 2));
mraa::printError(i2c->write(gain, 2));
mraa::printError(i2c->write(enable, 2));
*/
}
void get_Colors(mraa::I2c *i2c){
unsigned int clear_color = 0;
unsigned int red_color = 0;
unsigned int green_color = 0;
unsigned int blue_color = 0;
int i = 0;
colors[0] = ColorAddress;
colors[1] = 0x16;
std::cout << i2c->address(SensorAddress) << std::endl;
// i2c->writeByte(ColorAddress);
// for (int i = 0; i<9; i++){
uint8_t returnData = i2c->readReg(ColorAddress);
printf("Data:%d\n", returnData);
// }
/*
uint8_t color_values[8];
for (i = 0; i<8; i++){
color_values[i] = i2c->read(color_values, 1);
}
printf("color values%d\n", color_values);
clear_color = (unsigned int)(color_values[1]<<8) + (unsigned int)color_values[0];
red_color = (unsigned int)(color_values[3]<<8) + (unsigned int)color_values[2];
green_color = (unsigned int)(color_values[5]<<8) + (unsigned int)color_values[4];
blue_color = (unsigned int)(color_values[7]<<8) + (unsigned int)color_values[6];
printf("clear_color:%d\n", (int)clear_color);
printf("red_color:%d\n", (int)red_color);
printf("blue_color%d\n", (int)blue_color);
printf("green_color%d\n", (int)green_color);
/*
i2c->address(SensorAddress);
uint16_t clear_value = i2c->readWordReg(ColorAddress);
//printf("uint%d", color_value);
i2c->address(SensorAddress);
uint16_t red_value = i2c->readWordReg(ColorAddress + 2);
//printf("uint%d", color_value);
i2c->address(SensorAddress);
uint16_t blue_value = i2c->readWordReg(ColorAddress + 4);
//printf("uint%d", color_value);
i2c->address(SensorAddress);
uint16_t green_value = i2c->readWordReg(ColorAddress + 6);
//printf("uint%d", color_value);
*/
}
int main(){
// Handle Ctrl-C quit
signal(SIGINT, sig_handler);
// Edison i2c bus is 6
mraa::I2c *i2c = new mraa::I2c(6);
assert(i2c != NULL);
init_TCS34725(i2c);
while (running){
get_Colors(i2c);
}
}