⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyboard.cpp

📁 dsp2812上的零耗时键盘 c++源代码例子
💻 CPP
字号:
#include "keyboard.h"

KeyboardObj::KeyboardObj(void)
{
    Init();
}

void KeyboardObj::Init(void)
{
    for (int i = 0; i < sizeof(PressCount); i ++)
    {
        PressCount[i] = 0;
    }
    Count = 0;
}

void KeyboardObj::Exec(void)
{
typedef void (KeyboardObj::*FunctionPtr)(void);//函数指针
static const FunctionPtr KeyboardCommandTab[3][KeyboardNumbers] =
{
    {
        &KeyboardObj::Key01,
        &KeyboardObj::Key02,
        &KeyboardObj::Key03,
        &KeyboardObj::Key04,
        &KeyboardObj::Key05,
        &KeyboardObj::Key06,
        &KeyboardObj::Key07,
        &KeyboardObj::Key08
    },//放键表
    {
        &KeyboardObj::Key11,
        &KeyboardObj::Key12,
        &KeyboardObj::Key13,
        &KeyboardObj::Key14,
        &KeyboardObj::Key15,
        &KeyboardObj::Key16,
        &KeyboardObj::Key17,
        &KeyboardObj::Key18
    },//短压表
    {
        &KeyboardObj::Key21,
        &KeyboardObj::Key22,
        &KeyboardObj::Key23,
        &KeyboardObj::Key24,
        &KeyboardObj::Key25,
        &KeyboardObj::Key26,
        &KeyboardObj::Key27,
        &KeyboardObj::Key28
    } //长压表
};
unsigned char KeyCount;
    Count %= KeyboardNumbers;
    KeyCount = PressCount[Count];//取压键次数
    if (Scan() == Count + 1)//有键压下
    {
		KeyCount ++;//压键计数	
        if (KeyCount >= KeyboardPushTimes)
        {
            if (KeyCount == KeyboardPushTimes)//短压键
            {
//            	System.BeepOn();
                (::Keyboard.*KeyboardCommandTab[1][Count])();
            }
            if (KeyCount == KeyboardLongPushTimes)//长压键
            {
                (::Keyboard.*KeyboardCommandTab[2][Count])();
                KeyCount = KeyboardPushTimes;	
            }
        }
    }
    else//无键压下
    {
        if (KeyCount > 0)//以前有键压下
        {
            if (KeyCount > KeyboardPushTimes)	
            {
                KeyCount = KeyboardPushTimes;	
            }
            else
            {
                KeyCount --;//放键计数
                if (KeyCount == 0)//键释放
                {
            	    System.BeepOff();
                    (::Keyboard.*KeyboardCommandTab[0][Count])();
                }	
            }
        }
        else
        {
            KeyCount = 0;
        }
    }
    PressCount[Count] = KeyCount;
    Count ++;
    Count %= KeyboardNumbers;
}

unsigned char KeyboardObj::Scan(void)
{
volatile unsigned char KeyVal = 0;
    Count %= KeyboardNumbers;
    KeyVal = LedKeyRegs;
    switch (Count)
    {
        case 0:
            if (!(KeyVal & ConstKEY1))
            {
                KeyVal = Count + 1;
            }
            break;
        case 1:
            if (!(KeyVal & ConstKEY2))
            {
                KeyVal = Count + 1;
            }
            break;
        case 2:
            if (!(KeyVal & ConstKEY3))
            {
                KeyVal = Count + 1;
            }
            break;
        case 3:
            if (!(KeyVal & ConstKEY4))
            {
                KeyVal = Count + 1;
            }
            break;
        case 4:
            if (!(KeyVal & ConstKEY5))
            {
                KeyVal = Count + 1;
            }
            break;
        case 5:
            if (!(KeyVal & ConstKEY6))
            {
                KeyVal = Count + 1;
            }
            break;
        case 6:
            if (!(KeyVal & ConstKEY7))
            {
                KeyVal = Count + 1;
            }
            break;
        case 7:
            if (!(KeyVal & ConstKEY8))
            {
                KeyVal = Count + 1;
            }
            break;
    }
    return KeyVal;
}

//KEY1放键事件处理
void KeyboardObj::Key01(void)
{
	Lcd.SetDisplayPos(3, 0);//汉字定位到上行左端
	Lcd.Display("KEY1键释放事件");
}

