/* GR-SAKURA Sketch Template V2.13 */
#include <Arduino.h>
#include <SPI.h>
#include <RX231_Shield.h>

typedef unsigned char uint8_t;
RX231_Shield Shield = RX231_Shield();

unsigned char flg = 1;

void setup()
{
	Serial.begin(9600);
	Shield.begin();
}

void loop()
{
	uint8_t buf[14] = {0,};
	uint8_t * rx_buf;
	if (flg == 1) {
	  delay(500);
	  while(digitalRead(3)){
	    digitalWrite(2, 1);    
	    delay(5);
	    digitalWrite(2, 0);
	  }
	  delay(100);
	buf[0] = CMD_IRRX;
	Shield.sendData(CMD_SET, buf);
	flg--;
	} else {
	if (digitalRead(3)) {
	  rx_buf = Shield.receiveData();
	      if (rx_buf[1] == CMD_IRRX) {
	        Serial.print("StartBit:");
	        Serial.print((rx_buf[2] << 8) | rx_buf[3]);
            Serial.print("\tCustomBit:");
            Serial.print(rx_buf[4]);
            Serial.print("\t/CustomBit:");
            Serial.print(rx_buf[5]);
            Serial.print("\tDataBit:");
            Serial.print(rx_buf[6]);
            Serial.print("\t/DataBit:");
            Serial.println(rx_buf[7]);
	      } else if (buf[1] == CMD_LED) {
	        Serial.print(rx_buf[2]);
	        Serial.println("  ON");
	      }
		}
	delay(3);
	}
}