/**
 * @file main.c
 * @brief Demo Button Toggle
 *
 * In this example one LED is toggled every time one button is pressed.
 *
 * @date 2021
 * @author Strahinja Jankovic (jankovics@etf.bg.ac.rs)
 * @author Marija Bezulj (meja@etf.bg.ac.rs)
 *
 * @version [2.0 @ 04/2021] Hardware change
 * @version [1.1 @ 03/2019] Code refactoring
 * @version [1.0 @ 03/2017] Initial version
 */

#include <msp430.h> 
#include <stdint.h>

/**
 * @brief Main function
 *
 * Demo toggles one LED every time one button is pressed
 */
int main(void)
{
    // variable for storing previous button state
    uint8_t P1IN_old = 1;

	WDTCTL = WDTPW | WDTHOLD;	// stop watchdog timer
	
    // GPIO initialization
    // LED1
    P2OUT &= ~BIT4;             // clear P2.4
    P2DIR |= BIT4;              // configure P2.4 as out

    // Switch S1
    P1DIR &= ~BIT5;             // configure P1.5 as in
    P1REN |= BIT5;              // enable pull up/down
    P1OUT |= BIT5;              // configure pull up

    //main loop
    while(1)
    {
        // detect falling edge
        if (((P1IN_old & BIT5) != 0) & ((P1IN & BIT5) == 0))
        {
            P2OUT ^= BIT4;      // toggle led
        }
        P1IN_old = P1IN;        // update latest value
    }

}
