|
ArduboyI2C Library
|
#include <ArduboyI2C.h>
Public Types | |
| enum class | Mode : uint8_t { Async = 0 , Sync = 1 } |
| The mode of an I2C write operation. More... | |
| enum class | Error : uint8_t { WriteAddrNack = TW_MT_SLA_NACK , WriteDataNack = TW_MT_DATA_NACK , ReadAddrNack = TW_MR_SLA_NACK , Bus = TW_BUS_ERROR , None = 0xFF } |
| The error code of the last I2C controller (master) operation. More... | |
| enum class | Role : uint8_t { Controller = 0 , Target = 1 } |
Static Public Member Functions | |
| static void | begin () |
| Initializes I2C hardware. | |
| static void | end () |
| Deinitializes I2C hardware. | |
| static void | setAddress (uint8_t address) |
| Sets the 7-bit address of this device. | |
| static uint8_t | getAddress () |
| Gets the 7-bit address of this device. | |
| static void | write (uint8_t address, const void *buffer, uint8_t size, I2C::Mode mode) |
| Attempts to become the bus controller (master) and sends data over I2C to the specified address. | |
| template<typename T> | |
| static void | write (uint8_t address, const T &object, I2C::Mode mode) |
| template<typename T, size_t N> | |
| static void | write (uint8_t address, const T(&buffer)[N], I2C::Mode mode) |
| static void | read (uint8_t address, void *buffer, uint8_t size) |
| Attempts to become the bus controller (master) and reads data over I2C from the specified address. | |
| template<typename T> | |
| static void | read (uint8_t address, T &object) |
| template<typename T, size_t N> | |
| static void | read (uint8_t address, T(&buffer)[N]) |
| static void | reply (const void *buffer, uint8_t size) |
| Replies back to the controller (master). | |
| template<typename T> | |
| static void | reply (const T &object) |
| template<typename T, size_t N> | |
| static void | reply (const T(&buffer)[N]) |
| static void | onRequest (void(*function)()) |
| Sets up/disables the callback to be called when data is requested from the device's address (a read). | |
| static void | onReceive (void(*function)()) |
| Sets up/disables the callback to be called when data is sent to the device's address (a write). | |
| static Error | getError () |
| Gets the hardware error which happened in a previous read or write. | |
| static uint8_t * | getBuffer () |
| Gets a pointer to the internal buffer used for I2C communication. | |
| static uint8_t | getBufferSize () |
| Gets the size of the data in the internal buffer. | |
| static bool | isActive () |
| Gets whether or not the controller (master) is currently transmitting or receiving data. | |
| static void | checkCableFlipped (void(*startFunction)()=nullptr, void(*loopFunction)()=nullptr) |
| Checks if the I2C cable is flipped, calling a function if it is and waiting for it to be flipped back. | |
| static I2C::Role | handshake (void(*startFunction)()=nullptr, void(*loopFunction)()=nullptr) |
| Waits for another device and returns whether this device is the controller (master) or target (slave). | |
Static Public Attributes | |
| static constexpr uint8_t | targetAddress = 0x08 |
| The 7-bit address of the target (slave) device, set by I2C::handshake(). | |
| static constexpr uint8_t | nullAddress = 0x09 |
| The 7-bit address used to indicate a null or invalid address. | |
Provides all I2C functionality.
|
strong |
The mode of an I2C write operation.
| Enumerator | |
|---|---|
| Async | Asynchronous write. The function will return immediately, and the write will be completed in the background. isActive() checks if the write is still in progress. |
| Sync | Synchronous write. The function will block until the write is complete. |
|
strong |
The error code of the last I2C controller (master) operation.
|
strong |
|
static |
|
static |
|
static |
Sets the 7-bit address of this device.
| address | The 7-bit address to set. Addresses 0-7 and 120-127 are reserved by the standard and should not be used. |
|
static |
Gets the 7-bit address of this device.
|
static |
Attempts to become the bus controller (master) and sends data over I2C to the specified address.
| address | The 7-bit address to send the data to. Addresses 0-7 and 120-127 are reserved by the standard and should not be used. |
| buffer | A pointer to the data to send. |
| size | The amount of data in bytes to send. This cannot be zero. |
| mode | The mode of the write operation. I2C::Mode::Sync blocks until the write is complete; I2C::Mode::Async returns immediately and completes in the background. |
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| T | The type of the object to send. To prevent bugs, T cannot be a pointer. |
| address | The 7-bit address to send the data to. Addresses 0-7 and 120-127 are reserved by the standard and should not be used. |
| object | A reference to the object to send. |
| mode | The mode of the write operation. I2C::Mode::Sync blocks until the write is complete; I2C::Mode::Async returns immediately and completes in the background. |
This function will automatically deduce the size of the object. Objects with sizes greater than or equal to 255 should not be used with this function.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| T | The type of the array to send. |
| N | The number of elements in the array. |
| address | The 7-bit address to send the data to. Addresses 0-7 and 120-127 are reserved by the standard and should not be used. |
| buffer | A reference to the array to send. |
| mode | The mode of the write operation. I2C::Mode::Sync blocks until the write is complete; I2C::Mode::Async returns immediately and completes in the background. |
This function will automatically deduce the size of the array. Arrays with sizes greater than or equal to 255 should not be used with this function.
|
static |
Attempts to become the bus controller (master) and reads data over I2C from the specified address.
| address | The 7-bit address to receive the data from. Addresses 0-7 and 120-127 are reserved by the standard and should not be used. |
| buffer | A pointer to the buffer in which to store the data. |
| size | The maximum amount of bytes to receive. This cannot be 0 or 255. |
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| T | The type of the object to read. To prevent bugs, T cannot be a pointer. |
| address | The 7-bit address to receive the data from. Addresses 0-7 and 120-127 are reserved by the standard and should not be used. |
| object | A reference to the object in which to store the data. |
This function will automatically deduce the size of the object. Objects with sizes greater than or equal to 255 should not be used with this function.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| T | The type of the array to read. |
| N | The number of elements in the array. |
| address | The 7-bit address to receive the data from. Addresses 0-7 and 120-127 are reserved by the standard and should not be used. |
| buffer | A reference to the array in which to store the data. |
This function will automatically deduce the size of the array. Arrays with sizes greater than or equal to 255 should not be used with this function.
|
static |
Replies back to the controller (master).
| buffer | A pointer to the data to reply with. |
| size | The amount of the data in bytes to reply with. |
This function is intended to be called inside the onRequest callback. It fills the I2C buffer with data to then be sent one byte at a time. If it is called more than once, only the last call will be sent.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| T | The type of the object to reply with. To prevent bugs, T cannot be a pointer. |
| object | A reference to the object to reply with. |
This function will automatically deduce the size of the object. Objects with sizes greater than or equal to 255 should not be used with this function.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| T | The type of the array to reply with. |
| N | The number of elements in the array. |
| buffer | A reference to the array to reply with. |
This function will automatically deduce the size of the array. Arrays with sizes greater than or equal to 255 should not be used with this function.
|
static |
Sets up/disables the callback to be called when data is requested from the device's address (a read).
| function | The function to be called when data is requested, or nullptr to disable. |
Example Callback and Usage:
|
static |
Sets up/disables the callback to be called when data is sent to the device's address (a write).
| function | The function to be called when data is received, or nullptr to disable. |
Example Callback and Usage:
|
static |
Gets the hardware error which happened in a previous read or write.
|
static |
Gets a pointer to the internal buffer used for I2C communication.
This function is intended to be used in the onReceive callback to get the data sent by the controller (master).
|
static |
Gets the size of the data in the internal buffer.
This function is intended to be used in the onReceive callback to get the size of the data sent by the controller (master).
|
static |
Gets whether or not the controller (master) is currently transmitting or receiving data.
This function is intended to be used to check if an asynchronous write has completed.
|
static |
Checks if the I2C cable is flipped, calling a function if it is and waiting for it to be flipped back.
| startFunction | The function to be called if the cable is flipped. Pass nullptr to disable. |
| loopFunction | The function to be called while waiting for the cable to be flipped back. Pass nullptr to disable. |
This is only needed on the FX-C, as the Arduboy Mini does not have a way to flip the cable. This method must be used with I2C::handshake or an equivalent handshaking method that sends 0b00000000 at a regular interval. Example Usage:
|
static |
Waits for another device and returns whether this device is the controller (master) or target (slave).
| startFunction | The function to be called while waiting for another device. Pass nullptr to disable. |
| loopFunction | The function to be called while waiting for another device. Pass nullptr to disable. |
|
staticconstexpr |
The 7-bit address used to indicate a null or invalid address.
This is provided for convenience to let a target (slave) soft disconnect from the bus.