2021-01-29 06:31:58 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: PCF8575.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// DATE: 2020-07-20
|
2023-02-04 13:21:27 -05:00
|
|
|
// VERSION: 0.1.8
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: Arduino library for PCF8575 - 16 channel I2C IO expander
|
|
|
|
// URL: https://github.com/RobTillaart/PCF8575
|
|
|
|
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Wire.h"
|
|
|
|
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2023-02-04 13:21:27 -05:00
|
|
|
#define PCF8575_LIB_VERSION (F("0.1.8"))
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
#ifndef PCF8575_INITIAL_VALUE
|
|
|
|
#define PCF8575_INITIAL_VALUE 0xFFFF
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PCF8575_OK 0x00
|
|
|
|
#define PCF8575_PIN_ERROR 0x81
|
|
|
|
#define PCF8575_I2C_ERROR 0x82
|
|
|
|
|
|
|
|
|
|
|
|
class PCF8575
|
|
|
|
{
|
|
|
|
public:
|
2022-11-21 14:26:00 -05:00
|
|
|
// deviceAddress base = 0x20 + depends on address bits
|
2021-12-23 07:53:35 -05:00
|
|
|
explicit PCF8575(const uint8_t deviceAddress = 0x20, TwoWire *wire = &Wire);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
#if defined (ESP8266) || defined(ESP32)
|
2022-04-11 04:49:34 -04:00
|
|
|
bool begin(int sda, int scl, uint16_t value = PCF8575_INITIAL_VALUE);
|
2021-01-29 06:31:58 -05:00
|
|
|
#endif
|
2021-12-23 07:53:35 -05:00
|
|
|
bool begin(uint16_t value = PCF8575_INITIAL_VALUE);
|
2021-01-29 06:31:58 -05:00
|
|
|
bool isConnected();
|
|
|
|
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2022-11-21 14:26:00 -05:00
|
|
|
// note: setting the address may corrupt internal buffer values
|
|
|
|
// a read16() / write16() call updates them.
|
2023-02-04 13:21:27 -05:00
|
|
|
bool setAddress(const uint8_t deviceAddress);
|
|
|
|
uint8_t getAddress();
|
2021-07-09 07:02:35 -04:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint16_t read16();
|
|
|
|
uint8_t read(uint8_t pin);
|
2022-11-21 14:26:00 -05:00
|
|
|
uint16_t value();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void write16(const uint16_t value);
|
|
|
|
void write(const uint8_t pin, const uint8_t value);
|
2022-11-21 14:26:00 -05:00
|
|
|
uint16_t valueOut();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// added 0.1.07/08 Septillion
|
2022-11-21 14:26:00 -05:00
|
|
|
uint16_t readButton16();
|
2021-01-29 06:31:58 -05:00
|
|
|
uint16_t readButton16(const uint16_t mask);
|
|
|
|
uint8_t readButton(const uint8_t pin);
|
2022-11-21 14:26:00 -05:00
|
|
|
void setButtonMask(uint16_t mask);
|
|
|
|
uint16_t getButtonMask();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2022-11-21 14:26:00 -05:00
|
|
|
// rotate, shift, toggle, reverse expect all lines are output
|
2021-01-29 06:31:58 -05:00
|
|
|
void toggle(const uint8_t pin);
|
2022-11-21 14:26:00 -05:00
|
|
|
void toggleMask(const uint16_t mask = 0xFFFF); // 0xFFFF == invertAll()
|
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();
|
|
|
|
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2022-11-21 14:26:00 -05:00
|
|
|
void select(const uint8_t pin);
|
|
|
|
void selectN(const uint8_t pin);
|
|
|
|
void selectNone();
|
|
|
|
void selectAll();
|
2022-06-19 04:49:56 -04:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int lastError();
|
|
|
|
|
2021-12-01 08:50:20 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
private:
|
|
|
|
uint8_t _address;
|
|
|
|
uint16_t _dataIn;
|
|
|
|
uint16_t _dataOut;
|
|
|
|
uint16_t _buttonMask;
|
|
|
|
int _error;
|
|
|
|
|
|
|
|
TwoWire* _wire;
|
|
|
|
};
|
|
|
|
|
2021-07-09 07:02:35 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-23 07:53:35 -05:00
|
|
|
|