initial commit
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
// ColorSensor Library for Studuino
|
||||
//
|
||||
#include "ColorSensor.h"
|
||||
#include <Wire.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
COLOR::COLOR() {
|
||||
}
|
||||
|
||||
bool COLOR::testConnection() {
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.write(REG_ID);
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.requestFrom(COLOR_SENSOR_ADDR, 1);
|
||||
if(Wire.available()) {
|
||||
byte msg = Wire.read();
|
||||
// ID register contains PARTNO(upper 4bits) and REVNO(lower 4bits).
|
||||
// TCS3410 and TCS341x's Part No. are 0000 and 0001, respectively.
|
||||
return ((msg & 0xf0) >> 4 == 0x01);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void COLOR::initialize() {
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.write(REG_TIMING);
|
||||
Wire.write(INTEG_MODE_FREE);
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.write(REG_INT_SOURCE);
|
||||
Wire.write(INT_SOURCE_GREEN);
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.write(REG_INT);
|
||||
Wire.write(INTR_LEVEL|INTR_PERSIST_EVERY);
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.write(REG_GAIN);
|
||||
Wire.write(GAIN_1|PRESCALER_4);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
void COLOR::update() {
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.write(REG_CTL);
|
||||
Wire.write(CTL_DAT_INIITIATE);
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.write(REG_BLOCK_READ);
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.requestFrom(COLOR_SENSOR_ADDR,8);
|
||||
if(8<= Wire.available()) {
|
||||
for(byte i = 0; i < 8; i++)
|
||||
{
|
||||
readingdata[i]=Wire.read();
|
||||
//Serial.println(readingdata[i],BIN);
|
||||
}
|
||||
}
|
||||
green = readingdata[1] * 256 + readingdata[0];
|
||||
red = readingdata[3] * 256 + readingdata[2];
|
||||
blue = readingdata[5] * 256 + readingdata[4];
|
||||
clr = readingdata[7] * 256 + readingdata[6];
|
||||
|
||||
X = (-0.14282) * red + (1.54924) * green + (-0.95641) * blue;
|
||||
Y = (-0.32466) * red + (1.57837) * green + (-0.73191) * blue;
|
||||
Z = (-0.68202) * red + (0.77073) * green + (0.56332) * blue;
|
||||
x = X / (X + Y + Z);
|
||||
y = Y / (X + Y + Z);
|
||||
|
||||
Wire.beginTransmission(COLOR_SENSOR_ADDR);
|
||||
Wire.write(CLR_INT);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
// Getting the color code corresponding to Studuino ICON Programming.
|
||||
int COLOR::getColorCode() {
|
||||
byte val = COLOR_UNDEF;
|
||||
|
||||
if (red > MIN_R_WHITE && red < MAX_R_WHITE && green > MIN_G_WHITE && green < MAX_G_WHITE &&
|
||||
blue > MIN_B_WHITE && blue < MAX_B_WHITE && clr > MIN_C_WHITE && clr < MAX_C_WHITE) { // White
|
||||
val = COLOR_WHITE;
|
||||
} else if (red > MIN_R_BLACK && red < MAX_R_BLACK && green > MIN_G_BLACK && green < MAX_G_BLACK &&
|
||||
blue > MIN_B_BLACK && blue < MAX_B_BLACK && clr > MIN_C_BLACK && clr < MAX_C_BLACK) { // Black
|
||||
val = COLOR_BLACK;
|
||||
} else if (red > MIN_R_YELLOW && red < MAX_R_YELLOW && green > MIN_G_YELLOW && green < MAX_G_YELLOW &&
|
||||
blue > MIN_B_YELLOW && blue < MAX_B_YELLOW && clr > MIN_C_YELLOW && clr < MAX_C_YELLOW) { // Yellow
|
||||
val = COLOR_YELLOW;
|
||||
} else if (red > MIN_R_RED && red < MAX_R_RED && green > MIN_G_RED && green < MAX_G_RED &&
|
||||
blue > MIN_B_RED && blue < MAX_B_RED && clr > MIN_C_RED && clr < MAX_C_RED && X > MIN_X_RED) { // Red
|
||||
val = COLOR_RED;
|
||||
if (red < 1350 && clr > 3000) { // Brown
|
||||
val = COLOR_BROWN;
|
||||
}
|
||||
} else if (red > MIN_R_BROWN && red < MAX_R_BROWN && green > MIN_G_BROWN && green < MAX_G_BROWN &&
|
||||
blue > MIN_B_BROWN && blue < MAX_B_BROWN && clr > MIN_C_BROWN && clr < MAX_C_BROWN) { // Brown
|
||||
val = COLOR_BROWN;
|
||||
} else if (red > MIN_R_GREEN && red < MAX_R_GREEN && green > MIN_G_GREEN && green < MAX_G_GREEN &&
|
||||
blue > MIN_B_GREEN && blue < MAX_B_GREEN && clr > MIN_C_GREEN && clr < MAX_C_GREEN) { // Green
|
||||
val = COLOR_GREEN;
|
||||
} else if (red > MIN_R_BLUE && red < MAX_R_BLUE && green > MIN_G_BLUE && green < MAX_G_BLUE &&
|
||||
blue > MIN_B_BLUE && blue < MAX_B_BLUE && clr > MIN_C_BLUE && clr < MAX_C_BLUE) { // Blue
|
||||
val = COLOR_BLUE;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
// Getting sensor value of each filter;
|
||||
unsigned int COLOR::getColorRed() {
|
||||
return red;
|
||||
}
|
||||
unsigned int COLOR::getColorGreen() {
|
||||
return green;
|
||||
}
|
||||
unsigned int COLOR::getColorBlue() {
|
||||
return blue;
|
||||
}
|
||||
unsigned int COLOR::getColorClear() {
|
||||
return clr;
|
||||
}
|
||||
|
||||
// Getting x value of chromaticity coordinate(x, y).
|
||||
double COLOR::getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
// Getting y value of chromaticity coordinate(x, y).
|
||||
double COLOR::getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
#ifndef _COLORSENSOR_H_
|
||||
#define _COLORSENSOR_H_
|
||||
|
||||
#define COLOR_SENSOR_ADDR 0x39//the I2C address for the color sensor
|
||||
#define REG_CTL 0x80
|
||||
#define REG_TIMING 0x81
|
||||
#define REG_INT 0x82
|
||||
#define REG_INT_SOURCE 0x83
|
||||
#define REG_ID 0x84
|
||||
#define REG_GAIN 0x87
|
||||
#define REG_LOW_THRESH_LOW_BYTE 0x88
|
||||
#define REG_LOW_THRESH_HIGH_BYTE 0x89
|
||||
#define REG_HIGH_THRESH_LOW_BYTE 0x8A
|
||||
#define REG_HIGH_THRESH_HIGH_BYTE 0x8B
|
||||
#define REG_BLOCK_READ 0xCF
|
||||
#define REG_GREEN_LOW 0xD0
|
||||
#define REG_GREEN_HIGH 0xD1
|
||||
#define REG_RED_LOW 0xD2
|
||||
#define REG_RED_HIGH 0xD3
|
||||
#define REG_BLUE_LOW 0xD4
|
||||
#define REG_BLUE_HIGH 0xD5
|
||||
#define REG_CLEAR_LOW 0xD6
|
||||
#define REG_CLEAR_HIGH 0xD7
|
||||
#define CTL_DAT_INIITIATE 0x03
|
||||
#define CLR_INT 0xE0
|
||||
//Timing Register
|
||||
#define SYNC_EDGE 0x40
|
||||
#define INTEG_MODE_FREE 0x00
|
||||
#define INTEG_MODE_MANUAL 0x10
|
||||
#define INTEG_MODE_SYN_SINGLE 0x20
|
||||
#define INTEG_MODE_SYN_MULTI 0x30
|
||||
|
||||
#define INTEG_PARAM_PULSE_COUNT1 0x00
|
||||
#define INTEG_PARAM_PULSE_COUNT2 0x01
|
||||
#define INTEG_PARAM_PULSE_COUNT4 0x02
|
||||
#define INTEG_PARAM_PULSE_COUNT8 0x03
|
||||
//Interrupt Control Register
|
||||
#define INTR_STOP 40
|
||||
#define INTR_DISABLE 0x00
|
||||
#define INTR_LEVEL 0x10
|
||||
#define INTR_PERSIST_EVERY 0x00
|
||||
#define INTR_PERSIST_SINGLE 0x01
|
||||
//Interrupt Souce Register
|
||||
#define INT_SOURCE_GREEN 0x00
|
||||
#define INT_SOURCE_RED 0x01
|
||||
#define INT_SOURCE_BLUE 0x10
|
||||
#define INT_SOURCE_CLEAR 0x03
|
||||
//Gain Register
|
||||
#define GAIN_1 0x00
|
||||
#define GAIN_4 0x10
|
||||
#define GAIN_16 0x20
|
||||
#define GANI_64 0x30
|
||||
#define PRESCALER_1 0x00
|
||||
#define PRESCALER_2 0x01
|
||||
#define PRESCALER_4 0x02
|
||||
#define PRESCALER_8 0x03
|
||||
#define PRESCALER_16 0x04
|
||||
#define PRESCALER_32 0x05
|
||||
#define PRESCALER_64 0x06
|
||||
|
||||
// Color code corresponding to Studuino ICON Programming.
|
||||
#define COLOR_UNDEF 0
|
||||
#define COLOR_RED 1
|
||||
#define COLOR_GREEN 2
|
||||
#define COLOR_BLUE 3
|
||||
#define COLOR_WHITE 4
|
||||
#define COLOR_YELLOW 5
|
||||
#define COLOR_BROWN 6
|
||||
#define COLOR_BLACK 7
|
||||
// Color Value Type
|
||||
#define VALUE_RED 0
|
||||
#define VALUE_GREEN 1
|
||||
#define VALUE_BLUE 2
|
||||
#define VALUE_CLEAR 3
|
||||
|
||||
// Definition of color sensor's red value for each block.
|
||||
#define MIN_R_RED 1000
|
||||
#define MAX_R_RED 2400
|
||||
#define MIN_R_BLUE 200
|
||||
#define MAX_R_BLUE 550
|
||||
#define MIN_R_GREEN 200
|
||||
#define MAX_R_GREEN 800
|
||||
#define MIN_R_YELLOW 1500
|
||||
#define MAX_R_YELLOW 4400
|
||||
#define MIN_R_BROWN 700
|
||||
#define MAX_R_BROWN 1900
|
||||
#define MIN_R_WHITE 1700
|
||||
#define MAX_R_WHITE 4900
|
||||
#define MIN_R_BLACK 200
|
||||
#define MAX_R_BLACK 300
|
||||
|
||||
// Definition of color sensor's green value for each block.
|
||||
#define MIN_G_RED 700
|
||||
#define MAX_G_RED 1300
|
||||
#define MIN_G_BLUE 700
|
||||
#define MAX_G_BLUE 1300
|
||||
#define MIN_G_GREEN 1200
|
||||
#define MAX_G_GREEN 2600
|
||||
#define MIN_G_YELLOW 2200
|
||||
#define MAX_G_YELLOW 6000
|
||||
#define MIN_G_BROWN 1000
|
||||
#define MAX_G_BROWN 1900
|
||||
#define MIN_G_WHITE 2800
|
||||
#define MAX_G_WHITE 7700
|
||||
#define MIN_G_BLACK 500
|
||||
#define MAX_G_BLACK 800
|
||||
|
||||
// Definition of color sensor's blue value for each block.
|
||||
#define MIN_B_RED 200
|
||||
#define MAX_B_RED 800
|
||||
#define MIN_B_BLUE 700
|
||||
#define MAX_B_BLUE 1600
|
||||
#define MIN_B_GREEN 500
|
||||
#define MAX_B_GREEN 1100
|
||||
#define MIN_B_YELLOW 700
|
||||
#define MAX_B_YELLOW 1300
|
||||
#define MIN_B_BROWN 500
|
||||
#define MAX_B_BROWN 800
|
||||
#define MIN_B_WHITE 1700
|
||||
#define MAX_B_WHITE 4700
|
||||
#define MIN_B_BLACK 200
|
||||
#define MAX_B_BLACK 550
|
||||
|
||||
// Definition of color sensor's clear value for each block.
|
||||
#define MIN_C_RED 2200
|
||||
#define MAX_C_RED 4400
|
||||
#define MIN_C_BLUE 2000
|
||||
#define MAX_C_BLUE 3650
|
||||
#define MIN_C_GREEN 2500
|
||||
#define MAX_C_GREEN 4900
|
||||
#define MIN_C_YELLOW 4800
|
||||
#define MAX_C_YELLOW 12100
|
||||
#define MIN_C_BROWN 2200
|
||||
#define MAX_C_BROWN 4700
|
||||
#define MIN_C_WHITE 6600
|
||||
#define MAX_C_WHITE 19000
|
||||
#define MIN_C_BLACK 1000
|
||||
#define MAX_C_BLACK 1800
|
||||
|
||||
#define MIN_X_RED 0.44
|
||||
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class COLOR {
|
||||
private:
|
||||
int readingdata[8];
|
||||
unsigned int green, red, blue, clr;
|
||||
double X, Y, Z, x, y, z;
|
||||
void updateValue();
|
||||
public:
|
||||
COLOR();
|
||||
bool testConnection();
|
||||
void initialize(void);
|
||||
void update(void);
|
||||
int getColorCode(void);
|
||||
unsigned int getColorRed();
|
||||
unsigned int getColorGreen();
|
||||
unsigned int getColorBlue();
|
||||
unsigned int getColorClear();
|
||||
double getX(void);
|
||||
double getY(void);
|
||||
};
|
||||
#endif // _COLORSENSOR_H_
|
||||
|
||||
Reference in New Issue
Block a user