Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
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.

145 lines
3.4 KiB

  1. /*
  2. * https://github.com/mrshu/GPIOlib
  3. * Copyright (c) 2011, Copyright (c) 2011 mr.Shu
  4. * All rights reserved.
  5. *
  6. * Modified on 24 June 2012, 11:06 AM
  7. * File: gpio.cpp
  8. * Author: purinda (purinda@gmail.com)
  9. *
  10. * Patched for filedescriptor catching and error control by L Diaz 2018
  11. */
  12. #include "gpio.h"
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. std::map<int,GPIOfdCache_t> GPIO::cache;
  19. GPIO::GPIO() {
  20. }
  21. GPIO::~GPIO() {
  22. }
  23. void GPIO::open(int port, int DDR)
  24. {
  25. FILE *f;
  26. f = fopen("/sys/class/gpio/export", "w");
  27. if(f==NULL) throw GPIOException("can't export GPIO pin .check access rights");
  28. fprintf(f, "%d\n", port);
  29. fclose(f);
  30. int counter = 0;
  31. char file[128];
  32. sprintf(file, "/sys/class/gpio/gpio%d/direction", port);
  33. while( ( f = fopen(file,"w")) == NULL ){ //Wait 10 seconds for the file to be accessible if not open on first attempt
  34. sleep(1);
  35. counter++;
  36. if(counter > 10){
  37. throw GPIOException("can't access /sys/class/gpio/gpio%d/direction GPIO pin. check access rights");
  38. /*perror("Could not open /sys/class/gpio/gpio%d/direction");
  39. exit(0); */
  40. }
  41. }
  42. int l=(DDR==0) ? fprintf(f, "in\n") : fprintf(f, "out\n");
  43. if(!(l==3 || l==4)) {
  44. fclose(f);
  45. throw GPIOException("can't set direction on GPIO pin. check access rights");
  46. }
  47. /*
  48. if (DDR == 0)
  49. fprintf(f, "in\n");
  50. else printf(f, "out\n");
  51. */
  52. fclose(f);
  53. // Caches the GPIO descriptor;
  54. sprintf(file, "/sys/class/gpio/gpio%d/value", port);
  55. int flags= (DDR==0) ? O_RDONLY : O_WRONLY;
  56. int fd=::open(file,flags);
  57. if(fd<0) {
  58. throw GPIOException("Can't open the GPIO");
  59. } else {
  60. cache[port]=fd; // cache the fd;
  61. lseek(fd,SEEK_SET,0);
  62. }
  63. }
  64. void GPIO::close(int port)
  65. {
  66. std::map<int,GPIOfdCache_t>::iterator i;
  67. i=cache.find(port);
  68. if(i!=cache.end()){
  69. close(i->second); // close the cached fd
  70. cache.erase(i); // Delete cache entry
  71. }
  72. // Do unexport
  73. FILE *f;
  74. f = fopen("/sys/class/gpio/unexport", "w");
  75. if(f!=NULL) {
  76. fprintf(f, "%d\n", port);
  77. fclose(f);
  78. }
  79. }
  80. int GPIO::read(int port)
  81. {
  82. std::map<int,GPIOfdCache_t>::iterator i;
  83. int fd;
  84. i=cache.find(port);
  85. if(i==cache.end()){ // Fallback to open the gpio
  86. GPIO::open(port,GPIO::DIRECTION_IN);
  87. i=cache.find(port);
  88. if(i==cache.end()) throw GPIOException("can't access to GPIO");
  89. else fd=i->second;
  90. } else fd=i->second;
  91. char c;
  92. if(lseek(fd,0,SEEK_SET)==0 && ::read(fd,&c,1)==1){
  93. return (c=='0') ? 0 : 1;
  94. } else throw GPIOException("can't access to GPIO");
  95. /*FILE *f;
  96. char file[128];
  97. sprintf(file, "/sys/class/gpio/gpio%d/value", port);
  98. f = fopen(file, "r");
  99. int i;
  100. fscanf(f, "%d", &i);
  101. fclose(f);
  102. return i;
  103. */
  104. }
  105. void GPIO::write(int port, int value){
  106. std::map<int,GPIOfdCache_t>::iterator i;
  107. int fd;
  108. i=cache.find(port);
  109. if(i==cache.end()){ // Fallback to open the gpio
  110. GPIO::open(port,GPIO::DIRECTION_OUT);
  111. i=cache.find(port);
  112. if(i==cache.end()) throw GPIOException("can't access to GPIO");
  113. else fd=i->second;
  114. } else fd=i->second;
  115. if(lseek(fd,0,SEEK_SET)!=0) throw GPIOException("can't access to GPIO");
  116. int l=(value==0) ? ::write(fd,"0\n",2) : ::write(fd,"1\n",2);
  117. if(l!=2) throw GPIOException("can't access to GPIO");
  118. /*FILE *f;
  119. char file[128];
  120. sprintf(file, "/sys/class/gpio/gpio%d/value", port);
  121. f = fopen(file, "w");
  122. if (value == 0) fprintf(f, "0\n");
  123. else fprintf(f, "1\n");
  124. fclose(f);*/
  125. }