Beispiel #1
0
void verifySequence_tick() {
  static uint16_t index = 0;
  static uint16_t timeOutTimer = 0; // counter to track whether a timeout occurs

  /////////////////////////////////
  // Perform state action first. //
  /////////////////////////////////
  switch (verifySequence_state) {
    case init_st:
      isTimeOutError = false;
      isUserInputError = false;
      index = 0;
      timeOutTimer = 0;
      break;
    case wait_for_timeout_st:
      timeOutTimer++;
      buttonHandler_enable();
      break;
    case wait_for_release_st:
      break;
    case verification_st:
      break;

    case complete_st:

      break;
    default:
      printf("Default case hit in verifySequence tick.\n");
      break;
  }

  ////////////////////////////////
  // Perform state update next. //
  ////////////////////////////////
  switch (verifySequence_state) {
    case init_st:
      if (enabled) {
        buttonHandler_enable();
        verifySequence_state = wait_for_timeout_st;
      }
      else {
        verifySequence_state = init_st;
      }
      break;
    case wait_for_timeout_st:
      if(display_isTouched()) {
        timeOutTimer = 0;
        verifySequence_state = wait_for_release_st;
      }
      // If the use waits too long before doing anything
      else if (timeOutTimer >= WAIT_TIMEOUT)
      {
        //printf("userTouchTimer: %d", userTouchTimer);
        isTimeOutError = true;
        timeOutTimer = 0;
        verifySequence_state = complete_st;
      }
      else {
        verifySequence_state = wait_for_timeout_st;
      }
      break;
    case wait_for_release_st:
      if (buttonHandler_releaseDetected())
      {
        buttonHandler_disable();
        verifySequence_state = verification_st;
      }
      else {
        verifySequence_state = wait_for_release_st;
      }
      break;
    case verification_st:
      uint8_t tempSequenceValue, tempRegionNumber;
      tempSequenceValue = globals_getSequenceValue(index);
      tempRegionNumber = buttonHandler_getRegionNumber();

      if(tempSequenceValue == tempRegionNumber) { //user touched the correct square
        // If that was the last square
        if (globals_getSequenceIterationLength() == (index + 1)) { //because index starts at 0 and length at 1
          verifySequence_state = complete_st;
        }
        else {
          index++;
          verifySequence_state = wait_for_timeout_st;
        }
      }
      else {
        isUserInputError = true;
        //printf("USER INPUT ERROR");
        verifySequence_state = complete_st;
      }
      break;
    case complete_st:
      if (!enabled) {
        verifySequence_state = init_st;
      }
      else {
        verifySequence_state = complete_st;
      }
      break;
    default:
      printf("Default case hit in verifySequence tick.\n");
      break;
  }
}
//*********************************************************************************************
// verifySequence_tick()
// verify sequence state machine
//*********************************************************************************************
void verifySequence_tick()
{
	switch(verify_state)
	{

    //verify_init
    //Resets all error cases and sets all timers to zero
    //Gets the current sequence length
    //enables the button handler
	case verify_init:
		verify_isTimeOutError = V_DISABLED;
		verify_isUserInputError = V_DISABLED;
		verify_completed = V_DISABLED;
		timer_waiting = INIT_ZERO;
		verifyValue = INIT_ZERO;
            
		verify_current_sequence_length = globals_getSequenceIterationLength();

		if(verify_enable)
		{
			simonbuttonHandler_enable();
			verify_state = verify_waiting;
		}
		break;

            
    //verify_waiting
    //provides the user with time to touch the screen
    //if the user does not touch before TIMER_EXPIRED_VALUE, verify_isTimeOutError occurs
    //disables the button handler after waiting is over
	case verify_waiting:
        timer_waiting++;
        if(timer_waiting == TIMER_EXPIRED_VALUE)
        {
            verify_isTimeOutError = V_ENABLED;
            simonbuttonHandler_disable();

            verify_state = verify_complete;
        }

        if(display_isTouched())
        {
            if(!simonbuttonHandler_releaseDetected() && timer_waiting < TIMER_EXPIRED_VALUE)
            {
                verify_state = verify_wait_release;
            }
        }
        break;

            
    //verify_wait_release
    //waits for the user to release the touch screen and disables the button handler
	case verify_wait_release:

        if(simonbuttonHandler_releaseDetected())
        {
            simonRegion = simonbuttonHandler_getRegionNumber();
            timer_waiting = INIT_ZERO;
            simonbuttonHandler_disable();
               
            verify_state = verify_validate;
        }
        break;


    //verify_validate
    //compares the verifyValue to the sequence value to see if the sequence is over
    //if they are not the same, user input error has occurred
    case verify_validate:

        if(simonRegion == globals_getSequenceValue(verifyValue))
        {
            verifyValue++;
            if(verifyValue == verify_current_sequence_length)
            {
                verifyValue = INIT_ZERO;
                verify_state = verify_complete;
            }
                
            else
            {
                timer_waiting = INIT_ZERO;
                verify_state = verify_waiting;
                simonbuttonHandler_enable();
            }
        }
            
        else
        {
            verify_isUserInputError = V_ENABLED;
            verify_state = verify_complete;
        }
        break;


    //verify_complete
    //signifies the state machine has completed
    //restarts if and only if the state machine is enabled
    case verify_complete:

        verify_completed = V_ENABLED;
        timer_waiting = INIT_ZERO;

        if(verify_enable)
        {
            verify_state = verify_init;
        }
        break;


	default:
            verify_state = verify_init;
            break;
	}
}
Beispiel #3
0
// Standard tick function.
void verifySequence_tick(){
    // state actions
    switch (verifyState){
    case init_st:
        break;
    case enable_button_st:
        // enabble the buttonHandler state machine
        buttonHandler_enable();
        break;
    case wait_for_release_st:
        // Increment timout value while waiting for the display to be relased
        timeOut++;
        break;
    case verify_region_st:
        // disable the buttonhandler state machine
        buttonHandler_disable();
        // if the user tapped the wrong sequence, raise the errorFlag
        if (globals_getSequenceValue(index) != buttonHandler_getRegionNumber()){
            userErrorFlag = 1;
        }
        break;
    case increment_st:
        break;
    case done_st:
        break;
    }

    // state transitions
    switch (verifyState){
    case init_st:
        // wait for the state machine to be enabled
        if (verifyEnableFlag) {
            // reset all flags and counters
            index = 0;
            completeFlag = 0;
            timeOutFlag = 0;
            userErrorFlag = 0;
            timeOut = 0;
            verifyState = enable_button_st;
        }
        break;
    case enable_button_st:
        verifyState = wait_for_release_st;
        break;
    case wait_for_release_st:
        // if the timout value is reached
        if (timeOut == TIME_OUT_VALUE){
            // raise the timeOutFlag
            timeOutFlag = 1;
            // raise the completeFlag
            completeFlag = 1;
            // erase all the buttons
            simonDisplay_eraseAllButtons();
            // and go to done
            verifyState = done_st;
        }
        // if the display is currently touched, reset the counter.
        // I didn't want it to timeout if the user is holding the buton.
        else if (display_isTouched()){
            timeOut = 0;
        }
        // otherwise if the display was released
        else if (buttonHandler_releaseDetected()){
            // go check the region pressed against the reion in the sequence
            verifyState = verify_region_st;
        }
        break;
    case verify_region_st:
        verifyState = increment_st;
        break;
    case increment_st:
        // if we're at the end of the sequence
        if (index == (globals_getSequenceIterationLength())){
            // raise completeFlag to indicate it's over
            completeFlag = 1;
            // erase all the buttons
            simonDisplay_eraseAllButtons();
            // go to done
            verifyState = done_st;
        }
        // if the user made an error
        else if (userErrorFlag){
            // raise completeFlag
            completeFlag = 1;
            // erase the buttons
            simonDisplay_eraseAllButtons();
            // go to done
            verifyState = done_st;
        }
        else {
            // otherwise increase the index and verify the next button
            // in the sequence
            index++;
            verifyState = enable_button_st;
        }
        break;
    case done_st:
        // if the enable flag goes low
        if (!verifyEnableFlag) {
            // go to init
            verifyState = init_st;
        }
        break;
    }
}