Merge 6b4faa3c69ec0a3a022c10a4cd7a04ee8dde6bd9 into c2ce3e8222ae33e52c1d7da75ced1ca0f7fc9d71

This commit is contained in:
Issam 2018-08-06 08:24:38 +00:00 committed by GitHub
commit 449c9af52d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 2331 additions and 0 deletions

69
examples/c++/bh1749.cxx Executable file
View File

@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <unistd.h>
#include <stdexcept>
#include <signal.h>
#include <vector>
#include "bh1749.hpp"
#include "upm_utilities.h"
bool isStopped;
upm::BH1749 dev;
void signal_int_handler(int signo)
{
if (signo == SIGINT)
isStopped = true;
}
void PrintData(void *args)
{
std::vector<uint16_t> result = dev.GetMeasurements();
std::cout << "R: " << result.at(0) <<
", G: " << result.at(1) <<
", B: " << result.at(2) <<
", IR: " << result.at(3) <<
", G2: " << result.at(4) << std::endl;
dev.ResetInterrupt();
}
int main(int argc, char **argv)
{
signal(SIGINT, signal_int_handler);
dev.SoftReset();
dev.SensorInit(INT_JUDGE_1, MEAS_240MS, RGB_GAIN_1X, IR_GAIN_1X, RED);
dev.SetThresholdHigh(511);
std::cout << "Installing ISR" << std::endl;
dev.InstallISR(MRAA_GPIO_EDGE_FALLING, 33, &PrintData, NULL);
dev.EnableInterrupt();
dev.Enable();
while(!isStopped) {
upm_delay_ms(1000);
}
return 0;
}

73
examples/c/bh1749.c Executable file
View File

@ -0,0 +1,73 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <signal.h>
#include "bh1749.h"
#include "upm_utilities.h"
bool isStopped;
#define SENSOR_ADDR 0x39
#define I2C_BUS 0
void signal_int_handler(int signo)
{
if (signo == SIGINT)
isStopped = true;
}
void print_data(void *dev)
{
uint16_t result[5];
bh1749_get_measurements((bh1749_context)dev, result);
printf("R: %d, G: %d, B: %d, IR: %d, G2: %d\n", result[0],
result[1], result[2], result[3], result[4]);
bh1749_reset_interrupt(dev);
}
int main(int argc, char **argv)
{
signal(SIGINT, signal_int_handler);
bh1749_context dev = bh1749_init(I2C_BUS, SENSOR_ADDR);
if (!dev) {
printf("bh1749_init() failed.\n");
return -1;
}
bh1749_soft_reset(dev);
bh1749_sensor_init(dev, INT_JUDGE_1, MEAS_240MS, RGB_GAIN_1X, IR_GAIN_1X, RED);
bh1749_set_threshold_high(dev, 511);
bh1749_enable_interrupt(dev);
printf("Installing ISR\n");
bh1749_install_isr(dev, MRAA_GPIO_EDGE_FALLING, 33, &print_data, (void *)dev);
bh1749_enable(dev);
while(!isStopped) {
upm_delay_ms(1000);
}
bh1749_close(dev);
return 0;
}

View File

@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class BH1749_Example {
public static void main(String[] args) throws InterruptedException {
int sampleCounter = 10;
long waitTime;
upm_bh1749.BH1749 bh1749;
upm_bh1749.uint16Vector result;
bh1749 = new upm_bh1749.BH1749();
bh1749.SensorInit(upm_bh1749.OPERATING_MODES.INT_JUDGE_1,
upm_bh1749.MEAS_TIMES.MEAS_240MS, upm_bh1749.RGB_GAINS.RGB_GAIN_1X,
upm_bh1749.IR_GAINS.IR_GAIN_1X, upm_bh1749.INT_SOURCES.RED);
bh1749.SetThresholdHigh(511);
bh1749.Enable();
waitTime = bh1749.GetMeasurementTime();
System.out.println("Color readings");
while (sampleCounter-- > 0) {
result = bh1749.GetMeasurements();
System.out.println("R: " + result.get(0) + ", G: " + result.get(1) +
", B: " + result.get(2) + ", IR: " + result.get(3) +
", G2: " + result.get(4));
Thread.sleep((long) waitTime);
}
}
}

View File

@ -74,6 +74,7 @@ add_example(Adxl345_Example adxl345)
add_example(AM2315_Example am2315) add_example(AM2315_Example am2315)
add_example(APA102_Example apa102) add_example(APA102_Example apa102)
add_example(Apds9002_Example apds9002) add_example(Apds9002_Example apds9002)
add_example(BH1749_Example bh1749)
add_example(BH1750_Example bh1750) add_example(BH1750_Example bh1750)
add_example(BISS0001_Example biss0001) add_example(BISS0001_Example biss0001)
add_example(BMA250E_Example bma250e) add_example(BMA250E_Example bma250e)

57
examples/javascript/bh1749.js Executable file
View File

@ -0,0 +1,57 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var bh1749 = require("jsupm_bh1749");
var bh1749_sensor = new bh1749.BH1749();
bh1749_sensor.SensorInit(
bh1749.INT_JUDGE_1,
bh1749.MEAS_240MS,
bh1749.RGB_GAIN_1X,
bh1749.IR_GAIN_1X,
bh1749.RED);
bh1749_sensor.SetThresholdHigh(511);
bh1749_sensor.Enable();
var waitTime = bh1749_sensor.GetMeasurementTime();
var counter = 10;
console.log("Color readings: ");
var interval = setInterval(function() {
data = bh1749_sensor.GetMeasurements();
console.log(
"R: " + data.get(0) + ", G: " + data.get(1) + ", B: " + data.get(2) +
", IR: " + data.get(3) + ", G2: " + data.get(4)
);
counter--;
if (counter == 0) {
clearInterval(interval);
}
}, waitTime);
process.on("SIGINT", function() {
clearInterval(interval);
console.log("Exiting...");
process.exit(0);
});

60
examples/python/bh1749.py Executable file
View File

@ -0,0 +1,60 @@
# The MIT License (MIT)
#
# Author: Assam Boudjelthia
# Copyright (c) 2018 Rohm Semiconductor.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import time, sys, signal, atexit
from upm import pyupm_bh1749
def main():
bh1749_sensor = pyupm_bh1749.BH1749()
bh1749_sensor.SensorInit(pyupm_bh1749.INT_JUDGE_1,
pyupm_bh1749.MEAS_240MS,
pyupm_bh1749.RGB_GAIN_1X,
pyupm_bh1749.IR_GAIN_1X,
pyupm_bh1749.RED)
bh1749_sensor.SetThresholdHigh(511)
# Prevent stack printing on CTRL^C
def SIGINTHandler(signum, frame):
raise SystemExit
def exitHandler():
print("Exiting")
sys.exit(0)
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
sampleCounter = 10
waitTime = bh1749_sensor.GetMeasurementTime()
print("Color readings: ")
while sampleCounter > 0:
[r, g, b, ir, g2] = bh1749_sensor.GetMeasurements()
print ("R: %d, y: %d, G: %d, IR: %d, G2: %d" % (r, g, b, ir, g2))
time.sleep(waitTime / 1000.0)
sampleCounter -= 1
if __name__ == '__main__':
main()

8
src/bh1749/CMakeLists.txt Executable file
View File

@ -0,0 +1,8 @@
upm_mixed_module_init (NAME bh1749
DESCRIPTION "ROHM Color Sensor"
C_HDR bh1749.h bh1749_registers.h
C_SRC bh1749.c
CPP_HDR bh1749.hpp
CPP_SRC bh1749.cxx
CPP_WRAPS_C
REQUIRES mraa)

702
src/bh1749/bh1749.c Executable file
View File

