78 lines
1.8 KiB
C
Raw Normal View History

2021-01-29 12:31:58 +01:00
#pragma once
//
// FILE: MCP23017.h
// AUTHOR: Rob Tillaart
2021-09-17 10:30:51 +02:00
// VERSION: 0.2.4
2021-01-29 12:31:58 +01:00
// PURPOSE: Arduino library for I2C MCP23017 16 channel port expander
// DATE: 2019-10-12
// URL: https://github.com/RobTillaart/MCP23017_RT
#include "Arduino.h"
#include "Wire.h"
2021-09-17 10:30:51 +02:00
#define MCP23017_LIB_VERSION (F("0.2.4"))
2021-01-29 12:31:58 +01:00
#define MCP23017_OK 0x00
#define MCP23017_PIN_ERROR 0x81
#define MCP23017_I2C_ERROR 0x82
#define MCP23017_VALUE_ERROR 0x83
#define MCP23017_PORT_ERROR 0x84
2021-06-06 19:58:43 +02:00
#define MCP23017_INVALID_READ -100
2021-01-29 12:31:58 +01:00
class MCP23017
{
public:
MCP23017(uint8_t addr, TwoWire *wire = &Wire);
2021-06-06 19:58:43 +02:00
2021-01-29 12:31:58 +01:00
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
bool begin();
bool isConnected();
2021-06-06 19:58:43 +02:00
2021-01-29 12:31:58 +01:00
// single pin interface
2021-06-06 19:58:43 +02:00
// mode = INPUT, OUTPUT or INPUT_PULLUP (==INPUT)
bool pinMode(uint8_t pin, uint8_t mode);
2021-01-29 12:31:58 +01:00
bool digitalWrite(uint8_t pin, uint8_t value);
uint8_t digitalRead(uint8_t pin);
2021-09-17 10:30:51 +02:00
bool setPolarity(uint8_t pin, bool reversed);
bool getPolarity(uint8_t pin, bool &reversed);
bool setPullup(uint8_t pin, bool pullup);
bool getPullup(uint8_t pin, bool &pullup);
2021-06-06 19:58:43 +02:00
2021-01-29 12:31:58 +01:00
// 8 pins interface
2021-06-06 19:58:43 +02:00
// port = 0..1
2021-09-17 10:30:51 +02:00
// value = bit pattern
2021-01-29 12:31:58 +01:00
bool pinMode8(uint8_t port, uint8_t value);
bool write8(uint8_t port, uint8_t value);
int read8(uint8_t port);
2021-09-17 10:30:51 +02:00
bool setPolarity8(uint8_t port, uint8_t mask);
bool getPolarity8(uint8_t port, uint8_t &mask);
bool setPullup8(uint8_t port, uint8_t mask);
bool getPullup8(uint8_t port, uint8_t &mask);
2021-01-29 12:31:58 +01:00
int lastError();
2021-09-17 10:30:51 +02:00
2021-01-29 12:31:58 +01:00
private:
bool writeReg(uint8_t reg, uint8_t value);
uint8_t readReg(uint8_t reg);
uint8_t _addr;
TwoWire* _wire;
uint8_t _error;
};
// -- END OF FILE --