Advertisement
arthurtung

Untitled

Aug 5th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 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. int displayValue=60;
  31. while (displayValue>0)
  32. {
  33. bool sw1_pressed = SW.getSwitch(1);
  34. bool sw2_pressed = SW.getSwitch(2);
  35. bool sw3_pressed = SW.getSwitch(3);
  36. switch (currentState)
  37. {
  38. case start:
  39. {
  40.  
  41. LD.setNumber(60);
  42.  
  43. LE.off(1);
  44. pin20.setLow();
  45. if (sw1_pressed && sw1last == false)
  46. {
  47. currentState = timer;
  48. printf("Now starting timer\n");
  49.  
  50. timerValue = 60;
  51. timerCounts = 0;
  52.  
  53. }
  54. if (sw3_pressed && sw3last == false)
  55. {
  56. currentState = reset;
  57.  
  58. pin20.setLow();
  59. printf("Reseting timer");
  60. }
  61. }
  62. break;
  63.  
  64. case timer:
  65. {
  66.  
  67. displayValue = timerValue - (timerCounts / 10);
  68.  
  69. LD.setNumber( displayValue );
  70. timerCounts = timerCounts + 1;
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. if (sw2_pressed && sw2last == false)
  79. {
  80. currentState = pause;
  81. printf("Now paused\n");
  82. }
  83. if (sw3_pressed && sw3last == false)
  84. {
  85. currentState = reset;
  86.  
  87. pin20.setLow();
  88. printf("Reseting timer");
  89. }
  90.  
  91. }
  92. break;
  93. case pause:
  94. {
  95. if (sw2_pressed && sw2last == false)
  96. {
  97. currentState = timer;
  98. printf("Resuming timer\n");
  99. pin20.setLow();
  100. }
  101. if (sw3_pressed && sw3last == false)
  102. {
  103. currentState = reset;
  104.  
  105. pin20.setLow();
  106. printf("Reseting timer");
  107. }
  108. }
  109. break;
  110. case reset:
  111. {
  112. LD.setNumber(0);
  113. if (sw1_pressed && sw1last == false)
  114. {
  115. currentState = start;
  116. pin20.setLow();
  117. printf("Starting timer");
  118. }
  119. }
  120. break;
  121. default:
  122. printf("State machine error\n");
  123.  
  124. }
  125.  
  126. sw1last = sw1_pressed;
  127. sw2last = sw2_pressed;
  128. sw3last = sw3_pressed;
  129. delay_ms(100);
  130. }
  131. LE.on(1);
  132. pin20.setHigh();
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement