IP Configuration

This commit is contained in:
Gašper Dobrovoljc
2023-03-11 15:11:03 +01:00
commit ec125f27db
662 changed files with 103738 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
#include <Wire.h>
#include "Adafruit_TCS34725.h"
// Pick analog outputs, for the UNO these three work well
// use ~560 ohm resistor between Red & Blue, ~1K for green (its brighter)
#define redpin 3
#define greenpin 5
#define bluepin 6
// for a common anode LED, connect the common pin to +5V
// for common cathode, connect the common to ground
// set to false if using a common cathode LED
#define commonAnode true
// our RGB -> eye-recognized gamma color
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
//Serial.println("Color View Test!");
if (tcs.begin()) {
//Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}
// use these three pins to drive an LED
#if defined(ARDUINO_ARCH_ESP32)
ledcAttachPin(redpin, 1);
ledcSetup(1, 12000, 8);
ledcAttachPin(greenpin, 2);
ledcSetup(2, 12000, 8);
ledcAttachPin(bluepin, 3);
ledcSetup(3, 12000, 8);
#else
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
#endif
// thanks PhilB for this gamma table!
// it helps convert RGB colors to what humans see
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
if (commonAnode) {
gammatable[i] = 255 - x;
} else {
gammatable[i] = x;
}
//Serial.println(gammatable[i]);
}
}
// The commented out code in loop is example of getRawData with clear value.
// Processing example colorview.pde can work with this kind of data too, but It requires manual conversion to
// [0-255] RGB value. You can still uncomments parts of colorview.pde and play with clear value.
void loop() {
float red, green, blue;
tcs.setInterrupt(false); // turn on LED
delay(60); // takes 50ms to read
tcs.getRGB(&red, &green, &blue);
tcs.setInterrupt(true); // turn off LED
Serial.print("R:\t"); Serial.print(int(red));
Serial.print("\tG:\t"); Serial.print(int(green));
Serial.print("\tB:\t"); Serial.print(int(blue));
// Serial.print("\t");
// Serial.print((int)red, HEX); Serial.print((int)green, HEX); Serial.print((int)blue, HEX);
Serial.print("\n");
// uint16_t red, green, blue, clear;
//
// tcs.setInterrupt(false); // turn on LED
//
// delay(60); // takes 50ms to read
//
// tcs.getRawData(&red, &green, &blue, &clear);
//
// tcs.setInterrupt(true); // turn off LED
//
// Serial.print("C:\t"); Serial.print(int(clear));
// Serial.print("R:\t"); Serial.print(int(red));
// Serial.print("\tG:\t"); Serial.print(int(green));
// Serial.print("\tB:\t"); Serial.print(int(blue));
// Serial.println();
#if defined(ARDUINO_ARCH_ESP32)
ledcWrite(1, gammatable[(int)red]);
ledcWrite(2, gammatable[(int)green]);
ledcWrite(3, gammatable[(int)blue]);
#else
analogWrite(redpin, gammatable[(int)red]);
analogWrite(greenpin, gammatable[(int)green]);
analogWrite(bluepin, gammatable[(int)blue]);
#endif
}

View File