//KEY2放键事件处理
void KeyboardObj::Key02(void)
{
	Lcd.SetDisplayPos(3, 0);//汉字定位到上行左端
	Lcd.Display("KEY2键释放事件");
}

//KEY3放键事件处理
void KeyboardObj::Key03(void)
{
	Lcd.SetDisplayPos(3, 0);//汉字定位到上行左端
	Lcd.Display("KEY3键释放事件");
}

//KEY4放键事件处理
void KeyboardObj::Key04(void)
{
	Lcd.SetDisplayPos(3, 0);//汉字定位到上行左端
	Lcd.Display("KEY4键释放事件");
}

//KEY5放键事件处理
void KeyboardObj::Key05(void)
{
	Lcd.SetDisplayPos(3, 0);//汉字定位到上行左端
	Lcd.Display("KEY5键释放事件");
}

//KEY6放键事件处理
void KeyboardObj::Key06(void)
{
	Lcd.SetDisplayPos(3, 0);//汉字定位到上行左端
	Lcd.Display("KEY6键释放事件");
}

//KEY7放键事件处理
void KeyboardObj::Key07(void)
{
	Lcd.SetDisplayPos(3, 0);//汉字定位到上行左端
	Lcd.Display("KEY7键释放事件");
}

//KEY8放键事件处理
void KeyboardObj::Key08(void)
{
	Lcd.SetDisplayPos(3, 0);//汉字定位到上行左端
	Lcd.Display("KEY8键释放事件");
}

//KEY1压键事件处理
void KeyboardObj::Key11(void)
{
//特别注意菜农的组合键用法,可以看出"零耗时键盘"的非典之处
	if (PressCount[7] >= KeyboardPushTimes)//KEY8已先压下
	{
		Key8_Key1();//执行KEY8_KEY1组合键事件
	}
	else
	{
//	   	System.BeepOn();
		Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
		Lcd.Display("KEY1键单击事件");
	}
}

//KEY2压键事件处理
void KeyboardObj::Key12(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY2键单击事件");
}

//KEY3压键事件处理
void KeyboardObj::Key13(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY3键单击事件");
}

//KEY4压键事件处理
void KeyboardObj::Key14(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY4键单击事件");
}


//KEY5压键事件处理
void KeyboardObj::Key15(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY5键单击事件");
}


//KEY6压键事件处理
void KeyboardObj::Key16(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY6键单击事件");
}


//KEY7压键事件处理
void KeyboardObj::Key17(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY7键单击事件");
}

//KEY8压键事件处理
void KeyboardObj::Key18(void)
{
//特别注意菜农的组合键用法,可以看出"零耗时键盘"的非典之处
	if (PressCount[0] >= KeyboardPushTimes)//KEY1已先压下
	{
		Key1_Key8();//执行KEY1_KEY8组合键事件
	}
	else
	{
//	   	System.BeepOn();
		Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
		Lcd.Display("KEY8键单击事件");
	}
}

//KEY1长压键事件处理
void KeyboardObj::Key21(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY1键长压事件");
}

//KEY2长压键事件处理
void KeyboardObj::Key22(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY2键长压事件");
}

//KEY3长压键事件处理
void KeyboardObj::Key23(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY3键长压事件");
}

//KEY4长压键事件处理
void KeyboardObj::Key24(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY4键长压事件");
}

//KEY5长压键事件处理
void KeyboardObj::Key25(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY5键长压事件");
}

//KEY6长压键事件处理
void KeyboardObj::Key26(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY6键长压事件");
}

//KEY7长压键事件处理
void KeyboardObj::Key27(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY7键长压事件");
}

//KEY8长压键事件处理
void KeyboardObj::Key28(void)
{
   	System.BeepOn();
	Lcd.SetDisplayPos(2, 0);//汉字定位到上行左端
	Lcd.Display("KEY8键长压事件");
}

//KEY1-KEY8组合键事件处理
void KeyboardObj::Key1_Key8(void)
{
	Lcd.SetDisplayPos(1, 0);//汉字定位到上行左端
	Lcd.Display("KEY1KEY8键事件");
}

//KEY8-KEY1组合键事件处理
void KeyboardObj::Key8_Key1(void)
{
	Lcd.SetDisplayPos(1, 0);//汉字定位到上行左端
	Lcd.Display("KEY8KEY1键事件");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -