#include <stdio.h>
#include "app.h"
#include "main.h"
#include "led_blink.h"
#include "transmit.h"

#include "stm32l1xx_hal.h"
#include "stm32l1xx_hal_gpio.h"

// For debug
  __IO uint32_t time_start=0,time_stop=0,time_tot=0;

void switch2transmit() {

  disableRx = true;
  // ------------------ GO TO TRANSMIT STATE -------------------------
  digitalWrite(txEnablePin, HIGH);
  delayMicroseconds( 400 );
  // ------------------ GO TO TRANSMIT STATE -------------------------
        time_start++;
      time_stop++;
      time_tot++;
}

void switch2receive() {
  // ------------------ GO TO RECEIVE STATE -------------------------
  // From Transmission Mode to Receiver Mode follow the procedure:
  // Drive down pin 5 (RX/TX), after 40us drive down pin 6 (ENABLE) 20us long.
  //After 200 us the device is ready for reception.
  digitalWrite(txEnablePin, LOW); 
  delayMicroseconds( 40 ); 
  digitalWrite(enablePin, LOW); 
  delayMicroseconds( 20 ); 
  digitalWrite(enablePin, HIGH);  
  delayMicroseconds( 200 );
  // ------------------ GO TO RECEIVE STATE -------------------------
  disableRx = false;
}

void sendPulse(int high, int low) {

  digitalWrite(txPin, HIGH); 
  delayMicroseconds( high ); 
  digitalWrite(txPin, LOW); 
  delayMicroseconds( low );  
}

// Nexa : http://elektronikforumet.com/wiki/index.php/RF_Protokoll_-_Nexa_sj%C3%A4lvl%C3%A4rande
// "1" = 295 µs hög och 170 µs låg
// "0" = 295 µs hög och 920 µs låg
// 
// Etta skickas som 10 och Nolla som 01
//
// Dessa siffror från web-sidan stämmer inte med den Nexa-kontroll som jag har.
// Jag har rättat dessa med siffror från verkligheten. Finns i define's nedan.

// <NT01995080>
// <NT01995090>

// Timing for NEXA Remote controls
#define HIGH_PULSE     240
#define LOW_PULSE_0    300
#define LOW_PULSE_1   1320
#define SYNC_PULSE    2700
#define STOP_PULSE   10000

void send1() {

  sendPulse( HIGH_PULSE, LOW_PULSE_1);    // 1
  sendPulse( HIGH_PULSE, LOW_PULSE_0 );   // 0
}

void send0() {

  sendPulse( HIGH_PULSE, LOW_PULSE_0 );   // 0
  sendPulse( HIGH_PULSE, LOW_PULSE_1);    // 1  
}

void sendSync() {
  sendPulse( HIGH_PULSE, SYNC_PULSE );  
}

void sendStop() {
  sendPulse( HIGH_PULSE, STOP_PULSE );
}

void sendNexaCodeStr(unsigned char *c) {

  unsigned long int orig_code=0;  

  if( sscanf((const char *)&(c[2]),"%08lX",&orig_code) == 1 ) {
    sendNexaCodeNo(orig_code);
  }
}

void sendNexaCodeNo(unsigned long orig_code) {

  unsigned long code=0;
  //unsigned long int orig_code=0;  
  int i;
  int loop;
  
  blinkTheTxLED();

  if( 1 ) {

    switch2transmit();

    for( loop=4; loop>0; loop--) {
      
      code = orig_code;
      
      time_start = micros();
      
      sendSync();

      for(i=0; i < 32; i++) {

        if( code & 0x80000000 ) {
          send1();
        }
        else {
          send0();
        }
        code = (code << 1);
      }
      
      sendStop();    
      
      time_stop = micros();
      
      time_tot = time_stop - time_start; // Should be 71580uS
      
      time_start++;
      time_stop++;

    }

    switch2receive();
  }
}



