
#include "globals.h"
#include <stdio.h>


//char* Send_Buffer_Button;


/*----------------------------------------------------------------------------
 *        IRQ 'AButton_IRQHandler': External Interrupt Handler
 *---------------------------------------------------------------------------*/
void Button_IRQHandler (void) {

    isr_evt_set(0x0001, idButton_Task); // send an event to the Button Task

}

/*----------------------------------------------------------------------------
 *        Task 'LED_Task': Alive Task, toggles LED on GPIO
 *---------------------------------------------------------------------------*/
__task void LED_Task () {

    while(1) {
        LED_On (1);
        os_dly_wait (25);
        LED_Off(1);
        os_dly_wait (25);
    }

}


/*----------------------------------------------------------------------------
 *        Task 'Button_Task': Sends a message when button is pressed
 *---------------------------------------------------------------------------*/
__task void Button_Task () {
    char* Message;	 								// pointer for messages

    while (1) {
        os_dly_wait(10);							// debounce button
        EnableButtonIRQ();						    // enable button IRQ
        os_evt_wait_or(0x0001, 0xFFFF);				// wait for IRQ event
        os_evt_clr (0x0001, idButton_Task);			// clear event flags
        do {
            Message = _alloc_box(mpoolCom);			// allocate memory (125bytes) from mpoolcom, if available
													// initialises the pointer to the start of this block
        } while(!Message);							// we wait here until we get memory allocated
        sprintf (Message, "Button was pressed\n");	// prints a string into the memory pointed to by message
        printf ("%s", Message);						// actually print out what is pointed to by message
		_free_box(mpoolCom, Message);			    // after the message has been sent
													// the allocated memory is released

    }
}

/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/
