Advertisement
arthurtung

Untitled

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