#include "directkey.h"
#include "main.h"


uint8_t ReadDirectKey(void) {
    static uint8_t KeyState = State0;
    static uint8_t KeyOld;
    uint8_t KeyPress;
    uint8_t KeyValue = NoKey;

    KeyPress = KeyPort->IDR & 0x000F; // 读按键

    switch (KeyState) {
    case State0:
        if (KeyPress != NoKey) {
            KeyOld = KeyPress; // 保存原来的键
            KeyState = State1; // 切换状态
        }
        break;
    
    case State1:
        if (KeyPress == KeyOld) {
            KeyState = State2;
            // KeyValue = KeyOld; // 按下有效
        } else {
            KeyState = State0;
        }
        break;
    
    case State2:
        if (KeyPress == NoKey) {
            KeyState = State0;
            KeyValue = KeyOld; // 抬起有效
        } else {
        }
        break;

    default:
        break;
    }
    return KeyValue;
}