Michael Wilkens / TCA9548

Fork of libTCS34725 by Michael Wilkens

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCA9548.h Source File

TCA9548.h

00001 // TCA9548 I2C Mux
00002 #ifndef MBED_TCA9548_H
00003 #define MBED_TCA9548_H
00004 
00005 #include "mbed.h"
00006 
00007 //I2C Address
00008 
00009 #define DEV_ADDR (0x70<<1)
00010 
00011 // CHANNEL VALUES
00012 #define CH0 0x01
00013 #define CH1 0x02
00014 #define CH2 0x04
00015 #define CH3 0x08
00016 #define CH4 0x10
00017 #define CH5 0x20
00018 #define CH6 0x40
00019 #define CH7 0x80
00020 
00021 
00022 /** TCA9548 control class.
00023  *
00024  * Example:
00025  * @code
00026  * //Perform readings on two i2c devices with the same address
00027  * #include "TCA9548.h"
00028  * #include "mbed.h"
00029  * #include "VL6180.h"
00030  *
00031  * TCA9548 i2cMux(SDA, SCL);
00032  * VL6180 laser1(SDA, SCL); // on SD0 and SC0
00033  * VL6180 laser2(SDA, SCL); // on SD1 and SC1
00034  * Serial pc(USBTX, USBRX); //USB serial
00035  *
00036  * int main() {
00037  *   float reading;
00038  *   i2cMux.ch(CH0);
00039  *   reading = laser1.read();
00040  *   pc.printf("Laser 1: %f\n", reading);
00041  *   i2cMux.ch(CH1); //for two channels: i2cMux.ch(CH1 | CH5);
00042  *   pc.printf("Laser 2: %f\n", reading);
00043  *   reading = laser2.read();
00044  *       
00045  *   while(1) {
00046  *   }
00047  * }
00048  * @endcode
00049  */
00050 
00051 class TCA9548 {
00052     private:
00053         I2C i2c;
00054         uint8_t channel;
00055         void i2cWrite8(uint8_t addr, char data);
00056     public:
00057         /** Initialize object with default i2c pins */
00058         TCA9548();
00059         
00060         /** Initialize object with specific i2c pins
00061         * 
00062         * @param i2c_sda SDA pin
00063         * @param i2c_scl SCL pin
00064         */
00065         TCA9548(PinName i2c_sda, PinName i2c_scl);
00066         
00067         /** Boot up the mux and change to initial channel (defaults to 0)
00068         * 
00069         * @param iCh Initial Channel
00070         * @return 
00071         *   1 if failed
00072         *`  0 if successful
00073         */
00074         bool init(uint8_t iCh);
00075         bool init();
00076         
00077         /** Changes the i2c channel
00078         * 
00079         * @param newCh New channel for mux
00080         */
00081         void ch(uint8_t newCh);
00082 };
00083 
00084 #endif