first upload

This commit is contained in:
fatimaxsen committed 2025-04-24 09:34:04 +03:00
commit 1f9edb14b4
13 files changed
+357

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
#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
@@ -0,0 +1,36 @@
#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
@@ -0,0 +1,14 @@
#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
+35
View File
@@ -0,0 +1,35 @@
#include <Wire.h>
#include <Arduino.h>
#include "I2CRelay.h"
#include "I2CMasterUtils.h"
#include "I2CCommands.h"
RelayI2CMaster::RelayI2CMaster(uint8_t slaveAddress) : _slaveAddress(slaveAddress) {}
void RelayI2CMaster::setAddress(uint8_t newAddress) {
_slaveAddress = newAddress;
}
void RelayI2CMaster::begin() {
Wire.begin();
}
void RelayI2CMaster::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 count = 0;
for(uint8_t addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
if(Wire.endTransmission() == 0) {
if(count < maxDevices) {
foundAddresses[count++] = addr;
}
}
}
return count;
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef RELAY_I2C_MASTER_H
#define RELAY_I2C_MASTER_H
#include <Arduino.h>
#include <Wire.h>
class RelayI2CMaster {
public:
RelayI2CMaster(uint8_t slaveAddress);
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();
private:
uint8_t _slaveAddress;
};
#endif