arthurtung

Untitled

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