Advertisement
Guest User

Untitled

a guest
May 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <curses.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <unistd.h>
  7. #include <wiringPi.h>
  8. #include <errno.h>
  9.  
  10. #define DEBOUNCING 100
  11.  
  12. #define LEFT_BUTTON_PIN 17
  13. #define MIDDLE_BUTTON_PIN 4
  14. #define RIGHT_BUTTON_PIN 22
  15. #define RED_DIODE_PIN 6
  16. #define GREEN_DIODE_PIN 26
  17.  
  18. bool checkDebouncing(unsigned int* time);
  19. int setupPi();
  20. void rightButtonInterrupt();
  21. void leftButtonInterrupt();
  22. void middleButtonInterrupt();
  23.  
  24. unsigned int leftButtonTime = 0;
  25. unsigned int middleButtonTime = 0;
  26. unsigned int rightButtonTime = 0;
  27.  
  28. int* order = { 0, 1, 0, 2, 0 };
  29. int currentIndex = 0;
  30. int exitFlag = 1;
  31. int currentlyPressed = -1;
  32.  
  33. int main()
  34. {
  35. int orderSize = sizeof(order) / sizeof(order[0]);
  36.  
  37. if (setupPi() != 0) {
  38. return -1;
  39. }
  40.  
  41. printf("Let's begin\n\n");
  42. while (exitFlag) {
  43. if (currentlyPressed >= 0) {
  44. printf("\t1\n");
  45. if (currentlyPressed == order[currentIndex]) {
  46. printf("\t1\n");
  47. currentIndex++;
  48. if (currentIndex == orderSize) {
  49. printf("\t1\n");
  50. break;
  51. }
  52. printf("Dobry przycisk! Pozostalo: %d\n", orderSize - currentIndex);
  53. }
  54. else {
  55.  
  56. printf("Niestety, zaczynasz od poczatku\n");
  57. }
  58. currentlyPressed = -1;
  59. }
  60. }
  61.  
  62. printf("*************success******************");
  63. return 0;
  64. }
  65.  
  66. bool checkDebouncing(unsigned int* time)
  67. {
  68. unsigned int currentTime = millis();
  69. if (currentTime < *time) {
  70. return true;
  71. }
  72. *time = currentTime + DEBOUNCING;
  73.  
  74. return false;
  75. }
  76.  
  77. int setupPi()
  78. {
  79.  
  80. if (wiringPiSetupGpio() < 0) {
  81. fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
  82. return -1;
  83. }
  84. pinMode(LEFT_BUTTON_PIN, INPUT);
  85. pinMode(RIGHT_BUTTON_PIN, INPUT);
  86. pinMode(MIDDLE_BUTTON_PIN, INPUT);
  87. pinMode(RED_DIODE_PIN, OUTPUT);
  88. pinMode(GREEN_DIODE_PIN, OUTPUT);
  89.  
  90. if (wiringPiISR(LEFT_BUTTON_PIN, INT_EDGE_BOTH, &leftButtonInterrupt) < 0) {
  91. fprintf(stderr, "Unable to setup ISR: %s\n", strerror(errno));
  92. return -1;
  93. }
  94. if (wiringPiISR(RIGHT_BUTTON_PIN, INT_EDGE_BOTH, &rightButtonInterrupt) < 0) {
  95. fprintf(stderr, "Unable to setup ISR: %s\n", strerror(errno));
  96. return -1;
  97. }
  98. if (wiringPiISR(MIDDLE_BUTTON_PIN, INT_EDGE_BOTH, &middleButtonInterrupt) < 0) {
  99. fprintf(stderr, "Unable to setup ISR: %s\n", strerror(errno));
  100. return -1;
  101. }
  102.  
  103. digitalWrite(GREEN_DIODE_PIN, LOW);
  104. digitalWrite(RED_DIODE_PIN, LOW);
  105.  
  106. return 0;
  107. }
  108.  
  109. void leftButtonInterrupt()
  110. {
  111. if (digitalRead(LEFT_BUTTON_PIN) == 0) {
  112. if (checkDebouncing(&leftButtonTime)) {
  113. return;
  114. }
  115. currentlyPressed++;
  116. }
  117. else {
  118. return;
  119. }
  120. }
  121.  
  122. void middleButtonInterrupt()
  123. {
  124. if (digitalRead(MIDDLE_BUTTON_PIN) == 0) {
  125. }
  126. else {
  127. return;
  128. }
  129. }
  130.  
  131. void rightButtonInterrupt()
  132. {
  133. if (digitalRead(RIGHT_BUTTON_PIN) == 0) {
  134. }
  135. else {
  136. return;
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement