#include "sdcard.h"
extern SdFs SD;
extern FsFile LOG_FILE;
extern uint16_t imageSelect;
// read SD information and print to logfile
void readSDCardInfo()
{
  cid_t sd_cid;

  if(SD.card()->readCID(&sd_cid))
  {
    LOG("Sd MID:");
    LOG(sd_cid.mid); //,16
    LOG(" OID:");
    LOG(sd_cid.oid[0]);
    LOGN(sd_cid.oid[1]);

    LOG("Sd Name:");
    LOG(sd_cid.pnm[0]);
    LOG(sd_cid.pnm[1]);
    LOG(sd_cid.pnm[2]);
    LOG(sd_cid.pnm[3]);
    LOGN(sd_cid.pnm[4]);

    LOG("Sd Date:");
    LOG(sd_cid.mdtMonth());

    LOGN(sd_cid.mdtYear());

    LOG("Sd Serial:");
    LOGN(sd_cid.psn());

  }
}
void readSDCardInfo2()
{
  cid_t sd_cid;

  if(SD.card()->readCID(&sd_cid))
  {
    LOG_FILE.print("Sd MID:");
    LOG_FILE.print(sd_cid.mid, 16);
    LOG_FILE.print(" OID:");
    LOG_FILE.print(sd_cid.oid[0]);
    LOG_FILE.println(sd_cid.oid[1]);

    LOG_FILE.print("Sd Name:");
    LOG_FILE.print(sd_cid.pnm[0]);
    LOG_FILE.print(sd_cid.pnm[1]);
    LOG_FILE.print(sd_cid.pnm[2]);
    LOG_FILE.print(sd_cid.pnm[3]);
    LOG_FILE.println(sd_cid.pnm[4]);

    LOG_FILE.print("Sd Date:");
    LOG_FILE.print(sd_cid.mdtMonth());

    LOG_FILE.println(sd_cid.mdtYear());


    LOG_FILE.print("Sd Serial:");
    LOG_FILE.println(sd_cid.psn());
    LOG_FILE.sync();
  }
}

/*
 * No SC Card found, blink 5x fast
 */
void noSDCardFound(void)
{
  while(!SD.begin(SdSpiConfig(SS, DEDICATED_SPI))) {
    for(int i = 0; i < 5; i++) {
      LED3_ON();
      delay(500);
      LED3_OFF();
      delay(500);
    }
    delay(1000);
  }
}

void sdCardInsert(void){
  if (digitalRead(BOARD_SDCARD_INSERT)){
    LED1_OFF();
  }else{
    LED1_ON();
  }
}

void switchImage(void){

    uint8_t oldimageSelect = imageSelect;
    imageSelect = (~(digitalRead(BOARD_SWITCH1_PIN) | 
                  digitalRead(BOARD_SWITCH2_PIN) << 1 |  
                  digitalRead(BOARD_SWITCH3_PIN) << 2 |  
                  digitalRead(BOARD_SWITCH4_PIN) << 3)) & 0x0F;
    if ( oldimageSelect!=imageSelect){
    LOG("Image: ");
    LOGN(imageSelect);

    }


}