PCF8574_library/PCF8574.h

267 lines
7.7 KiB
C
Raw Normal View History

2019-04-29 07:17:34 +00:00
/*
* PCF8574 GPIO Port Expand
2020-05-20 19:47:15 +00:00
*
* AUTHOR: Renzo Mischianti
* VERSION: 2.2.2
2020-05-20 19:47:15 +00:00
*
2019-04-29 07:17:34 +00:00
* https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
2017-11-13 20:44:50 +00:00
*
2019-04-29 07:17:34 +00:00
* The MIT License (MIT)
*
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
*
2020-05-20 19:47:15 +00:00
* You may copy, alter and reuse this code in any way you like, but please leave
* reference to www.mischianti.org in your comments if you redistribute this code.
*
2019-04-29 07:17:34 +00:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
2017-11-13 20:44:50 +00:00
*/
2018-05-31 20:54:51 +00:00
#ifndef PCF8574_h
#define PCF8574_h
2017-11-13 20:44:50 +00:00
#include "Wire.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
2020-10-26 22:15:13 +00:00
#define DEFAULT_SDA SDA;
#define DEFAULT_SCL SCL;
2017-11-13 20:44:50 +00:00
// Uncomment to enable printing out nice debug messages.
// #define PCF8574_DEBUG
2017-11-13 20:44:50 +00:00
// Uncomment for low memory usage this prevent use of complex DigitalInput structure and free 7byte of memory
// #define PCF8574_LOW_MEMORY
2020-03-06 20:56:24 +00:00
// Uncomment for low memory usage this prevent use of complex DigitalInput structure and free 7byte of memory
// #define PCF8574_LOW_LATENCY
//#define PCF8574_SOFT_INITIALIZATION
2020-04-11 14:40:51 +00:00
// Select an algorithm to manage encoder progression
#define BASIC_ENCODER_ALGORITHM
// #define MISCHIANTI_ENCODER_ALGORITHM
// #define SEQUENCE_ENCODER_ALGORITHM_REDUCED
// #define SEQUENCE_ENCODER_ALGORITHM
// #define POKI_ENCODER_ALGORITHM
2020-04-11 14:40:51 +00:00
2017-11-13 20:44:50 +00:00
// Define where debug output will be printed.
#define DEBUG_PRINTER Serial
// Setup debug printing macros.
#ifdef PCF8574_DEBUG
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#endif
2020-03-06 20:56:24 +00:00
#ifdef PCF8574_LOW_LATENCY
2020-03-13 20:52:51 +00:00
#define READ_ELAPSED_TIME 0
2020-03-06 20:56:24 +00:00
#else
#define READ_ELAPSED_TIME 10
#endif
2017-11-13 20:44:50 +00:00
//#define P0 B00000001
//#define P1 B00000010
//#define P2 B00000100
//#define P3 B00001000
//#define P4 B00010000
//#define P5 B00100000
//#define P6 B01000000
//#define P7 B10000000
//
#define P0 0
#define P1 1
#define P2 2
#define P3 3
#define P4 4
#define P5 5
#define P6 6
#define P7 7
#include <math.h>
class PCF8574 {
public:
PCF8574(uint8_t address);
PCF8574(uint8_t address, uint8_t interruptPin, void (*interruptFunction)() );
2019-04-29 07:17:34 +00:00
2019-09-13 14:02:23 +00:00
#if !defined(__AVR) && !defined(__STM32F1__) && !defined(TEENSYDUINO)
2019-04-29 07:17:34 +00:00
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
2017-11-13 20:44:50 +00:00
PCF8574(uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)());
2019-04-29 07:17:34 +00:00
#endif
#ifdef ESP32
///// changes for second i2c bus
PCF8574(TwoWire *pWire, uint8_t address);
PCF8574(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl);
PCF8574(TwoWire *pWire, uint8_t address, uint8_t interruptPin, void (*interruptFunction)() );
PCF8574(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)());
#endif
2017-11-13 20:44:50 +00:00
bool begin();
2020-03-06 20:56:24 +00:00
void pinMode(uint8_t pin, uint8_t mode, uint8_t output_start = HIGH);
2017-11-13 20:44:50 +00:00
2020-03-13 20:52:51 +00:00
void encoder(uint8_t pinA, uint8_t pinB);
2020-04-11 14:40:51 +00:00
void attachInterrupt();
void detachInterrupt();
2017-11-13 20:44:50 +00:00
void readBuffer(bool force = true);
2020-03-13 20:52:51 +00:00
uint8_t digitalRead(uint8_t pin, bool forceReadNow = false);
#ifndef PCF8574_LOW_MEMORY
struct DigitalInput {
uint8_t p0;
uint8_t p1;
uint8_t p2;
uint8_t p3;
uint8_t p4;
uint8_t p5;
uint8_t p6;
uint8_t p7;
} digitalInput;
DigitalInput digitalReadAll(void);
2020-10-03 21:31:53 +00:00
bool digitalWriteAll(PCF8574::DigitalInput digitalInput);
#else
byte digitalReadAll(void);
2020-10-03 21:31:53 +00:00
bool digitalWriteAll(byte digitalInput);
#endif
bool digitalWrite(uint8_t pin, uint8_t value);
2017-11-13 20:44:50 +00:00
#ifdef MISCHIANTI_ENCODER_ALGORITHM
bool readEncoderValueMischianti(uint8_t pinA, uint8_t pinB, volatile long *encoderValue, bool reverseRotation = false);
int8_t readEncoderValueMischianti(uint8_t pinA, uint8_t pinB);
#endif
#ifdef POKI_ENCODER_ALGORITHM
bool readEncoderValuePoki(uint8_t pinA, uint8_t pinB, volatile long *encoderValue, bool reverseRotation = false);
int8_t readEncoderValuePoki(uint8_t pinA, uint8_t pinB);
#endif
// bool readEncoderValueEvolved(uint8_t pinA, uint8_t pinB, volatile long *encoderValue, bool reverseRotation = false);
// int8_t readEncoderValueEvolved(uint8_t pinA, uint8_t pinB);
#ifdef SEQUENCE_ENCODER_ALGORITHM
bool readEncoderValueSequence(uint8_t pinA, uint8_t pinB, volatile long *encoderValue, bool reverseRotation = false);
int8_t readEncoderValueSequence(uint8_t pinA, uint8_t pinB);
#endif
#ifdef SEQUENCE_ENCODER_ALGORITHM_REDUCED
bool readEncoderValueSequenceReduced(uint8_t pinA, uint8_t pinB, volatile long *encoderValue, bool reverseRotation = false);
int8_t readEncoderValueSequenceReduced(uint8_t pinA, uint8_t pinB);
#endif
#ifdef BASIC_ENCODER_ALGORITHM
bool readEncoderValue(uint8_t pinA, uint8_t pinB, volatile long *encoderValue, bool reverseRotation = false);
2020-03-13 20:52:51 +00:00
int8_t readEncoderValue(uint8_t pinA, uint8_t pinB);
#endif
2020-03-13 20:52:51 +00:00
int getLatency() const {
return latency;
}
void setLatency(int latency = READ_ELAPSED_TIME) {
this->latency = latency;
}
uint8_t getTransmissionStatusCode() const {
return transmissionStatus;
}
bool isLastTransmissionSuccess(){
return transmissionStatus==0;
}
2017-11-13 20:44:50 +00:00
private:
uint8_t _address;
2020-10-16 10:26:01 +00:00
#if !defined(DEFAULT_SDA)
# if defined(__STM32F1__)
# define DEFAULT_SDA PB7
# elif defined(ESP8266)
# define DEFAULT_SDA 4
# elif defined(SDA)
# define DEFAULT_SDA SDA
# else
2020-10-26 22:23:46 +00:00
# error "Error define DEFAULT_SDA, SDA not declared, if you have this error contact the mantainer"
2020-10-16 10:26:01 +00:00
# endif
#endif
2020-10-16 10:26:01 +00:00
#if !defined(DEFAULT_SCL)
# if defined(__STM32F1__)
# define DEFAULT_SCL PB6
# elif defined(ESP8266)
# define DEFAULT_SCL 5
# elif defined(SDA)
# define DEFAULT_SCL SCL
# else
2020-10-26 22:23:46 +00:00
# error "Error define DEFAULT_SCL, SCL not declared, if you have this error contact the mantainer"
2020-10-16 10:26:01 +00:00
# endif
2019-10-17 06:44:54 +00:00
#endif
2020-10-16 10:26:25 +00:00
uint8_t _sda = DEFAULT_SDA;
uint8_t _scl = DEFAULT_SCL;
2017-11-13 20:44:50 +00:00
2019-04-29 07:17:34 +00:00
TwoWire *_wire;
2017-11-13 20:44:50 +00:00
bool _usingInterrupt = false;
uint8_t _interruptPin = 2;
void (*_interruptFunction)(){};
2019-10-29 21:04:03 +00:00
byte writeMode = B00000000;
2020-03-06 20:56:24 +00:00
byte writeModeUp = B00000000;
2019-10-29 21:04:03 +00:00
byte readMode = B00000000;
byte readModePullUp = B00000000;
byte readModePullDown = B00000000;
byte byteBuffered = B00000000;
2020-03-06 20:56:24 +00:00
byte resetInitial = B00000000;
byte initialBuffer = B00000000;
2017-11-13 20:44:50 +00:00
unsigned long lastReadMillis = 0;
byte writeByteBuffered = B00000000;
volatile byte encoderValues = B00000000;
2020-03-13 20:52:51 +00:00
uint8_t prevNextCode = 0;
uint16_t store=0;
int latency = READ_ELAPSED_TIME;
bool checkProgression(byte oldValA, byte newValA, byte oldValB, byte newValB, byte validProgression);
// byte validCW = B11100001;
// byte validCCW = B01001011;
byte validCW = B01001011;
byte validCCW = B11100001;
2020-04-11 14:40:51 +00:00
uint8_t transmissionStatus = 0;
2020-10-03 21:31:53 +00:00
void setVal(uint8_t pin, uint8_t value);
bool digitalWriteAllBytes(byte allpins);
2017-11-13 20:44:50 +00:00
};
#endif
2020-10-16 10:31:39 +00:00