【问题标题】:finite state machine for reading password读取密码的有限状态机
【发布时间】:2018-03-21 13:34:36
【问题描述】:

我正在尝试使用 4x4 键盘读取用户的输入,并使用有限状态机比较输入,如下所示。

当我遇到第四种情况时,我的状态机不会转到检查情况而是转到默认情况,然后按一个键立即转到 CHECK,这使用户按五次而不是 4 次。

while (1)
{
    key=get_key();
    if (key>=0 && key <=9){
        switch (password){
            case FIRST:
                if (key==7 && i==0)
                {
                    temp++; // increasing every time we enter correct number
                    password=SECOND;
                    HAL_Delay(300);
                    break;
                }
                HAL_Delay(300);
                i++; // To check if we have fed in wrong digit
                if(i==4) {
                    password=CHECK;
                }
                break;
//**********************************************************************************************************
            case SECOND:
                if (key==3 && temp==1){
                    temp++;
                    HAL_Delay(300);
                    password=THIRD;
                    break;
                }
                HAL_Delay(300);
                i++;
                if((i==3) || (i==2)) {
                    password=CHECK;
                }
                break;
//****************************************************************************************************
            case THIRD:
                if (key==9 && temp==2)
                {
                    temp++;
                    HAL_Delay(300);
                    password = FOURTH;
                    break;
                }
                HAL_Delay(300);
                i++;
                if((i==2) || (i==1)) {
                    password=CHECK;
                }
                break;
//*******************************************************************************************************
            case FOURTH:
                if (key==2 && temp==3)
                {
                    temp++;
                    password=CHECK;
                    break;
                }
                //HAL_Delay(300);
                i++;
                if(i==1) {
                    password=CHECK;
                }
                break;
//****************************************************************************************************
            case CHECK:
                if (temp==4){ // if temp has incresed to 4 that means that all the numbers entered are correct
                    HAL_GPIO_WritePin(Led_test_GPIO_Port,Led_test_Pin,1);
                    HAL_GPIO_WritePin(Led2_test_GPIO_Port,Led2_test_Pin,0);
                    password=IDLE_PASSWORD;
                }
                else if((i==4) || (i==3) || (i==2) || (i==1)) {
                    HAL_GPIO_WritePin(Led2_test_GPIO_Port,Led2_test_Pin,1);
                    HAL_GPIO_WritePin(Led_test_GPIO_Port,Led_test_Pin,0);
                    password=IDLE_PASSWORD;
                }
                break;
//******************************************************************************************************
            case IDLE_PASSWORD:
                temp=0;
                i=0;
                password = FIRST;
                break;
                default:
                password = FIRST;
                break;
        }
    }
}

【问题讨论】:

  • 您可以在get_key() 之前检查temp == 4,或者您可以从FOURTH 案例声明中删除break...我会使用第一个解决方案
  • check temp == 4 是什么意思?
  • 如果我理解正确,您需要第五次按键才能实际检查输入的密码...在这种情况下,您可以通过 if (temp != 4) key=get_key(); 忽略此按键,这是一种解决方法,但它应该可以工作 - 当然您需要验证您是否正确初始化和处理了temp 变量
  • 如果您查看状态机,从一种状态到另一种状态的每次转换都需要调用get_key()。所以 IDLE->FIRST->SECOND->THIRD->FOURTH->CHECK 需要五次调用。 @Dagan 建议您应该将检查作为第四状态的一部分进行,这样您就不需要再按一次按键来进行检查。同样,您可能希望将处于 IDLE 状态的代码移动到 FIRST 状态的开头。
  • 当我现在运行这个函数时,有一个案例可以检查密码是否正确,它看起来像是比较第一个元素并忽略其余元素

标签: c embedded stm32f4


【解决方案1】:
while (1)
{
    static uint8 Password[4] = {7, 3, 9, 2};
    static uint8 Guesses = 0;
    static uint8 Wrong = 0;

    /*Get the key*/
    key=get_key();

    /*If the key is not in range...*/
    if (key<0 || key >9)
    {
        /*Return early*/
        return;
    }

    /*Wait 300*/
    HAL_Delay(300);

    /*If the value at the Guess position is not equal to what they pressed...*/
    if (key != Password[Guesses])
    {
        /*Mark the input as wrong*/
        Wrong = 1
    }

    /*Increment Guesses for the next time*/
    Guesses++;

    /*After their fourth input...*/
    if (Guesses == 4)
    {
        /*Reet Guesses*/
        Guesses = 0;
        /*If they were wrong...*/
        if (Wrong == 1)
        {
            /*Reset Wrong*/
            Wrong = 0;
        }
        /*Otherwise if they were right...*/
        else
        {
            /*Do something*/
        }
    }
}

这是未经测试的 - 调试一下,它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多