Updating Readme file, and src files

This commit is contained in:
fatimaxsen committed 2025-05-12 11:24:46 +03:00
1 parent 2ea33655a5
commit 400a727892
10 files changed
+239 -272

No files matched your search

-6
View File
@@ -1,6 +0,0 @@
#ifndef I2C_COMMANDS_H
#define I2C_COMMANDS_H
#define CMD_CHANGE_ADDR 0xAA // Use the same value as in your slave
#endif
-36
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(CMD_CHANGE_ADDR); // Use the command code from I2CCommands.h
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;
}
-14
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
+91 -24
View File
@@ -1,35 +1,102 @@
#include <Wire.h>
#include <Arduino.h>
#include "I2CRelay.h"
#include "I2CMasterUtils.h"
#include "I2CCommands.h"
I2CRelay::I2CRelay(uint8_t slaveAddress) : _slaveAddress(slaveAddress) {}
void I2CRelay::setAddress(uint8_t newAddress) {
_slaveAddress = newAddress;
}
I2CRelay::I2CRelay(uint8_t address, uint8_t sda, uint8_t scl)
: _address(address), _sda(sda), _scl(scl) {}
void I2CRelay::begin() {
Wire.begin();
Wire.begin(_sda, _scl);
}
void I2CRelay::setRelay(uint8_t relayIndex, bool state) {
Wire.beginTransmission(_slaveAddress);
Wire.write(relayIndex);
Wire.write(state ? 1 : 0);
void I2CRelay::setRelay(uint8_t relayNum, uint8_t state) {
if (relayNum < 1 || relayNum > 8) return;
Wire.beginTransmission(_address);
Wire.write(0xA0);
Wire.write(relayNum - 1); // Map 1-8 to 0-7
Wire.write(state);
Wire.endTransmission();
}
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);
if(Wire.endTransmission() == 0) {
if(count < maxDevices) {
foundAddresses[count++] = addr;
}
}
void I2CRelay::setAllRelays(uint8_t state) {
for (uint8_t i = 1; i <= 8; i++) {
setRelay(i, state);
delay(10);
}
}
void I2CRelay::setDeviceName(const String &name) {
Wire.beginTransmission(_address);
Wire.write(0xD0);
for (uint8_t i = 0; i < name.length() && i < 24; i++) {
Wire.write(name[i]);
}
Wire.endTransmission();
}
void I2CRelay::setI2CAddress(uint8_t newAddr) {
if (newAddr < 8 || newAddr > 119) return;
Wire.beginTransmission(_address);
Wire.write(0xC0);
Wire.write(newAddr);
Wire.endTransmission();
}
String I2CRelay::getIdentification() {
Wire.requestFrom(_address, (uint8_t)40);
String id = "";
while (Wire.available()) {
char c = Wire.read();
id += c;
}
return id;
}
void I2CRelay::handleSerialCommand(const String &cmd) {
if (cmd.startsWith("r ")) {
int space1 = cmd.indexOf(' ');
int space2 = cmd.indexOf(' ', space1 + 1);
if (space1 > 0 && space2 > space1) {
int relay = cmd.substring(space1 + 1, space2).toInt();
int state = cmd.substring(space2 + 1).toInt();
if (relay >= 1 && relay <= 8 && (state == 0 || state == 1)) {
setRelay(relay, state);
Serial.print("Relay ");
Serial.print(relay);
Serial.print(" set to ");
Serial.println(state ? "ON" : "OFF");
} else {
Serial.println("Invalid relay number or state. Use 1-8 and 0/1.");
}
} else {
Serial.println("Usage: r <relayNum 1-8> <0|1>");
}
} else if (cmd.startsWith("all ")) {
int state = cmd.substring(4).toInt();
if (state == 0 || state == 1) {
setAllRelays(state);
Serial.print("All relays set to ");
Serial.println(state ? "ON" : "OFF");
} else {
Serial.println("Invalid state for all.");
}
} else if (cmd.startsWith("name ")) {
String name = cmd.substring(5);
setDeviceName(name);
Serial.print("Device name set to: ");
Serial.println(name);
} else if (cmd.startsWith("addr ")) {
int newAddr = cmd.substring(5).toInt();
if (newAddr >= 8 && newAddr <= 119) {
setI2CAddress(newAddr);
Serial.print("I2C address set to: ");
Serial.println(newAddr, HEX);
} else {
Serial.println("Invalid address.");
}
} else if (cmd == "id") {
String id = getIdentification();
Serial.print("ID: ");
Serial.println(id);
} else {
Serial.println("Unknown command.");
}
return count;
}
+16 -9
View File
@@ -1,20 +1,27 @@
#ifndef I2CRelay_H
#define I2CRelay_H
#ifndef I2CRELAY_H
#define I2CRELAY_H
#include <Arduino.h>
#include <Wire.h>
class I2CRelay {
public:
I2CRelay(uint8_t slaveAddress);
I2CRelay(uint8_t address = 0x08, uint8_t sda = 8, uint8_t scl = 3);
void begin();
void setRelay(uint8_t relayIndex, bool state);
uint8_t scanDevices(uint8_t* foundAddresses, uint8_t maxDevices);
void setAddress(uint8_t newAddress);
void handleI2CCommand();
void setRelay(uint8_t relayNum, uint8_t state);
void setAllRelays(uint8_t state);
void setDeviceName(const String &name);
void setI2CAddress(uint8_t newAddr);
String getIdentification();
// Helper for command parsing (optional)
void handleSerialCommand(const String &cmd);
private:
uint8_t _slaveAddress;
uint8_t _address;
uint8_t _sda;
uint8_t _scl;
};
#endif