nerve.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Compile:
  3. g++ -g -o nerve nerve.cpp -lwiringPi
  4. Run:
  5. cd /var/www/yana-server
  6. sudo chmod +x ./nerve
  7. sudo ./nerve /var/www/yana-server/action.php
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <wiringPi.h>
  14. #include <iostream>
  15. #include <sys/time.h>
  16. #include <sstream>
  17. #define BUTTON_PIN 0
  18. std::string phppath;
  19. std::string longToString(long mylong){
  20. std::string mystring;
  21. std::stringstream mystream;
  22. mystream << mylong;
  23. return mystream.str();
  24. }
  25. void gpio_change(int pin){
  26. long state = digitalRead(pin);
  27. printf( "GPIO %d CHANGED TO %d\n", pin,state);
  28. std::string command;
  29. std::string path = "php ";
  30. path.append(phppath);
  31. command = path+" GPIO_HAS_CHANGED";
  32. command.append(" "+longToString(pin));
  33. command.append(" "+longToString(state));
  34. printf( "LAUNCH %s\n", command.c_str());
  35. system(command.c_str());
  36. }
  37. void gpio_0() {
  38. gpio_change(0);
  39. }
  40. void gpio_1() {
  41. gpio_change(1);
  42. }
  43. void gpio_2() {
  44. gpio_change(2);
  45. }
  46. void gpio_3() {
  47. gpio_change(3);
  48. }
  49. void gpio_4() {
  50. gpio_change(4);
  51. }
  52. void gpio_5() {
  53. gpio_change(5);
  54. }
  55. void gpio_6() {
  56. gpio_change(6);
  57. }
  58. void gpio_7() {
  59. gpio_change(7);
  60. }
  61. void gpio_8() {
  62. gpio_change(8);
  63. }
  64. void gpio_9() {
  65. gpio_change(9);
  66. }
  67. void gpio_10() {
  68. gpio_change(10);
  69. }
  70. void gpio_11() {
  71. gpio_change(11);
  72. }
  73. void gpio_12() {
  74. gpio_change(12);
  75. }
  76. void gpio_13() {
  77. gpio_change(13);
  78. }
  79. void gpio_14() {
  80. gpio_change(14);
  81. }
  82. void gpio_15() {
  83. gpio_change(15);
  84. }
  85. void gpio_16() {
  86. gpio_change(16);
  87. }
  88. void gpio_17() {
  89. gpio_change(17);
  90. }
  91. void gpio_18() {
  92. gpio_change(18);
  93. }
  94. void gpio_19() {
  95. gpio_change(19);
  96. }
  97. void gpio_20() {
  98. gpio_change(20);
  99. }
  100. void gpio_21() {
  101. gpio_change(21);
  102. }
  103. void gpio_22() {
  104. gpio_change(22);
  105. }
  106. void gpio_23() {
  107. gpio_change(23);
  108. }
  109. void gpio_24() {
  110. gpio_change(24);
  111. }
  112. void gpio_25() {
  113. gpio_change(25);
  114. }
  115. void gpio_26() {
  116. gpio_change(26);
  117. }
  118. void gpio_27() {
  119. gpio_change(27);
  120. }
  121. void gpio_28() {
  122. gpio_change(28);
  123. }
  124. void gpio_29() {
  125. gpio_change(29);
  126. }
  127. void logline(std::string a){
  128. std::cout << a << std::endl;
  129. }
  130. // -------------------------------------------------------------------------
  131. // main
  132. int main(int argc, char** argv) {
  133. logline("Launch nerves...");
  134. phppath = argv[1];
  135. // sets up the wiringPi library
  136. if (wiringPiSetup () < 0) {
  137. fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
  138. return 1;
  139. }
  140. //for (long i=0;i<40;i++)
  141. // {
  142. if ( wiringPiISR (0, INT_EDGE_BOTH, &gpio_0) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  143. if ( wiringPiISR (1, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  144. if ( wiringPiISR (2, INT_EDGE_BOTH, &gpio_2) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  145. if ( wiringPiISR (3, INT_EDGE_BOTH, &gpio_3) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  146. if ( wiringPiISR (4, INT_EDGE_BOTH, &gpio_4) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  147. if ( wiringPiISR (5, INT_EDGE_BOTH, &gpio_5) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  148. if ( wiringPiISR (6, INT_EDGE_BOTH, &gpio_6) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  149. if ( wiringPiISR (7, INT_EDGE_BOTH, &gpio_7) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  150. /*if ( wiringPiISR (8, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  151. if ( wiringPiISR (9, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  152. if ( wiringPiISR (10, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  153. if ( wiringPiISR (11, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  154. if ( wiringPiISR (12, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  155. if ( wiringPiISR (13, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  156. if ( wiringPiISR (14, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  157. if ( wiringPiISR (15, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  158. if ( wiringPiISR (16, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  159. if ( wiringPiISR (17, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  160. if ( wiringPiISR (18, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  161. if ( wiringPiISR (19, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  162. if ( wiringPiISR (20, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  163. if ( wiringPiISR (21, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  164. if ( wiringPiISR (22, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  165. if ( wiringPiISR (23, INT_EDGE_BOTH, &gpio_1) < 0 ) fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  166. */
  167. // }
  168. logline("Nerves ready, waiting life.");
  169. while(1){
  170. }
  171. return 0;
  172. }