3.0 KiB
I2C Communication Library
This library provides functionalities for I2C communication, specifically designed for controlling devices that support the I2C protocol. It includes utilities for scanning I2C buses, changing slave addresses, and handling commands.
Table of Contents
Overview
The library provides functionalities for I2C communication, specifically designed for controlling devices that support the I2C protocol. It includes utilities for scanning I2C buses, changing slave addresses, and handling commands.
Files
The library is composed of several files, each with a specific role:
-
I2CMasterUtils.h [3]
- This header file defines the
I2CMasterUtils
class which contains static methods for I2C operations such as scanning for devices and changing slave addresses.
- This header file defines the
-
I2CMasterUtils.cpp [2]
- Contains the implementation of methods defined in
I2CMasterUtils.h
.
- Contains the implementation of methods defined in
-
I2CRelay.h [5]
- Defines the
I2CRelay
class, which extends the functionality to control relays over I2C.
- Defines the
-
I2CRelay.cpp [4]
- Contains the implementation of methods for the
I2CRelay
class.
- Contains the implementation of methods for the
Class: I2CMasterUtils
Static Methods
-
scanI2C(Stream &output = Serial)
- Scans the I2C bus for connected devices and prints their addresses.
-
changeSlaveAddress(uint8_t oldAddr, uint8_t newAddr, Stream &output = Serial)
- Sends a command to change the slave address of a device.
-
parseChangeAddressCommand(const String &input, uint8_t &oldAddr, uint8_t &newAddr)
- Parses an input string to extract the old and new slave addresses from a "ch" command.
Class: I2CRelay
Methods
-
I2CRelay(uint8_t slaveAddress)
- Constructor that initializes the
I2CRelay
object with the given slave address.
- Constructor that initializes the
-
begin()
- Initializes the I2C bus for communication.
-
setRelay(uint8_t relayIndex, bool state)
- Sets the state of a specific relay on the slave device.
-
scanDevices(uint8_t foundAddresses, uint8_t maxDevices)*
- Scans the I2C bus for connected devices and returns their addresses up to
maxDevices
.
- Scans the I2C bus for connected devices and returns their addresses up to
-
setAddress(uint8_t newAddress)
- Changes the address of the relay slave.
-
handleI2CCommand()
- Handles incoming I2C commands.
Usage Example
To use this library, you would typically include the necessary files and create instances of I2CMasterUtils
and I2CRelay
. Here’s a simple example:
#include "I2CMasterUtils.h"
#include "I2CRelay.h"
void setup() {
Serial.begin(9600);
I2CMasterUtils::scanI2C();
uint8_t newAddress = 0x54;
uint8_t oldAddress = CMD_CHANGE_ADDR; // Assuming CMD_CHANGE_ADDR is defined in I2CCommands.h
I2CMasterUtils::changeSlaveAddress(oldAddress, newAddress);
}
void loop() {
// Example usage of I2CRelay
I2CRelay relay(0x54); // Use the newly assigned address
relay.begin();
relay.setRelay(1, HIGH); // Set relay 1 to ON
}