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.

GPIO.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "GPIO.h"
  20. #include <dirent.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #include "log.h"
  27. // Declare a single default instance
  28. GPIOClass GPIO = GPIOClass();
  29. GPIOClass::GPIOClass()
  30. {
  31. FILE *f;
  32. DIR* dp;
  33. char file[64];
  34. dp = opendir("/sys/class/gpio");
  35. if (dp == NULL) {
  36. logError("Could not open /sys/class/gpio directory");
  37. exit(1);
  38. }
  39. lastPinNum = 0;
  40. while (true) {
  41. dirent *de = readdir(dp);
  42. if (de == NULL) {
  43. break;
  44. }
  45. if (strncmp("gpiochip", de->d_name, 8) == 0) {
  46. sprintf(file, "/sys/class/gpio/%s/base", de->d_name);
  47. f = fopen(file, "r");
  48. int base;
  49. if (fscanf(f, "%d", &base) == EOF) {
  50. logError("Failed to open %s\n", file);
  51. base = 0;
  52. }
  53. fclose(f);
  54. sprintf(file, "/sys/class/gpio/%s/ngpio", de->d_name);
  55. f = fopen(file, "r");
  56. int ngpio;
  57. if (fscanf(f, "%d", &ngpio) == EOF) {
  58. logError("Failed to open %s\n", file);
  59. ngpio = 0;
  60. }
  61. fclose(f);
  62. int max = ngpio + base - 1;
  63. if (lastPinNum < max) {
  64. lastPinNum = max;
  65. }
  66. }
  67. }
  68. closedir(dp);
  69. exportedPins = new uint8_t[lastPinNum + 1];
  70. for (int i = 0; i < lastPinNum + 1; ++i) {
  71. exportedPins[i] = 0;
  72. }
  73. }
  74. GPIOClass::GPIOClass(const GPIOClass& other)
  75. {
  76. lastPinNum = other.lastPinNum;
  77. exportedPins = new uint8_t[lastPinNum + 1];
  78. for (int i = 0; i < lastPinNum + 1; ++i) {
  79. exportedPins[i] = other.exportedPins[i];
  80. }
  81. }
  82. GPIOClass::~GPIOClass()
  83. {
  84. FILE *f;
  85. for (int i = 0; i < lastPinNum + 1; ++i) {
  86. if (exportedPins[i]) {
  87. f = fopen("/sys/class/gpio/unexport", "w");
  88. fprintf(f, "%d\n", i);
  89. fclose(f);
  90. }
  91. }
  92. delete [] exportedPins;
  93. }
  94. void GPIOClass::pinMode(uint8_t pin, uint8_t mode)
  95. {
  96. FILE *f;
  97. if (pin > lastPinNum) {
  98. return;
  99. }
  100. f = fopen("/sys/class/gpio/export", "w");
  101. fprintf(f, "%d\n", pin);
  102. fclose(f);
  103. int counter = 0;
  104. char file[128];
  105. sprintf(file, "/sys/class/gpio/gpio%d/direction", pin);
  106. while ((f = fopen(file,"w")) == NULL) {
  107. // Wait 10 seconds for the file to be accessible if not open on first attempt
  108. sleep(1);
  109. counter++;
  110. if (counter > 10) {
  111. logError("Could not open /sys/class/gpio/gpio%u/direction", pin);
  112. exit(1);
  113. }
  114. }
  115. if (mode == INPUT) {
  116. fprintf(f, "in\n");
  117. } else {
  118. fprintf(f, "out\n");
  119. }
  120. exportedPins[pin] = 1;
  121. fclose(f);
  122. }
  123. void GPIOClass::digitalWrite(uint8_t pin, uint8_t value)
  124. {
  125. FILE *f;
  126. char file[128];
  127. if (pin > lastPinNum) {
  128. return;
  129. }
  130. if (0 == exportedPins[pin]) {
  131. pinMode(pin, OUTPUT);
  132. }
  133. sprintf(file, "/sys/class/gpio/gpio%d/value", pin);
  134. f = fopen(file, "w");
  135. if (value == 0) {
  136. fprintf(f, "0\n");
  137. } else {
  138. fprintf(f, "1\n");
  139. }
  140. fclose(f);
  141. }
  142. uint8_t GPIOClass::digitalRead(uint8_t pin)
  143. {
  144. FILE *f;
  145. char file[128];
  146. if (pin > lastPinNum) {
  147. return 0;
  148. }
  149. if (0 == exportedPins[pin]) {
  150. pinMode(pin, INPUT);
  151. }
  152. sprintf(file, "/sys/class/gpio/gpio%d/value", pin);
  153. f = fopen(file, "r");
  154. int i;
  155. if (fscanf(f, "%d", &i) == EOF) {
  156. logError("digitalRead: failed to read pin %u\n", pin);
  157. i = 0;
  158. }
  159. fclose(f);
  160. return i;
  161. }
  162. uint8_t GPIOClass::digitalPinToInterrupt(uint8_t pin)
  163. {
  164. return pin;
  165. }
  166. GPIOClass& GPIOClass::operator=(const GPIOClass& other)
  167. {
  168. if (this != &other) {
  169. lastPinNum = other.lastPinNum;
  170. exportedPins = new uint8_t[lastPinNum + 1];
  171. for (int i = 0; i < lastPinNum + 1; ++i) {
  172. exportedPins[i] = other.exportedPins[i];
  173. }
  174. }
  175. return *this;
  176. }