Charith Dassanayake / CPPToPigpio
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2C.h Source File

I2C.h

00001 //I2C.h
00002 #ifndef CPP_TO_PIGPIO_I2C_HEADER
00003 #define CPP_TO_PIGPIO_I2C_HEADER
00004 namespace CPPToPigpio{
00005     /** The I2C interface provides I2C Master functionality.
00006      */
00007     class I2C: public CPPToPigpio{
00008         private:
00009             short _pinsda;
00010             short _pinscl;
00011             int _clk;
00012             int _handle;//current i2c device handle
00013             unsigned _I2CBus; //1 or 0 depending on bus being used (cannot mix)
00014             bool _repeated;
00015             int _baud;
00016             bool _bbstarted;
00017             
00018         public:
00019             /** Create an I2C Master interface, connected to the specified pins
00020              *
00021              *  @param sda I2C data line pin
00022              *  @param scl I2C clock line pin
00023              */
00024             I2C(PinName sda, PinName scl); //Construct I2C
00025             
00026             /** Create an I2C Master interface, connected to the specified pins
00027              *
00028              *  @param sda I2C data line pin
00029              *  @param scl I2C clock line pin
00030              */
00031             I2C(int sda, int scl); //Used for int args
00032             
00033             /** Set the frequency of the I2C clock frequency for bit banging
00034              *
00035              *  @param hz The bus frequency in hertz
00036              */
00037             void frequency(int hz);
00038             
00039             /** Read from an I2C slave
00040              *
00041              * Performs a complete read transaction. 
00042              * Read "length" bytes from device at "address" into "buf". The bottom bit of
00043              * the address is forced to 1 to indicate a read.
00044              *
00045              *  @param address 8-bit I2C slave address [ addr | 1 ]
00046              *  @param data Pointer to the byte-array to read data in to
00047              *  @param length Number of bytes to read
00048              *  @param repeated Repeated start, true - don't send stop at end
00049              *
00050              *  @returns
00051              *       0 on success (ack),
00052              *   non-0 on failure (nack)
00053              */
00054             int read(unsigned address, char* buf, int length, bool repeated); //multi-byte read
00055 
00056             /** Read a single byte from the I2C bus
00057              *
00058              *  @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
00059              *
00060              *  @returns
00061              *    the byte read
00062              */
00063             int read(int ack);//single-byte read (??)
00064 
00065             /** Write to an I2C slave
00066              *
00067              * Performs a complete write transaction. 
00068              * Write "length" bytes from buf to device at "address". The bottom bit of
00069              * the address is forced to 0 to indicate a write.
00070              *
00071              *  @param address 8-bit I2C slave address [ addr | 0 ]
00072              *  @param data Pointer to the byte-array data to send
00073              *  @param length Number of bytes to send
00074              *  @param repeated Repeated start, true - do not send stop at end
00075              *
00076              *  @returns
00077              *       0 on success (ack),
00078              *   non-0 on failure (nack)
00079              */
00080             int write(unsigned address, char* buf, int length, bool repeated); //Set the value of the output
00081             
00082             /** Write single byte out on the I2C bus
00083              *
00084              *  @param data data to write out on bus
00085              *
00086              *  @returns
00087              *    '0' - NAK was received
00088              *    '1' - ACK was received,
00089              *    '2' - timeout
00090              */
00091             int write(int data); //single-byte write
00092             
00093             /** Creates a start condition on the I2C bus
00094             */  
00095             void start(void);
00096 
00097             /** Creates a stop condition on the I2C bus
00098             */
00099             void stop(void);
00100 
00101             /** Allows construction of arbitrary I2C command. 
00102              * Basically a wrapper for pigpio's bbi2cZip
00103              * 
00104              * @param data Pointer to the byte-array to read data in to
00105              * @param length Number of bytes to read
00106              * @param data Pointer to the byte-array data to send
00107              * @param length Number of bytes to send
00108              * @param repeated Repeated start, true - do not send stop at end
00109              */
00110             int bang(char* inBuf, unsigned inLen, char* outBuf, unsigned outLen, bool cont);
00111             int writeAddr(unsigned address);
00112             ~I2C();
00113 
00114             enum cmdEnum{End=0x00, Escape=0x01, Start=0x02, Stop=0x03, Addr=0x04, Flags=0x05, Read=0x06, Write=0x07};
00115             
00116     };
00117 }
00118 
00119 #endif