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.

RPi.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "RPi.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include "log.h"
  25. const static int phys_to_gpio_rev1[41] = {-1, -1, -1, 0, -1, 1, -1, 4, 14, -1, 15, 17, 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  26. const static int phys_to_gpio_rev2[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 26, 20, -1, 21};
  27. // Declare a single default instance
  28. RPiClass RPi = RPiClass();
  29. const int* RPiClass::phys_to_gpio = NULL;
  30. void RPiClass::pinMode(uint8_t physPin, uint8_t mode)
  31. {
  32. uint8_t gpioPin;
  33. if (physToGPIO(physPin, &gpioPin) != 0) {
  34. logError("pinMode: invalid pin: %d\n", physPin);
  35. return;
  36. }
  37. BCM.pinMode(gpioPin, mode);
  38. }
  39. void RPiClass::digitalWrite(uint8_t physPin, uint8_t value)
  40. {
  41. uint8_t gpioPin;
  42. if (physToGPIO(physPin, &gpioPin) != 0) {
  43. logError("digitalWrite: invalid pin: %d\n", physPin);
  44. return;
  45. }
  46. BCM.digitalWrite(gpioPin, value);
  47. }
  48. uint8_t RPiClass::digitalRead(uint8_t physPin)
  49. {
  50. uint8_t gpioPin;
  51. if (physToGPIO(physPin, &gpioPin) != 0) {
  52. logError("digitalRead: invalid pin: %d\n", physPin);
  53. return 0;
  54. }
  55. return BCM.digitalRead(gpioPin);
  56. }
  57. uint8_t RPiClass::digitalPinToInterrupt(uint8_t physPin)
  58. {
  59. uint8_t gpioPin;
  60. if (physToGPIO(physPin, &gpioPin) != 0) {
  61. logError("digitalPinToInterrupt: invalid pin: %d\n", physPin);
  62. return 0;
  63. }
  64. return gpioPin;
  65. }
  66. int RPiClass::rpiGpioLayout()
  67. {
  68. /*
  69. * Based on wiringPi Copyright (c) 2012 Gordon Henderson.
  70. */
  71. FILE *fd;
  72. char line[120];
  73. char *c;
  74. if ((fd = fopen("/proc/cpuinfo", "r")) == NULL) {
  75. return -1;
  76. }
  77. while (fgets(line, 120, fd) != NULL) {
  78. if (strncmp(line, "Revision", 8) == 0) {
  79. fclose(fd);
  80. // Chop trailing CR/NL
  81. for (c = &line[strlen(line) - 1]; (*c == '\n') || (*c == '\r'); --c) {
  82. *c = 0;
  83. }
  84. // Scan to the first character of the revision number
  85. for (c = line; *c; ++c) {
  86. if (*c == ':') {
  87. // Chop spaces
  88. ++c;
  89. while (isspace(*c)) {
  90. ++c;
  91. }
  92. // Check hex digit at start
  93. if (!isxdigit(*c)) {
  94. return -1;
  95. }
  96. // Check bogus revision line (too small)
  97. if (strlen(c) < 4) {
  98. return -1;
  99. }
  100. // Isolate last 4 characters: (in-case of overvolting or new encoding scheme)
  101. c = c + strlen(c) - 4;
  102. if ((strcmp(c, "0002") == 0) || (strcmp(c, "0003") == 0) ||
  103. (strcmp(c, "0004") == 0) || (strcmp(c, "0005") == 0) ||
  104. (strcmp(c, "0006") == 0) || (strcmp(c, "0007") == 0) ||
  105. (strcmp(c, "0008") == 0) || (strcmp(c, "0009") == 0) ||
  106. (strcmp(c, "000d") == 0) || (strcmp(c, "000e") == 0) ||
  107. (strcmp(c, "000f") == 0)) {
  108. return 1;
  109. } else {
  110. return 2;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. fclose(fd);
  117. return -1;
  118. }
  119. int RPiClass::physToGPIO(uint8_t physPin, uint8_t *gpio)
  120. {
  121. if (phys_to_gpio == NULL) {
  122. if (rpiGpioLayout() == 1) {
  123. // A, B, Rev 1, 1.1
  124. phys_to_gpio = &phys_to_gpio_rev1[0];
  125. } else {
  126. // A2, B2, A+, B+, CM, Pi2, Pi3, Zero
  127. phys_to_gpio = &phys_to_gpio_rev2[0];
  128. }
  129. }
  130. if (gpio == NULL || physPin > 40) {
  131. return -1;
  132. }
  133. int pin = *(phys_to_gpio+physPin);
  134. if (pin == -1) {
  135. return -1;
  136. } else {
  137. *gpio = pin;
  138. }
  139. return 0;
  140. }