有限状态机使用4x4键盘获取用户密码 [英] Finite state machine to take the user password using 4x4 keypad

查看:110
本文介绍了有限状态机使用4x4键盘获取用户密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此函数时,现在有一个案例来检查密码是否正确它看起来比较第一个元素而忽略其余部分这是在c



when i run this function now that has a case to check whether the password is correct or not it looks like it compares the first element and ignore the rest this is in c

void Enter_password(){

	key= get_key();

	if (key >= 0 && key <=9){

		switch (password_input){

		case FIRST:
			password[0]= key;

			HAL_Delay(300);
			password_input = SECOND;

			break;

		case SECOND:

			password[1]= key;

			HAL_Delay(300);
			password_input = THIRD;

			break;

		case THIRD:

			password[2]= key;

			HAL_Delay(300);
			password_input = FOURTH;

			break;

		case FOURTH:

			password[3]= key;

			HAL_Delay(300);
			password_input = CHECK;

		case CHECK:

			for (int i=0; i<sizeof(password_ACT_DEACT); i++){

				if (password[i] != password_ACT_DEACT[i]){

					TextLCD_Cmd(&lcd,0x80);
					TextLCD_Puts(&lcd,"Wrong!");
FLAG= false;
                    HAL_Delay(2000);
                    password_input= FIRST;
					break;
				}

				else {

					TextLCD_Cmd(&lcd,0x80);
					TextLCD_Puts(&lcd,"RIGHT!!");
FLAG=true;
					HAL_Delay(2000);
					password_input= FIRST;

					break;

				}

			}

		default:
			TextLCD_Clear(&lcd);
			password_input= FIRST;
			break;
		}
	}

}





我尝试过:



我做了一个有限状态机来取用户4位数输入并检查其是否正确



What I have tried:

I have done a finite state machine to take the user 4 digit input and check if its correct or not

推荐答案

你的代码应该包含在一个无休止的循环中

Your code should be enclosed in a neverending loop
void Enter_password()
{
  for (;;)
  {
    key= get_key();
    if (key >= 0 && key <=9){
      //...
    }//<- if closing brace
  }
}





请注意您的代码可以简化不使用使用状态机:a for 循环后跟密码检查就足够了。



Please note your code could be simplified without using a state machine: a for loop followed by a password check would be just enough.


首先,由于这是Q& A部分,所以请记住提问。您需要将switch语句中的CHECK大小写修改为:
First, since this is the Q&A section, always remember to ask a question. You need to revise the CHECK case in your switch statement to something like this :
case CHECK :
    Flag = true;    // initialize to true
    for( int i=0; i < sizeof( password_ACT_DEACT ); i++ )
    {
        if( password[i] != password_ACT_DEACT[i] )
        {
            Flag = false;   // character did not match
            break;          // end the loop, have an answer
        }
    }

    TextLCD_Cmd(&lcd,0x80);
    if( Flag )
        TextLCD_Puts(&lcd,"RIGHT!!");
    else
        TextLCD_Puts(&lcd,"Wrong!");

    HAL_Delay(2000);
    password_input= FIRST;
    break;


这篇关于有限状态机使用4x4键盘获取用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