Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

interrupt.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * Based on wiringPi Copyright (c) 2012 Gordon Henderson.
  20. */
  21. #include "interrupt.h"
  22. #include <pthread.h>
  23. #include <stdlib.h>
  24. #include <sys/ioctl.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <poll.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <stropts.h>
  33. #include <errno.h>
  34. #include <sched.h>
  35. #include "log.h"
  36. struct ThreadArgs {
  37. void (*func)();
  38. int gpioPin;
  39. };
  40. volatile bool interruptsEnabled = true;
  41. static pthread_mutex_t intMutex = PTHREAD_MUTEX_INITIALIZER;
  42. static pthread_t *threadIds[64] = {NULL};
  43. // sysFds:
  44. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  45. static int sysFds[64] = {
  46. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  47. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  48. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  49. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  50. };
  51. /*
  52. * Part of wiringPi: Simple way to get your program running at high priority
  53. * with realtime schedulling.
  54. */
  55. int piHiPri(const int pri)
  56. {
  57. struct sched_param sched ;
  58. memset (&sched, 0, sizeof(sched)) ;
  59. if (pri > sched_get_priority_max (SCHED_RR)) {
  60. sched.sched_priority = sched_get_priority_max (SCHED_RR) ;
  61. } else {
  62. sched.sched_priority = pri ;
  63. }
  64. return sched_setscheduler (0, SCHED_RR, &sched) ;
  65. }
  66. void *interruptHandler(void *args)
  67. {
  68. int fd;
  69. struct pollfd polls;
  70. char c;
  71. struct ThreadArgs *arguments = (struct ThreadArgs *)args;
  72. int gpioPin = arguments->gpioPin;
  73. void (*func)() = arguments->func;
  74. delete arguments;
  75. (void)piHiPri(55); // Only effective if we run as root
  76. if ((fd = sysFds[gpioPin]) == -1) {
  77. logError("Failed to attach interrupt for pin %d\n", gpioPin);
  78. return NULL;
  79. }
  80. // Setup poll structure
  81. polls.fd = fd;
  82. polls.events = POLLPRI | POLLERR;
  83. while (1) {
  84. // Wait for it ...
  85. int ret = poll(&polls, 1, -1);
  86. if (ret < 0) {
  87. logError("Error waiting for interrupt: %s\n", strerror(errno));
  88. break;
  89. }
  90. // Do a dummy read to clear the interrupt
  91. // A one character read appars to be enough.
  92. if (lseek (fd, 0, SEEK_SET) < 0) {
  93. logError("Interrupt handler error: %s\n", strerror(errno));
  94. break;
  95. }
  96. if (read (fd, &c, 1) < 0) {
  97. logError("Interrupt handler error: %s\n", strerror(errno));
  98. break;
  99. }
  100. // Call user function.
  101. pthread_mutex_lock(&intMutex);
  102. if (interruptsEnabled) {
  103. pthread_mutex_unlock(&intMutex);
  104. func();
  105. } else {
  106. pthread_mutex_unlock(&intMutex);
  107. }
  108. }
  109. close(fd);
  110. return NULL;
  111. }
  112. void attachInterrupt(uint8_t gpioPin, void (*func)(), uint8_t mode)
  113. {
  114. FILE *fd;
  115. char fName[40];
  116. char c;
  117. int count, i;
  118. if (threadIds[gpioPin] == NULL) {
  119. threadIds[gpioPin] = new pthread_t;
  120. } else {
  121. // Cancel the existing thread for that pin
  122. pthread_cancel(*threadIds[gpioPin]);
  123. // Wait a bit
  124. usleep(1000);
  125. }
  126. // Export pin for interrupt
  127. if ((fd = fopen("/sys/class/gpio/export", "w")) == NULL) {
  128. logError("attachInterrupt: Unable to export pin %d for interrupt: %s\n", gpioPin, strerror(errno));
  129. exit(1);
  130. }
  131. fprintf(fd, "%d\n", gpioPin);
  132. fclose(fd);
  133. // Wait a bit the system to create /sys/class/gpio/gpio<GPIO number>
  134. usleep(1000);
  135. snprintf(fName, sizeof(fName), "/sys/class/gpio/gpio%d/direction", gpioPin) ;
  136. if ((fd = fopen (fName, "w")) == NULL) {
  137. logError("attachInterrupt: Unable to open GPIO direction interface for pin %d: %s\n",
  138. gpioPin, strerror(errno));
  139. exit(1) ;
  140. }
  141. fprintf(fd, "in\n") ;
  142. fclose(fd) ;
  143. snprintf(fName, sizeof(fName), "/sys/class/gpio/gpio%d/edge", gpioPin) ;
  144. if ((fd = fopen(fName, "w")) == NULL) {
  145. logError("attachInterrupt: Unable to open GPIO edge interface for pin %d: %s\n", gpioPin,
  146. strerror(errno));
  147. exit(1) ;
  148. }
  149. switch (mode) {
  150. case CHANGE:
  151. fprintf(fd, "both\n");
  152. break;
  153. case FALLING:
  154. fprintf(fd, "falling\n");
  155. break;
  156. case RISING:
  157. fprintf(fd, "rising\n");
  158. break;
  159. case NONE:
  160. fprintf(fd, "none\n");
  161. break;
  162. default:
  163. logError("attachInterrupt: Invalid mode\n");
  164. fclose(fd);
  165. return;
  166. }
  167. fclose(fd);
  168. if (sysFds[gpioPin] == -1) {
  169. snprintf(fName, sizeof(fName), "/sys/class/gpio/gpio%d/value", gpioPin);
  170. if ((sysFds[gpioPin] = open(fName, O_RDWR)) < 0) {
  171. logError("Error reading pin %d: %s\n", gpioPin, strerror(errno));
  172. exit(1);
  173. }
  174. }
  175. // Clear any initial pending interrupt
  176. ioctl(sysFds[gpioPin], FIONREAD, &count);
  177. for (i = 0; i < count; ++i) {
  178. if (read(sysFds[gpioPin], &c, 1) == -1) {
  179. logError("attachInterrupt: failed to read pin status: %s\n", strerror(errno));
  180. }
  181. }
  182. struct ThreadArgs *threadArgs = new struct ThreadArgs;
  183. threadArgs->func = func;
  184. threadArgs->gpioPin = gpioPin;
  185. // Create a thread passing the pin and function
  186. pthread_create(threadIds[gpioPin], NULL, interruptHandler, (void *)threadArgs);
  187. }
  188. void detachInterrupt(uint8_t gpioPin)
  189. {
  190. // Cancel the thread
  191. if (threadIds[gpioPin] != NULL) {
  192. pthread_cancel(*threadIds[gpioPin]);
  193. delete threadIds[gpioPin];
  194. threadIds[gpioPin] = NULL;
  195. }
  196. // Close filehandle
  197. if (sysFds[gpioPin] != -1) {
  198. close(sysFds[gpioPin]);
  199. sysFds[gpioPin] = -1;
  200. }
  201. FILE *fp = fopen("/sys/class/gpio/unexport", "w");
  202. if (fp == NULL) {
  203. logError("Unable to unexport pin %d for interrupt\n", gpioPin);
  204. exit(1);
  205. }
  206. fprintf(fp, "%d", gpioPin);
  207. fclose(fp);
  208. }
  209. void interrupts()
  210. {
  211. pthread_mutex_lock(&intMutex);
  212. interruptsEnabled = true;
  213. pthread_mutex_unlock(&intMutex);
  214. }
  215. void noInterrupts()
  216. {
  217. pthread_mutex_lock(&intMutex);
  218. interruptsEnabled = false;
  219. pthread_mutex_unlock(&intMutex);
  220. }