Skip to content

Instantly share code, notes, and snippets.

@blindman2k
Last active December 24, 2015 00:49
Show Gist options
  • Save blindman2k/6718938 to your computer and use it in GitHub Desktop.
Save blindman2k/6718938 to your computer and use it in GitHub Desktop.
// RGB sensor code, stripped to basics, to get initialization working...
const i2c_ioexp = 0x7C; // I2C channel base adddreses
const i2c_als = 0xE8; // adjusted to 8 bit values
// Initialise the imp hardware
local i2c = hardware.i2c89;
i2c.configure(CLOCK_SPEED_100_KHZ);
// ------------------------------------------------ rgbdcs register definitions -----------------------------------------------------
const rgbdcsRED = 0; const rgbdcsGREEN = 1; const rgbdcsBLUE = 2; const rgbdcsCLEAR = 3;
const rgbdcsControl = 0x00; const rgbdcsConfig = 0x01;
const rgbdcsCapRed = 0x06; // -? 0x09 (x4) for Red, Green, Blue & Clear
const rgbdcsIntRedLo = 0x0A; const rgbdcsIntRedHi = 0x0B; // -> 0x11 (x4) for Red, Green, Blue & Clear
const rgbdcsDatRedLo = 0x40; const rgbdcsDatRedHi = 0x41; // -> 0x47 (x4) for Red, Green, Blue & Clear
const rgbdcsOffsetRed = 0x48; // -> 0x4B (x4) for Red, Green, Blue & Clear
function InitRGBSensor()
{
local rgbdcsCapas = {}, rgbdcsInteg = null, status = null, data = null, subaddr = null, capas = null, color = null;
// Start by forcing a RESET on the ADJD-S311-CR999 (through RESET from SX1509 on Hannah)
// consecutive 0x12 and 0x34 to RegReset (0x7C)
i2c.write(i2c_ioexp, "\x7C\x12");
i2c.write(i2c_ioexp, "\x7C\x34");
i2c.write(i2c_ioexp, format("%c%c", 0x0E, i2c.read(i2c_ioexp, "\x0E", 1)[0] & ~0x2));
i2c.write(i2c_ioexp, format("%c%c", 0x10, i2c.read(i2c_ioexp, "\x10", 1)[0] & ~0x2));
// Now cycle through colors from RED thru CLEAR
local sensorVal = null;
for (color = rgbdcsRED; color <= rgbdcsCLEAR; color++ ) {
// Now cycle thru all Cap values 0x00 -> 0x0F
for (capas = 0; capas <= 0x0F; capas++ ) {
// Write the Cap register (for a color) with the current Cap value
i2c.write(i2c_als, format("%c%c", rgbdcsCapRed + color, capas));
// Write the Intensity register with my default value (2048 or 0x0800)
i2c.write(i2c_als, format("%c%c%c", rgbdcsIntRedLo + (color * 2), 0x00, 0x08));
i2c.write(i2c_als, "\x00\x01"); // Tell ConTRoL reg we want to read sensor (0x01)
imp.sleep(0.200); // Wait for conversion to finish
status = i2c.read(i2c_als, "\x00", 1); // Check the control register to be ready 0x00
if (status == null) {
server.log("[ERROR] rgbdcs: ConTRoL register read error");
} else {
// Now try to read the sensor
data = i2c.read(i2c_als, format("%c",rgbdcsDatRedLo + (color * 2)), 2); // Read actual raw Lo&Hi values
if (data == null) {
server.log("[ERROR] rgbdcs: Data Register read error");
} else {
sensorVal = ((data[1] & 0x0F) << 4) + data[0];
// WOW, we are below the threshold
if (sensorVal <= 1000) {
rgbdcsCapas[color] <- capas;
capas = 0x0F;
}
}
}
}
if ((capas > 0x0F) && (sensorVal > 1000))
server.log(format("Calibration Error Cap=%d Col=%d Val=%d",capas, color, sensorVal));
}
server.log(format("[DEBUG] Capas R=%d G=%d B=%d C=%d", rgbdcsCapas[0], rgbdcsCapas[1], rgbdcsCapas[2], rgbdcsCapas[3]));
}
function ReadRGBSensor(color) {
i2c.write(i2c_als, "\x00\x01");
imp.sleep(0.050); // Wait for conversion to finish
local data = i2c.read(i2c_als, format("%c",rgbdcsControl), 1);
if (data == null) {
server.log("[ERROR] rgbdcs: error status from ConTRoL (Sensor) read request");
return (ERROR);
}
local byteLo = i2c.read(i2c_als, format("%c",rgbdcsDatRedLo+(color<1)), 1); // Read actual raw LO value
local byteHi = i2c.read(i2c_als, format("%c",rgbdcsDatRedHi+(color<1)), 1); // Read actual raw HI value
return (((byteHi[0] & 0x0F) << 4) + byteLo[0]); // return unsigned 12 bit value
}
function MainLoop()
{
local tempRED = ReadRGBSensor(rgbdcsRED);
local tempGREEN = ReadRGBSensor(rgbdcsGREEN);
local tempBLUE = ReadRGBSensor(rgbdcsGREEN);
local tempCLEAR = ReadRGBSensor(rgbdcsCLEAR);
local Message = format("als: R=%d G=%d B=%d C=%d", tempRED, tempGREEN, tempBLUE, tempCLEAR);
server.log(Message);
imp.wakeup(5, MainLoop); // continue sleep, wake up every xx seconds
}
InitRGBSensor();
MainLoop();
imp.configure("RGB Sensor (stripped)", [], []) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment