Advertisement
arthurtung

Untitled

Aug 5th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 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 (1)
  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. int displayValue = timerValue - (timerCounts / 10);
  68.  
  69. LD.setNumber( displayValue );
  70. timerCounts = timerCounts + 1;
  71. if(displayValue==50)
  72. {
  73. LE.on(1);
  74. pin20.setHigh();
  75. break;
  76. }
  77.  
  78.  
  79.  
  80. if (sw2_pressed && sw2last == false)
  81. {
  82. currentState = pause;
  83. printf("Now paused\n");
  84. }
  85. if (sw3_pressed && sw3last == false)
  86. {
  87. currentState = reset;
  88.  
  89. pin20.setLow();
  90. printf("Reseting timer");
  91. }
  92.  
  93. }
  94. break;
  95. case pause:
  96. {
  97. if (sw2_pressed && sw2last == false)
  98. {
  99. currentState = timer;
  100. printf("Resuming timer\n");
  101. pin20.setLow();
  102. }
  103. if (sw3_pressed && sw3last == false)
  104. {
  105. currentState = reset;
  106.  
  107. pin20.setLow();
  108. printf("Reseting timer");
  109. }
  110. }
  111. break;
  112. case reset:
  113. {
  114. LD.setNumber(0);
  115. if (sw1_pressed && sw1last == false)
  116. {
  117. currentState = start;
  118. pin20.setLow();
  119. printf("Starting timer");
  120. }
  121. }
  122. break;
  123. default:
  124. printf("State machine error\n");
  125.  
  126. }
  127.  
  128. sw1last = sw1_pressed;
  129. sw2last = sw2_pressed;
  130. sw3last = sw3_pressed;
  131. delay_ms(100);
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement