//======================================================================
// TR.h
//======================================================================

#ifndef __TR_h__
#define __TR_h__

#include <Bounce2.h>
#include "Debug.h"
#include "DSP.h"

#define UBITX_TR_OUT_PIN 2
#define UBITX_TR_PTT_PIN 3
#define UBITX_TR_VOX_PIN 4
#define UBITX_TR_KEY_PIN 5

const int uBitxTROutPin = UBITX_TR_OUT_PIN;
const int uBitxTRPttPin = UBITX_TR_PTT_PIN;
const int uBitxTRVoxPin = UBITX_TR_VOX_PIN;
const int uBitxTRKeyPin = UBITX_TR_KEY_PIN;

class UBitxTR {
  public:
  UBitxTR(UBitxDSP& d, int out = uBitxTROutPin, int p = uBitxTRPttPin, int v = uBitxTRVoxPin, int k = uBitxTRKeyPin):
  dsp(d), outPin(out), pttPin(p), voxPin(v), keyPin(k) {}

  void begin() {
    pinMode(outPin, OUTPUT);
    pinMode(voxPin, INPUT_PULLUP);
    pinMode(keyPin, INPUT_PULLUP);
    ptt.attach(pttPin, INPUT_PULLUP);
    ptt.interval(5);

    // default configuration: PTT, key, and CAT enabled; VOX disabled
    DBGCMD( enablePTT()  );
    DBGCMD( disableVOX() );
    DBGCMD( enableKey()  );
    DBGCMD( enableCAT()  );

    DBGCMD( setRX() );
  }

  inline void enablePTT() { pttEnable = true; }
  inline void enableVOX() { voxEnable = true; }
  inline void enableKey() { keyEnable = true; }
  inline void enableCAT() { catEnable = true; }
  inline void disablePTT() { pttEnable = false; }
  inline void disableVOX() { voxEnable = false; }
  inline void disableKey() { keyEnable = false; }
  inline void disableCAT() { catEnable = false; }

  inline bool pttEnabled() { return pttEnable; }
  inline bool voxEnabled() { return voxEnable; }
  inline bool keyEnabled() { return keyEnable; }
  inline bool catEnabled() { return catEnable; }
  
  inline bool pttPressed() { return ptt.fell(); }
  inline bool pttReleased() { return ptt.rose(); }
  inline bool voxActivated() { return (L_voxActive != voxActive) && L_voxActive; }
  inline bool voxDeactivated() { return (L_voxActive != voxActive) && voxActive; }
  inline bool keyPressed() { return (L_keyDown != keyDown) && L_keyDown; }
  inline bool keyReleased() { return (L_keyDown != keyDown) && keyDown; }
  inline bool catActivated() { return (L_catActive != catActive) && L_catActive; }
  inline bool catDeactivated() { return (L_catActive != catActive) && catActive; }
  
  inline void catTX() {
    L_catActive = catActive;
    catActive = true;
  }

  inline void catRX() {
    L_catActive = catActive;
    catActive = false;
  }

//======================================================================

  inline bool transmitting() { return isTX;  }
  inline bool receiving() { return !isTX; }

  /*!
   *  @brief  Check if any of the PTT's have been pressed or released 
   *          since the last update.  Only one thing is allowed to occur
   *          based on an order of precedence.  The highest priority is
   *          to stop transmitting.
   *          
   *  @param  cw
   *          True if CW mode is currently active; false otherwise.  
   *          Different/faster logic is used in CW mode.
   */
  void update(bool cw = false);
  
  void end() {
  }
  
  private:

  inline void setTX() {
    digitalWrite(outPin, LOW);
    isTX = true;
  }

  inline void setRX() {
    isTX = false;
    digitalWrite(outPin, HIGH);
  }

  inline void updatePTT() {
    ptt.update();
  }

  inline void updateVOX() {
    L_voxActive = voxActive;
    voxActive = (digitalRead(voxPin) == LOW);
  }

  inline void updateKey() {
    L_keyDown = keyDown;
    keyDown = (digitalRead(keyPin) == LOW);
  }

  UBitxDSP& dsp;

  Bounce ptt;
  
  int outPin;
  int pttPin;
  int voxPin;
  int keyPin;
  
  bool isTX = false;

  bool pttEnable = false;
  bool voxEnable = false;
  bool keyEnable = false;
  bool catEnable = false;

  bool voxActive = false;
  bool L_voxActive = false;
  bool keyDown = false;
  bool L_keyDown = false;
  bool catActive = false;
  bool L_catActive = false;
};

extern UBitxTR TR;

#endif

//======================================================================
// EOF
//======================================================================