@@ -0,0 +1,80 @@
/* For use with the colorview Arduino example sketch
Update the Serial() new call to match your serial port
e.g. COM4, /dev/usbserial, etc!
*/
import processing.serial.*;
import java.awt.datatransfer.*;
import java.awt.Toolkit;
Serial port;
void setup(){
size(200,200);
port = new Serial(this, "COM4", 9600); //remember to replace COM20 with the appropriate serial port on your computer
}
String buff = "";
int wRed, wGreen, wBlue, wClear;
String hexColor = "ffffff";
void draw(){
background(wRed,wGreen,wBlue);
// check for serial, and process
while (port.available() > 0) {
serialEvent(port.read());
}
}
void serialEvent(int serial) {
if(serial != '\n') {
buff += char(serial);
} else {
//println(buff);
int cRed = buff.indexOf("R");
int cGreen = buff.indexOf("G");
int cBlue = buff.indexOf("B");
int clear = buff.indexOf("C");
if(clear >=0){
String val = buff.substring(clear+3);
val = val.split("\t")[0];
wClear = Integer.parseInt(val.trim());
} else { return; }
if(cRed >=0){
String val = buff.substring(cRed+3);
val = val.split("\t")[0];
wRed = Integer.parseInt(val.trim());
} else { return; }
if(cGreen >=0) {
String val = buff.substring(cGreen+3);
val = val.split("\t")[0];
wGreen = Integer.parseInt(val.trim());
} else { return; }
if(cBlue >=0) {
String val = buff.substring(cBlue+3);
val = val.split("\t")[0];
wBlue = Integer.parseInt(val.trim());
} else { return; }
print("Red: "); print(wRed);
print("\tGrn: "); print(wGreen);
print("\tBlue: "); print(wBlue);
print("\tClr: "); println(wClear);
wRed *= 255; wRed /= wClear;
wGreen *= 255; wGreen /= wClear;
wBlue *= 255; wBlue /= wClear;
hexColor = hex(color(wRed, wGreen, wBlue), 6);
println(hexColor);
buff = "";
}
}

View File

@@ -0,0 +1,69 @@
#include <Wire.h>
#include <Adafruit_TCS34725.h>
/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
const int interruptPin = 2;
volatile boolean state = false;
//Interrupt Service Routine
void isr()
{
state = true;
}
/* tcs.getRawData() does a delay(Integration_Time) after the sensor readout.
We don't need to wait for the next integration cycle because we receive an interrupt when the integration cycle is complete*/
void getRawData_noDelay(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c)
{
*c = tcs.read16(TCS34725_CDATAL);
*r = tcs.read16(TCS34725_RDATAL);
*g = tcs.read16(TCS34725_GDATAL);
*b = tcs.read16(TCS34725_BDATAL);
}
void setup() {
pinMode(interruptPin, INPUT_PULLUP); //TCS interrupt output is Active-LOW and Open-Drain
attachInterrupt(digitalPinToInterrupt(interruptPin), isr, FALLING);
Serial.begin(9600);
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
// Set persistence filter to generate an interrupt for every RGB Cycle, regardless of the integration limits
tcs.write8(TCS34725_PERS, TCS34725_PERS_NONE);
tcs.setInterrupt(true);
Serial.flush();
}
void loop() {
if (state) {
uint16_t r, g, b, c, colorTemp, lux;
getRawData_noDelay(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);
Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
Serial.println(" ");
Serial.flush();
tcs.clearInterrupt();
state = false;
}
}

View File

@@ -0,0 +1,45 @@
#include <Wire.h>
#include "Adafruit_TCS34725.h"
/* Example code for the Adafruit TCS34725 breakout library */
/* Connect SCL to analog 5
Connect SDA to analog 4
Connect VDD to 3.3V DC
Connect GROUND to common ground */
/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();
/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
void setup(void) {
Serial.begin(9600);
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
// Now we're ready to get readings!
}
void loop(void) {
uint16_t r, g, b, c, colorTemp, lux;
tcs.getRawData(&r, &g, &b, &c);
// colorTemp = tcs.calculateColorTemperature(r, g, b);
colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
lux = tcs.calculateLux(r, g, b);
Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
Serial.println(" ");
}

View File

@@ -0,0 +1,44 @@
#include <Wire.h>
#include "Adafruit_TCS34725.h"
/* Example code for the Adafruit TCS34725 breakout library */
/* Connect SCL to analog 5
Connect SDA to analog 4
Connect VDD to 3.3V DC
Connect GROUND to common ground */
/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();
/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
void setup(void) {
Serial.begin(9600);
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
// Now we're ready to get readings!
}
void loop(void) {
uint16_t r, g, b, c, colorTemp, lux;
tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);
Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
Serial.println(" ");
}

View File

