123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- #include "includes.h"
- unsigned long g_ulKeyValue;
- unsigned char g_ucKeyFree=0;
- unsigned char g_ucKeyMode=0;
- unsigned char g_ucKeyPPTPress=0;
- unsigned char g_ucUKC=0;
- unsigned char g_iEncode=0;
- //void CheckPwrKeyOn(void)
- //{//长按电源键直到电筒灯亮后释放
- // unsigned int tick=0;
- // if(KeyPwrStatus !=0)
- // {
- // POWER_ON_FAILED:
- // SlwTrace(INF, "\r\nPwrOnFailed",1);
- // DelayMs(10);
- // PWR_EN_LOW;
- // while(1);
- // }
- //
- // while(KeyPwrStatus==0)
- // {
- // if(++tick >= 200000)
- // {
- // MODEM_LED2_HIGH;
- // SlwTrace(INF, "\r\nPwrOnOk",1);
- //// while(KeyPwrStatus==0){
- ////// DelayMs(30);
- ////// MODEM_LED2_LOW;
- ////// DelayMs(30);
- ////// MODEM_LED2_HIGH;
- //// IWDG_ReloadCounter();
- //// }
- // //MODEM_LED2_LOW;
- //
- // PWR_EN_HIGH;//
- // return;
- // }
- // }
- // SlwTrace(INF,"\r\nPwr erro",1);
- // goto POWER_ON_FAILED;
- //}
- //检测关机
- void SoftPwrOffCheck(void)
- {
- // static unsigned int seconds;
- //
- // if(!seconds) seconds=uSysTickGet()+500;//首次进入
- //
- // if(KeyPwrStatus==0)
- // {
- //
- // if(uSysTickGet() > seconds)
- // {
- // SlwTrace(INF, "PwrOffOk",1);
- //// MODEM_LED1_HIGH;
- // if(g_ucTMMode==1)while(1);
- //
- // UISetNextStatus(UIS_POWEROFF_WAIT);
- //// GuiClearAll();
- //// GuiShowBmp(0,0,"BYEBYE.bmp");
- //// MODEM_PWREN_LOW;
- //// PWR_EN_LOW;
- //// MODEM_LED1_LOW;
- //// while(1);
- // }
- // }else seconds=uSysTickGet()+100;//200
- //
- }
- void KeyInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
-
- /*GPIO先全部设置为上拉输入*/
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- //PPT
- GPIO_InitStructure.GPIO_Pin=KEY_PPT_PIN;
- GPIO_Init(KEY_PPT_PORT, &GPIO_InitStructure);
-
- //MIC PTT
- GPIO_InitStructure.GPIO_Pin=KEY_MICPTT_PIN;
- GPIO_Init(KEY_MICPTT_PORT, &GPIO_InitStructure);
-
- // INFO VOL+
- GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN;
- GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure);
- // FUNC VOL-
- GPIO_InitStructure.GPIO_Pin=KEY_FUNC_PIN;
- GPIO_Init(KEY_FUNC_PORT, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=CHARGE_PIN;
- GPIO_Init(CHARGE_PORT, &GPIO_InitStructure);
-
- //HEADSET_PIN 耳机检测
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=HEADSET_PIN;
- GPIO_Init(HEADSET_PORT, &GPIO_InitStructure);
- //CheckPwrKeyOn();
- g_ucKeyMode=0;//连续触发模式
- }
- static unsigned long KeyScanPort(void)
- {
- unsigned long k1,k2;
-
- k1=GPIOB->IDR;
- k2=GPIOC->IDR;
- k1&=KEY_ALL_PORTB;
- k1<<=16;
- k2&=KEY_ALL_PORTC;
-
- return (k1|k2);
- }
- /************************************************************
- *GetKey
- 读键盘数据
- 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
- 参数:
- g_ucKeyMode=1时,单次模式
- g_ucKeyMode=0时,持续模式
- 返回值:
- 0--按键无变化
- 1 --按键有按下:
- 单次模式下,当按键第一次按下才返回一次该值;
- 持续模式下,按着按键不放将间隔返回该值
- g_ulKeyValue保存当前按键值
- -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
- 释放后,g_ulKeyValue保存上次按下的按键值
- *************************************************************/
- unsigned char PttKeyFlag;
- int GetKey(void)
- {
- static unsigned char sucCt=0;//连续模式下响应频率控制;
- static unsigned char sucFlag=0;
- static unsigned char sucPress=0;
- static unsigned char sucLastPress=0;
- static unsigned long Key=0;
- static unsigned long Key1=0;
- static unsigned long Key2=0;
-
- //去抖动 0 1
- if(++sucFlag>2){ //1
- sucFlag=0;
- }
-
- if(sucFlag==0){
- Key1=KeyScanPort();
- }else{
- Key2=KeyScanPort();
- }
- if(Key1!=Key2)return 0;
- //按键状态发生变更
- if(Key!=Key1){
- Key=Key1;
- if(Key!=((KEY_ALL_PORTB<<16)|(KEY_ALL_PORTC)))
- {
- sucPress=1; //有按下
- printf("KEY=%lx\r\n",Key);
-
- }
- else
- {
- sucPress=0; //无按下
- }
- }
- if(sucPress){//有按键按下
- if(g_ucKeyMode){ //单次模式
- if(sucLastPress!=sucPress){
- sucLastPress=sucPress;
- g_ulKeyValue=Key;
- if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
- // printf("##############\r\n");
- return 1;
- }else{
- return 0;
- }
- }else{ //持续模式
- sucLastPress=sucPress;
- if(0==sucCt){
- g_ulKeyValue=Key1;
- if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
- sucCt=100;//10ms*100=1000ms
- // printf("************\r\n");
- return 1;
- }else{
- sucCt--;
- return 0;
- }
- }
- }else{ //无按键按下
- sucCt=0;
- if(sucLastPress){
- sucLastPress=0;
- return -1;
- }
- }
- return 0;
- }
- void KeyCount(void)
- {
- //if(g_ucDKC<255)g_ucDKC++;
- if(g_ucUKC<255)g_ucUKC++;
- }
- int GetKeyVBAT(void)
- {
- static unsigned char sucCt=0;//连续模式下响应频率控制;
- static unsigned char sucFlag=0;
- static unsigned char sucPress=0;
- static unsigned char sucLastPress=0;
- static unsigned long Key=0;
- static unsigned long Key1=0;
- static unsigned long Key2=0;
- //去抖动 0 1
- if(++sucFlag>2){ //1
- sucFlag=0;
- }
- if(sucFlag==0){
- Key1=GetKeyVbat();
- }else{
- Key2=GetKeyVbat();
- }
- if(Key1!=Key2)return 0;
- //按键状态发生变更
- if(Key!=Key1){
- Key=Key1;
- if(Key<190)
- {
- sucPress=1; //有按下
- printf("KEY=%d.+++++++++\r\n",Key);
- }
- else
- {
- sucPress=0; //无按下
- }
- }
- if(sucPress){//有按键按下
- if(g_ucKeyMode){ //单次模式
- if(sucLastPress!=sucPress){
- sucLastPress=sucPress;
-
- if(Key==0){
- g_ulKeyValue=KEY_PANEL_MENU;
- }else if(Key<75){
- g_ulKeyValue=KEY_UP_DOWN_IP;
- }else if(Key<100){
- g_ulKeyValue=KEY_PANEL_OK;//;
- }else if(Key<190){
- g_ulKeyValue=KEY_PANEL_DOWN;
- }
- return 1;
- }else{
- return 0;
- }
- }else{ //持续模式
- sucLastPress=sucPress;
- // printf("KEYVbat=%d............\r\n",Key);
- if(0==sucCt){
-
- if(Key==0){
- g_ulKeyValue=KEY_PANEL_MENU;
- }else if(Key<75){
- g_ulKeyValue=KEY_UP_DOWN_IP;
- }else if(Key<100){
- g_ulKeyValue=KEY_PANEL_OK;//;
- }else if(Key<190){
- g_ulKeyValue=KEY_PANEL_DOWN;
- }
- sucCt=100;//10ms*100=1000ms
- return 1;
- }else{
- sucCt--;
- return 0;
- }
- }
- }else{ //无按键按下
- sucCt=0;
- if(sucLastPress){
- sucLastPress=0;
- return -1;
- }
- }
- return 0;
- }
- void OnOff_Init(void)
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO , ENABLE);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
- GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource3);
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line3;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
-
- }
- void EncodeInit()
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=KEY_ENCODE1_PIN|KEY_ENCODE2_PIN;
- GPIO_Init(KEY_ENCODE2_PORT, &GPIO_InitStructure);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5);
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line4 | EXTI_Line5;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- }
- void EXTI3_IRQHandler(void)
- {
- if(SET==EXTI_GetFlagStatus(EXTI_Line3))
- {//外部电源关了
-
- printf("Power down----\r\n");
-
- if(modemInitReady==1)
- {
- PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
- modemPwrOffNow=1;
- }
- EXTI_ClearFlag(EXTI_Line3);
- }
- }
- static char status=0,lastStatus;
- void EXTI4_IRQHandler()
- {
- if(EXTI_GetITStatus(EXTI_Line4))
- {
- if(KEY_ENCODE1_PORT->IDR & KEY_ENCODE1_PIN) status |=1;
- else status &= ~(1);
- EXTI_ClearITPendingBit(EXTI_Line4);
- }
-
- switch(status)
- {
- case 0:
- if(lastStatus == 1) g_iEncode=1;
- else g_iEncode=2;
- break;
-
- case 3:
- if(lastStatus == 1) g_iEncode=2;
- else g_iEncode=1;
- break;
- }
- lastStatus=status;
- }
- void EXTI9_5_IRQHandler()
- {
- if(EXTI_GetITStatus(EXTI_Line5))
- {
- if(KEY_ENCODE2_PORT->IDR & KEY_ENCODE2_PIN) status |=2;
- else status &= ~(2);
- EXTI_ClearITPendingBit(EXTI_Line5);
- if(sutPocStatus.QuickGroupUserCt==0&&newPara.PF1Status==1)ModemSendAt("AT+POC=3E0000\r\n");
-
- }
- switch(status)
- {
- case 0:
- if(lastStatus == 1) g_iEncode=1;
- else g_iEncode=2;
- break;
- case 3:
- if(lastStatus == 1) g_iEncode=2;
- else g_iEncode=1;
- break;
- }
- lastStatus=status;
- }
- void EncodeKeyProcess(void)
- {
- char buf[30];
- switch(g_iEncode)
- {
- case 1://顺时针
- LCDBackLight(1);
- LcdOnTimeCt=0;
- sutPocStatus.QuickGroupUserCt=3;//
- switch(newPara.PF1Status)
- {
- case 0:
- SpeakerCtrl(1);
- ModemSendAt(POC_CMD_UP);
- SlwTrace(INF,"[G++]",1);
- break;
-
- case 1:
-
- if(sutPocStatus.UserOnLineNum==0){
- sutPocStatus.UserOnLineIndex=0;
- MeSpeak(ENCODE_GBK, "2CCEDEC6E4CBFCB3C9D4B1D4DACFDF");// 无其他成员在线
- }else
- {
-
- snprintf(buf,sizeof(buf),"AT+POC=10000021%04x\r\n",sutPocStatus.UserOnLineIndex);
- SlwTrace(INF,buf,1);
- ModemSendAt(buf);
- }
- ++sutPocStatus.UserOnLineIndex;
- if(sutPocStatus.UserOnLineIndex>=sutPocStatus.UserOnLineNum){
- sutPocStatus.UserOnLineIndex=0;
- }
- SlwTrace(INF,"[User++]",1);
-
- break;
-
- }
- break;
-
- case 2://逆时针
- LCDBackLight(1);
- LcdOnTimeCt=0;
- sutPocStatus.QuickGroupUserCt=3;
- switch(newPara.PF1Status)
- {
- case 0:
- SpeakerCtrl(1);
- ModemSendAt(POC_CMD_DOWN);
- SlwTrace(INF,"[G--]",1);
- break;
-
- case 1:
- if(sutPocStatus.UserOnLineNum==0){
- sutPocStatus.UserOnLineIndex=0;
- MeSpeak(ENCODE_GBK, "2CCEDEC6E4CBFCB3C9D4B1D4DACFDF");// 无其他成员在线
- }else
- {
- snprintf(buf,sizeof(buf),"AT+POC=10000021%04x\r\n",sutPocStatus.UserOnLineIndex);
- SlwTrace(INF,buf,1);
- ModemSendAt(buf);
- }
- if(sutPocStatus.UserOnLineIndex>0)
- sutPocStatus.UserOnLineIndex--;
- else if(sutPocStatus.UserOnLineNum==0)
- sutPocStatus.UserOnLineIndex=0;
- else sutPocStatus.UserOnLineIndex=sutPocStatus.UserOnLineNum-1;
- SlwTrace(INF,"[User-]",1);
- break;
-
- }
- break;
-
- case 0://m_key.g_encodeCntP=0;
- break;
- default:
- break;
- }
- g_iEncode=0;
-
- }
|