// -----------------------------------
// Blinking the LED from the App
// -----------------------------------

/* instantiate the led variables to the pins that are connected*/

int led1 = D7;
//int led2 = D7;


//Declare the Particle Function for WITHIN THIS SPECIFIC PROGRAM
int appBlink(String command);



void setup()
{

   // Here's the pin configuration, same as last time
   pinMode(led1, OUTPUT);
  // pinMode(led2, OUTPUT);

   // Declare a Particle.function FOR THE CLOUD-- referenece the name in "" when in app
   Particle.function("ledAppToggle", appBlink);
   // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.

   // Start with LED OFF -- set up initial LED configuration:
   digitalWrite(led1, LOW);
  // digitalWrite(led2, LOW);

}


/* Last time, we wanted to continously blink the LED on and off
Since we're waiting for input through the cloud this time,
we don't actually need to put anything in the loop */

void loop()
{
   // Nothing to do here
}

//Declare the actual function specifics here:
    // Particle.functions ALWAYS TAKE STRINGS as arguments
    // ALWAYS RETURN INTEGERS

// -------------------- ledAppToggle function ---------------------------

int appBlink(String command) {
    /* Particle.functions always take a string as an argument and return an integer.
    Since we can pass a string, it means that we can give the program commands on how the function should be used.
    In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
    Then, the function returns a value to us to let us know what happened.
    In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
    and -1 if we received a totally bogus command that didn't do anything to the LEDs.
    */


    if (command=="on") {
        digitalWrite(led1,HIGH);
        return 1;           // 1 means light ON
    }
    else if (command=="off") {
        digitalWrite(led1,LOW);
        return 0;           // 0 means light OFF
    }
    else {
        return -1;
    }
}

// ----------------- ledAppToggle function End ------------------------
