main.c 1.86 KB
/*
 * Copyright (C) 2016 Inria
 *
 * This file is subject to the terms and conditions of the GNU Lesser General
 * Public License v3. See the file LICENSE in the top level directory for more
 * details.
 */

/**
 * @{
 *
 * @file
 * @brief       Sample application using GPIO and UART interface
 *
 * @author      Alexandre Abadie <alexandre.abadie@inria.fr>
 *
 * @}
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "board.h"
#include "msg.h"
#include "thread.h"
#include "xtimer.h"
#include "periph/gpio.h"


static uint32_t last_press;
static kernel_pid_t main_thread_pid;

static void gpio_cb(void *pin)
{
    /* prevent bounce effect with on board button */
    if (last_press + 100 < xtimer_now().ticks32) {
        printf("GPIO Callback\n");
        gpio_toggle(LED1_PIN);
        msg_t msg;
        msg.content.value = (uint32_t)(NULL);
        msg_send(&msg, main_thread_pid);
        last_press = xtimer_now().ticks32;
    }
}

int main(void)
{
    printf("GPIO to UART sample application\r\n");

    if (gpio_init(LED1_PIN, GPIO_OUT) < 0) {
        puts("Error while initializing LED GPIO as output\n");
        return 1;
    }
    printf("LED GPIO initialized successfully as output\n");
    /* Shutdown LED */
    gpio_clear(LED1_PIN);

    if (gpio_init_int(BTN0_PIN,GPIO_IN_PD, GPIO_RISING, gpio_cb,
                      (void *)BTN0_PIN) < 0) {
        puts("Error while initializing BUTTON USER as external interrupt\n");
        return 1;
    }
    printf("BUTTON GPIO initialized successfuly\n");

    last_press = xtimer_now().ticks32;

    /* Get main thread pid */
    main_thread_pid = thread_getpid();
    msg_t msg;
    for (;;) {
        msg_receive(&msg); /* This line blocks the loop until a message is
                              received. */
        printf("Message received, LED is %s\n", gpio_read(LED1_PIN)? "ON" : "OFF");
    }

    return 0;
}