Blame view

RIOT/tests/driver_pir/main.c 2.02 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
79
80
81
82
83
84
85
86
87
88
  /*
   * Copyright (C) 2014 Freie Universität Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup tests
   * @{
   *
   * @file
   * @brief       Test application for the PIR motion sensor driver
   *
   * @author      Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
   *
   * @}
   */
  
  #ifndef PIR_GPIO
  #error "PIR_GPIO not defined"
  #endif
  
  #include <stdio.h>
  
  #include "thread.h"
  #include "xtimer.h"
  #include "pir.h"
  
  static char pir_handler_stack[THREAD_STACKSIZE_MAIN];
  static pir_t dev;
  
  void* pir_handler(void *arg)
  {
      (void)arg;
      msg_t msg_q[1];
      msg_init_queue(msg_q, 1);
  
      printf("Registering PIR handler thread...     %s\n",
             pir_register_thread(&dev) == 0 ? "[OK]" : "[Failed]");
  
      msg_t m;
      while (msg_receive(&m)) {
          printf("PIR handler got a message: ");
          switch (m.type) {
              case PIR_STATUS_HI:
                  puts("something started moving.");
                  break;
              case PIR_STATUS_LO:
                  puts("the movement has ceased.");
                  break;
              default:
                  puts("stray message.");
                  break;
          }
      }
      puts("PIR handler: this should not have happened!");
  
      return NULL;
  }
  
  int main(void)
  {
      puts("PIR motion sensor test application\n");
      printf("Initializing PIR sensor at GPIO_%ld... ", (long)PIR_GPIO);
      if (pir_init(&dev, PIR_GPIO) == 0) {
          puts("[OK]\n");
      }
      else {
          puts("[Failed]");
          return 1;
      }
  
  #if TEST_PIR_POLLING
      puts("Printing sensor state every second.");
      while (1) {
          printf("Status: %s\n", pir_get_status(&dev) == PIR_STATUS_LO ? "lo" : "hi");
          xtimer_usleep(1000 * 1000);
      }
  #else
     thread_create(
             pir_handler_stack, sizeof(pir_handler_stack), THREAD_PRIORITY_MAIN - 1,
             THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
             pir_handler, NULL, "pir_handler");
  #endif
      return 0;
  }