#include <Arduino.h>
#include "gpio.h"
#include "blacksasi.h"
extern void sdCardInsert();
extern void switchImage();
bool gpioInit(void){
    // PIN initialization
    pinMode(BOARD_LED1_PIN , OUTPUT);
    pinMode(BOARD_LED2_PIN , OUTPUT);
    pinMode(BOARD_LED3_PIN , OUTPUT);
    pinMode(BOARD_SDCARD_INSERT, INPUT_PULLUP);
    pinMode(BOARD_SWITCH1_PIN , INPUT_PULLUP);
    pinMode(BOARD_SWITCH2_PIN , INPUT_PULLUP);
    pinMode(BOARD_SWITCH3_PIN , INPUT_PULLUP);
    pinMode(BOARD_SWITCH4_PIN , INPUT_PULLUP);
    pinMode(BOARD_REV_PIN0, INPUT_PULLDOWN); 
    pinMode(BOARD_REV_PIN1, INPUT_PULLDOWN);
    pinMode(BOARD_REV_PIN2, INPUT_PULLDOWN);
    pinMode(BOARD_SDIO_D1, INPUT_PULLDOWN);
    pinMode(BOARD_SDIO_D2, INPUT_PULLDOWN);
    pinMode(BOARD_SCSI_TERM_HIGH, OUTPUT);
    pinMode(BOARD_SCSI_TERM_LOW, OUTPUT);
    pinMode(BOARD_SCSI_DBX, OUTPUT);
    pinMode(BOARD_SCSI_TERM_POWER, INPUT); //Check if external termination power is provided

    attachInterrupt(BOARD_SDCARD_INSERT,sdCardInsert, CHANGE);
    attachInterrupt(BOARD_SWITCH1_PIN,switchImage, CHANGE);
    attachInterrupt(BOARD_SWITCH2_PIN,switchImage, CHANGE);
    attachInterrupt(BOARD_SWITCH3_PIN,switchImage, CHANGE);
    attachInterrupt(BOARD_SWITCH4_PIN,switchImage, CHANGE);

    // Control SCSI drivers
    pinMode(BOARD_SCSI_DTD, OUTPUT);
    pinMode(BOARD_SCSI_IND, OUTPUT);
    pinMode(BOARD_SCSI_TAD, OUTPUT);
    pinMode(BOARD_TRANS_OE, OUTPUT);
    digitalWrite(BOARD_SCSI_TERM_HIGH, LOW);
    digitalWrite(BOARD_SCSI_TERM_LOW, LOW);
    digitalWrite(BOARD_TRANS_OE, LOW);
    SCSI_SET_PULL() ;
    TRANSCEIVER_IO_SET(vDTD,DB_INPUT);
    
    // Initiator port
    pinMode(BOARD_SCSI_ATN, INPUT_PULLUP);
    pinMode(BOARD_SCSI_BSY, INPUT_PULLUP);
    pinMode(BOARD_SCSI_ACK, INPUT_PULLUP);
    pinMode(BOARD_SCSI_RST, INPUT_PULLUP);
    pinMode(BOARD_SCSI_SEL, INPUT_PULLUP);
    TRANSCEIVER_IO_SET(vIND,TR_INPUT);

    // Target port
    pinMode(BOARD_SCSI_MSG, INPUT_PULLUP);
    pinMode(BOARD_SCSI_CD, INPUT_PULLUP);
    pinMode(BOARD_SCSI_REQ, INPUT_PULLUP);
    pinMode(BOARD_SCSI_IO, INPUT_PULLUP);
    TRANSCEIVER_IO_SET(vTAD,TR_INPUT);

    SCSI_DB_INPUT()
    SCSI_TARGET_INACTIVE()
}
  