FabGL
ESP32 Display Controller and Graphics Library
fabgl::I2C Class Reference

I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs. More...

#include <tsi2c.h>

Public Member Functions

 I2C (int bus=0)
 I2C class constructor. More...
 
bool begin (gpio_num_t SDAGPIO, gpio_num_t SCLGPIO)
 Initializes I2C instance associating GPIOs to I2C signals. More...
 
int getMaxBufferLength ()
 Returns maximum size of read and write buffers. More...
 
int read (int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
 Receives a buffer from I2C bus. More...
 
bool write (int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
 Sends a buffer to I2C bus. More...
 

Detailed Description

I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs.

A single instance of I2C class can be shared among multiple tasks or timers (not interrupts).

Example:

fabgl::I2C I2C;

void setup() {
  I2C.begin(GPIO_NUM_4, GPIO_NUM_15);  // 4 = SDA, 15 = SCL
}
Examples:
SSD1306_OLED/128x32/CollisionDetection/CollisionDetection.ino, SSD1306_OLED/128x32/SimpleTerminalOut/SimpleTerminalOut.ino, SSD1306_OLED/128x64/CollisionDetection/CollisionDetection.ino, SSD1306_OLED/128x64/NetworkTerminal/NetworkTerminal.ino, and SSD1306_OLED/128x64/SimpleTerminalOut/SimpleTerminalOut.ino.

Constructor & Destructor Documentation

◆ I2C()

fabgl::I2C::I2C ( int  bus = 0)

I2C class constructor.

Parameters
busI2C bus to use. Allowed 0 or 1 (default is 0).

Member Function Documentation

◆ begin()

bool fabgl::I2C::begin ( gpio_num_t  SDAGPIO,
gpio_num_t  SCLGPIO 
)

Initializes I2C instance associating GPIOs to I2C signals.

Parameters
SDAGPIOPin to use for SDA signal
SCLGPIOPin to use for SCK signal

Example:

// 4 = SDA, 15 = SCL
I2C.begin(GPIO_NUM_4, GPIO_NUM_15);
Examples:
SSD1306_OLED/128x32/CollisionDetection/CollisionDetection.ino, SSD1306_OLED/128x32/SimpleTerminalOut/SimpleTerminalOut.ino, SSD1306_OLED/128x64/CollisionDetection/CollisionDetection.ino, SSD1306_OLED/128x64/NetworkTerminal/NetworkTerminal.ino, and SSD1306_OLED/128x64/SimpleTerminalOut/SimpleTerminalOut.ino.

◆ getMaxBufferLength()

int fabgl::I2C::getMaxBufferLength ( )
inline

Returns maximum size of read and write buffers.

Returns
Maximum size in bytes

◆ read()

int fabgl::I2C::read ( int  address,
uint8_t *  buffer,
int  size,
int  frequency = 100000,
int  timeOutMS = 50 
)

Receives a buffer from I2C bus.

This is a thread-safe operation. Multiple tasks can call this methods concurrently.

Parameters
addressI2C address of the source slave device
bufferBuffer where to put received data (must have space for at least "size" bytes)
sizeNumber of bytes to receive. Maximum size is the return value of getMaxBufferLength().
frequencyClock frequency
timeOutMSOperation timeout in milliseconds
Returns
Number of actually read bytes

◆ write()

bool fabgl::I2C::write ( int  address,
uint8_t *  buffer,
int  size,
int  frequency = 100000,
int  timeOutMS = 50 
)

Sends a buffer to I2C bus.

This is a thread-safe operation. Multiple tasks can call this method concurrently.

Parameters
addressI2C address of the destination slave device
bufferBuffer to send.
sizeNumber of bytes to send. Maximum size is the return value of getMaxBufferLength().
frequencyClock frequency
timeOutMSOperation timeout in milliseconds
Returns
True on success

The documentation for this class was generated from the following files: