Blame view

RIOT/examples/gpio_to_serial/main.c 1.86 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  /*
   * 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;
  }