#include #include #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; int timerValue; int timerCounts; 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(60); LE.off(1); pin20.setLow(); if (sw1_pressed && sw1last == false) { currentState = timer; printf("Now starting timer\n"); timerValue = 60; timerCounts = 0; } if (sw3_pressed && sw3last == false) { currentState = reset; pin20.setLow(); printf("Reseting timer"); } } break; case timer: { int displayValue = timerValue - (timerCounts / 10); LD.setNumber( displayValue ); timerCounts = timerCounts + 1; if(displayValue==0) { LE.on(1); pin20.setHigh(); } if (sw2_pressed && sw2last == false) { currentState = pause; printf("Now paused\n"); } if (sw3_pressed && sw3last == false) { currentState = reset; pin20.setLow(); printf("Reseting timer"); } } break; case pause: { if (sw2_pressed && sw2last == false) { currentState = timer; printf("Resuming timer\n"); pin20.setLow(); } if (sw3_pressed && sw3last == false) { currentState = reset; pin20.setLow(); printf("Reseting timer"); } } break; case reset: { LD.setNumber(0); 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); } }