@@ -0,0 +1,211 @@
#include <Wire.h>
#include "Adafruit_TCS34725.h"
//
// An experimental wrapper class that implements the improved lux and color temperature from
// TAOS and a basic autorange mechanism.
//
// Written by ductsoup, public domain
//
// RGB Color Sensor with IR filter and White LED - TCS34725
// I2C 7-bit address 0x29, 8-bit address 0x52
//
// http://www.adafruit.com/product/1334
// http://learn.adafruit.com/adafruit-color-sensors/overview
// http://www.adafruit.com/datasheets/TCS34725.pdf
// http://www.ams.com/eng/Products/Light-Sensors/Color-Sensor/TCS34725
// http://www.ams.com/eng/content/view/download/265215 <- DN40, calculations
// http://www.ams.com/eng/content/view/download/181895 <- DN39, some thoughts on autogain
// http://www.ams.com/eng/content/view/download/145158 <- DN25 (original Adafruit calculations)
//
// connect LED to digital 4 or GROUND for ambient light sensing
// connect SCL to analog 5
// connect SDA to analog 4
// connect Vin to 3.3-5V DC
// connect GROUND to common ground
// some magic numbers for this device from the DN40 application note
#define TCS34725_R_Coef 0.136
#define TCS34725_G_Coef 1.000
#define TCS34725_B_Coef -0.444
#define TCS34725_GA 1.0
#define TCS34725_DF 310.0
#define TCS34725_CT_Coef 3810.0
#define TCS34725_CT_Offset 1391.0
// Autorange class for TCS34725
class tcs34725 {
private:
struct tcs_agc {
tcs34725Gain_t ag;
tcs34725IntegrationTime_t at;
uint16_t mincnt;
uint16_t maxcnt;
};
static const tcs_agc agc_lst[];
uint16_t agc_cur;
void setGainTime(void);
Adafruit_TCS34725 tcs;
public:
tcs34725(void);
boolean begin(void);
void getData(void);
boolean isAvailable, isSaturated;
uint16_t againx, atime, atime_ms;
uint16_t r, g, b, c;
uint16_t ir;
uint16_t r_comp, g_comp, b_comp, c_comp;
uint16_t saturation, saturation75;
float cratio, cpl, ct, lux, maxlux;
};
//
// Gain/time combinations to use and the min/max limits for hysteresis
// that avoid saturation. They should be in order from dim to bright.
//
// Also set the first min count and the last max count to 0 to indicate
// the start and end of the list.
//
const tcs34725::tcs_agc tcs34725::agc_lst[] = {
{ TCS34725_GAIN_60X, TCS34725_INTEGRATIONTIME_700MS, 0, 20000 },
{ TCS34725_GAIN_60X, TCS34725_INTEGRATIONTIME_154MS, 4990, 63000 },
{ TCS34725_GAIN_16X, TCS34725_INTEGRATIONTIME_154MS, 16790, 63000 },
{ TCS34725_GAIN_4X, TCS34725_INTEGRATIONTIME_154MS, 15740, 63000 },
{ TCS34725_GAIN_1X, TCS34725_INTEGRATIONTIME_154MS, 15740, 0 }
};
tcs34725::tcs34725() : agc_cur(0), isAvailable(0), isSaturated(0) {
}
// initialize the sensor
boolean tcs34725::begin(void) {
tcs = Adafruit_TCS34725(agc_lst[agc_cur].at, agc_lst[agc_cur].ag);
if ((isAvailable = tcs.begin()))
setGainTime();
return(isAvailable);
}
// Set the gain and integration time
void tcs34725::setGainTime(void) {
tcs.setGain(agc_lst[agc_cur].ag);
tcs.setIntegrationTime(agc_lst[agc_cur].at);
atime = int(agc_lst[agc_cur].at);
atime_ms = ((256 - atime) * 2.4);
switch(agc_lst[agc_cur].ag) {
case TCS34725_GAIN_1X:
againx = 1;
break;
case TCS34725_GAIN_4X:
againx = 4;
break;
case TCS34725_GAIN_16X:
againx = 16;
break;
case TCS34725_GAIN_60X:
againx = 60;
break;
}
}
// Retrieve data from the sensor and do the calculations
void tcs34725::getData(void) {
// read the sensor and autorange if necessary
tcs.getRawData(&r, &g, &b, &c);
while(1) {
if (agc_lst[agc_cur].maxcnt && c > agc_lst[agc_cur].maxcnt)
agc_cur++;
else if (agc_lst[agc_cur].mincnt && c < agc_lst[agc_cur].mincnt)
agc_cur--;
else break;
setGainTime();
delay((256 - atime) * 2.4 * 2); // shock absorber
tcs.getRawData(&r, &g, &b, &c);
break;
}
// DN40 calculations
ir = (r + g + b > c) ? (r + g + b - c) / 2 : 0;
r_comp = r - ir;
g_comp = g - ir;
b_comp = b - ir;
c_comp = c - ir;
cratio = float(ir) / float(c);
saturation = ((256 - atime) > 63) ? 65535 : 1024 * (256 - atime);
saturation75 = (atime_ms < 150) ? (saturation - saturation / 4) : saturation;
isSaturated = (atime_ms < 150 && c > saturation75) ? 1 : 0;
cpl = (atime_ms * againx) / (TCS34725_GA * TCS34725_DF);
maxlux = 65535 / (cpl * 3);
lux = (TCS34725_R_Coef * float(r_comp) + TCS34725_G_Coef * float(g_comp) + TCS34725_B_Coef * float(b_comp)) / cpl;
ct = TCS34725_CT_Coef * float(b_comp) / float(r_comp) + TCS34725_CT_Offset;
}
tcs34725 rgb_sensor;
void setup(void) {
Serial.begin(115200);
rgb_sensor.begin();
pinMode(4, OUTPUT);
digitalWrite(4, LOW); // @gremlins Bright light, bright light!
}
void loop(void) {
rgb_sensor.getData();
Serial.print(F("Gain:"));
Serial.print(rgb_sensor.againx);
Serial.print(F("x "));
Serial.print(F("Time:"));
Serial.print(rgb_sensor.atime_ms);
Serial.print(F("ms (0x"));
Serial.print(rgb_sensor.atime, HEX);
Serial.println(F(")"));
Serial.print(F("Raw R:"));
Serial.print(rgb_sensor.r);
Serial.print(F(" G:"));
Serial.print(rgb_sensor.g);
Serial.print(F(" B:"));
Serial.print(rgb_sensor.b);
Serial.print(F(" C:"));
Serial.println(rgb_sensor.c);
Serial.print(F("IR:"));
Serial.print(rgb_sensor.ir);
Serial.print(F(" CRATIO:"));
Serial.print(rgb_sensor.cratio);
Serial.print(F(" Sat:"));
Serial.print(rgb_sensor.saturation);
Serial.print(F(" Sat75:"));
Serial.print(rgb_sensor.saturation75);
Serial.print(F(" "));
Serial.println(rgb_sensor.isSaturated ? "*SATURATED*" : "");
Serial.print(F("CPL:"));
Serial.print(rgb_sensor.cpl);
Serial.print(F(" Max lux:"));
Serial.println(rgb_sensor.maxlux);
Serial.print(F("Compensated R:"));
Serial.print(rgb_sensor.r_comp);
Serial.print(F(" G:"));
Serial.print(rgb_sensor.g_comp);
Serial.print(F(" B:"));
Serial.print(rgb_sensor.b_comp);
Serial.print(F(" C:"));
Serial.println(rgb_sensor.c_comp);
Serial.print(F("Lux:"));
Serial.print(rgb_sensor.lux);
Serial.print(F(" CT:"));
Serial.print(rgb_sensor.ct);
Serial.println(F("K"));
Serial.println();
delay(2000);
}