Key.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #include "includes.h"
  2. unsigned long g_ulKeyValue;
  3. unsigned char g_ucKeyFree=0;
  4. unsigned char g_ucKeyMode=0;
  5. unsigned char g_ucKeyPPTPress=0;
  6. unsigned char g_ucUKC=0;
  7. unsigned char g_iEncode=0;
  8. //void CheckPwrKeyOn(void)
  9. //{//长按电源键直到电筒灯亮后释放
  10. // unsigned int tick=0;
  11. // if(KeyPwrStatus !=0)
  12. // {
  13. // POWER_ON_FAILED:
  14. // SlwTrace(INF, "\r\nPwrOnFailed",1);
  15. // DelayMs(10);
  16. // PWR_EN_LOW;
  17. // while(1);
  18. // }
  19. //
  20. // while(KeyPwrStatus==0)
  21. // {
  22. // if(++tick >= 200000)
  23. // {
  24. // MODEM_LED2_HIGH;
  25. // SlwTrace(INF, "\r\nPwrOnOk",1);
  26. //// while(KeyPwrStatus==0){
  27. ////// DelayMs(30);
  28. ////// MODEM_LED2_LOW;
  29. ////// DelayMs(30);
  30. ////// MODEM_LED2_HIGH;
  31. //// IWDG_ReloadCounter();
  32. //// }
  33. // //MODEM_LED2_LOW;
  34. //
  35. // PWR_EN_HIGH;//
  36. // return;
  37. // }
  38. // }
  39. // SlwTrace(INF,"\r\nPwr erro",1);
  40. // goto POWER_ON_FAILED;
  41. //}
  42. //检测关机
  43. void SoftPwrOffCheck(void)
  44. {
  45. // static unsigned int seconds;
  46. //
  47. // if(!seconds) seconds=uSysTickGet()+500;//首次进入
  48. //
  49. // if(KeyPwrStatus==0)
  50. // {
  51. //
  52. // if(uSysTickGet() > seconds)
  53. // {
  54. // SlwTrace(INF, "PwrOffOk",1);
  55. //// MODEM_LED1_HIGH;
  56. // if(g_ucTMMode==1)while(1);
  57. //
  58. // UISetNextStatus(UIS_POWEROFF_WAIT);
  59. //// GuiClearAll();
  60. //// GuiShowBmp(0,0,"BYEBYE.bmp");
  61. //// MODEM_PWREN_LOW;
  62. //// PWR_EN_LOW;
  63. //// MODEM_LED1_LOW;
  64. //// while(1);
  65. // }
  66. // }else seconds=uSysTickGet()+100;//200
  67. //
  68. }
  69. void KeyInit(void)
  70. {
  71. GPIO_InitTypeDef GPIO_InitStructure;
  72. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
  73. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
  74. /*GPIO先全部设置为上拉输入*/
  75. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  76. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  77. //PPT
  78. GPIO_InitStructure.GPIO_Pin=KEY_PPT_PIN;
  79. GPIO_Init(KEY_PPT_PORT, &GPIO_InitStructure);
  80. //MIC PTT
  81. GPIO_InitStructure.GPIO_Pin=KEY_MICPTT_PIN;
  82. GPIO_Init(KEY_MICPTT_PORT, &GPIO_InitStructure);
  83. // INFO VOL+
  84. GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN;
  85. GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure);
  86. // FUNC VOL-
  87. GPIO_InitStructure.GPIO_Pin=KEY_FUNC_PIN;
  88. GPIO_Init(KEY_FUNC_PORT, &GPIO_InitStructure);
  89. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  90. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  91. GPIO_InitStructure.GPIO_Pin=CHARGE_PIN;
  92. GPIO_Init(CHARGE_PORT, &GPIO_InitStructure);
  93. //HEADSET_PIN 耳机检测
  94. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  95. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  96. GPIO_InitStructure.GPIO_Pin=HEADSET_PIN;
  97. GPIO_Init(HEADSET_PORT, &GPIO_InitStructure);
  98. //CheckPwrKeyOn();
  99. g_ucKeyMode=0;//连续触发模式
  100. }
  101. static unsigned long KeyScanPort(void)
  102. {
  103. unsigned long k1,k2;
  104. k1=GPIOB->IDR;
  105. k2=GPIOC->IDR;
  106. k1&=KEY_ALL_PORTB;
  107. k1<<=16;
  108. k2&=KEY_ALL_PORTC;
  109. return (k1|k2);
  110. }
  111. /************************************************************
  112. *GetKey
  113. 读键盘数据
  114. 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
  115. 参数:
  116. g_ucKeyMode=1时,单次模式
  117. g_ucKeyMode=0时,持续模式
  118. 返回值:
  119. 0--按键无变化
  120. 1 --按键有按下:
  121. 单次模式下,当按键第一次按下才返回一次该值;
  122. 持续模式下,按着按键不放将间隔返回该值
  123. g_ulKeyValue保存当前按键值
  124. -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
  125. 释放后,g_ulKeyValue保存上次按下的按键值
  126. *************************************************************/
  127. unsigned char PttKeyFlag;
  128. int GetKey(void)
  129. {
  130. static unsigned char sucCt=0;//连续模式下响应频率控制;
  131. static unsigned char sucFlag=0;
  132. static unsigned char sucPress=0;
  133. static unsigned char sucLastPress=0;
  134. static unsigned long Key=0;
  135. static unsigned long Key1=0;
  136. static unsigned long Key2=0;
  137. //去抖动 0 1
  138. if(++sucFlag>2){ //1
  139. sucFlag=0;
  140. }
  141. if(sucFlag==0){
  142. Key1=KeyScanPort();
  143. }else{
  144. Key2=KeyScanPort();
  145. }
  146. if(Key1!=Key2)return 0;
  147. //按键状态发生变更
  148. if(Key!=Key1){
  149. Key=Key1;
  150. if(Key!=((KEY_ALL_PORTB<<16)|(KEY_ALL_PORTC)))
  151. {
  152. sucPress=1; //有按下
  153. printf("KEY=%lx\r\n",Key);
  154. }
  155. else
  156. {
  157. sucPress=0; //无按下
  158. }
  159. }
  160. if(sucPress){//有按键按下
  161. if(g_ucKeyMode){ //单次模式
  162. if(sucLastPress!=sucPress){
  163. sucLastPress=sucPress;
  164. g_ulKeyValue=Key;
  165. if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
  166. // printf("##############\r\n");
  167. return 1;
  168. }else{
  169. return 0;
  170. }
  171. }else{ //持续模式
  172. sucLastPress=sucPress;
  173. if(0==sucCt){
  174. g_ulKeyValue=Key1;
  175. if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
  176. sucCt=100;//10ms*100=1000ms
  177. // printf("************\r\n");
  178. return 1;
  179. }else{
  180. sucCt--;
  181. return 0;
  182. }
  183. }
  184. }else{ //无按键按下
  185. sucCt=0;
  186. if(sucLastPress){
  187. sucLastPress=0;
  188. return -1;
  189. }
  190. }
  191. return 0;
  192. }
  193. void KeyCount(void)
  194. {
  195. //if(g_ucDKC<255)g_ucDKC++;
  196. if(g_ucUKC<255)g_ucUKC++;
  197. }
  198. int GetKeyVBAT(void)
  199. {
  200. static unsigned char sucCt=0;//连续模式下响应频率控制;
  201. static unsigned char sucFlag=0;
  202. static unsigned char sucPress=0;
  203. static unsigned char sucLastPress=0;
  204. static unsigned long Key=0;
  205. static unsigned long Key1=0;
  206. static unsigned long Key2=0;
  207. //去抖动 0 1
  208. if(++sucFlag>2){ //1
  209. sucFlag=0;
  210. }
  211. if(sucFlag==0){
  212. Key1=GetKeyVbat();
  213. }else{
  214. Key2=GetKeyVbat();
  215. }
  216. if(Key1!=Key2)return 0;
  217. //按键状态发生变更
  218. if(Key!=Key1){
  219. Key=Key1;
  220. if(Key<190)
  221. {
  222. sucPress=1; //有按下
  223. printf("KEY=%d.+++++++++\r\n",Key);
  224. }
  225. else
  226. {
  227. sucPress=0; //无按下
  228. }
  229. }
  230. if(sucPress){//有按键按下
  231. if(g_ucKeyMode){ //单次模式
  232. if(sucLastPress!=sucPress){
  233. sucLastPress=sucPress;
  234. if(Key==0){
  235. g_ulKeyValue=KEY_PANEL_MENU;
  236. }else if(Key<75){
  237. g_ulKeyValue=KEY_UP_DOWN_IP;
  238. }else if(Key<100){
  239. g_ulKeyValue=KEY_PANEL_OK;//;
  240. }else if(Key<190){
  241. g_ulKeyValue=KEY_PANEL_DOWN;
  242. }
  243. return 1;
  244. }else{
  245. return 0;
  246. }
  247. }else{ //持续模式
  248. sucLastPress=sucPress;
  249. // printf("KEYVbat=%d............\r\n",Key);
  250. if(0==sucCt){
  251. if(Key==0){
  252. g_ulKeyValue=KEY_PANEL_MENU;
  253. }else if(Key<75){
  254. g_ulKeyValue=KEY_UP_DOWN_IP;
  255. }else if(Key<100){
  256. g_ulKeyValue=KEY_PANEL_OK;//;
  257. }else if(Key<190){
  258. g_ulKeyValue=KEY_PANEL_DOWN;
  259. }
  260. sucCt=100;//10ms*100=1000ms
  261. return 1;
  262. }else{
  263. sucCt--;
  264. return 0;
  265. }
  266. }
  267. }else{ //无按键按下
  268. sucCt=0;
  269. if(sucLastPress){
  270. sucLastPress=0;
  271. return -1;
  272. }
  273. }
  274. return 0;
  275. }
  276. void OnOff_Init(void)
  277. {
  278. EXTI_InitTypeDef EXTI_InitStructure;
  279. GPIO_InitTypeDef GPIO_InitStructure;
  280. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO , ENABLE);
  281. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  282. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  283. GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
  284. GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
  285. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource3);
  286. EXTI_InitStructure.EXTI_Line = EXTI_Line3;
  287. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  288. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  289. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  290. EXTI_Init(&EXTI_InitStructure);
  291. }
  292. void EncodeInit()
  293. {
  294. GPIO_InitTypeDef GPIO_InitStructure;
  295. EXTI_InitTypeDef EXTI_InitStructure;
  296. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  297. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  298. GPIO_InitStructure.GPIO_Pin=KEY_ENCODE1_PIN|KEY_ENCODE2_PIN;
  299. GPIO_Init(KEY_ENCODE2_PORT, &GPIO_InitStructure);
  300. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4);
  301. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5);
  302. EXTI_InitStructure.EXTI_Line = EXTI_Line4 | EXTI_Line5;
  303. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  304. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  305. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  306. EXTI_Init(&EXTI_InitStructure);
  307. }
  308. void EXTI3_IRQHandler(void)
  309. {
  310. if(SET==EXTI_GetFlagStatus(EXTI_Line3))
  311. {//外部电源关了
  312. printf("Power down----\r\n");
  313. if(modemInitReady==1)
  314. {
  315. PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
  316. modemPwrOffNow=1;
  317. }
  318. EXTI_ClearFlag(EXTI_Line3);
  319. }
  320. }
  321. static char status=0,lastStatus;
  322. void EXTI4_IRQHandler()
  323. {
  324. if(EXTI_GetITStatus(EXTI_Line4))
  325. {
  326. if(KEY_ENCODE1_PORT->IDR & KEY_ENCODE1_PIN) status |=1;
  327. else status &= ~(1);
  328. EXTI_ClearITPendingBit(EXTI_Line4);
  329. }
  330. switch(status)
  331. {
  332. case 0:
  333. if(lastStatus == 1) g_iEncode=1;
  334. else g_iEncode=2;
  335. break;
  336. case 3:
  337. if(lastStatus == 1) g_iEncode=2;
  338. else g_iEncode=1;
  339. break;
  340. }
  341. lastStatus=status;
  342. }
  343. void EXTI9_5_IRQHandler()
  344. {
  345. if(EXTI_GetITStatus(EXTI_Line5))
  346. {
  347. if(KEY_ENCODE2_PORT->IDR & KEY_ENCODE2_PIN) status |=2;
  348. else status &= ~(2);
  349. EXTI_ClearITPendingBit(EXTI_Line5);
  350. if(sutPocStatus.QuickGroupUserCt==0&&newPara.PF1Status==1)ModemSendAt("AT+POC=3E0000\r\n");
  351. }
  352. switch(status)
  353. {
  354. case 0:
  355. if(lastStatus == 1) g_iEncode=1;
  356. else g_iEncode=2;
  357. break;
  358. case 3:
  359. if(lastStatus == 1) g_iEncode=2;
  360. else g_iEncode=1;
  361. break;
  362. }
  363. lastStatus=status;
  364. }
  365. void EncodeKeyProcess(void)
  366. {
  367. char buf[30];
  368. switch(g_iEncode)
  369. {
  370. case 1://顺时针
  371. LCDBackLight(1);
  372. LcdOnTimeCt=0;
  373. sutPocStatus.QuickGroupUserCt=3;//
  374. switch(newPara.PF1Status)
  375. {
  376. case 0:
  377. SpeakerCtrl(1);
  378. ModemSendAt(POC_CMD_UP);
  379. SlwTrace(INF,"[G++]",1);
  380. break;
  381. case 1:
  382. if(sutPocStatus.UserOnLineNum==0){
  383. sutPocStatus.UserOnLineIndex=0;
  384. MeSpeak(ENCODE_GBK, "2CCEDEC6E4CBFCB3C9D4B1D4DACFDF");// 无其他成员在线
  385. }else
  386. {
  387. snprintf(buf,sizeof(buf),"AT+POC=10000021%04x\r\n",sutPocStatus.UserOnLineIndex);
  388. SlwTrace(INF,buf,1);
  389. ModemSendAt(buf);
  390. }
  391. ++sutPocStatus.UserOnLineIndex;
  392. if(sutPocStatus.UserOnLineIndex>=sutPocStatus.UserOnLineNum){
  393. sutPocStatus.UserOnLineIndex=0;
  394. }
  395. SlwTrace(INF,"[User++]",1);
  396. break;
  397. }
  398. break;
  399. case 2://逆时针
  400. LCDBackLight(1);
  401. LcdOnTimeCt=0;
  402. sutPocStatus.QuickGroupUserCt=3;
  403. switch(newPara.PF1Status)
  404. {
  405. case 0:
  406. SpeakerCtrl(1);
  407. ModemSendAt(POC_CMD_DOWN);
  408. SlwTrace(INF,"[G--]",1);
  409. break;
  410. case 1:
  411. if(sutPocStatus.UserOnLineNum==0){
  412. sutPocStatus.UserOnLineIndex=0;
  413. MeSpeak(ENCODE_GBK, "2CCEDEC6E4CBFCB3C9D4B1D4DACFDF");// 无其他成员在线
  414. }else
  415. {
  416. snprintf(buf,sizeof(buf),"AT+POC=10000021%04x\r\n",sutPocStatus.UserOnLineIndex);
  417. SlwTrace(INF,buf,1);
  418. ModemSendAt(buf);
  419. }
  420. if(sutPocStatus.UserOnLineIndex>0)
  421. sutPocStatus.UserOnLineIndex--;
  422. else if(sutPocStatus.UserOnLineNum==0)
  423. sutPocStatus.UserOnLineIndex=0;
  424. else sutPocStatus.UserOnLineIndex=sutPocStatus.UserOnLineNum-1;
  425. SlwTrace(INF,"[User-]",1);
  426. break;
  427. }
  428. break;
  429. case 0://m_key.g_encodeCntP=0;
  430. break;
  431. default:
  432. break;
  433. }
  434. g_iEncode=0;
  435. }