57 lines
1.1 KiB
C++
57 lines
1.1 KiB
C++
|
#include <Wire.h>
|
||
|
#include "Pca9554.h"
|
||
|
|
||
|
enum class Pca9554_Registers : uint8_t {
|
||
|
INPUT_PORT = 0x00,
|
||
|
OUTPUT_PORT = 0x01,
|
||
|
POLARITY_INVERSION = 0x02,
|
||
|
CONFIGURATION = 0x03,
|
||
|
};
|
||
|
|
||
|
Pca9554::Pca9554(uint8_t slaveAddress) :
|
||
|
outputValues { 0 }, slaveAddress { slaveAddress }, inputMask { 0 }
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
Pca9554::~Pca9554()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
void Pca9554::begin(uint8_t inputMask, uint8_t outputs)
|
||
|
{
|
||
|
SetInputPins(inputMask);
|
||
|
SendOutputValues(outputValues = outputs);
|
||
|
}
|
||
|
|
||
|
void Pca9554::SetInputPins(uint8_t inputMask)
|
||
|
{
|
||
|
this->inputMask = inputMask;
|
||
|
Wire.beginTransmission(slaveAddress | 0x00);
|
||
|
Wire.write((uint8_t) Pca9554_Registers::CONFIGURATION);
|
||
|
Wire.write(inputMask);
|
||
|
Wire.endTransmission();
|
||
|
}
|
||
|
|
||
|
uint8_t Pca9554::ReadInputs()
|
||
|
{
|
||
|
Wire.beginTransmission(slaveAddress | 0x01);
|
||
|
int result = Wire.read() & inputMask;
|
||
|
Wire.endTransmission();
|
||
|
return (uint8_t) result;
|
||
|
}
|
||
|
|
||
|
void Pca9554::SendOutputValues()
|
||
|
{
|
||
|
SendOutputValues(outputValues);
|
||
|
}
|
||
|
|
||
|
void Pca9554::SendOutputValues(uint8_t values)
|
||
|
{
|
||
|
Wire.beginTransmission(slaveAddress | 0x00);
|
||
|
Wire.write((uint8_t) Pca9554_Registers::OUTPUT_PORT);
|
||
|
Wire.write(values & ~inputMask);
|
||
|
Wire.endTransmission();
|
||
|
}
|