// This is an Arduino sketch by W3SZ Roger Rehr
// to provide control of SM5BSZ's WSE hardware
// from Windows, using a USB connection.
// Details are at:  
// http://www.nitehawk.com/w3sz/Arduino_USB_WSE_Controller.html
// This was written and tested in February 2016.

String inputString = "";
boolean stringComplete = false;
String command = "";
//Output Pin Assignments
int clock_1 = 8;
int rx144_5 = 5;
int rx10700_6 = 6;
int rx70_7 = 7;
int gain_2 = 3;
int freq_3 = 4;
int data_17 = 9;

//Input Pin Assignments
int status_10 = 10;


void setup() {
  // setup code here runs once:

  //setup output pins
  pinMode (clock_1, OUTPUT);
  pinMode (rx144_5, OUTPUT);
  pinMode (rx10700_6, OUTPUT);
  pinMode (rx70_7, OUTPUT);
  pinMode (gain_2, OUTPUT);
  pinMode (freq_3, OUTPUT);
  pinMode (data_17, OUTPUT);

  //setup input pins
  pinMode (status_10, INPUT);

  digitalWrite(gain_2, LOW);
  digitalWrite(freq_3, LOW);
  digitalWrite(rx144_5, LOW);
  digitalWrite(rx70_7, LOW);
  digitalWrite(rx10700_6, LOW);
  digitalWrite(clock_1, HIGH); //parallel port inverts pins 1 & 17
  digitalWrite(data_17, HIGH); //parallel port inverts pins 1 & 17

  // initialize serial. This device should appear as a virtual com port such as: "Arduino Uno (COM7)"
  Serial.begin(9600, SERIAL_8N1); // 9600/8/N/1
  inputString.reserve(3);
}

void loop() { //MAIN
  // put your main code here, to run repeatedly:
  if (stringComplete)
  {
    command = inputString;
    inputString = "";
    stringComplete = false;
  }
  if (command.equals("0\n")) //data low
  {
    digitalWrite(gain_2, LOW); //Arduino pin3
    digitalWrite(freq_3, LOW); //Arduino pin4
    digitalWrite(rx144_5, LOW); //Arduino pin5
    digitalWrite(rx70_7, LOW); //Arduino pin7
    digitalWrite(rx10700_6, LOW); //Arduino pin6
  }

  if (command.equals("4\n")) //data high
  {
    digitalWrite(data_17, LOW); //Arduino pin 9  was high //parallel port inverts pins 1 & 17
  }
  if (command.equals("24\n")) //data low
  {
    digitalWrite(data_17, HIGH); //was low    //parallel port inverts pins 1 & 17
  }

  if (command.equals("30\n")) //clock high  //parallel port inverts pins 1 & 17
  {
    digitalWrite(clock_1, HIGH); //Arduino pin 8
  }

  if (command.equals("50\n")) //clock low  //parallel port inverts pins 1 & 17
  {
    digitalWrite(clock_1, LOW);
  }

  if (command.equals("1\n")) //
  {
    digitalWrite(gain_2, HIGH); //Arduino pin 3
  }

  if (command.equals("2\n"))
  {
    digitalWrite(freq_3, HIGH); //Arduino pin 4
  }

  if (command.equals("8\n"))
  {
    digitalWrite(rx144_5, HIGH); //Arduino pin 5
  }

  if (command.equals("32\n"))
  {
    digitalWrite(rx70_7, HIGH); //Arduino pin 7
  }

  if (command.equals("16\n"))
  {
    digitalWrite(rx10700_6, HIGH); //Arduino pin 6
  }

}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') { // Linrad commands all end with a newline
      stringComplete = true;
    }
  }
}