moved GY33_MCU library to own repository
moved GY33_MCU library to own repository
This commit is contained in:
@@ -1,224 +0,0 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file GY33_MCU.h
|
||||
@author BPoHVoodoo (FabLab Luenen)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018,
|
||||
All rights reserved.
|
||||
|
||||
Driver for the GY-33 MCU digital color sensors.
|
||||
|
||||
@section HISTORY
|
||||
|
||||
v1.0 - First release
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "GY33_MCU.h"
|
||||
|
||||
/*========================================================================*/
|
||||
/* PRIVATE FUNCTIONS */
|
||||
/*========================================================================*/
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Implements missing powf function
|
||||
*/
|
||||
/**************************************************************************/
|
||||
float powf(const float x, const float y)
|
||||
{
|
||||
return (float)(pow((double)x, (double)y));
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Writes a register and an 8 bit value over I2C
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t GY33_MCU::write8 (uint8_t reg, uint8_t val)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
brzo_i2c_start_transaction(MCU_ADDRESS, SCL_SPEED);
|
||||
buf[0]=reg;
|
||||
buf[1]=val;
|
||||
brzo_i2c_write(buf, 2, false);
|
||||
return brzo_i2c_end_transaction();
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Reads an 8 bit value over I2C
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t GY33_MCU::read8(uint8_t reg)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
brzo_i2c_start_transaction(MCU_ADDRESS, SCL_SPEED);
|
||||
buf[0]=reg;
|
||||
brzo_i2c_write(buf,1, true);
|
||||
brzo_i2c_read(buf, 1, false);
|
||||
brzo_i2c_end_transaction();
|
||||
return buf[0];
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Reads a 16 bit values over I2C
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint16_t GY33_MCU::read16(uint8_t reg)
|
||||
{
|
||||
uint16_t x; uint16_t t;
|
||||
uint8_t buf[2];
|
||||
brzo_i2c_start_transaction(MCU_ADDRESS, SCL_SPEED);
|
||||
buf[0]=reg;
|
||||
brzo_i2c_write(buf, 1, true);
|
||||
brzo_i2c_read(buf, 2, false);
|
||||
brzo_i2c_end_transaction();
|
||||
x = buf[0];
|
||||
t = buf[1];
|
||||
x <<= 8;
|
||||
x |= t;
|
||||
return buf[0] << 8 | buf[1];
|
||||
}
|
||||
|
||||
|
||||
/*========================================================================*/
|
||||
/* CONSTRUCTORS */
|
||||
/*========================================================================*/
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
Constructor
|
||||
*/
|
||||
/**************************************************************************/
|
||||
GY33_MCU::GY33_MCU()
|
||||
{
|
||||
_MCUInitialised = false;
|
||||
}
|
||||
|
||||
/*========================================================================*/
|
||||
/* PUBLIC FUNCTIONS */
|
||||
/*========================================================================*/
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
Initializes I2C and configures the sensor (call this function before
|
||||
doing anything else)
|
||||
*/
|
||||
/**************************************************************************/
|
||||
boolean GY33_MCU::begin(void)
|
||||
{
|
||||
brzo_i2c_setup(SDA, SCL, SCL_STRETCH_TIMEOUT);
|
||||
|
||||
/* Make sure we're actually connected */
|
||||
uint8_t x = read8(MCU_CONFIG);
|
||||
Serial.println(x, HEX);
|
||||
if (x != 0x10)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_MCUInitialised = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Reads the raw red, green, blue and clear channel values
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void GY33_MCU::getRawData (uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c, uint16_t *lux, uint16_t *ct)
|
||||
{
|
||||
if (!_MCUInitialised) begin();
|
||||
|
||||
*r = read16(MCU_RDATAH);
|
||||
*g = read16(MCU_GDATAH);
|
||||
*b = read16(MCU_BDATAH);
|
||||
*c = read16(MCU_CDATAH);
|
||||
*lux = read16(MCU_LDATAH);
|
||||
*ct = read16(MCU_CTDATAH);
|
||||
}
|
||||
|
||||
|
||||
void GY33_MCU::getData (uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *c, uint8_t *conf)
|
||||
{
|
||||
if (!_MCUInitialised) begin();
|
||||
|
||||
*r = read8(MCU_RDATA);
|
||||
*g = read8(MCU_GDATA);
|
||||
*b = read8(MCU_BDATA);
|
||||
*c = read8(MCU_COLDATA);
|
||||
*conf = read8(MCU_CONFIG);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Converts the raw R/G/B values to color temperature in degrees
|
||||
Kelvin
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint16_t GY33_MCU::calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b)
|
||||
{
|
||||
float X, Y, Z; /* RGB to XYZ correlation */
|
||||
float xc, yc; /* Chromaticity co-ordinates */
|
||||
float n; /* McCamy's formula */
|
||||
float cct;
|
||||
|
||||
/* 1. Map RGB values to their XYZ counterparts. */
|
||||
/* Based on 6500K fluorescent, 3000K fluorescent */
|
||||
/* and 60W incandescent values for a wide range. */
|
||||
/* Note: Y = Illuminance or lux */
|
||||
X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
|
||||
Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
|
||||
Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b);
|
||||
|
||||
/* 2. Calculate the chromaticity co-ordinates */
|
||||
xc = (X) / (X + Y + Z);
|
||||
yc = (Y) / (X + Y + Z);
|
||||
|
||||
/* 3. Use McCamy's formula to determine the CCT */
|
||||
n = (xc - 0.3320F) / (0.1858F - yc);
|
||||
|
||||
/* Calculate the final CCT */
|
||||
cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
|
||||
|
||||
/* Return the results in degrees Kelvin */
|
||||
return (uint16_t)cct;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Converts the raw R/G/B values to lux
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint16_t GY33_MCU::calculateLux(uint16_t r, uint16_t g, uint16_t b)
|
||||
{
|
||||
float illuminance;
|
||||
|
||||
/* This only uses RGB ... how can we integrate clear or calculate lux */
|
||||
/* based exclusively on clear since this might be more reliable? */
|
||||
illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
|
||||
|
||||
return (uint16_t)illuminance;
|
||||
}
|
||||
|
||||
void GY33_MCU::setConfig(uint8_t high, uint8_t low) {
|
||||
Serial.println("GY-33: ");
|
||||
Serial.println(high | low, HEX);
|
||||
write8(MCU_CONFIG, high | low);
|
||||
}
|
||||
uint8_t GY33_MCU::getConfig(void)
|
||||
{
|
||||
if (!_MCUInitialised) begin();
|
||||
|
||||
return read8(MCU_CONFIG);
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
#include <brzo_i2c.h> //https://github.com/pasko-zh/brzo_i2c
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file GY33_MCU.h
|
||||
@author BPoHVoodoo (FabLab Luenen)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018,
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
#ifndef _MCU_H_
|
||||
#define _MCU_H_
|
||||
|
||||
#include <brzo_i2c.h> // https://github.com/pasko-zh/brzo_i2c
|
||||
|
||||
#define MCU_ADDRESS (0x5A)
|
||||
|
||||
#define SCL_SPEED 100
|
||||
#define SCL_STRETCH_TIMEOUT 50000
|
||||
|
||||
#define MCU_LED_OFF (0xA0)
|
||||
#define MCU_LED_01 (0x90)
|
||||
#define MCU_LED_02 (0x80)
|
||||
#define MCU_LED_03 (0x70)
|
||||
#define MCU_LED_04 (0x60)
|
||||
#define MCU_LED_05 (0x50)
|
||||
#define MCU_LED_06 (0x40)
|
||||
#define MCU_LED_07 (0x30)
|
||||
#define MCU_LED_08 (0x20)
|
||||
#define MCU_LED_09 (0x10)
|
||||
#define MCU_LED_10 (0x00)
|
||||
#define MCU_WHITE_OFF (0x00) /* No Whitebalance */
|
||||
#define MCU_WHITE_ON (0x01) /* Whitebalance */
|
||||
|
||||
#define MCU_RDATAH (0x00) /* Raw Red channel data */
|
||||
#define MCU_RDATAL (0x01)
|
||||
#define MCU_GDATAH (0x02) /* Raw Green channel data */
|
||||
#define MCU_GDATAL (0x03)
|
||||
#define MCU_BDATAH (0x04) /* Raw Blue channel data */
|
||||
#define MCU_BDATAL (0x05)
|
||||
#define MCU_CDATAH (0x06) /* Clear channel data */
|
||||
#define MCU_CDATAL (0x07)
|
||||
#define MCU_LDATAH (0x08) /* Lux channel data */
|
||||
#define MCU_LDATAL (0x09)
|
||||
#define MCU_CTDATAH (0x0A) /* Colortemperature channel data */
|
||||
#define MCU_CTDATAL (0x0B)
|
||||
#define MCU_RDATA (0x0C) /* Red channel data */
|
||||
#define MCU_GDATA (0x0D) /* Green channel data */
|
||||
#define MCU_BDATA (0x0E) /* Blue channel data */
|
||||
#define MCU_COLDATA (0x0F) /* Blue channel data */
|
||||
#define MCU_CONFIG (0x10) /* Config channel data */
|
||||
|
||||
class GY33_MCU {
|
||||
public:
|
||||
GY33_MCU();
|
||||
|
||||
boolean begin(void);
|
||||
void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c, uint16_t *lux, uint16_t *ct);
|
||||
void getData(uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *c, uint8_t *conf);
|
||||
uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b);
|
||||
uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b);
|
||||
uint8_t write8 (uint8_t reg, uint8_t val);
|
||||
uint8_t read8 (uint8_t reg);
|
||||
uint16_t read16 (uint8_t reg);
|
||||
void setConfig(uint8_t h, uint8_t l);
|
||||
uint8_t getConfig(void);
|
||||
private:
|
||||
boolean _MCUInitialised;
|
||||
|
||||
void disable(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user