 #include "bitlash.h"
 #include "Timer.h"
 
+// macro
+#define ROW_COUNT(array)  (sizeof(array) / sizeof(*array))
+
 // const
 // pin 2 = interrupt 1 on UNO board
-const byte SERIAL_IN_PIN = 2;
-const byte SERIAL_IN_INT = 0;
+const byte SERIAL_IN_PIN = 19;
+const byte SERIAL_IN_INT = 4;
+
+// struct
+struct in_bytes {
+  byte value;
+  unsigned long time;
+};
 
 // vars
+in_bytes rx_bytes[30];
+byte rx_byte_index;
 word times_buffer[60];
-byte level;
 volatile word isr_times_buffer[60];
 byte isr_index_buffer;
-word start_bit;
 word max_step, min_step;
 byte max_index, min_index;
-unsigned long baud;
+unsigned long auto_baud;
+unsigned long uart_baud;
 Timer t;
 
 // ISR call when edges rising/falling occurs on SERIAL_IN_PIN

  sei();
  // init bitlash
  initBitlash(57600);
  // add bitlash function
  addBitlashFunction("raw", (bitlash_function) printRAW);
  addBitlashFunction("bd", (bitlash_function) printBaud);
  // fill in the UART file descriptor with pointer to writer
  fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
  // standard output device STDOUT is uart
  stdout = &uartout ;
  // launch timer task
  int updateJob = t.every(500, update_time_buffer);
  // IO init

// target : Arduino Mega (ATMega2560)
// note   : - use timer1 for 0.5 uS accuracy time measure 
//            (arduino core micros() is just 4us accuracy).
//          - signal to detect must be wire on pin 19 (Rx of Serial1).
//          - use bitlash for console manager (see https://github.com/billroy/bitlash)
//          - use Timer for schedule tasks (see  https://github.com/JChristensen/Timer)

