Advertisement
arthurtung

Untitled

Aug 5th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "utilities.h"
  4. #include "io.hpp"
  5. #include "gpio.hpp"
  6. void enableFlushAfterPrintf()
  7. {
  8. setvbuf(stdout, 0, _IONBF, 0);
  9. setvbuf(stdin, 0, _IONBF, 0);
  10. }
  11. int main(void)
  12. {
  13. enableFlushAfterPrintf();
  14. GPIO pin20(P1_20); /* Use P1.20 as General Purpose Input/Output (GPIO) */
  15. pin20.setAsOutput(); /* Use this pin as OUTPUT */
  16.  
  17. /* Turn on voltage to 3.3v */
  18.  
  19. typedef enum
  20. {
  21. start, timer, pause, reset
  22. } statetype; // These are the cases and buttons that
  23. statetype currentState = start;
  24. bool sw1last, sw2last, sw3last = false;
  25. sw1last = sw2last = sw3last = false;
  26. int ms;
  27. int pausedvalue;
  28. int timerValue;
  29. int timerCounts;
  30. while (1)
  31. {
  32. bool sw1_pressed = SW.getSwitch(1);
  33. bool sw2_pressed = SW.getSwitch(2);
  34. bool sw3_pressed = SW.getSwitch(3);
  35. switch (currentState)
  36. {
  37. case start:
  38. {
  39.  
  40. LD.setNumber(60);
  41.  
  42. LE.off(1);
  43. pin20.setLow();
  44. if (sw1_pressed && sw1last == false)
  45. {
  46. currentState = timer;
  47. printf("Now starting timer\n");
  48.  
  49. timerValue = 60;
  50. timerCounts = 0;
  51.  
  52. }
  53. if (sw3_pressed && sw3last == false)
  54. {
  55. currentState = reset;
  56.  
  57. pin20.setLow();
  58. printf("Reseting timer");
  59. }
  60. }
  61. break;
  62.  
  63. case timer:
  64. {
  65.  
  66. int displayValue = timerValue - (timerCounts / 10);
  67.  
  68. LD.setNumber( displayValue );
  69. timerCounts = timerCounts + 1;
  70.  
  71. if(displayValue==0)
  72. {
  73. LE.on(1);
  74. pin20.setHigh();
  75. }
  76. if (sw2_pressed && sw2last == false)
  77. {
  78. currentState = pause;
  79. printf("Now paused\n");
  80. }
  81. if (sw3_pressed && sw3last == false)
  82. {
  83. currentState = reset;
  84.  
  85. pin20.setLow();
  86. printf("Reseting timer");
  87. }
  88.  
  89. }
  90. break;
  91. case pause:
  92. {
  93. if (sw2_pressed && sw2last == false)
  94. {
  95. currentState = timer;
  96. printf("Resuming timer\n");
  97. pin20.setLow();
  98. }
  99. if (sw3_pressed && sw3last == false)
  100. {
  101. currentState = reset;
  102.  
  103. pin20.setLow();
  104. printf("Reseting timer");
  105. }
  106. }
  107. break;
  108. case reset:
  109. {
  110. LD.setNumber(0);
  111. if (sw1_pressed && sw1last == false)
  112. {
  113. currentState = start;
  114. pin20.setLow();
  115. printf("Starting timer");
  116. }
  117. }
  118. break;
  119. default:
  120. printf("State machine error\n");
  121.  
  122. }
  123.  
  124. sw1last = sw1_pressed;
  125. sw2last = sw2_pressed;
  126. sw3last = sw3_pressed;
  127. delay_ms(100);
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement