Added new utility library to drive a PCF8574/PCF8574A for the SAB
This commit is contained in:
parent
6ff9fcb242
commit
afc11705db
50
src/libs/PCF8574/PCF8574.cpp
Normal file
50
src/libs/PCF8574/PCF8574.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include "PCF8574.h"
|
||||
|
||||
PCF8574::PCF8574(uint8_t address, TwoWire &twc) : _twc(twc),_address(address),_pinConfig(0b11111111)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void PCF8574::begin()
|
||||
{
|
||||
_twc.begin();
|
||||
_twc.beginTransmission(_address);
|
||||
_twc.write(_pinConfig);
|
||||
_twc.endTransmission();
|
||||
}
|
||||
|
||||
void PCF8574::pinMode(Pin pin, boolean mode)
|
||||
{
|
||||
uint8_t mask = mode ? ~pin : pin; //0 INPUT - 1 OUTPUT
|
||||
|
||||
if(mode)
|
||||
_pinConfig = _pinConfig & mask;
|
||||
else
|
||||
_pinConfig = _pinConfig | mask;
|
||||
|
||||
_pinConfig = _pinConfig | mask;
|
||||
_twc.beginTransmission(_address);
|
||||
_twc.write(_pinConfig);
|
||||
_twc.endTransmission();
|
||||
}
|
||||
|
||||
void PCF8574::digitalWrite(Pin pin, boolean mode)
|
||||
{
|
||||
uint8_t mask = mode ? pin : ~pin; //0 LOW, 1 HIGH
|
||||
if(mode)
|
||||
_pinConfig = _pinConfig | mask;
|
||||
else
|
||||
_pinConfig = _pinConfig & mask;
|
||||
|
||||
_twc.beginTransmission(_address);
|
||||
_twc.write(_pinConfig);
|
||||
_twc.endTransmission();
|
||||
}
|
||||
|
||||
boolean PCF8574::digitalRead(Pin pin)
|
||||
{
|
||||
uint8_t reg = 0b00000000;
|
||||
_twc.requestFrom((uint8_t)_address,(uint8_t)1,(uint8_t)true);
|
||||
while(_twc.available())reg = _twc.read();
|
||||
return (reg & pin) == 0 ? 0 : 1;
|
||||
}
|
29
src/libs/PCF8574/PCF8574.h
Normal file
29
src/libs/PCF8574/PCF8574.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef PCF8574_H
|
||||
#define PCF8574_H
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
/***
|
||||
* A very simple class to drive a PCF8574 chip
|
||||
*
|
||||
* Anatole SCHRAMM-HENRY -- Th3maz1ng
|
||||
***/
|
||||
|
||||
class PCF8574
|
||||
{
|
||||
public:
|
||||
enum Pin{P0 = 1, P1 = 2, P2 = 4, P3 = 8, P4 = 16, P5 = 32, P6 = 64, P7 = 128};
|
||||
|
||||
PCF8574(uint8_t address, TwoWire &twc = Wire);
|
||||
void begin();
|
||||
void pinMode(Pin pin, boolean mode = INPUT);
|
||||
void digitalWrite(Pin pin, boolean mode);
|
||||
boolean digitalRead(Pin pin);
|
||||
private:
|
||||
TwoWire &_twc;
|
||||
uint8_t _address;
|
||||
uint8_t _pinConfig;
|
||||
protected:
|
||||
};
|
||||
|
||||
#endif
|
22
src/libs/PCF8574/PCF8574.ino
Normal file
22
src/libs/PCF8574/PCF8574.ino
Normal file
@ -0,0 +1,22 @@
|
||||
#include "PCF8574.h"
|
||||
|
||||
//Example programm with blink on P0 and digitalRead on P1 - P7
|
||||
|
||||
PCF8574 pcf(0x27);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
// put your setup code here, to run once:
|
||||
pcf.begin();
|
||||
pcf.pinMode(PCF8574::P0,OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
Serial.print(pcf.digitalRead(PCF8574::P0));Serial.print(pcf.digitalRead(PCF8574::P1));Serial.print(pcf.digitalRead(PCF8574::P2));Serial.print(pcf.digitalRead(PCF8574::P3));Serial.print(pcf.digitalRead(PCF8574::P4));Serial.print(pcf.digitalRead(PCF8574::P5));Serial.print(pcf.digitalRead(PCF8574::P6));Serial.println(pcf.digitalRead(PCF8574::P7));
|
||||
//pcf.digitalRead(PCF8574::P0);
|
||||
pcf.pinMode(PCF8574::P0, HIGH);
|
||||
delay(200);
|
||||
pcf.pinMode(PCF8574::P0, LOW);
|
||||
delay(200);
|
||||
}
|
Loading…
Reference in New Issue
Block a user