Drivers for I2C Communication

These drivers support I2C devices. They provide a universal I2C interface for sensor communication across hardware architectures.

Note: in the future these drivers will support Linux and firmware devices.

Prerequisites

  • Linux device with an AMG8833 connected via I2C, such as any version of Forward Loop Zero with the optional grid-eye infrared.
  • (Optional) Docker on the Linux device. We recommend you check out floop, our simple, open-source tool for working with embedded Docker on multiple devices at the same time.

Install

You can install the C++ header files for use in your own applications.

In order to use the drivers with Linux, you need to install some I2C dependencies:

sudo apt-get install -y python-smbus python-dev i2c-tools libi2c-dev

You then need to install the I2C headers, then include them during compilation of your application. Inside of your application folder, you can install the headers:

mkdir -p ./floop/i2c/ && \
wget -O ./floop/i2c/i2c.h https://github.com/ForwardLoopLLC/i2c/blob/master/linux/i2c.h

When you compile your application, make sure to include the path to the driver headers. For example, add -I./floop/ during compilation. You can then access the drivers by including the i2c header:

#include "linux/i2c.h"

Note that you can also use these steps to install the drivers inside of a Docker container.

Example

The example shows the major functionality of the drivers.

#include "linux/i2c.h" //drivers

int main() {
    // Start communicating on I2C bus 0 at address 0x68
    I2C i2c(0, 0x68); 
    if(i2c.error()) {
        printf("ERROR: I2C failed to initialize.\n");
    }
    printf("I2c initialized.\n");

    // configure SMBus packet error checking
    if(!i2c.enablePacketErrorChecking()) {
        printf("ERROR: failed to enable PEC.\n");
    }
    if(!i2c.disablePacketErrorChecking()) {
        printf("ERROR: failed to disable PEC.\n");
    }

    // read bytes, words, and blocks of bytes
    // read a byte from register 0x00
    uint8_t byte = i2c.read_byte(0x00); 

    // read a word from register 0x00
    uint16_t word = i2c.read_word(0x00); 

    // read eight bytes starting from register 0x00
    uint8_t block[8];
    if(!i2c.read_block(0x00, 8, block)) {
        printf("ERROR: failed to read block of bytes.\n");
    }

    // write bytes and words
    // write 0xFF to register 0x00
    if(!i2c.write_byte(0x00, 0xFF)) {
        printf("ERROR: failed to write byte.\n");
    }

    // write 0xFFFF to register 0x00
    if(!i2c.write_word(0x00, 0xFFFF)) {
        printf("ERROR: failed to write word.\n");
    }

    return 0;
}

API Documentation

C++ API
Driver interface for I2C
Source Code
Our MIT license lets you use, change, and sell the drivers for free
Forward Loop Docs
Check out other drivers, develop tools, and code