/* 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;
unsigned char L1, L2, L3, L4, L5, L6;
	        
void setup()
{
	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_BLT;
	Shield.sendData(CMD_SET, buf);
	flg--;
	} else {
	if (digitalRead(3)) {
	  rx_buf = Shield.receiveData();
	      if (rx_buf[1] == CMD_BLT) {
	        L1 = (rx_buf[2] & 0x01);
	        L2 = (rx_buf[2] & 0x02);
	        L3 = (rx_buf[2] & 0x08);
	        L4 = (rx_buf[2] & 0x20);
	        L5 = (rx_buf[2] & 0x40);
	        L6 = (rx_buf[2] & 0x80);
	        Shield.setLed(L1, L2, L3, L4, L5, L6);
	      }
		
	        if (rx_buf[1] == CMD_LED) {
	          if(rx_buf[2] >= '1' && rx_buf[2] <= '6'){
	            Shield.requestBLT(rx_buf[2]);  
	          }
	        }
		}
	delay(3);
	}
}