GY-63_MS5611/libraries/MCP23S08/MCP23S08.h

93 lines
2.3 KiB
C
Raw Normal View History

2022-01-10 06:52:28 -05:00
#pragma once
//
// FILE: MCP23S08.h
// AUTHOR: Rob Tillaart
2022-11-17 07:36:23 -05:00
// VERSION: 0.1.2
2022-01-10 06:52:28 -05:00
// PURPOSE: Arduino library for SPI MCP23S08 8 channel port expander
// DATE: 2022-01-10
// URL: https://github.com/RobTillaart/MCP23S08
#include "Arduino.h"
#include "SPI.h"
2022-11-17 07:36:23 -05:00
#define MCP23S08_LIB_VERSION (F("0.1.2"))
2022-01-10 06:52:28 -05:00
#define MCP23S08_OK 0x00
#define MCP23S08_PIN_ERROR 0x81
#define MCP23S08_SPI_ERROR 0x82
#define MCP23S08_VALUE_ERROR 0x83
#define MCP23S08_PORT_ERROR 0x84
2022-09-28 07:59:32 -04:00
#define MCP23S08_REGISTER_ERROR 0xFF
2022-01-10 06:52:28 -05:00
#define MCP23S08_INVALID_READ -100
class MCP23S08
{
public:
MCP23S08(uint8_t select, uint8_t dataIn, uint8_t dataOut, uint8_t clock, uint8_t address = 0x00);
MCP23S08(uint8_t select, uint8_t address = 0x00);
bool begin();
bool isConnected(); // needed ?
2022-09-28 07:59:32 -04:00
// single pin interface
// mode = INPUT, OUTPUT or INPUT_PULLUP (==INPUT)
2022-01-10 06:52:28 -05:00
bool pinMode(uint8_t pin, uint8_t mode);
bool digitalWrite(uint8_t pin, uint8_t value);
uint8_t digitalRead(uint8_t pin);
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);
2022-09-28 07:59:32 -04:00
// 8 pins interface
// port = 0..1
// value = bit pattern
2022-01-10 06:52:28 -05:00
bool pinMode8(uint8_t value);
bool write8(uint8_t value);
int read8();
bool setPolarity8(uint8_t mask);
bool getPolarity8(uint8_t &mask);
bool setPullup8(uint8_t mask);
bool getPullup8(uint8_t &mask);
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
2022-09-28 07:59:32 -04:00
// debugging
2022-01-10 06:52:28 -05:00
bool usesHWSPI() { return _hwSPI; };
int lastError();
private:
bool writeReg(uint8_t reg, uint8_t value);
uint8_t readReg(uint8_t reg);
uint8_t _address = 0;
uint8_t _select = 0;
uint8_t _dataOut = 0;
uint8_t _dataIn = 0;
uint8_t _clock = 0;
uint8_t _error = MCP23S08_OK;
bool _hwSPI = false;
2022-09-28 07:59:32 -04:00
// 1 MHz is a safe value TODO CHECK datasheet
uint32_t _SPIspeed = 8000000UL;
2022-01-10 06:52:28 -05:00
SPIClass * mySPI;
SPISettings _spi_settings;
uint8_t swSPI_transfer(uint8_t val);
};
// -- END OF FILE --