
#ifndef _PCA9555_H_
#define _PCA9555_H_

#include <iostream>
#include "../../communication/i2c/i2c.hpp"
#include "../../algorithms/bitgestion/bitgestion.h"

#define PCA9555_ADDR 0x20 // Device Adress 

#define PCA9555_INPUT_PORT_0_REG 0x00
#define PCA9555_INPUT_PORT_1_REG 0x01
#define PCA9555_OUTPUT_PORT_0_REG 0x02
#define PCA9555_OUTPUT_PORT_1_REG 0x03
#define PCA9555_POLARITY_INV_0_REG 0x04
#define PCA9555_POLARITY_INV_1_REG 0x05
#define PCA9555_CONFIG_0_REG 0x06
#define PCA9555_CONFIG_1_REG 0x07

class Pca9555
{
	public: 
		
		enum errors
		{
			bankInvalid, 
			oufOfLoop, 
			pinInvalid, 
			invalidOut,
			invalidDir,
			regInvalid
		};

		
		Pca9555(I2C *i2c);
		uint8_t readPin(uint8_t bank, uint8_t pin); 
		void writePin(uint8_t bank, uint8_t pin, uint8_t value); 
		void setPin(uint8_t bank, uint8_t pin, uint8_t direction); // 1 = input 0 = output
		void setPinPolarity(uint8_t bank, uint8_t pin, uint8_t polarity); // 1 = Inverted 0 = Normal
		uint8_t getInterrupt(); 
		void setAllOut(); 
		void setAllIn(); 
		void setAllHigh(); 
		void setAllLow(); 
	

	private: 
		I2C* i2c_pca9555;
		uint8_t intPinSrc; 
		uint8_t intPinDir; 
		uint8_t intPinPol; 
		uint8_t pinIo0Direction;
		uint8_t pinIo1Direction;
		uint8_t pinIo0State;
		uint8_t pinIo1State;
		uint8_t pinIo0Polarity;
		uint8_t pinIo1Polarity;

		void throwError(errors error); 
};

#endif 
