I2CRelay/src/I2CRelay.cpp
2025-04-24 10:09:36 +03:00

36 lines
873 B
C++

#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;
}
void I2CRelay::begin() {
Wire.begin();
}
void I2CRelay::setRelay(uint8_t relayIndex, bool state) {
Wire.beginTransmission(_slaveAddress);
Wire.write(relayIndex);
Wire.write(state ? 1 : 0);
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;
}
}
}
return count;
}