updated files

This commit is contained in:
fatimaxsen 2025-04-24 10:09:36 +03:00
parent 1f9edb14b4
commit 4264de514d
6 changed files with 11 additions and 61 deletions

View File

@ -1,36 +0,0 @@
#include "I2CMasterUtils.h"
#include "I2CCommands.h"
void I2CMasterUtils::scanI2C(Stream &output) {
output.println("Scanning for I2C devices...");
for (uint8_t addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
output.print("Found device at 0x");
output.println(addr, HEX);
}
}
}
void I2CMasterUtils::changeSlaveAddress(uint8_t oldAddr, uint8_t newAddr, Stream &output) {
Wire.beginTransmission(oldAddr);
Wire.write(0xAA); // Command code for address change
Wire.write(newAddr);
Wire.endTransmission();
output.print("Sent address change command to 0x");
output.print(oldAddr, HEX);
output.print(" -> 0x");
output.println(newAddr, HEX);
}
bool I2CMasterUtils::parseChangeAddressCommand(const String &input, uint8_t &oldAddr, uint8_t &newAddr) {
if (!input.startsWith("ch")) return false;
int o, n;
int matched = sscanf(input.c_str(), "ch %x %x", &o, &n);
if (matched == 2) {
oldAddr = (uint8_t)o;
newAddr = (uint8_t)n;
return true;
}
return false;
}

View File

@ -1,14 +0,0 @@
#ifndef I2CMASTERUTILS_H
#define I2CMASTERUTILS_H
#include <Arduino.h>
#include <Wire.h>
class I2CMasterUtils {
public:
static void scanI2C(Stream &output = Serial);
static void changeSlaveAddress(uint8_t oldAddr, uint8_t newAddr, Stream &output = Serial);
static bool parseChangeAddressCommand(const String &input, uint8_t &oldAddr, uint8_t &newAddr);
};
#endif

View File

@ -2,7 +2,7 @@
#include <I2CRelay.h>
#include <I2CMasterUtils.h>
RelayI2CMaster relayMaster(0x08); // Default address, can be changed via setAddress()
I2CRelay relayMaster(0x08); // Default address, can be changed via setAddress()
void setup() {
Serial.begin(115200);

View File

@ -1,4 +1,4 @@
name=I2CMasterUtils
name=I2C Relay
version=1.0.0
author=Fatima Idrees <fatimaxidrees@gmail.com>
maintainer=Fatima Idrees <fatimaxidrees@gmail.com>

View File

@ -4,24 +4,24 @@
#include "I2CMasterUtils.h"
#include "I2CCommands.h"
RelayI2CMaster::RelayI2CMaster(uint8_t slaveAddress) : _slaveAddress(slaveAddress) {}
I2CRelay::I2CRelay(uint8_t slaveAddress) : _slaveAddress(slaveAddress) {}
void RelayI2CMaster::setAddress(uint8_t newAddress) {
void I2CRelay::setAddress(uint8_t newAddress) {
_slaveAddress = newAddress;
}
void RelayI2CMaster::begin() {
void I2CRelay::begin() {
Wire.begin();
}
void RelayI2CMaster::setRelay(uint8_t relayIndex, bool state) {
void I2CRelay::setRelay(uint8_t relayIndex, bool state) {
Wire.beginTransmission(_slaveAddress);
Wire.write(relayIndex);
Wire.write(state ? 1 : 0);
Wire.endTransmission();
}
uint8_t RelayI2CMaster::scanDevices(uint8_t* foundAddresses, uint8_t maxDevices) {
uint8_t I2CRelay::scanDevices(uint8_t* foundAddresses, uint8_t maxDevices) {
uint8_t count = 0;
for(uint8_t addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);

View File

@ -1,12 +1,12 @@
#ifndef RELAY_I2C_MASTER_H
#define RELAY_I2C_MASTER_H
#ifndef I2CRelay_H
#define I2CRelay_H
#include <Arduino.h>
#include <Wire.h>
class RelayI2CMaster {
class I2CRelay {
public:
RelayI2CMaster(uint8_t slaveAddress);
I2CRelay(uint8_t slaveAddress);
void begin();
void setRelay(uint8_t relayIndex, bool state);
uint8_t scanDevices(uint8_t* foundAddresses, uint8_t maxDevices);