2025-04-24 13:49:25 +03:00
2025-04-24 10:09:36 +03:00
2025-04-24 10:09:36 +03:00
2025-04-24 11:54:16 +03:00
2025-04-24 09:34:04 +03:00
2025-04-24 10:18:41 +03:00
2025-04-24 09:34:04 +03:00
2025-04-24 13:49:25 +03:00

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

  1. Overview
  2. Files
  3. Class: I2CMasterUtils
  4. Class: I2CRelay
  5. Usage Example

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:

  1. 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.
  2. I2CMasterUtils.cpp [2]

    • Contains the implementation of methods defined in I2CMasterUtils.h.
  3. I2CRelay.h [5]

    • Defines the I2CRelay class, which extends the functionality to control relays over I2C.
  4. I2CRelay.cpp [4]

    • Contains the implementation of methods for the I2CRelay class.

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.
  • 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.
  • 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. Heres 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
}
Description
No description provided
Readme MIT 160 KiB
2025-04-24 16:13:08 +03:00
Languages
C++ 100%