#include <stdbool.h>
#include <stdint.h>
#include "alps_task.h"
#include "bsp.h"
#include "nordic_common.h"
#include "sdk_errors.h"
#include "app_error.h"
#include "nrf_log.h"
#include "adc.h"
#include "gpio.h"
#include "platform_if.h"
#include "button.h"


extern void log_task_init(void);
extern void led_task_init(void);
extern void check_board_init(void);
extern void touch_test_task_init(void);
extern void sample_task_init(void);

TaskHandle_t startupHandle;

void button_event_callback(alps_event_t event)
{
    event = event;
    //NRF_LOG_INFO("button_event_callback=%d", event);
}

void startup_task(void *args)
{
    taskENTER_CRITICAL();
    gpio_init(alps_gpio_interrupt_handler);
    button_init(button_event_callback);
    //sensor_serial_init();

    log_task_init();
    led_task_init();
    //touch_test_task_init();

    //check_board_init();
    sample_task_init();
    /* Activate deep sleep mode */
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
    taskEXIT_CRITICAL();
    vTaskDelete(startupHandle); // delete start task
    while(1)
    {
        vTaskDelay(1000);
    };
}

int main(void)
{
    /* Initialize clock driver for better time accuracy in FREERTOS */
    clocks_start();

    xTaskCreate(startup_task, "startup", 60, NULL,
                1, 
                &startupHandle);
    /* Start FreeRTOS scheduler. */
    vTaskStartScheduler();

    while (true)
    {
        /* FreeRTOS should not be here... FreeRTOS goes back to the start of stack
         * in vTaskStartScheduler function. */
        vTaskDelay(1000);
    }
}


