Rohm / BH1749NUC

Dependents:   rohm-SensorShield-example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BH1749NUC.cpp Source File

BH1749NUC.cpp

00001 /*****************************************************************************
00002   BH1749NUC.cpp
00003 
00004  Copyright (c) 2018-2019 ROHM Co.,Ltd.
00005 
00006  Permission is hereby granted, free of charge, to any person obtaining a copy
00007  of this software and associated documentation files (the "Software"), to deal
00008  in the Software without restriction, including without limitation the rights
00009  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  copies of the Software, and to permit persons to whom the Software is
00011  furnished to do so, subject to the following conditions:
00012 
00013  The above copyright notice and this permission notice shall be included in
00014  all copies or substantial portions of the Software.
00015 
00016  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  THE SOFTWARE.
00023 ******************************************************************************/
00024 #include "mbed.h"
00025 #include "BH1749NUC.h"
00026 
00027 BH1749NUC::BH1749NUC(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
00028 {
00029 }
00030 
00031 BH1749NUC::~BH1749NUC()
00032 {
00033     m_addr = 0;
00034 }
00035 
00036 int BH1749NUC::init(void)
00037 {
00038     int rc;
00039     char reg;
00040 
00041     rc = read(BH1749NUC_SYSTEM_CONTROL, &reg, sizeof(reg));
00042     if (rc != 0) {
00043         DEBUG_PRINT("Can't access BH1749NUC\n");
00044         return (rc);
00045     }
00046     reg = reg & 0x3F;
00047     DEBUG_PRINT("BH1749NUC Part ID Value = %02x\n", reg);
00048 
00049     if (reg != BH1749NUC_PART_ID_VAL) {
00050         DEBUG_PRINT("Can't find BH1749NUC\n");
00051         return (rc);
00052     }
00053 
00054     rc = read(BH1749NUC_MANUFACTURER_ID, &reg, sizeof(reg));
00055     if (rc != 0) {
00056         DEBUG_PRINT("Can't access BH1749NUC\n");
00057         return (rc);
00058     }
00059     DEBUG_PRINT("BH1749NUC MANUFACTURER ID Register Value = %02x\n", reg);
00060 
00061     if (reg != BH1749NUC_MANUFACT_ID_VAL) {
00062         DEBUG_PRINT("Can't find BH1749NUC\n");
00063         return (rc);
00064     }
00065 
00066     reg = BH1749NUC_MODE_CONTROL1_VAL;
00067     rc = write(BH1749NUC_MODE_CONTROL1, &reg, sizeof(reg));
00068     if (rc != 0) {
00069         DEBUG_PRINT("Can't write BH1749NUC MODE_CONTROL1 register\n");
00070         return (rc);
00071     }
00072 
00073     reg = BH1749NUC_MODE_CONTROL2_VAL;
00074     rc = write(BH1749NUC_MODE_CONTROL2, &reg, sizeof(reg));
00075     if (rc != 0) {
00076         DEBUG_PRINT("Can't write BH1749NUC MODE_CONTROL2 register\n");
00077         return (rc);
00078     }
00079 
00080     wait_ms(WAIT_TMT2_MAX);
00081     return (rc);
00082 }
00083 
00084 int BH1749NUC::get_rawval(char *data)
00085 {
00086     int rc;
00087 
00088     rc = read(BH1749NUC_RED_DATA_LSB, data, GET_BYTE_RED_TO_GREEN2);
00089     if (rc != 0) {
00090         DEBUG_PRINT("Can't get BH1749NUC RGB, IR and GREEN2 value\n");
00091     }
00092 
00093     return (rc);
00094 }
00095 
00096 int BH1749NUC::get_val(unsigned short *data)
00097 {
00098     int rc;
00099     char val[GET_BYTE_RED_TO_GREEN2];
00100 
00101     rc = get_rawval(val);
00102     if (rc != 0) {
00103         return (rc);
00104     }
00105 
00106     //val[6] and val[7] are RESERVED Register Value
00107     data[0] = ((unsigned short)val[1] << 8) | val[0];
00108     data[1] = ((unsigned short)val[3] << 8) | val[2];
00109     data[2] = ((unsigned short)val[5] << 8) | val[4];
00110     data[3] = ((unsigned short)val[9] << 8) | val[8];
00111     data[4] = ((unsigned short)val[11] << 8) | val[10];
00112 
00113     return (rc);
00114 }
00115 
00116 int BH1749NUC::write(char memory_address, char *data, int size)
00117 {
00118     int rc = 0;
00119 
00120     char t = memory_address;
00121     m_i2c.write(m_addr, &t, 1, true);
00122     rc = m_i2c.write(m_addr, data, size);
00123     return (rc);
00124 }
00125 
00126 int BH1749NUC::read(char memory_address, char *data, int size)
00127 {
00128     char t = memory_address;
00129     m_i2c.write(m_addr, &t, 1, true);
00130     return m_i2c.read(m_addr, data, size);
00131 }