/*******************************************************************************
* rp-mcp23017        Example circuit to drive LED using MOSFET.                *
*                                                                              *
* JST Lawrence, 2019 Novel Devices                                             *
* Source code at http://www.noveldevices.co.uk/rpdl/rp-mcp23017.c              *
*                                                                              *
* V1.0    18-Mar-2019 Written.                                                 *
*                                                                              *
*                                                                              *
*******************************************************************************/

#include <stdio.h>
#include <wiringPi.h>
#include <mcp23017.h>
#include <unistd.h>

#define PIN_BASE 100
#define MCP23017 0x20
#define GPA0 (PIN_BASE+0)
#define GPB0 (PIN_BASE+8)

int main (void)
{

// Initialise wiringPi
  wiringPiSetup();
  
// Set up pin base number and chip address - remember to provide an external bias on each address pin!
  mcp23017Setup(PIN_BASE,MCP23017);

  printf("MCP23017 demonstration\n") ;

// Set up input pin on GPA0
  pinMode(GPA0,INPUT);

// Set up internal pull-up
  pullUpDnControl(GPA0,PUD_UP);

// Set up output pin
  pinMode(GPB0,OUTPUT);

// Loop continuously but pause when button is pushed
  for(;;)
  { digitalWrite(GPB0,HIGH);
	usleep(250000);

	while(digitalRead(GPA0)==0);

	digitalWrite(GPB0,LOW);
	usleep(250000);

	while(digitalRead(GPA0)==0);
}	
  
  return 0 ;
}