mbed library for the TCA9548 mux

Fork of libTCS34725 by Michael Wilkens

Files at this revision

API Documentation at this revision

Comitter:
mwilkens241
Date:
Thu Mar 09 18:42:41 2017 +0000
Parent:
3:afb107db7994
Child:
6:79996efbdcb2
Commit message:
working mux

Changed in this revision

TCA9548.cpp Show annotated file Show diff for this revision Revisions of this file
TCA9548.h Show annotated file Show diff for this revision Revisions of this file
TCS34725.cpp Show diff for this revision Revisions of this file
TCS34725.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCA9548.cpp	Thu Mar 09 18:42:41 2017 +0000
@@ -0,0 +1,31 @@
+#include "TCA9548.h"
+
+TCA9548::TCA9548() : i2c(SDA,SCL) {}
+TCA9548::TCA9548(PinName i2c_sda, PinName i2c_scl) : i2c(i2c_sda,i2c_scl) {}
+
+void TCA9548::i2cWrite8(uint8_t addr, char data){
+    char packet[1] = {data & 0xFF}; 
+    i2c.write(addr,packet,1,false); //only write address and data ~ no reg
+    wait(0.01);
+}
+
+bool TCA9548::init(uint8_t iCh){
+    i2c.frequency(400000);
+    channel = iCh;
+    i2cWrite8(DEV_ADDR,(char)iCh);
+    return true;
+}
+
+bool TCA9548::init(){
+    i2c.frequency(400000);
+    channel = 0;
+    i2cWrite8(DEV_ADDR,0);
+    wait(0.05);
+    return true;
+}
+
+void TCA9548::ch(uint8_t newCh){
+    channel = newCh;
+    i2cWrite8(DEV_ADDR,(char)newCh);
+    wait(0.05);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCA9548.h	Thu Mar 09 18:42:41 2017 +0000
@@ -0,0 +1,88 @@
+// TCA9548 I2C Mux
+#ifndef MBED_TCA9548_H
+#define MBED_TCA9548_H
+
+#include "mbed.h"
+
+//These pins should be the only piece specific to the F030R8
+#define SCL            PB_8
+#define SDA            PB_9
+
+//I2C Address
+
+#define DEV_ADDR (0x70<<1)
+
+// CHANNEL VALUES
+#define CH0 0x01
+#define CH1 0x02
+#define CH2 0x04
+#define CH3 0x08
+#define CH4 0x10
+#define CH5 0x20
+#define CH6 0x40
+#define CH7 0x80
+
+
+/** TCA9548 control class.
+ *
+ * Example:
+ * @code
+ * //Perform readings on two i2c devices with the same address
+ * #include "TCA9548.h"
+ * #include "mbed.h"
+ * #include "VL6180.h"
+ *
+ * TCA9548 i2cMux(SDA, SCL);
+ * VL6180 laser1(SDA, SCL); // on SD0 and SC0
+ * VL6180 laser2(SDA, SCL); // on SD1 and SC1
+ * Serial pc(USBTX, USBRX); //USB serial
+ *
+ * int main() {
+ *   float reading;
+ *   i2cMux.ch(CH0);
+ *   reading = laser1.read();
+ *   pc.printf("Laser 1: %f\n", reading);
+ *   i2cMux.ch(CH1); //for two channels: i2cMux.ch(CH1 | CH5);
+ *   pc.printf("Laser 2: %f\n", reading);
+ *   reading = laser2.read();
+ *       
+ *   while(1) {
+ *   }
+ * }
+ * @endcode
+ */
+
+class TCA9548 {
+    private:
+        I2C i2c;
+        uint8_t channel;
+        void i2cWrite8(uint8_t addr, char data);
+    public:
+        /** Initialize object with default i2c pins */
+        TCA9548();
+        
+        /** Initialize object with specific i2c pins
+        * 
+        * @param i2c_sda SDA pin
+        * @param i2c_scl SCL pin
+        */
+        TCA9548(PinName i2c_sda, PinName i2c_scl);
+        
+        /** Boot up the mux and change to initial channel (defaults to 0)
+        * 
+        * @param iCh Initial Channel
+        * @return 
+        *   1 if failed
+        *`  0 if successful
+        */
+        bool init(uint8_t iCh);
+        bool init();
+        
+        /** Changes the i2c channel
+        * 
+        * @param newCh New channel for mux
+        */
+        void ch(uint8_t newCh);
+};
+
+#endif
\ No newline at end of file
--- a/TCS34725.cpp	Wed Jan 25 20:17:57 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,83 +0,0 @@
-#include "TCS34725.h"
-
-TCS34725::TCS34725() : i2c(SDA,SCL) {}
-TCS34725::TCS34725(PinName i2c_sda, PinName i2c_scl) : i2c(i2c_sda,i2c_scl) {}
-
-void TCS34725::i2cWrite8(uint8_t addr, char reg, char data){
-    char packet[2] = {reg | COMMAND_BIT ,data & 0xFF}; 
-    i2c.write(addr,packet,2,false);
-    wait(0.01);
-}
-
-uint8_t TCS34725::i2cRead8(uint8_t addr, char reg){
-    char packet[1] = {reg | COMMAND_BIT};
-    char data[1] = {0};
-    i2c.write(addr,packet,1, true);
-    i2c.read(addr,data,1,false);
-    return (uint8_t)data[0];
-}
-
-uint16_t TCS34725::i2cRead16(uint8_t addr, char reg){
-    char packet[1] = {reg | COMMAND_BIT};
-    char data[2] = {0,0};
-    i2c.write(addr,packet,1, true);
-    i2c.read(addr,data,2, false);
-    return ((uint16_t)data[1] << 8) | (uint16_t)data[0];
-}
-
-bool TCS34725::init(char intTime, char gain){
-    i2c.frequency(400000);
-    
-    uint8_t id = i2cRead8(SENSOR_ADDR, TCS34725_ID);
-    if(id != 0x44)return false;
-    
-    i2cWrite8(SENSOR_ADDR,TCS34725_ATIME, intTime);
-    t_intTime = intTime;
-    i2cWrite8(SENSOR_ADDR,TCS34725_CONTROL, gain);
-    t_gain = gain;
-    i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON);
-    wait(0.003);
-    i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
-    return true;
-}
-
-void TCS34725::config(char intTime, char gain){
-    uint8_t reg = i2cRead8(SENSOR_ADDR, TCS34725_ENABLE);
-    i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, reg | ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
-    init(intTime,gain);
-}
-
-void TCS34725::getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c){
-    *c = i2cRead16(SENSOR_ADDR, TCS34725_CDATAL);
-    *r = i2cRead16(SENSOR_ADDR, TCS34725_RDATAL);
-    *g = i2cRead16(SENSOR_ADDR, TCS34725_GDATAL);
-    *b = i2cRead16(SENSOR_ADDR, TCS34725_BDATAL);
-    switch(t_intTime){
-        case TCS34725_INTEGRATIONTIME_2_4MS:
-            wait(0.003);
-            break;
-        case TCS34725_INTEGRATIONTIME_24MS:
-            wait(0.024);
-            break;
-        case TCS34725_INTEGRATIONTIME_50MS:
-            wait(0.05);
-            break;
-        case TCS34725_INTEGRATIONTIME_101MS:
-            wait(0.101);
-            break;
-        case TCS34725_INTEGRATIONTIME_154MS:
-            wait(0.154);
-            break;
-        case TCS34725_INTEGRATIONTIME_700MS:
-            wait(0.7);
-            break;
-    }
-}
-
-void TCS34725::DEBUG(Serial * deb){
-    deb->printf("ATIME:%d ENABLE:%d CONTROL:%d ID:%d\n",
-    i2cRead8(SENSOR_ADDR, TCS34725_ATIME),
-    i2cRead8(SENSOR_ADDR, TCS34725_ENABLE),
-    i2cRead8(SENSOR_ADDR, TCS34725_CONTROL),
-    i2cRead16(SENSOR_ADDR, TCS34725_ID));
-}
\ No newline at end of file
--- a/TCS34725.h	Wed Jan 25 20:17:57 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,125 +0,0 @@
-// TCS34725 RGB Color I2C Sensor breakout library for F030R8 Nucleo board
-#ifndef MBED_TCS34725_H
-#define MBED_TCS34725_H
-
-#include "mbed.h"
-
-//These pins should be the only piece specific to the F030R8
-#define SCL            PB_8
-#define SDA            PB_9
-
-//I2C Address
-
-#define SENSOR_ADDR (0x29<<1)
-#define COMMAND_BIT 0x80
-
-//Important Registers
-
-#define TCS34725_ENABLE                  (0x00) //for turning on the device
-#define TCS34725_ATIME                   (0x01)  //for integration time
-#define TCS34725_CONTROL                 (0x0F)  //for setting the gain
-#define TCS34725_ID                      (0x12)
-#define TCS34725_CDATAL                  (0x14)    /* Clear channel data */
-#define TCS34725_CDATAH                  (0x15)
-#define TCS34725_RDATAL                  (0x16)    /* Red channel data */
-#define TCS34725_RDATAH                  (0x17)
-#define TCS34725_GDATAL                  (0x18)    /* Green channel data */
-#define TCS34725_GDATAH                  (0x19)
-#define TCS34725_BDATAL                  (0x1A)    /* Blue channel data */
-#define TCS34725_BDATAH                  (0x1B)
-
-//Configuration Bits
-
-#define TCS34725_ENABLE_AEN              (0x02)    /* RGBC Enable - Writing 1 actives the ADC, 0 disables it */
-#define TCS34725_ENABLE_PON              (0x01) /* Power on - Writing 1 activates the internal oscillator, 0 disables it */
-#define TCS34725_INTEGRATIONTIME_2_4MS    0xFF   /**<  2.4ms - 1 cycle    - Max Count: 1024  */
-#define TCS34725_INTEGRATIONTIME_24MS     0xF6   /**<  24ms  - 10 cycles  - Max Count: 10240 */
-#define TCS34725_INTEGRATIONTIME_50MS     0xEB   /**<  50ms  - 20 cycles  - Max Count: 20480 */
-#define TCS34725_INTEGRATIONTIME_101MS    0xD5   /**<  101ms - 42 cycles  - Max Count: 43008 */
-#define TCS34725_INTEGRATIONTIME_154MS    0xC0   /**<  154ms - 64 cycles  - Max Count: 65535 */
-#define TCS34725_INTEGRATIONTIME_700MS    0x00   /**<  700ms - 256 cycles - Max Count: 65535 */
-#define TCS34725_GAIN_1X                  0x00   /**<  No gain  */
-#define TCS34725_GAIN_4X                  0x01   /**<  4x gain  */
-#define TCS34725_GAIN_16X                 0x02   /**<  16x gain */
-#define TCS34725_GAIN_60X                 0x03   /**<  60x gain */
-
-/** TCS34725 control class.
- *
- * Example:
- * @code
- * //Send rgb data to the serial port
- * #include "TCS34725.h"
- * #include "mbed.h"
- *
- * TCS34725 colorSens(p9, p10); //I2C sda and scl
- * Serial pc(USBTX, USBRX); //USB serial
- *
- * int main() {
- *   uint16_t r,g,b,c;
- *       
- *   if(!colorSens.init(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_60X)){
- *       pc.printf("ERROR\n"); //check to see if i2c is responding
- *   }
- *
- *   while(1) {
-        colorSens.getColor(&r,&g,&b,&c); //pass variables by reference...
- *      pc.printf("DATA: r%d g%d b%d c%d", r, g, b, c);
- *      wait(0.5);
- *  }
- * }
- * @endcode
- */
-
-class TCS34725 {
-    private:
-        I2C i2c;
-        uint8_t t_intTime;
-        uint8_t t_gain;
-        void i2cWrite8(uint8_t addr, char reg, char data);
-        uint8_t i2cRead8(uint8_t addr, char reg);
-        uint16_t i2cRead16(uint8_t addr, char reg);
-    public:
-        /** Initialize object with default i2c pins */
-        TCS34725();
-        
-        /** Initialize object with specific i2c pins
-        * 
-        * @param i2c_sda SDA pin
-        * @param i2c_scl SCL pin
-        */
-        TCS34725(PinName i2c_sda, PinName i2c_scl);
-        
-        /** Boot up the sensor and checks if acking (see header for param defines)
-        * 
-        * @param intTime Integration time for reading (will delay accordingly)
-        * @param i2c_scl Gain value
-        */
-        bool init(char intTime, char gain);
-        
-        /** Configure after initial boot (will restart sensor)
-        * 
-        * @param intTime Integration time for reading (will delay accordingly)
-        * @param i2c_scl Gain value
-        * @return 
-        *   1 if failed
-        *`  0 if successful
-        */
-        void config(char intTime, char gain);
-        
-        /** Returns rgbc reading from the sensor.
-        * 
-        * @param r Red value (passed by reference)
-        * @param g Green value (passed by reference)
-        * @param b Blue value (passed by reference)
-        * @param c Clear value (all wavelengths - essentially shade) (passed by reference)
-        */
-        void getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
-        
-        /** Debug function... probably not useful unless youre debugging your i2c line
-        * 
-        * @param deb Serial object for debugging (passed by reference)
-        */
-        void DEBUG(Serial *deb);
-};
-
-#endif
\ No newline at end of file