#include <SPI.h>
#include "at86rf2xx.h"


int received = 0;

int IRQ = 2;
int RESET = 8;
int SLP_TR = 7;
int SEL = 6;

void setup() {
  Serial.begin(115200);
  at86rf2xx.init(SEL, IRQ, SLP_TR, RESET);
  at86rf2xx.set_chan(26); // set channel to 26
  pinMode(3,INPUT);
  pinMode(14,OUTPUT);
}

void loop() {
  digitalWrite(14,HIGH);
  if (at86rf2xx.events)
    at86rf2xx_eventHandler();
  // if (digitalRead(3) == LOW){
  //   digitalWrite(14,LOW);
  //   Serial.print("Bouton\n");
  //   //uint8_t data[53] = Serial.readBytes(53);
  //   //uint8_t data[53] = "61886F472400001BB9480200001BB91EB42883C80000013CE801008D150001000112000401017818230A55002101007B44D6649A4C";
  //   int good=1;
  //   uint8_t rec_data[53];
  //   char data[107]; // chaine de caractères pouvant contenir les 53 valeurs
  //   delay(5);
  //   do{
  //     //Serial.print("Attente python");
  //     Serial.begin(115200);
  //     if (Serial.available()) {  // Si des données sont disponibles sur la liaison série
  //       //uint16_t data[53] = Serial.readBytes();  // Lecture d'un caractère
  //       //uint16_t data[53];
  //       for (int i = 0; i < 53; i++) {
  //         while (!Serial.available()) { }  // Attendez que les données soient disponibles
  //         rec_data[i] = Serial.read();  // Lire un octet
  //       }
  //       Serial.print("Données reçues : ");
  //       //for (int i = 0; i < 53; i++) {
  //         //Serial.println(data[i]);
  //       //}

  //       for (int i = 0; i < 53; i++) {
  //         sprintf(data + (i * 2), "%02X", rec_data[i]); // formatage de chaque valeur en hexadécimal
  //       }
  //       good=0;
  //     }
  //   }while(!good);
  //   delay(20);
  //   Serial.print(data);//, HEX);
  //   at86rf2xx.send(data,53);
  //   at86rf2xx.init(SEL, IRQ, SLP_TR, RESET);
  //   at86rf2xx.set_chan(26); // set channel to 26
  //   while(digitalRead(3) == LOW);
  // }
  return;
}

void at86rf2xx_eventHandler() {
  /* One less event to handle! */
  at86rf2xx.events--;

  /* If transceiver is sleeping register access is impossible and frames are
   * lost anyway, so return immediately.
   */
  byte state = at86rf2xx.get_status();
  if(state == AT86RF2XX_STATE_SLEEP)
    return;

  /* read (consume) device status */
  byte irq_mask = at86rf2xx.reg_read(AT86RF2XX_REG__IRQ_STATUS);

  /*  Incoming radio frame! */
  if (irq_mask & AT86RF2XX_IRQ_STATUS_MASK__RX_START)
    //Serial.println("[at86rf2xx] EVT - RX_START");

  /*  Done receiving radio frame; call our receive_data function.
   */
  if (irq_mask & AT86RF2XX_IRQ_STATUS_MASK__TRX_END)
  {
    if(state == AT86RF2XX_STATE_RX_AACK_ON || state == AT86RF2XX_STATE_BUSY_RX_AACK) {
      //Serial.println("[at86rf2xx] EVT - RX_END");
      at86rf2xx_receive_data();
    }
  }
}

void at86rf2xx_receive_data() {
  /*  print the length of the frame
   *  (including the header)
   */
  size_t pkt_len = at86rf2xx.rx_len();
  //Serial.print("Frame length: ");
  //Serial.print(pkt_len);
  //Serial.println(" bytes");

  /*  Print the frame, byte for byte  */
  //Serial.println("Frame dump (HEXA):");
  uint8_t data[pkt_len];
  at86rf2xx.rx_read(data, pkt_len, 0);
  for (int d=0; d<pkt_len; d++) {
    if (data[d] < 0x10) {
      Serial.print("0");
    }
    Serial.print(data[d], HEX);
  }
  Serial.println();

  /* How many frames is this so far?  */
  //Serial.print("[[Total frames received: ");
  //Serial.print(
  ++received;
  //);
  //Serial.println("]]\n");
}
