/*
 * FT817 Mini DIN Sequencer
 * PCB V2.2 
 * Processor - ATTiny45
*/  
 
#include <elapsedMillis.h>
#define PTT_Toggle  PINB = (1<<3) //Write a 1 to PIN register PB3 to toggle the PTT state
//#define PTT_Toggle PINB = 0b00001000 //another way to do the same thing

//Global Variables
elapsedMillis Hangtime; //declare global if you don't want it reset every time loop runs
elapsedMillis Start;
int HangtimeDelay;

// ****** ENTER StartDelay DURATION ON THE NEXT LINE ******
int StartDelay = 25; //start delay duration in ms


//Constants used to set pin numbers
const int TxInh = 2;
const int PTT = 3;
const int TxGnd = 1;
const int PTTHiLo = A0; //Analog A0 used to determin PTT active hi or low
const int J4 = 4; //solder jumper MSB used to determine Hangtime
const int J0 = 0; //solder jumper LSB used to determine Hangtime


 void setup() {
//Define local variables
  int PTTState;
  
pinMode(TxInh, OUTPUT);
digitalWrite(TxInh,1); //begin with Tx inhibited
pinMode(PTT, OUTPUT);
pinMode(TxGnd, INPUT_PULLUP);
pinMode(J0, INPUT_PULLUP);
pinMode(J4, INPUT_PULLUP);
//Determine if R2 is installed.  If so PTT is active high (output is low when not transmitting)         
PTTState = analogRead(A0);
      if (PTTState <= 716) { //If true R2 is installed so PTT is to be set active high.
      digitalWrite(PTT,HIGH); //input <= 3.5V so set mode for PTT active hi
      //rem this is the gate drive not the output so HIGH = gate on, drain low; state=Rx
      }
            else {
            digitalWrite(PTT,LOW);// PB5 input is high so R2 not installed, set mode for PTT active low
            //rem this is the gate drive not the output so LOW = gate off, drain high; state=Rx
            }
// Determine the state of PB0 and PB4. These pins connect to ground via J0 and J4 to define Hangtime
int J0State;
int J4State;
int HangState; 
if(digitalRead (J4) == LOW){//turn J4 into an integer as Arduino IDE does not guarantee data type stored with digitalRead
      J4State=0;
      }
          else{
            J4State=1;
          }
J4State = J4State << 1; //make J4State the MSB to be ored with J0 state
if(digitalRead (J0) == LOW){ //turn J0 into an integer
      J0State=0;
      }
        else{
          J0State=1;
        }
HangState =  J4State | J0State; //determine hangtime ... OR values found on pins 4 (J4 MSB) and 0 (J0 LSB)
      switch (HangState)
      { //******** HANGTIME IN ms IS SET BY THE NUMBERS IN THE NEXT FOUR CASES DEPENDING ON WHICH JUMPERS ARE SHORTED TO GROUND ********
        case 0:
        HangtimeDelay = 1000; //J0 & J4 are shorted to ground
        break;
        case 1:
        HangtimeDelay = 300; //J0 is open J4 shorted to ground
        break;
        case 2:
        HangtimeDelay = 650; //J0 is shorted to grount J4 is open
        break;
        default: // or case 3 
        HangtimeDelay = 25; // both J0 and J4 are open
        break;
      }
//Save power by removing the pull-up resistors from inputs that won't be used in the loop routine
pinMode(J0, INPUT);
pinMode(J4, INPUT);
ADCSRA=0; //turn off the ADC converter to save current...not sure if the analogRead routine does this but just in case...
          // bit 7 of the ADCSRA is ADEN the A-D enable bit 1=enable 0=disable, but setting the whle register to 0= init default
pinMode(5, INPUT); //make IC pin1,PB5, the pin that determines PTTState (also reset) an input just to keep things quiet
}

void loop() {
  while (digitalRead(TxGnd) == HIGH); // if TxGnd input = key-up just wait around for TxGnd to go low.        

// TxGnd was low when entering the loop or is now low so turn on the transverter and begin the StartDelay
  PTT_Toggle;
  Start=0; //initialize StartDelay
        while (Start <= StartDelay){ //only at the end of StartDelay enable RF
            if(digitalRead (TxGnd) == HIGH){ //if a TxGnd changes state during this interval it's bouncing so restart the delay
                  Start=0; //TxGnd has gone high so stay put and reset the start delay until PTT is low for > StartDelay
                 }
        }
        digitalWrite(TxInh, LOW);  //enable RF through the system

 //wait around for the TxGnd pin to go high again
    while (digitalRead(TxGnd) == LOW); //Start delay has expired and the key is still down so wait around for key-up (TxGnd=HIGH).       
   
 //TxGnd is now high (key-up) so begin Hangtime
   Hangtime = 0;
        while (Hangtime <= HangtimeDelay){
              if (digitalRead(TxGnd) == LOW){ //key down happened so reset hangtime
                Hangtime = 0;  //keep resetting hamgtime until TxGnd goes high for longer than HangtimeDelay 
              }
        }
         
 // Hangtime has expired so inhibit RF through the system and change PTT state
   digitalWrite(TxInh, HIGH);
   delay(5); //put a little time between the two state changes just so TxInh is guaranteed to be first.
   PTT_Toggle;         
           }
