Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include "utilities.h"
- #include "io.hpp"
- #include "gpio.hpp"
- void enableFlushAfterPrintf()
- {
- setvbuf(stdout, 0, _IONBF, 0);
- setvbuf(stdin, 0, _IONBF, 0);
- }
- int main(void)
- {
- enableFlushAfterPrintf();
- GPIO pin20(P1_20); /* Use P1.20 as General Purpose Input/Output (GPIO) */
- pin20.setAsOutput(); /* Use this pin as OUTPUT */
- /* Turn on voltage to 3.3v */
- typedef enum
- {
- start, timer, pause, reset
- } statetype; // These are the cases and buttons that
- statetype currentState = start;
- bool sw1last, sw2last, sw3last = false;
- sw1last = sw2last = sw3last = false;
- int ms; int pausedvalue;
- while (1)
- {
- bool sw1_pressed = SW.getSwitch(1);
- bool sw2_pressed = SW.getSwitch(2);
- bool sw3_pressed = SW.getSwitch(3);
- switch (currentState)
- {
- case start:
- {
- LD.setNumber(0);
- LE.off(1);
- pin20.setLow();
- if (sw1_pressed && sw1last == false)
- {
- currentState = timer;
- for (int i = 0; i < 61; i++)
- {
- ms = i;
- for(int j=0;j<10;j++)
- {
- delay_ms(100);
- }
- pausedvalue=i;
- }
- if(ms==60)
- {
- LE.on(1);
- pin20.setHigh();
- }
- printf("Now starting timer\n");
- }
- }
- break;
- case timer:
- {
- if (sw2_pressed && sw2last == false)
- {
- currentState = pause;
- if(pausedvalue)
- {
- for(int f=pausedvalue; f<61; f++)
- {
- ms=pausedvalue;
- delay_ms(50);
- }
- printf("Now paused\n");
- }
- }
- break;
- case pause:
- if (sw2_pressed && sw2last == false)
- {
- currentState = timer;
- for( int i=ms; i<61; i++)
- {
- for(int j=0;j<10;j++)
- {
- delay_ms(100);
- }
- }
- printf("Resuming timer\n");
- pin20.setLow();
- }
- if (sw3_pressed && sw3last == false)
- {
- currentState = reset;
- pin20.setLow();
- printf("Reseting timer");
- }
- case reset:
- if (sw1_pressed && sw1last == false)
- {
- currentState = start;
- pin20.setLow();
- printf("Starting timer");
- }
- break;
- default:
- printf("State machine error\n");
- }
- sw1last = sw1_pressed;
- sw2last = sw2_pressed;
- sw3last = sw3_pressed;
- delay_ms(100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement