//*****************************************
//***** Libreria PC_SIMU para Arduino *****
//*****************************************
// Original code by canalplc
// http://canalplc.blogspot.com.es/
// Version 1.0
// July 2016
//*****************************************
//
#include <TimerOne.h>
#include <PC_SIMU.h>
byte DDRDD;
byte DDRBB;
PC_SIMU::PC_SIMU(){} 
void PC_SIMU::INI() {
  Serial.begin(9600, SERIAL_8E1); //9600,8,Even,1
  Timer1.initialize(100000);
  Timer1.attachInterrupt(WRITE); // Activa la interrupcion y la asocia a WRITE
  interrupts();
  DDRDD = DDRD;
  DDRBB = DDRB;
  DDRD = 0b11111111;
  DDRB = 0b11111111;
  PORTD = 0b00000000;
  PORTB = 0b00000000;
}

void WRITE() {
  byte output_Byte[41];
  output_Byte[0] = 0x68;
  output_Byte[1] = 0x23;
  output_Byte[2] = 0x23;
  output_Byte[3] = 0x68;
  output_Byte[4] = 0x01;
  output_Byte[5] = 0x02;
  output_Byte[6] = 0x08;
  output_Byte[7] = 0x32;
  output_Byte[8] = 0x03;
  output_Byte[9] = 0x00;
  output_Byte[10] = 0x00;
  output_Byte[11] = 0x00;
  output_Byte[12] = 0x00;
  output_Byte[13] = 0x00;
  output_Byte[14] = 0x02;
  output_Byte[15] = 0x00;
  output_Byte[16] = 0x12;
  output_Byte[17] = 0x00;
  output_Byte[18] = 0x00;
  output_Byte[19] = 0x04;
  output_Byte[20] = 0x02;
  output_Byte[21] = 0xFF;
  output_Byte[22] = 0x04;
  output_Byte[23] = 0x00;
  output_Byte[24] = 0x40;
  //
  byte BYTE = 0x00;
  byte BIT = 0x04;
  for(int i = 2; i <= 13; i++)
  {
    // Pin 2 a 7
    if(i <= 7)
    {
      // Output port D
      if((DDRDD & BIT) != 0) BYTE = BYTE + (digitalRead(i) * BIT);		
    }
    // Pin 8 a 13
    else
    {
      if(i == 8) 
      {
	output_Byte[25] = BYTE;
        BYTE = 0x00;
	BIT = 0x01;
      }
      // Output port B
      if((DDRBB & BIT) != 0) BYTE = BYTE + (digitalRead(i) * BIT);
    }
    BIT = BIT * 2;   
  }
  output_Byte[26] = BYTE;
  //
  output_Byte[27] = 0x3F;
  output_Byte[28] = 0x00;
  output_Byte[29] = 0x00;
  output_Byte[30] = 0x00;
  output_Byte[31] = 0x00;
  output_Byte[32] = 0x00;
  output_Byte[33] = 0xFF;
  output_Byte[34] = 0x04;
  output_Byte[35] = 0x00;
  output_Byte[36] = 0x10;  
  output_Byte[37] = 0x00;  
  output_Byte[38] = 0x00;  
  output_Byte[39] = 0xEF;  
  output_Byte[40] = 0x16;  
  Serial.write(output_Byte, sizeof(output_Byte));
}

void serialEvent() {
 byte input_Byte[53];
 input_Byte[45] = 0x00;
 input_Byte[46] = 0x00;
 byte BIT = 0x04;
 while(!(input_Byte[0] == 0x68 && input_Byte[1] == 0x2D && input_Byte[2] == 0x2D && input_Byte[3] == 0x68 && input_Byte[41] == 0x81 && input_Byte[44] == 0x00)) 
 {
   Serial.readBytesUntil(0x16, input_Byte, sizeof(input_Byte));
 }
 if(input_Byte[0] == 0x68 && input_Byte[1] == 0x2D && input_Byte[2] == 0x2D && input_Byte[3] == 0x68 && input_Byte[41] == 0x81 && input_Byte[44] == 0x00) 
 {
   for(int i = 2; i <= 13; i++)
   {
    // Pin 2 a 7
    if(i <= 7)
    {
      // Input port D
      if((DDRDD & BIT) == 0)
      {
	if((input_Byte[45] & BIT) != 0) digitalWrite(i, 1);
	else digitalWrite(i, 0);
      }              
    }
    // Pin 8 a 13
    else
    {
      if(i == 8) BIT = 1;
      // Input port B
      if((DDRBB & BIT) == 0)
      {
	if((input_Byte[46] & BIT) != 0) digitalWrite(i, 1);
	else digitalWrite(i, 0); 
      }
    }
    BIT = BIT * 2;   
   }  
 }
}
