状态机的伪代码:
在定时中断或保证定时调用下面的过程,假设间隔为 10 ms.
switch (state) {
case 0:
LED = 0; //turn off led
if (keyPressed) { //有键按下
timer = 150;
state++; //进入下一个状态
}
break;
case 1:
LED_Blink(); //led 闪烁
if (timer == 0) {
state++; //1.5s 外的状态
}
else {
timer--;
if (keyPressed) { //1.5s 内有键按下
PlayMusic();
timer = 150; //根据需要,也可以用新的另一个状态来处理
}
}
break;
case 2:
if (keyPressed)
state = 0;
break; |