Prusa MINI Firmware overview
TWIBus Class Reference

#include <twibus.h>

Collaboration diagram for TWIBus:

Public Member Functions

 TWIBus ()
 Class constructor. More...
 
void reset ()
 Reset the buffer. More...
 
void send ()
 Send the buffer data to the bus. More...
 
void addbyte (const char c)
 Add one byte to the buffer. More...
 
void addbytes (char src[], uint8_t bytes)
 Add some bytes to the buffer. More...
 
void addstring (char str[])
 Add a null-terminated string to the buffer. More...
 
void address (const uint8_t adr)
 Set the target slave address. More...
 
void echobuffer (const char prefix[], uint8_t adr)
 Echo data in the buffer to serial. More...
 
bool request (const uint8_t bytes)
 Request data from the slave device and wait. More...
 
uint8_t capture (char *dst, const uint8_t bytes)
 Capture data from the bus into the buffer. More...
 
void relay (const uint8_t bytes)
 Request data from the slave device, echo to serial. More...
 

Static Public Member Functions

static void echoprefix (uint8_t bytes, const char prefix[], uint8_t adr)
 Prefix for echo to serial. More...
 
static void echodata (uint8_t bytes, const char prefix[], uint8_t adr)
 Echo data on the bus to serial. More...
 
static void flush ()
 Flush the i2c bus. More...
 

Public Attributes

uint8_t addr = 0
 Target device address @description The target device address. Persists until changed. More...
 

Detailed Description

TWIBUS class

This class implements a wrapper around the two wire (I2C) bus, allowing Marlin to send and request data from any slave device on the bus.

The two main consumers of this class are M260 and M261. M260 provides a way to send an I2C packet to a device (no repeated starts) by caching up to 32 bytes in a buffer and then sending the buffer. M261 requests data from a device. The received data is relayed to serial out for the host to interpret.

For more information see

Constructor & Destructor Documentation

◆ TWIBus()

TWIBus::TWIBus ( )

Class constructor.

Initialize the TWI bus and clear the buffer

Member Function Documentation

◆ reset()

void TWIBus::reset ( )

Reset the buffer.

Set the buffer to a known-empty state

◆ send()

void TWIBus::send ( )

Send the buffer data to the bus.

Flush the buffer to the target address

◆ addbyte()

void TWIBus::addbyte ( const char  c)

Add one byte to the buffer.

Add a byte to the end of the buffer. Silently fails if the buffer is full.

Parameters
ca data byte

◆ addbytes()

void TWIBus::addbytes ( char  src[],
uint8_t  bytes 
)

Add some bytes to the buffer.

Add bytes to the end of the buffer. Concatenates at the buffer size.

Parameters
srcsource data address
bytesthe number of bytes to add

◆ addstring()

void TWIBus::addstring ( char  str[])

Add a null-terminated string to the buffer.

Add bytes to the end of the buffer up to a nul. Concatenates at the buffer size.

Parameters
strsource string address

◆ address()

void TWIBus::address ( const uint8_t  adr)

Set the target slave address.

The target slave address for sending the full packet

Parameters
adr7-bit integer address

◆ echoprefix()

static void TWIBus::echoprefix ( uint8_t  bytes,
const char  prefix[],
uint8_t  adr 
)
static

Prefix for echo to serial.

Echo a label, length, address, and "data:"

Parameters
bytesthe number of bytes to request

◆ echodata()

static void TWIBus::echodata ( uint8_t  bytes,
const char  prefix[],
uint8_t  adr 
)
static

Echo data on the bus to serial.

Echo some number of bytes from the bus to serial in a parser-friendly format.

Parameters
bytesthe number of bytes to request

◆ echobuffer()

void TWIBus::echobuffer ( const char  prefix[],
uint8_t  adr 
)

Echo data in the buffer to serial.

Echo the entire buffer to serial to serial in a parser-friendly format.

Parameters
bytesthe number of bytes to request

◆ request()

bool TWIBus::request ( const uint8_t  bytes)

Request data from the slave device and wait.

Request a number of bytes from a slave device. Wait for the data to arrive, and return true on success.

Parameters
bytesthe number of bytes to request
Returns
status of the request: true=success, false=fail

◆ capture()

uint8_t TWIBus::capture ( char *  dst,
const uint8_t  bytes 
)

Capture data from the bus into the buffer.

Capture data after a request has succeeded.

Parameters
bytesthe number of bytes to request
Returns
the number of bytes captured to the buffer

◆ flush()

static void TWIBus::flush ( )
static

Flush the i2c bus.

Get all bytes on the bus and throw them away.

◆ relay()

void TWIBus::relay ( const uint8_t  bytes)

Request data from the slave device, echo to serial.

Request a number of bytes from a slave device and output the returned data to serial in a parser-friendly format.

Parameters
bytesthe number of bytes to request

Member Data Documentation

◆ addr

uint8_t TWIBus::addr = 0

Target device address @description The target device address. Persists until changed.