Rohm BH1749NUC color sensor library

Dependents:   rohm-SensorShield-example

Files at this revision

API Documentation at this revision

Comitter:
MACRUM
Date:
Wed Feb 27 06:48:28 2019 +0000
Commit message:
Initial commit

Changed in this revision

BH1749NUC.cpp Show annotated file Show diff for this revision Revisions of this file
BH1749NUC.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fe0f43b69595 BH1749NUC.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BH1749NUC.cpp	Wed Feb 27 06:48:28 2019 +0000
@@ -0,0 +1,131 @@
+/*****************************************************************************
+  BH1749NUC.cpp
+
+ Copyright (c) 2018-2019 ROHM Co.,Ltd.
+
+ 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 "mbed.h"
+#include "BH1749NUC.h"
+
+BH1749NUC::BH1749NUC(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
+{
+}
+
+BH1749NUC::~BH1749NUC()
+{
+    m_addr = 0;
+}
+
+int BH1749NUC::init(void)
+{
+    int rc;
+    char reg;
+
+    rc = read(BH1749NUC_SYSTEM_CONTROL, &reg, sizeof(reg));
+    if (rc != 0) {
+        DEBUG_PRINT("Can't access BH1749NUC\n");
+        return (rc);
+    }
+    reg = reg & 0x3F;
+    DEBUG_PRINT("BH1749NUC Part ID Value = %02x\n", reg);
+
+    if (reg != BH1749NUC_PART_ID_VAL) {
+        DEBUG_PRINT("Can't find BH1749NUC\n");
+        return (rc);
+    }
+
+    rc = read(BH1749NUC_MANUFACTURER_ID, &reg, sizeof(reg));
+    if (rc != 0) {
+        DEBUG_PRINT("Can't access BH1749NUC\n");
+        return (rc);
+    }
+    DEBUG_PRINT("BH1749NUC MANUFACTURER ID Register Value = %02x\n", reg);
+
+    if (reg != BH1749NUC_MANUFACT_ID_VAL) {
+        DEBUG_PRINT("Can't find BH1749NUC\n");
+        return (rc);
+    }
+
+    reg = BH1749NUC_MODE_CONTROL1_VAL;
+    rc = write(BH1749NUC_MODE_CONTROL1, &reg, sizeof(reg));
+    if (rc != 0) {
+        DEBUG_PRINT("Can't write BH1749NUC MODE_CONTROL1 register\n");
+        return (rc);
+    }
+
+    reg = BH1749NUC_MODE_CONTROL2_VAL;
+    rc = write(BH1749NUC_MODE_CONTROL2, &reg, sizeof(reg));
+    if (rc != 0) {
+        DEBUG_PRINT("Can't write BH1749NUC MODE_CONTROL2 register\n");
+        return (rc);
+    }
+
+    wait_ms(WAIT_TMT2_MAX);
+    return (rc);
+}
+
+int BH1749NUC::get_rawval(char *data)
+{
+    int rc;
+
+    rc = read(BH1749NUC_RED_DATA_LSB, data, GET_BYTE_RED_TO_GREEN2);
+    if (rc != 0) {
+        DEBUG_PRINT("Can't get BH1749NUC RGB, IR and GREEN2 value\n");
+    }
+
+    return (rc);
+}
+
+int BH1749NUC::get_val(unsigned short *data)
+{
+    int rc;
+    char val[GET_BYTE_RED_TO_GREEN2];
+
+    rc = get_rawval(val);
+    if (rc != 0) {
+        return (rc);
+    }
+
+    //val[6] and val[7] are RESERVED Register Value
+    data[0] = ((unsigned short)val[1] << 8) | val[0];
+    data[1] = ((unsigned short)val[3] << 8) | val[2];
+    data[2] = ((unsigned short)val[5] << 8) | val[4];
+    data[3] = ((unsigned short)val[9] << 8) | val[8];
+    data[4] = ((unsigned short)val[11] << 8) | val[10];
+
+    return (rc);
+}
+
+int BH1749NUC::write(char memory_address, char *data, int size)
+{
+    int rc = 0;
+
+    char t = memory_address;
+    m_i2c.write(m_addr, &t, 1, true);
+    rc = m_i2c.write(m_addr, data, size);
+    return (rc);
+}
+
+int BH1749NUC::read(char memory_address, char *data, int size)
+{
+    char t = memory_address;
+    m_i2c.write(m_addr, &t, 1, true);
+    return m_i2c.read(m_addr, data, size);
+}
diff -r 000000000000 -r fe0f43b69595 BH1749NUC.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BH1749NUC.h	Wed Feb 27 06:48:28 2019 +0000
@@ -0,0 +1,123 @@
+/*****************************************************************************
+  BH1749NUC.h
+
+ Copyright (c) 2018 ROHM Co.,Ltd.
+
+ 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 _BH1749NUC_H_
+#define _BH1749NUC_H_
+
+#include "mbed.h"
+
+#ifdef _DEBUG
+#undef DEBUG_PRINT
+#define DEBUG_PRINT(...) printf(__VA_ARGS__)
+#else
+#define DEBUG_PRINT(...)
+#endif
+
+#define BH1749NUC_DEVICE_ADDRESS_38             (0x38 << 1) // 7bit Addrss
+#define BH1749NUC_DEVICE_ADDRESS_39             (0x39 << 1) // 7bit Addrss
+#define BH1749NUC_PART_ID_VAL                   (0x0D)
+#define BH1749NUC_MANUFACT_ID_VAL               (0xE0)
+
+#define BH1749NUC_SYSTEM_CONTROL                (0x40)
+#define BH1749NUC_MODE_CONTROL1                 (0x41)
+#define BH1749NUC_MODE_CONTROL2                 (0x42)
+#define BH1749NUC_RED_DATA_LSB                  (0x50)
+#define BH1749NUC_MANUFACTURER_ID               (0x92)
+
+#define BH1749NUC_MODE_CONTROL1_MEAS_MODE_120MS (2)
+#define BH1749NUC_MODE_CONTROL1_MEAS_MODE_240MS (3)
+#define BH1749NUC_MODE_CONTROL1_RGB_GAIN_X1     (1 << 3)
+#define BH1749NUC_MODE_CONTROL1_RGB_GAIN_X32    (3 << 3)
+#define BH1749NUC_MODE_CONTROL1_IR_GAIN_X1      (1 << 5)
+#define BH1749NUC_MODE_CONTROL1_IR_GAIN_X32     (3 << 5)
+#define BH1749NUC_MODE_CONTROL2_RGB_EN          (1 << 4)
+
+#define BH1749NUC_MODE_CONTROL1_VAL             (BH1749NUC_MODE_CONTROL1_MEAS_MODE_120MS | BH1749NUC_MODE_CONTROL1_RGB_GAIN_X1 | BH1749NUC_MODE_CONTROL1_IR_GAIN_X1)
+#define BH1749NUC_MODE_CONTROL2_VAL             (BH1749NUC_MODE_CONTROL2_RGB_EN)
+
+#define GET_BYTE_RED_TO_GREEN2 (12)
+#define WAIT_TMT2_MAX          (340)
+
+/**  Interface for controlling BME280 Combined humidity and pressure sensor
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "BH1749NUC.h"
+ * 
+ * BH1749NUC color(I2C_SDA, I2C_SCL);
+ * 
+ * int main(void) {
+ *     uint16_t buf[5];
+ *     color.init();
+ *  
+ *     while (1) {
+ *         color.get_val(buf);
+ *         printf("BH1749 color : [R] %04x, [G] %04x, [B] %04x, [IR] %04x, [G2] %04x\n", buf[0], buf[1], buf[2], buf[3], buf[4]);
+ *         wait(1);
+ *     }
+ * }
+ * @endcode
+ */
+
+/** BH1749NUC class
+ *
+ *  BH1749NUC: A library to correct color data using Rohm BH1749NUC color sensor device
+ *
+ */ 
+class BH1749NUC
+{
+public:
+    /** Create a BH1749NUC instance
+    *   which is connected to specified I2C pins with specified address
+    *
+    * @param sda I2C-bus SDA pin
+    * @param sdl I2C-bus SCL pin
+    * @param addr slave address of the I2C peripheral (default: 0x72)
+    */
+    BH1749NUC(PinName sda, PinName scl, int addr = BH1749NUC_DEVICE_ADDRESS_39);
+    /**
+    * BH1749NUC destructor
+    */
+    ~BH1749NUC();
+    /**
+    * Initialize BH1749NUC device
+    */
+    int init(void) ;
+    /**
+    * Get color sensor raw value
+    * @param data buffer to store raw data
+    */
+    int get_rawval(char *data);
+    /**
+    * Get color sensor value
+    * @param data buffer to store sensor data (R, B, G, IR and G2)
+    */
+    int get_val(unsigned short *data);
+private:
+    int write(char memory_address, char *data, int size);
+    int read(char memory_address, char *data, int size);
+    I2C m_i2c;
+    int m_addr;
+};
+
+#endif // _BH1749NUC_H_