2020-05-23 07:33:43 -04:00
|
|
|
#pragma once
|
2013-09-30 12:15:02 -04:00
|
|
|
//
|
2021-12-23 06:51:26 -05:00
|
|
|
// FILE: PCF8574.h
|
2013-09-30 12:15:02 -04:00
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// DATE: 02-febr-2013
|
2023-09-23 11:22:26 -04:00
|
|
|
// VERSION: 0.3.9
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: Arduino library for PCF8574 - 8 channel I2C IO expander
|
2020-05-23 07:33:43 -04:00
|
|
|
// URL: https://github.com/RobTillaart/PCF8574
|
|
|
|
// http://forum.arduino.cc/index.php?topic=184800
|
2013-09-30 12:15:02 -04:00
|
|
|
|
2021-07-05 04:01:35 -04:00
|
|
|
|
2013-09-30 12:15:02 -04:00
|
|
|
#include "Arduino.h"
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "Wire.h"
|
2013-09-30 12:15:02 -04:00
|
|
|
|
2021-07-05 04:01:35 -04:00
|
|
|
|
2023-09-23 11:22:26 -04:00
|
|
|
#define PCF8574_LIB_VERSION (F("0.3.9"))
|
2016-05-02 13:23:06 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#ifndef PCF8574_INITIAL_VALUE
|
2021-12-23 06:51:26 -05:00
|
|
|
#define PCF8574_INITIAL_VALUE 0xFF
|
2021-01-29 06:31:58 -05:00
|
|
|
#endif
|
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
#define PCF8574_OK 0x00
|
|
|
|
#define PCF8574_PIN_ERROR 0x81
|
|
|
|
#define PCF8574_I2C_ERROR 0x82
|
2016-05-02 13:23:06 -04:00
|
|
|
|
2013-09-30 12:15:02 -04:00
|
|
|
|
|
|
|
class PCF8574
|
|
|
|
{
|
2015-05-09 11:07:09 -04:00
|
|
|
public:
|
2021-12-23 06:51:26 -05:00
|
|
|
explicit PCF8574(const uint8_t deviceAddress = 0x20, TwoWire *wire = &Wire);
|
2015-05-09 11:07:09 -04:00
|
|
|
|
2020-05-23 07:33:43 -04:00
|
|
|
#if defined (ESP8266) || defined(ESP32)
|
2022-04-11 04:50:17 -04:00
|
|
|
bool begin(int sda, int scl, uint8_t value = PCF8574_INITIAL_VALUE);
|
2020-05-23 07:33:43 -04:00
|
|
|
#endif
|
2021-12-23 06:51:26 -05:00
|
|
|
bool begin(uint8_t value = PCF8574_INITIAL_VALUE);
|
2021-01-29 06:31:58 -05:00
|
|
|
bool isConnected();
|
2016-05-02 13:23:06 -04:00
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2022-10-19 05:36:34 -04:00
|
|
|
// note: setting the address corrupt internal buffer values
|
|
|
|
// a read8() / write8() call updates them.
|
2021-07-05 04:01:35 -04:00
|
|
|
bool setAddress(const uint8_t deviceAddress);
|
2023-02-04 10:53:26 -05:00
|
|
|
uint8_t getAddress();
|
2021-07-05 04:01:35 -04:00
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t read8();
|
2021-07-05 04:01:35 -04:00
|
|
|
uint8_t read(const uint8_t pin);
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t value() const { return _dataIn; };
|
2015-05-09 11:07:09 -04:00
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void write8(const uint8_t value);
|
|
|
|
void write(const uint8_t pin, const uint8_t value);
|
|
|
|
uint8_t valueOut() const { return _dataOut; }
|
2016-05-01 04:02:01 -04:00
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2022-10-19 05:36:34 -04:00
|
|
|
// added 0.1.07/08 Septillion
|
2022-06-18 03:44:19 -04:00
|
|
|
uint8_t readButton8() { return PCF8574::readButton8(_buttonMask); }
|
|
|
|
uint8_t readButton8(const uint8_t mask);
|
|
|
|
uint8_t readButton(const uint8_t pin);
|
|
|
|
void setButtonMask(const uint8_t mask) { _buttonMask = mask; };
|
|
|
|
uint8_t getButtonMask() { return _buttonMask; };
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2016-05-01 04:02:01 -04:00
|
|
|
|
2022-10-19 05:36:34 -04:00
|
|
|
// rotate, shift, toggle, reverse expect all lines are output
|
2021-01-29 06:31:58 -05:00
|
|
|
void toggle(const uint8_t pin);
|
2023-02-04 10:53:26 -05:00
|
|
|
// default 0xFF ==> invertAll()
|
|
|
|
void toggleMask(const uint8_t mask = 0xFF);
|
2021-01-29 06:31:58 -05:00
|
|
|
void shiftRight(const uint8_t n = 1);
|
|
|
|
void shiftLeft(const uint8_t n = 1);
|
|
|
|
void rotateRight(const uint8_t n = 1);
|
|
|
|
void rotateLeft(const uint8_t n = 1);
|
|
|
|
void reverse();
|
2015-05-09 11:07:09 -04:00
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2022-06-18 03:44:19 -04:00
|
|
|
void select(const uint8_t pin);
|
|
|
|
void selectN(const uint8_t pin);
|
|
|
|
void selectNone() { write8(0x00); };
|
|
|
|
void selectAll() { write8(0xFF); };
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int lastError();
|
2015-05-09 11:07:09 -04:00
|
|
|
|
2021-12-23 06:51:26 -05:00
|
|
|
|
2015-05-09 11:07:09 -04:00
|
|
|
private:
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t _address;
|
|
|
|
uint8_t _dataIn;
|
|
|
|
uint8_t _dataOut;
|
|
|
|
uint8_t _buttonMask;
|
|
|
|
int _error;
|
2013-09-30 12:15:02 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
TwoWire* _wire;
|
|
|
|
};
|
2020-05-23 07:33:43 -04:00
|
|
|
|
2021-07-05 04:01:35 -04:00
|
|
|
|
2023-02-04 10:53:26 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-23 06:51:26 -05:00
|
|
|
|