@ -0,0 +1,702 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include "bh1749.h"
#define DEFAULT_OP_MODE INT_JUDGE_1
#define DEFAULT_MEAS_TIME MEAS_240MS
#define DEFAULT_RGB_GAIN RGB_GAIN_1X
#define DEFAULT_IR_GAIN IR_GAIN_1X
#define DEFAULT_INT_SOURCE RED
#define DEFUALT_THRESHOLD_HIGH 511
/**
* @brief Reads the value of a register
*
* @param dev The sensor context
* @param reg Register address
* @param data Pointer of uint8_t to save register value
* @return UPM result
*/
static upm_result_t bh1749_read_register(bh1749_context dev, uint8_t reg, uint8_t *data)
{
uint8_t value = mraa_i2c_read_byte_data(dev->i2c, reg);
if(value < 0) {
printf("%s: mraa_i2c_read_byte_data() failed\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
*data = value;
return UPM_SUCCESS;
}
/**
* @brief Reads the values of a set of registers
*
* @param dev The sensor context
* @param reg Register address
* @param data Pointer of uint8_t to save registers values
* @return UPM result
*/
static upm_result_t bh1749_read_registers(bh1749_context dev, uint8_t reg, uint8_t *data, uint8_t len)
{
uint8_t value;
for(int i = 0; i < len; i++) {
if(bh1749_read_register(dev, reg + i, &value) != UPM_SUCCESS) {
return UPM_ERROR_OPERATION_FAILED;
}
*(data + i) = value;
}
return UPM_SUCCESS;
}
/**
* @brief Writes a value to a register
*
* @param dev The sensor context
* @param value Value to write
* @param reg Register address
* @return UPM result
*/
static upm_result_t bh1749_write_register(bh1749_context dev, uint8_t value, uint8_t reg)
{
if (mraa_i2c_write_byte_data(dev->i2c, value, reg) != MRAA_SUCCESS) {
printf("%s: mraa_i2c_write_byte_data() failed\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
return UPM_SUCCESS;
}
/**
* @brief Sets a bit on in a register with a bit mask
*
* @param dev The sensor context
* @param reg Register address
* @param bit_mask Mask to use
* @return UPM result
*/
static upm_result_t bh1749_set_bit_on(bh1749_context dev, uint8_t reg, uint8_t bit_mask)
{
uint8_t reg_value;
int status;
status = bh1749_read_register(dev, reg, &reg_value);
if(status < 0) {
printf("%s: Failed to read register 0x%2X\n", __FUNCTION__, reg);
return UPM_ERROR_OPERATION_FAILED;
}
reg_value |= bit_mask;
return bh1749_write_register(dev, reg_value, reg);
}
/**
* @brief Sets a bit off in a register with a bit mask
*
* @param dev The sensor context
* @param reg Register address
* @param bit_mask Mask to use
* @return UPM result
*/
static upm_result_t bh1749_set_bit_off(bh1749_context dev, uint8_t reg, uint8_t bit_mask)
{
uint8_t reg_value;
int status;
status = bh1749_read_register(dev, reg, &reg_value);
if(status < 0) {
printf("%s: Failed to read register 0x%2X\n", __FUNCTION__, reg);
return UPM_ERROR_OPERATION_FAILED;
}
reg_value &= ~bit_mask;
return bh1749_write_register(dev, reg_value, reg);
}
/**
* @brief Sets the value of a register as a given pattern with a bit mask
*
* @param dev The sensor context
* @param reg Register address
* @param value Value to write
* @param bit_mask Mask to use
* @return UPM result
*/
static upm_result_t bh1749_set_bits_with_mask(bh1749_context dev, uint8_t reg, uint8_t value, uint8_t bit_mask)
{
uint8_t reg_value;
int status;
status = bh1749_read_register(dev, reg, &reg_value);
if(status < 0) {
printf("%s: Failed to read register 0x%2X\n", __FUNCTION__, reg);
return UPM_ERROR_OPERATION_FAILED;
}
reg_value &= ~bit_mask;
reg_value |= value;
return bh1749_write_register(dev, reg_value, reg);
}
upm_result_t bh1749_check_who_am_i(bh1749_context dev)
{
uint8_t partId;
if(bh1749_read_register(dev, BH1749_SYSTEM_CONTROL, &partId) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
partId &= BH1749_SYSTEM_CONTROL_PART_MASK;
if(partId != BH1749_SYSTEM_CONTROL_PART_ID) {
printf("%s: wrong manufacturer ID\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
return UPM_SUCCESS;
}
bh1749_context bh1749_init(int bus, int addr)
{
bh1749_context dev = (bh1749_context)malloc(sizeof(struct _bh1749_context));
if (!dev)
return NULL;
dev->i2c = NULL;
dev->interrupt = NULL;
if (mraa_init() != MRAA_SUCCESS) {
printf("%s: mraa_init() failed.\n", __FUNCTION__);
bh1749_close(dev);
return NULL;
}
if (!(dev->i2c = mraa_i2c_init(bus))) {
printf("%s: mraa_i2c_init() failed.\n", __FUNCTION__);
bh1749_close(dev);
return NULL;
}
if (mraa_i2c_address(dev->i2c, addr)) {
printf("%s: mraa_i2c_address() failed.\n", __FUNCTION__);
bh1749_close(dev);
return NULL;
}
if(bh1749_check_who_am_i(dev) != UPM_SUCCESS)
return NULL;
dev->enabled = false;
dev->isrEnabled = false;
if(bh1749_sensor_init(dev, DEFAULT_OP_MODE, DEFAULT_MEAS_TIME,
DEFAULT_RGB_GAIN, DEFAULT_IR_GAIN, DEFAULT_INT_SOURCE) != UPM_SUCCESS)
return NULL;
bh1749_set_threshold_high(dev, DEFUALT_THRESHOLD_HIGH);
return dev;
}
void bh1749_close(bh1749_context dev)
{
if(dev->isrEnabled)
bh1749_remove_isr(dev);
if (dev->i2c)
mraa_i2c_stop(dev->i2c);
free(dev);
}
upm_result_t bh1749_enable(bh1749_context dev)
{
int status;
status = bh1749_set_bit_on(dev, BH1749_MODE_CONTROL2,
BH1749_MODE_CONTROL2_RGB_MEASUREMENT_MASK);
if(status != UPM_SUCCESS) {
printf("%s: Failed to bh1749_enable RGB measurement\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->enabled = true;
return UPM_SUCCESS;
}
upm_result_t bh1749_disable(bh1749_context dev)
{
int status;
status = bh1749_set_bit_off(dev, BH1749_MODE_CONTROL2,
BH1749_MODE_CONTROL2_RGB_MEASUREMENT_MASK);
if(status != UPM_SUCCESS) {
printf("%s: Failed to bh1749_disable RGB measurement\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->enabled = false;
return UPM_SUCCESS;
}
upm_result_t bh1749_sensor_init(bh1749_context dev, OPERATING_MODES opMode,
MEAS_TIMES measTime,
RGB_GAINS rgbGain,
IR_GAINS irGain,
INT_SOURCES intSource)
{
if(bh1749_set_operating_mode(dev, opMode) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
if(bh1749_set_measurement_time(dev, measTime) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
if(bh1749_set_rgb_gain(dev, rgbGain) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
if(bh1749_set_ir_gain(dev, irGain) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
return bh1749_set_int_source(dev, intSource);
}
upm_result_t bh1749_set_operating_mode(bh1749_context dev, OPERATING_MODES opMode)
{
if(bh1749_write_register(dev, opMode, BH1749_PERSISTENCE) != UPM_SUCCESS) {
printf("%s: Setting operating mode failed\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->operating_mode = opMode;
return UPM_SUCCESS;
}
upm_result_t bh1749_get_operating_mode(bh1749_context dev, uint8_t *opMode)
{
uint8_t value;
if(bh1749_read_register(dev, BH1749_PERSISTENCE, &value) != UPM_SUCCESS) {
printf("%s: Failed to read operating mode\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
value &= BH1749_PERSISTENCE_MODE_MASK;
if(value != INT_JUDGE_0 && value != INT_JUDGE_1 &&
value != INT_JUDGE_4 && value != INT_JUDGE_4) {
printf("%s: Returned invalid mode\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
*opMode = value;
return UPM_SUCCESS;
}
upm_result_t bh1749_set_measurement_time(bh1749_context dev, MEAS_TIMES measTime)
{
if(bh1749_set_bits_with_mask(dev, BH1749_MODE_CONTROL1, measTime,
BH1749_MODE_CONTROL1_ODR_MASK) != UPM_SUCCESS) {
printf("%s: Failed setting measurement time\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->meas_time = measTime;
return UPM_SUCCESS;
}
upm_result_t bh1749_get_measurement_time(bh1749_context dev, uint8_t *meas_time)
{
uint8_t time_reg;
if(bh1749_read_register(dev, BH1749_MODE_CONTROL1, &time_reg) != UPM_SUCCESS) {
printf("%s: Failed to read measurement time\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
time_reg &= BH1749_MODE_CONTROL1_ODR_MASK;
if(time_reg == MEAS_35MS)
*meas_time = 35;
else if(time_reg == MEAS_120MS)
*meas_time = 120;
else if(time_reg == MEAS_240MS)
*meas_time = 240;
else {
printf("%s: Returned invalid time\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
return UPM_SUCCESS;
}
upm_result_t bh1749_set_rgb_gain(bh1749_context dev, RGB_GAINS rgbGain)
{
if(bh1749_set_bits_with_mask(dev, BH1749_MODE_CONTROL1, rgbGain,
BH1749_MODE_CONTROL1_RGB_GAIN_MASK) != UPM_SUCCESS) {
printf("%s: Failed setting RGB gain\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->rgb_gain = rgbGain;
return UPM_SUCCESS;
}
upm_result_t bh1749_get_rgb_gain(bh1749_context dev, uint8_t *gain)
{
uint8_t rgb_gain;
if(bh1749_read_register(dev, BH1749_MODE_CONTROL1, &rgb_gain) != UPM_SUCCESS) {
printf("%s: Failed to read rgb gain\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
rgb_gain &= BH1749_MODE_CONTROL1_RGB_GAIN_MASK;
if(rgb_gain == RGB_GAIN_1X)
*gain = 1;
else if(rgb_gain == RGB_GAIN_32X)
*gain = 32;
else {
printf("%s: Returned invalid gain\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
return UPM_SUCCESS;
}
upm_result_t bh1749_set_ir_gain(bh1749_context dev, IR_GAINS irGain)
{
if(bh1749_set_bits_with_mask(dev, BH1749_MODE_CONTROL1, irGain,
BH1749_MODE_CONTROL1_IR_GAIN_MASK) != UPM_SUCCESS) {
printf("%s: Failed setting IR gain\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->ir_gain = irGain;
return UPM_SUCCESS;
}
upm_result_t bh1749_get_ir_gain(bh1749_context dev, uint8_t *gain)
{
uint8_t ir_gain;
if(bh1749_read_register(dev, BH1749_MODE_CONTROL1, &ir_gain) != UPM_SUCCESS) {
printf("%s: Failed to read rgb gain\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
ir_gain &= BH1749_MODE_CONTROL1_IR_GAIN_MASK;
if(ir_gain == IR_GAIN_1X)
*gain = 1;
else if(ir_gain == IR_GAIN_32X)
*gain = 32;
else {
printf("%s: Returned invalid gain\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
return UPM_SUCCESS;
}
upm_result_t bh1749_set_int_source(bh1749_context dev, INT_SOURCES intSource)
{
if(bh1749_set_bits_with_mask(dev, BH1749_INTERRUPT, intSource,
BH1749_INTERRUPT_SOURCE_MASK) != UPM_SUCCESS) {
printf("%s: Failed setting interrupt source gain\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->int_src = intSource;
return UPM_SUCCESS;
}
char bh1749_get_interrupt_source_char(bh1749_context dev)
{
char intSourceChar = ' ';
uint8_t intSource;
if(bh1749_read_register(dev, BH1749_INTERRUPT, &intSource) != UPM_SUCCESS) {
printf("%s: Failed to read interrupt source\n", __FUNCTION__);
return intSourceChar;
}
intSource &= BH1749_INTERRUPT_SOURCE_MASK;
if(intSource == RED)
intSourceChar = 'r';
else if(intSource == GREEN)
intSourceChar = 'g';
else if(intSource == BLUE)
intSourceChar = 'b';
else {
printf("%s: Returned invalid interrupt source\n", __FUNCTION__);
return intSourceChar;
}
return intSourceChar;
}
upm_result_t bh1749_enable_interrupt(bh1749_context dev)
{
if(bh1749_set_bit_on(dev, BH1749_INTERRUPT, BH1749_INTERRUPT_EN_MASK) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
return bh1749_reset_interrupt(dev);
}
upm_result_t bh1749_disable_interrupt(bh1749_context dev)
{
return bh1749_set_bit_off(dev, BH1749_INTERRUPT, BH1749_INTERRUPT_EN_MASK);
}
upm_result_t bh1749_reset_interrupt(bh1749_context dev)
{
return bh1749_set_bit_on(dev, BH1749_SYSTEM_CONTROL, BH1749_SYSTEM_CONTROL_INT_RESET);
}
bool bh1749_is_interrupted(bh1749_context dev)
{
uint8_t intStatus;
if(bh1749_read_register(dev, BH1749_INTERRUPT, &intStatus) != UPM_SUCCESS) {
printf("%s: Failed to read interrupt status\n", __FUNCTION__);
return false;
}
intStatus &= BH1749_INTERRUPT_STATUS_MASK;
if(intStatus != BH1749_INTERRUPT_STATUS_ACTIVE)
return false;
return true;
}
bool bh1749_is_interrupt_enabled(bh1749_context dev)
{
uint8_t intStatus;
if(bh1749_read_register(dev, BH1749_INTERRUPT, &intStatus) != UPM_SUCCESS) {
printf("%s: Failed to read interrupt enabled\n", __FUNCTION__);
return false;
}
intStatus &= BH1749_INTERRUPT_EN_MASK;
if(intStatus != BH1749_INTERRUPT_EN_ENABLE)
return false;
return true;
}
upm_result_t bh1749_soft_reset(bh1749_context dev)
{
return bh1749_set_bit_on(dev, BH1749_SYSTEM_CONTROL, BH1749_SYSTEM_CONTROL_SW_RESET_MASK);
}
upm_result_t bh1749_set_threshold_high(bh1749_context dev, uint16_t threshold)
{
if(bh1749_write_register(dev, threshold, BH1749_TH_LSBS) != UPM_SUCCESS ||
bh1749_write_register(dev, threshold >> 8, BH1749_TH_MSBS) != UPM_SUCCESS) {
printf("%s: Failed to write high threshold\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->int_thh = threshold;
return UPM_SUCCESS;
}
upm_result_t bh1749_get_threshold_high(bh1749_context dev, uint16_t *threshold)
{
uint8_t data[2];
if(bh1749_read_register(dev, BH1749_TH_MSBS, &data[0]) ||
bh1749_read_register(dev, BH1749_TH_LSBS, &data[1]) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
*threshold = data[0] << 8 | data[1];
return UPM_SUCCESS;
}
upm_result_t bh1749_set_threshold_low(bh1749_context dev, uint16_t threshold)
{
if(bh1749_write_register(dev, threshold, BH1749_TL_LSBS) != UPM_SUCCESS ||
bh1749_write_register(dev, threshold >> 8, BH1749_TL_MSBS) != UPM_SUCCESS) {
printf("%s: Failed to write low threshold\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->int_thl = threshold;
return UPM_SUCCESS;
}
upm_result_t bh1749_get_threshold_low(bh1749_context dev, uint16_t *threshold)
{
uint8_t data[2];
if(bh1749_read_register(dev, BH1749_TL_MSBS, &data[0]) ||
bh1749_read_register(dev, BH1749_TL_LSBS, &data[1]) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
*threshold = data[0] << 8 | data[1];
return UPM_SUCCESS;
}
upm_result_t bh1749_get_red(bh1749_context dev, uint16_t *red)
{
uint8_t data[2];
if(bh1749_read_register(dev, BH1749_RED_DATA_MSBS, &data[0]) != UPM_SUCCESS ||
bh1749_read_register(dev, BH1749_RED_DATA_LSBS, &data[1]) != UPM_SUCCESS)
return -UPM_ERROR_OPERATION_FAILED;
*red = data[0] << 8 | data[1];
return UPM_SUCCESS;
}
upm_result_t bh1749_get_green(bh1749_context dev, uint16_t *green)
{
uint8_t data[2];
if(bh1749_read_register(dev, BH1749_GREEN_DATA_MSBS, &data[0]) != UPM_SUCCESS ||
bh1749_read_register(dev, BH1749_GREEN_DATA_LSBS, &data[1]) != UPM_SUCCESS)
return -UPM_ERROR_OPERATION_FAILED;
*green = data[0] << 8 | data[1];
return UPM_SUCCESS;
}
upm_result_t bh1749_get_blue(bh1749_context dev, uint16_t *blue)
{
uint8_t data[2];
if(bh1749_read_register(dev, BH1749_BLUE_DATA_MSBS, &data[0]) != UPM_SUCCESS ||
bh1749_read_register(dev, BH1749_BLUE_DATA_LSBS, &data[1]) != UPM_SUCCESS)
return -UPM_ERROR_OPERATION_FAILED;
*blue = data[0] << 8 | data[1];
return UPM_SUCCESS;
}
upm_result_t bh1749_get_ir(bh1749_context dev, uint16_t *ir)
{
uint8_t data[2];
if(bh1749_read_register(dev, BH1749_IR_DATA_MSBS, &data[0]) != UPM_SUCCESS ||
bh1749_read_register(dev, BH1749_IR_DATA_LSBS, &data[1]) != UPM_SUCCESS)
return -UPM_ERROR_OPERATION_FAILED;
*ir = data[0] << 8 | data[1];
return UPM_SUCCESS;
}
upm_result_t bh1749_get_green2(bh1749_context dev, uint16_t *green2)
{
uint8_t data[2];
if(bh1749_read_register(dev, BH1749_GREEN2_DATA_MSBS, &data[0]) != UPM_SUCCESS ||
bh1749_read_register(dev, BH1749_GREEN2_DATA_LSBS, &data[1]) != UPM_SUCCESS)
return -UPM_ERROR_OPERATION_FAILED;
*green2 = data[0] << 8 | data[1];
return UPM_SUCCESS;
}
upm_result_t bh1749_get_measurements(bh1749_context dev, uint16_t *result)
{
uint16_t value;
int step = 0;
if(bh1749_get_red(dev, &value) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
*(result + step++) = value;
if(bh1749_get_green(dev, &value) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
*(result + step++) = value;
if(bh1749_get_blue(dev, &value) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
*(result + step++) = value;
if(bh1749_get_ir(dev, &value) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
*(result + step++) = value;
if(bh1749_get_green2(dev, &value) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
*(result + step++) = value;
return UPM_SUCCESS;
}
upm_result_t bh1749_install_isr(bh1749_context dev, mraa_gpio_edge_t edge, int pin,
void (*isr)(void *), void *isr_args)
{
if(dev->isrEnabled)
bh1749_remove_isr(dev);
mraa_gpio_context isr_gpio = NULL;
if (!(isr_gpio = mraa_gpio_init(pin))) {
printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
mraa_gpio_dir(isr_gpio, MRAA_GPIO_IN);
if (mraa_gpio_isr(isr_gpio, edge, isr, isr_args) != MRAA_SUCCESS) {
mraa_gpio_close(isr_gpio);
printf("%s: mraa_gpio_isr() failed.\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->interrupt = isr_gpio;
dev->isrEnabled = true;
return UPM_SUCCESS;
}
void bh1749_remove_isr(bh1749_context dev)
{
mraa_gpio_isr_exit(dev->interrupt);
mraa_gpio_close(dev->interrupt);
dev->interrupt = NULL;
dev->isrEnabled = false;
}
upm_result_t bh1749_registers_dump(bh1749_context dev, char *dump)
{
uint8_t reg_values[10];
int count = 0;
int len = 3;
if(bh1749_read_registers(dev, BH1749_SYSTEM_CONTROL, reg_values, len) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
count += sprintf(dump, "0x40 ");
for(int i = 0; i < len; i++)
count += sprintf(dump + count, "%02X ", reg_values[i]);
sprintf(dump + count - 1, "\n");
len = 6;
if(bh1749_read_registers(dev, BH1749_INTERRUPT, reg_values, len) != UPM_SUCCESS)
return UPM_ERROR_OPERATION_FAILED;
count += sprintf(dump + count, "0x60 ");
for(int i = 0; i < len; i++)
count += sprintf(dump + count, "%02X ", reg_values[i]);
return UPM_SUCCESS;
}

304
src/bh1749/bh1749.cxx Executable file
View File

@ -0,0 +1,304 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <string>
#include <stdexcept>
#include "bh1749.hpp"
using namespace upm;
void BH1749::CheckWhoAmI()
{
if(bh1749_check_who_am_i(m_bh1749) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_check_who_am_i() failed");
}
BH1749::BH1749(int bus, int addr) : m_bh1749(bh1749_init(bus, addr))
{
if(!m_bh1749)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_init() failed");
}
BH1749::~BH1749()
{
bh1749_close(m_bh1749);
}
void BH1749::Enable()
{
if(bh1749_enable(m_bh1749) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_enable() failed");
}
void BH1749::Disable()
{
if(bh1749_disable(m_bh1749) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_disable() failed");
}
void BH1749::SensorInit(OPERATING_MODES opMode,
MEAS_TIMES measTime,
RGB_GAINS rgbGain,
IR_GAINS irGain,
INT_SOURCES intSource)
{
if(bh1749_sensor_init(m_bh1749, opMode, measTime, rgbGain, irGain, intSource) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_sensor_init() failed");
}
void BH1749::SetOperatingMode(OPERATING_MODES opMode)
{
if(bh1749_set_operating_mode(m_bh1749, opMode) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_set_operating_mode() failed");
}
int BH1749::GetOperatingMode()
{
uint8_t opMode;
if(bh1749_get_operating_mode(m_bh1749, &opMode) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_operating_mode() failed");
return opMode;
}
void BH1749::SetMeasurementTime(MEAS_TIMES measTime)
{
if(bh1749_set_measurement_time(m_bh1749, measTime) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_set_measurement_time() failed");
}
int BH1749::GetMeasurementTime()
{
uint8_t measTime;
if(bh1749_get_measurement_time(m_bh1749, &measTime) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_measurement_time() failed");
return measTime;
}
void BH1749::SetRgbGain(RGB_GAINS rgbGain)
{
if(bh1749_set_rgb_gain(m_bh1749, rgbGain) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_set_rgb_gain() failed");
}
int BH1749::GetRgbGain()
{
uint8_t rgbGain;
if(bh1749_get_rgb_gain(m_bh1749, &rgbGain) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_rgb_gain() failed");
return rgbGain;
}
void BH1749::SetIrGain(IR_GAINS irGain)
{
if(bh1749_set_ir_gain(m_bh1749, irGain) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_set_ir_gain() failed");
}
int BH1749::GetIrGain()
{
uint8_t irGain;
if(bh1749_get_ir_gain(m_bh1749, &irGain) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_ir_gain() failed");
return irGain;
}
void BH1749::SetIntSource(INT_SOURCES intSource)
{
if(bh1749_set_int_source(m_bh1749, intSource) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_set_int_source() failed");
}
char BH1749::GetInterruptSourceChar()
{
char intSource = bh1749_get_interrupt_source_char(m_bh1749);
if( intSource == ' ')
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_interrupt_source_char() failed");
return intSource;
}
void BH1749::EnableInterrupt()
{
if(bh1749_enable_interrupt(m_bh1749) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_enable_interrupt() failed");
}
void BH1749::DisableInterrupt()
{
if(bh1749_disable_interrupt(m_bh1749) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_disable_interrupt() failed");
}
void BH1749::ResetInterrupt()
{
if(bh1749_reset_interrupt(m_bh1749) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_reset_interrupt() failed");
}
bool BH1749::IsInterrupted()
{
return bh1749_is_interrupted(m_bh1749);
}
bool BH1749::IsInterruptEnabled()
{
return bh1749_is_interrupt_enabled(m_bh1749);
}
void BH1749::SoftReset()
{
if(bh1749_soft_reset(m_bh1749) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_soft_reset() failed");
}
void BH1749::SetThresholdHigh(uint16_t threshold)
{
if(bh1749_set_threshold_high(m_bh1749, threshold) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_set_threshold_high() failed");
}
int BH1749::GetThresholdHigh()
{
uint16_t threhold;
if(bh1749_get_threshold_high(m_bh1749, &threhold) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_threshold_high() failed");
return threhold;
}
void BH1749::SetThresholdLow(uint16_t threshold)
{
if(bh1749_set_threshold_low(m_bh1749, threshold) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_set_threshold_low() failed");
}
int BH1749::GetThresholdLow()
{
uint16_t threhold;
if(bh1749_get_threshold_low(m_bh1749, &threhold) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_threshold_low() failed");
return threhold;
}
uint16_t BH1749::GetRed()
{
uint16_t red;
if(bh1749_get_red(m_bh1749, &red) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_red() failed");
return red;
}
uint16_t BH1749::GetGreen()
{
uint16_t green;
if(bh1749_get_green(m_bh1749, &green) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_green() failed");
return green;
}
uint16_t BH1749::GetBlue()
{
uint16_t blue;
if(bh1749_get_blue(m_bh1749, &blue) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_blue() failed");
return blue;
}
uint16_t BH1749::GetIr()
{
uint16_t ir;
if(bh1749_get_ir(m_bh1749, &ir) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_ir() failed");
return ir;
}
uint16_t BH1749::GetGeen2()
{
uint16_t green2;
if(bh1749_get_green2(m_bh1749, &green2) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_green2() failed");
return green2;
}
std::vector<uint16_t> BH1749::GetMeasurements()
{
uint16_t res[5];
if(bh1749_get_measurements(m_bh1749, res) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_get_measurements() failed");
std::vector<uint16_t> result(res, res + sizeof res / sizeof res[0]);
return result;
}
void BH1749::InstallISR(mraa_gpio_edge_t edge, int pin, void (*isr)(void *), void *isr_args)
{
if(bh1749_install_isr(m_bh1749, edge, pin, isr, isr_args) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_install_isr() failed");
}
void BH1749::RemoveISR()
{
bh1749_remove_isr(m_bh1749);
}
std::string BH1749::RegistersDump()
{
char dump[255];
std::string dumpStr;
if(bh1749_registers_dump(m_bh1749, dump) != UPM_SUCCESS)
throw std::runtime_error(std::string(__FUNCTION__) +
"bh1749_registers_dump() failed");
dumpStr = dump;
return dumpStr;
}

437
src/bh1749/bh1749.h Executable file
View File

@ -0,0 +1,437 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <mraa/i2c.h>
#include <mraa/gpio.h>
#include <unistd.h>
#include <upm_types.h>
#include "bh1749_registers.h"
/**
* @brief C API for the bh1749 color sensor driver
* @defgroup bh1749 libupm-bh1749
* @ingroup ROHM i2c color
*/
/**
* @library bh1749
* @sensor bh1749
* @comname ROHM Color Sensor
* @type color
* @man ROHM
* @con i2c
*
* @brief C API for the bh1749 driver
*/
/**
* @brief Operation modes enum for interrupt modes (persistance)
*/
typedef enum {
INT_JUDGE_0 = BH1749_PERSISTENCE_MODE_STATUS_ACTIVE_AFTER_MEASUREMENT,
INT_JUDGE_1 = BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_MEASUREMENT,
INT_JUDGE_4 = BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_4_SAME,
INT_JUDGE_8 = BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_8_SAME,
} OPERATING_MODES;
/**
* @brief Measuremnt time choices
*/
typedef enum {
MEAS_35MS = BH1749_MODE_CONTROL1_ODR_28P6,
MEAS_120MS = BH1749_MODE_CONTROL1_ODR_8P333,
MEAS_240MS = BH1749_MODE_CONTROL1_ODR_4P167
} MEAS_TIMES;
/**
* @brief RGB gain choices
*/
typedef enum {
RGB_GAIN_1X = BH1749_MODE_CONTROL1_RGB_GAIN_1X,
RGB_GAIN_32X = BH1749_MODE_CONTROL1_RGB_GAIN_32X
} RGB_GAINS;
/**
* @brief IR gain choices
*/
typedef enum {
IR_GAIN_1X = BH1749_MODE_CONTROL1_IR_GAIN_1X,
IR_GAIN_32X = BH1749_MODE_CONTROL1_IR_GAIN_32X
} IR_GAINS;
/**
* @brief Interrupt source choices
*/
typedef enum {
RED = BH1749_INTERRUPT_SOURCE_SELECT_RED,
GREEN = BH1749_INTERRUPT_SOURCE_SELECT_GREEN,
BLUE = BH1749_INTERRUPT_SOURCE_SELECT_BLUE
} INT_SOURCES;
/**
* @brief The full sensor context
*/
typedef struct _bh1749_context
{
mraa_i2c_context i2c;
mraa_gpio_context interrupt;
bool enabled;
bool isrEnabled;
uint16_t int_thh;
uint16_t int_thl;
INT_SOURCES int_src;
IR_GAINS ir_gain;
RGB_GAINS rgb_gain;
MEAS_TIMES meas_time;
OPERATING_MODES operating_mode;
} *bh1749_context;
/**
* @brief Check "who am I" register value to identify the sensor
*
* @param dev Sensor context
* @return UPM result
*/
upm_result_t bh1749_check_who_am_i(bh1749_context dev);
/**
* @brief Init the sensor with specific bus and address. This function calls
* the sensor_init() function to set default values for operating mode, gains,
* measurement time, interrupt source and then sets threshold high to 511.
*
* @param bus I2C bus number
* @param addr I2C sensor address
* @return context of initialized sensor
*/
bh1749_context bh1749_init(int bus, int addr);
/**
* @brief Close and free sensor context
*
* @param dev Sensor context
*/
void bh1749_close(bh1749_context dev);
/**
* @brief Enables RGB color measurement on the sensor
*
* @param dev Sensor context
* @return UPM result
*/
upm_result_t bh1749_enable(bh1749_context dev);
/**
* @brief Disables RGB color measurement on the sensor
*
* @param dev Sensor context
* @return UPM result
*/
upm_result_t bh1749_disable(bh1749_context dev);
/**
* @brief Initializes (writes) configuration values to sensor
*
* @param dev Sensor context
* @param opMode Operating mode choice, a value of OPERATING_MODES enum
* @param measTime Measurement time choice, a value of MEAS_TIMES enum
* @param rgbGain RGB gain choice, a value of RGB_GAINS enum
* @param irGain IR gain choice, a value of IR_GAINS enum
* @param intSource interrupt source choice, a value of INT_SOURCES enum
* @return UPM result
*/
upm_result_t bh1749_sensor_init(bh1749_context dev, OPERATING_MODES opMode,
MEAS_TIMES measTime,
RGB_GAINS rgbGain,
IR_GAINS irGain,
INT_SOURCES intSource);
/**
* @brief Sets operating mode (interrupt persistance)
*
* @param dev Sensor context
* @param opMode Operating mode choice, a value of OPERATING_MODES enum
* @return UPM result
*/
upm_result_t bh1749_set_operating_mode(bh1749_context dev, OPERATING_MODES opMode);
/**
* @brief Gets operating mode (interrupt persistance) value
*
* @param dev Sensor context
* @param opMode pointer (uint8_t) to save value
* @return UPM result
*/
upm_result_t bh1749_get_operating_mode(bh1749_context dev, uint8_t *opMode);
/**
* @brief Sets measurement time (ODR)
*
* @param dev Sensor context
* @param measTime measurement time choice, a value of MEAS_TIMES enum
* @return UPM result
*/
upm_result_t bh1749_set_measurement_time(bh1749_context dev, MEAS_TIMES measTime);
/**
* @brief Sets measurement time (ODR)
*
* @param dev Sensor context
* @param meas_time pointer (uint8_t) to save value
* @return UPM result
*/
upm_result_t bh1749_get_measurement_time(bh1749_context dev, uint8_t *meas_time);
/**
* @brief Sets RGB gain values
*
* @param dev Sensor context
* @param rgbGain RGB gain choice, a value of RGB_GAINS enum
* @return UPM result
*/
upm_result_t bh1749_set_rgb_gain(bh1749_context dev, RGB_GAINS rgbGain);
/**
* @brief Gets RGB gain value
*
* @param dev Sensor context
* @param gain pointer (uint8_t) to save value
* @return UPM result
*/
upm_result_t bh1749_get_rgb_gain(bh1749_context dev, uint8_t *gain);
/**
* @brief Sets IR gain values
*
* @param dev Sensor context
* @param irGain IR gain choice, a value of IR_GAINS enum
* @return UPM result
*/
upm_result_t bh1749_set_ir_gain(bh1749_context dev, IR_GAINS irGain);
/**
* @brief Gets IR gain value
*
* @param dev Sensor context
* @param gain pointer (uint8_t) to save value
* @return UPM result
*/
upm_result_t bh1749_get_ir_gain(bh1749_context dev, uint8_t *gain);
/**
* @brief Sets interrupt source value
*
* @param dev Sensor context
* @param intSource interrupt source choice, a value of INT_SOURCES enum
* @return UPM result
*/
upm_result_t bh1749_set_int_source(bh1749_context dev, INT_SOURCES intSource);
/**
* @brief Gets interrupt source value
*
* @param dev Sensor context
* @return character of interrupt source
*/
char bh1749_get_interrupt_source_char(bh1749_context dev);
/**
* @brief Enables interrupt mode and resets the interrupt status (clear)
*
* @param dev Sensor context
* @return UPM result
*/
upm_result_t bh1749_enable_interrupt(bh1749_context dev);
/**
* @brief Disables interrupt mode
*
* @param dev Sensor context
* @return UPM result
*/
upm_result_t bh1749_disable_interrupt(bh1749_context dev);
/**
* @brief Resets interrupt status (clear) to allow new interrupts
*
* @param dev Sensor context
* @return UPM result
*/
upm_result_t bh1749_reset_interrupt(bh1749_context dev);
/**
* @brief Checks the status of the interrupt
*
* @param dev Sensor context
* @return true if there is interrupt, otherwise false
*/
bool bh1749_is_interrupted(bh1749_context dev);
/**
* @brief Checks whether interrupt mode is enabled
*
* @param dev Sensor context
* @return true if interrupt is enabled, otherwise false
*/
bool bh1749_is_interrupt_enabled(bh1749_context dev);
/**
* @brief Initiates a software reset to the sensor. All register values will
* be written to their defaults, thus sensor_init() must be called after this,
* and thresholds also needs to be set.
*
* @param dev Sensor context
* @return UPM result
*/
upm_result_t bh1749_soft_reset(bh1749_context dev);
/**
* @brief Sets interrupt threshold high value
*
* @param dev Sensor context
* @param threshold Value to be written, range 0-65536
* @return UPM result
*/
upm_result_t bh1749_set_threshold_high(bh1749_context dev, uint16_t threshold);
/**
* @brief Gets interrupt threshold high value
*
* @param dev Sensor context
* @param threshold Pointer (uint16_t) to write value
* @return UPM result
*/
upm_result_t bh1749_get_threshold_high(bh1749_context dev, uint16_t *threshold);
/**
* @brief Sets interrupt threshold low value
*
* @param dev Sensor context
* @param threshold Value to be written, range 0-65536
* @return UPM result
*/
upm_result_t bh1749_set_threshold_low(bh1749_context dev, uint16_t threshold);
/**
* @brief Gets interrupt threshold low value
*
* @param dev Sensor context
* @param threshold Pointer (uint16_t) to write value
* @return UPM result
*/
upm_result_t bh1749_get_threshold_low(bh1749_context dev, uint16_t *threshold);
/**
* @brief Gets value of Red color channel
*
* @param dev Sensor context
* @param red Pointer (uint16_t) to write value
* @return UPM result
*/
upm_result_t bh1749_get_red(bh1749_context dev, uint16_t *red);
/**
* @brief Gets value of Green color channel
*
* @param dev Sensor context
* @param green Pointer (uint16_t) to write value
* @return UPM result
*/
upm_result_t bh1749_get_green(bh1749_context dev, uint16_t *green);
/**
* @brief Gets value of Blue color channel
*
* @param dev Sensor context
* @param blue Pointer (uint16_t) to write value
* @return UPM result
*/
upm_result_t bh1749_get_blue(bh1749_context dev, uint16_t *blue);
/**
* @brief Gets value of IR color channel
*
* @param dev Sensor context
* @param ir Pointer (uint16_t) to write value
* @return UPM result
*/
upm_result_t bh1749_get_ir(bh1749_context dev, uint16_t *ir);
/**
* @brief Gets value of Green2 color channel
*
* @param dev Sensor context
* @param green Pointer (uint16_t) to write value
* @return UPM result
*/
upm_result_t bh1749_get_green2(bh1749_context dev, uint16_t *green2);
/**
* @brief Gets all channels measurements values
*
* @param dev Sensor context
* @param result Pointer of uint16_t to write all values ordered as:
* Red, Green, Blue, IR, Green2
*
* @return UPM result
*/
upm_result_t bh1749_get_measurements(bh1749_context dev, uint16_t *result);
/**
* @brief Installs the ISR to a given GPIO pin
*
* @param dev Sensor context
* @param edge Edge type to raise ISR with, of type mraa_gpio_edge_t
* @param pin GPIO pin number
* @param isr Pointer to ISR function
* @param isr_args Arguments to pass the ISR function
* @return UPM result
*/
upm_result_t bh1749_install_isr(bh1749_context dev, mraa_gpio_edge_t edge, int pin,
void (*isr)(void *), void *isr_args);
/**
* @brief Removes the ISR if it is installed
*
* @param dev Sensor context
*/
void bh1749_remove_isr(bh1749_context dev);
/**
* @brief Gets a dump of configuration registers as a string
*
* @param dev Sensor context
* @param dump Pointer of char to save dump string
* @return UPM result
*/
upm_result_t bh1749_registers_dump(bh1749_context dev, char *dump);
#ifdef __cplusplus
}
#endif

342
src/bh1749/bh1749.hpp Executable file
View File

@ -0,0 +1,342 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <vector>
#include "bh1749.h"
/**
* @brief C++ API wrapper for the bh1749 color sensor driver
* @defgroup bh1749 libupm-bh1749
* @ingroup ROHM i2c color
*/
/**
* @library bh1749
* @sensor bh1749
* @comname Color Sensor
* @type color sensor
* @man ROHM
* @con i2c
*
* @brief C++ API wrapper for the bh1749 driver
*/
namespace upm {
class BH1749 {
public:
/**
* @brief Initialize a sensor instance with bus and address.
*
* This calls SensorInit() to set default values for operating mode,
* gains, measurement time, interrupt source and then sets threshold
* high to 511.
*
* @param bus I2C bus number
* @param addr I2C sensor address
* @throws std::runtime_error on initialization failure
*/
BH1749(int bus = 0, int addr = 0x39);
/**
* @brief Close and free sensor
*/
virtual ~BH1749();
/**
* @brief Enables RGB color measurement on the sensor
*
* @throws std::runtime_error if bh1749_enable() fails
*/
void Enable();
/**
* @brief Disables RGB color measurement on the sensor
*
* @throws std::runtime_error if bh1749_disable() fails
*/
void Disable();
/**
* @brief Check "who am I" register value to identify the sensor
*
* @throws std::runtime_error if this value is incorrect
*/
void CheckWhoAmI();
/**
* @brief Initializes (writes) configuration values to sensor
*
* @param opMode Operating mode choice, a value of OPERATING_MODES enum
* @param measTime Measurement time choice, a value of MEAS_TIMES enum
* @param rgbGain RGB gain choice, a value of RGB_GAINS enum
* @param irGain IR gain choice, a value of IR_GAINS enum
* @param intSource interrupt source choice, a value of INT_SOURCES enum
*
* @throws std::runtime_error if one of values setting fails
*/
void SensorInit(OPERATING_MODES opMode,
MEAS_TIMES measTime,
RGB_GAINS rgbGain,
IR_GAINS irGain,
INT_SOURCES intSource);
/**
* @brief Sets operating mode (interrupt persistance)
*
* @param opMode Operating mode choice, a value of OPERATING_MODES enum
* @throws std::runtime_error if writing value fails
*/
void SetOperatingMode(OPERATING_MODES opMode);
/**
* @brief Gets operating mode (interrupt persistance) value
*
* @param opMode pointer (uint8_t) to save value
* @return operating mode value
* @throws std::runtime_error if reading the value fails
*/
int GetOperatingMode();
/**
* @brief Sets measurement time (ODR)
*
* @param measTime measurement time choice, a value of MEAS_TIMES enum
* @throws std::runtime_error if writing value fails
*/
void SetMeasurementTime(MEAS_TIMES measTime);
/**
* @brief Gets measurement time (ODR) value
*
* @return measurement time value
* @throws std::runtime_error if reading the value fails
*/
int GetMeasurementTime();
/**
* @brief Sets RGB gain value
*
* @param rgbGain RGB gain choice, a value of RGB_GAINS enum
* @throws std::runtime_error if writing value fails
*/
void SetRgbGain(RGB_GAINS rgbGain);
/**
* @brief Gets RGB gain value
*
* @return RGB gain value value
* @throws std::runtime_error if reading the value fails
*/
int GetRgbGain();
/**
* @brief Sets IR gain value
*
* @param irGain IR gain choice, a value of IR_GAINS enum
* @throws std::runtime_error if writing value fails
*/
void SetIrGain(IR_GAINS irGain);
/**
* @brief Gets IR gain value
*
* @return IR gain value value
* @throws std::runtime_error if reading the value fails
*/
int GetIrGain();
/**
* @brief Sets interrupt source value
*
* @param intSource interrupt source choice, a value of INT_SOURCES enum
* @throws std::runtime_error if writing value fails
*/
void SetIntSource(INT_SOURCES intSource);
/**
* @brief Gets interrupt source value
*
* @return interrupt source value
* @throws std::runtime_error if reading the value fails
*/
char GetInterruptSourceChar();
/**
* @brief Enables interrupt mode
*
* @throws std::runtime_error if writing enable bit fails
*/
void EnableInterrupt();
/**
* @brief Disables interrupt mode
*
* @throws std::runtime_error if writing disable bit fails
*/
void DisableInterrupt();
/**
* @brief Resets interrupt status (clear) to allow new interrupt reads
*
* @throws std::runtime_error if writing reset bit fails
*/
void ResetInterrupt();
/**
* @brief Gets interrupt status, whether interrupt is raised or not
*
* @return true if interrupt is raised, otherwise false
*/
bool IsInterrupted();
/**
* @brief Gets interrupt mode status
*
* @return true if interrupt is enabled, otherwise false
*/
bool IsInterruptEnabled();
/**
* @brief Initiates a software reset to the sensor. All register values
* will be written to their defaults, thus sensor_init() must be called
* after this, and thresholds also needs to be set.
*
* @throws std::runtime_error if writing reset bit fails
*/
void SoftReset();
/**
* @brief Sets threshold high value
*
* @param threshold interrupt threshold high value, range 0-65536
* @throws std::runtime_error if writing value fails
*/
void SetThresholdHigh(uint16_t threshold);
/**
* @brief Gets threshold high value
*
* @return threshold high value
* @throws std::runtime_error if reading the value fails
*/
int GetThresholdHigh();
/**
* @brief Sets threshold low value
*
* @param threshold interrupt threshold low value, range 0-65536
* @throws std::runtime_error if writing value fails
*/
void SetThresholdLow(uint16_t threshold);
/**
* @brief Gets threshold low value
*
* @return threshold low value
* @throws std::runtime_error if reading the value fails
*/
int GetThresholdLow();
/**
* @brief Gets value of Red color channel
*
* @return red channel value
* @throws std::runtime_error if reading the value fails
*/
uint16_t GetRed();
/**
* @brief Gets value of Green color channel
*
* @return green channel value
* @throws std::runtime_error if reading the value fails
*/
uint16_t GetGreen();
/**
* @brief Gets value of Blue color channel
*
* @return blue channel value
* @throws std::runtime_error if reading the value fails
*/
uint16_t GetBlue();
/**
* @brief Gets value of IR color channel
*
* @return ir channel value
* @throws std::runtime_error if reading the value fails
*/
uint16_t GetIr();
/**
* @brief Gets value of Green2 color channel
*
* @return green2 channel value
* @throws std::runtime_error if reading the value fails
*/
uint16_t GetGeen2();
/**
* @brief Gets all channels measurements values
*
* @return vector with all channels values ordered as:
* Red, Green, Blue, IR, Green2
* @throws std::runtime_error if reading one of the values fails
*/
std::vector<uint16_t> GetMeasurements();
/**
* @brief Installs the ISR to a given GPIO pin
*
* @param edge Edge type to raise ISR with, of type mraa_gpio_edge_t
* @param pin GPIO pin number
* @param isr Pointer to ISR function
* @param isr_args Arguments to pass the ISR function
* @throws std::runtime_error if installing ISR fails
*/
void InstallISR(mraa_gpio_edge_t edge, int pin, void (*isr)(void *), void *isr_args);
/**
* @brief Removes the ISR if it is installed
*/
void RemoveISR();
/**
* @brief Gets a dump of configuration registers as a string
*
* @return string of dumped registers
* @throws std::runtime_error if reading one of the registers fails
*/
std::string RegistersDump();
private:
bh1749_context m_bh1749;
/* Disable implicit copy and assignment operators */
BH1749(const BH1749 &) = delete;
BH1749 &operator=(const BH1749 &) = delete;
};
}

18
src/bh1749/bh1749.i Executable file
View File

@ -0,0 +1,18 @@
%include "../common_top.i"
/* BEGIN Java syntax ------------------------------------------------------- */
#ifdef SWIGJAVA
JAVA_JNI_LOADLIBRARY(javaupm_bh1749)
#endif
/* END Java syntax */
/* BEGIN Common SWIG syntax ------------------------------------------------- */
%include "std_vector.i"
%template(uint16Vector) std::vector<uint16_t>;
%{
#include "bh1749.hpp"
%}
%include "bh1749.h"
%include "bh1749.hpp"
/* END Common SWIG syntax */

49
src/bh1749/bh1749.json Executable file
View File

@ -0,0 +1,49 @@
{
"Library": "BH1749",
"Description": "ROHM BH1749 Color sensor (16-bit) library",
"Sensor Class": {
"BH1749": {
"Name": "ROHM BH1749 Color sensor (16-bit)",
"Description": "BH1749NUC is a digital color sensor IC with I2C bus interface. This IC senses Red, Green, Blue (RGB) and Infrared and converts them to digital values. The high sensitivity, wide dynamic range and excellent Ircut characteristics make it possible for this IC to obtain the accurate illuminance and color temperature of ambient light. It is ideal for adjusting LCD backlight of TV, mobile phone and tablet PC.",
"Categories": ["color sensor"],
"Connections": ["i2c"],
"Project Type": ["prototyping", "commercial"],
"Manufacturers": ["ROHM"],
"Examples": {
"C++": ["bh1749.cxx"],
"C": ["bh1749.c"],
"Java": ["BH1749_Example.java"],
"Python": ["bh1749.py"],
"Node.js": ["bh1749.js"]
},
"Specifications": {
"Supply Voltage (VDD)": {
"unit": "V",
"min": 2.3,
"max": 3.6
},
"Supply Current": {
"unit": "uA",
"min": 0.8,
"max": 190
},
"Operating Temperature": {
"unit": "°C",
"min": -40,
"max": 85
}, "Illumination Detection": {
"unit" : "klx",
"value": 80
}
},
"Urls": {
"Product Pages": [
"https://www.rohm.com/products/sensors-mems/color-sensor-ics/bh1745nuc-product"
],
"Datasheets": [
"https://www.rohm.com/datasheet/BH1749NUC/bh1749nuc-e"
]
}
}
}
}

160
src/bh1749/bh1749_registers.h Executable file
View File

@ -0,0 +1,160 @@
/*
The MIT License (MIT)
Copyright (c) 2017 Rohm Semiconductor
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __BH1749_REGISTERS_H__
#define __BH1749_REGISTERS_H__
/* registers */
#define BH1749_REGISTER_DUMP_START 0x40
#define BH1749_SYSTEM_CONTROL 0x40
#define BH1749_MODE_CONTROL1 0x41
#define BH1749_MODE_CONTROL2 0x42
// Least significant byte of uint16 RED measurement value
#define BH1749_RED_DATA_LSBS 0x50
// Most significant byte of uint16 RED measurement value
#define BH1749_RED_DATA_MSBS 0x51
// Least significant byte of uint16 GREEN measurement value
#define BH1749_GREEN_DATA_LSBS 0x52
// Most significant byte of uint16 GREEN measurement value
#define BH1749_GREEN_DATA_MSBS 0x53
// Least significant byte of uint16 BLUE measurement value
#define BH1749_BLUE_DATA_LSBS 0x54
// Most significant byte of uint16 BLUE measurement value
#define BH1749_BLUE_DATA_MSBS 0x55
// Least significant byte of uint16 IR measurement value
#define BH1749_IR_DATA_LSBS 0x58
// Most significant byte of uint16 IR measurement value
#define BH1749_IR_DATA_MSBS 0x59
// Least significant byte of uint16 GREEN2 measurement value
#define BH1749_GREEN2_DATA_LSBS 0x5A
// Most significant byte of uint16 GREEN2 measurement value
#define BH1749_GREEN2_DATA_MSBS 0x5B
// Interrupt control register
#define BH1749_INTERRUPT 0x60
// Interrupt status update control register
#define BH1749_PERSISTENCE 0x61
// Least significant byte of interrupt threshold high level
#define BH1749_TH_LSBS 0x62
// Most significant byte of interrupt threshold high level
#define BH1749_TH_MSBS 0x63
// Least significant byte of interrupt threshold low level
#define BH1749_TL_LSBS 0x64
// Most significant byte of interrupt threshold low level
#define BH1749_TL_MSBS 0x65
#define BH1749_ID_REG 0x92
#define BH1749_REGISTER_DUMP_END 0x92
/* registers bits */
// Software reset is not done
#define BH1749_SYSTEM_CONTROL_SW_RESET_NOT_DONE (0x00 << 7)
// Software reset is done
#define BH1749_SYSTEM_CONTROL_SW_RESET_DONE (0x01 << 7)
// INT pin status is not changed
#define BH1749_SYSTEM_CONTROL_INT_NO_ACTION (0x00 << 6)
// INT pin becomes inactive (high impedance)
#define BH1749_SYSTEM_CONTROL_INT_RESET (0x01 << 6)
#define BH1749_SYSTEM_CONTROL_PART_ID (0x0D << 0)
#define BH1749_MODE_CONTROL1_RESERVED7_WRITE0 (0x00 << 7)
#define BH1749_MODE_CONTROL1_IR_GAIN_RESERVED0 (0x00 << 5)
#define BH1749_MODE_CONTROL1_IR_GAIN_1X (0x01 << 5)
#define BH1749_MODE_CONTROL1_IR_GAIN_RESERVED1 (0x02 << 5)
#define BH1749_MODE_CONTROL1_IR_GAIN_32X (0x03 << 5)
#define BH1749_MODE_CONTROL1_RGB_GAIN_RESERVED0 (0x00 << 3)
#define BH1749_MODE_CONTROL1_RGB_GAIN_1X (0x01 << 3)
#define BH1749_MODE_CONTROL1_RGB_GAIN_RESERVED1 (0x02 << 3)
#define BH1749_MODE_CONTROL1_RGB_GAIN_32X (0x03 << 3)
// Reserved value
#define BH1749_MODE_CONTROL1_ODR_RESERVED0 (0x00 << 0)
// Reserved value
#define BH1749_MODE_CONTROL1_ODR_RESERVED1 (0x01 << 0)
// 120ms measurement time
#define BH1749_MODE_CONTROL1_ODR_8P333 (0x02 << 0)
// 240ms measurement time
#define BH1749_MODE_CONTROL1_ODR_4P167 (0x03 << 0)
// Reserved value
#define BH1749_MODE_CONTROL1_ODR_RESERVED2 (0x04 << 0)
// 35ms measurement time
#define BH1749_MODE_CONTROL1_ODR_28P6 (0x05 << 0)
// Reserved value
#define BH1749_MODE_CONTROL1_ODR_RESERVED3 (0x06 << 0)
// Reserved value
#define BH1749_MODE_CONTROL1_ODR_RESERVED4 (0x07 << 0)
#define BH1749_MODE_CONTROL2_VALID_NO (0x00 << 7)
#define BH1749_MODE_CONTROL2_VALID_YES (0x01 << 7)
#define BH1749_MODE_CONTROL2_RESERVED65_WRITE00 (0x00 << 5)
#define BH1749_MODE_CONTROL2_RGB_MEASUREMENT_INACTIVE (0x00 << 4)
#define BH1749_MODE_CONTROL2_RGB_MEASUREMENT_ACTIVE (0x01 << 4)
#define BH1749_MODE_CONTROL2_RESERVED30_WRITE0000 (0x00 << 0)
#define BH1749_INTERRUPT_STATUS_INACTIVE (0x00 << 7)
#define BH1749_INTERRUPT_STATUS_ACTIVE (0x01 << 7)
#define BH1749_INTERRUPT_RESERVED64_WRITE000 (0x00 << 4)
// red channel
#define BH1749_INTERRUPT_SOURCE_SELECT_RED (0x00 << 2)
// green channel
#define BH1749_INTERRUPT_SOURCE_SELECT_GREEN (0x01 << 2)
// blue channel
#define BH1749_INTERRUPT_SOURCE_SELECT_BLUE (0x02 << 2)
// Reserved value
#define BH1749_INTERRUPT_SOURCE_RESERVED0 (0x03 << 2)
#define BH1749_INTERRUPT_RESERVED1_WRITE0 (0x00 << 1)
#define BH1749_INTERRUPT_EN_DISABLE (0x00 << 0)
#define BH1749_INTERRUPT_EN_ENABLE (0x01 << 0)
#define BH1749_PERSISTENCE_RESERVED72_WRITE000000 (0x00 << 2)
// Interrupt status becomes active at each measurement end.
#define BH1749_PERSISTENCE_MODE_STATUS_ACTIVE_AFTER_MEASUREMENT (0x00 << 0)
// Interrupt status is updated at each measurement end.
#define BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_MEASUREMENT (0x01 << 0)
// Interrupt status is updated if 4 consecutive threshold judgements are the same
#define BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_4_SAME (0x02 << 0)
// Interrupt status is updated if 8 consecutive threshold judgements are the same
#define BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_8_SAME (0x03 << 0)
// Manufacturer ID
#define BH1749_ID_REG_MANUFACTURER_ID (0xE0 << 0)
/*registers bit masks */
#define BH1749_SYSTEM_CONTROL_SW_RESET_MASK 0x80
#define BH1749_SYSTEM_CONTROL_INT_MASK 0x40
#define BH1749_SYSTEM_CONTROL_PART_MASK 0x3F
#define BH1749_MODE_CONTROL1_RESERVED7_MASK 0x80
#define BH1749_MODE_CONTROL1_IR_GAIN_MASK 0x60
#define BH1749_MODE_CONTROL1_RGB_GAIN_MASK 0x18
#define BH1749_MODE_CONTROL1_ODR_MASK 0x07
// Measurement data update flag. Sets to 0 if MODE_CONTROL1/2 reg,
// MODE_CONTROL2, INTERRUPT, T(H/L)_(LSB/MSB) is written or
// MODE_CONTROL2 read. In specification named as VALID.
#define BH1749_MODE_CONTROL2_VALID_MASK 0x80
#define BH1749_MODE_CONTROL2_RESERVED65_MASK 0x60
#define BH1749_MODE_CONTROL2_RGB_MEASUREMENT_MASK 0x10
#define BH1749_MODE_CONTROL2_RESERVED30_MASK 0x0F
#define BH1749_INTERRUPT_STATUS_MASK 0x80
#define BH1749_INTERRUPT_RESERVED64_MASK 0x70
#define BH1749_INTERRUPT_SOURCE_MASK 0x0C
// Write 0
#define BH1749_INTERRUPT_RESERVED1_MASK 0x02
#define BH1749_INTERRUPT_EN_MASK 0x01
#define BH1749_PERSISTENCE_RESERVED72_MASK 0xFC
#define BH1749_PERSISTENCE_MODE_MASK 0x03
#define BH1749_ID_REG_MANUFACTURER_MASK 0xFF
#endif