Key.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. void CheckPwrKeyOn(void)
  8. {//长按电源键直到电筒灯亮后释放
  9. unsigned int tick=0;
  10. if(KeyPwrStatus !=0)
  11. {
  12. POWER_ON_FAILED:
  13. SlwTrace(INF, "\r\nPwrOnFailed",1);
  14. DelayMs(10);
  15. PWR_EN_LOW;
  16. while(1);
  17. }
  18. while(KeyPwrStatus==0)
  19. {
  20. if(++tick >= 200000)
  21. {
  22. MODEM_LED2_HIGH;
  23. SlwTrace(INF, "\r\nPwrOnOk",1);
  24. // while(KeyPwrStatus==0){
  25. //// DelayMs(30);
  26. //// MODEM_LED2_LOW;
  27. //// DelayMs(30);
  28. //// MODEM_LED2_HIGH;
  29. // IWDG_ReloadCounter();
  30. // }
  31. //MODEM_LED2_LOW;
  32. PWR_EN_HIGH;//
  33. return;
  34. }
  35. }
  36. SlwTrace(INF,"\r\nPwr erro",1);
  37. goto POWER_ON_FAILED;
  38. }
  39. //检测关机
  40. void SoftPwrOffCheck(void)
  41. {
  42. static unsigned int seconds;
  43. if(!seconds) seconds=uSysTickGet()+500;//首次进入
  44. if(KeyPwrStatus==0)
  45. {
  46. if(uSysTickGet() > seconds)
  47. {
  48. SlwTrace(INF, "PwrOffOk",1);
  49. // MODEM_LED1_HIGH;
  50. if(g_ucTMMode==1)while(1);
  51. UISetNextStatus(UIS_POWEROFF_WAIT);
  52. // GuiClearAll();
  53. // GuiShowBmp(0,0,"BYEBYE.bmp");
  54. // MODEM_PWREN_LOW;
  55. // PWR_EN_LOW;
  56. // MODEM_LED1_LOW;
  57. // while(1);
  58. }
  59. }else seconds=uSysTickGet()+100;//200
  60. }
  61. void KeyInit(void)
  62. {
  63. GPIO_InitTypeDef GPIO_InitStructure;
  64. //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  65. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
  66. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
  67. /*GPIO先全部设置为上拉输入*/
  68. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  69. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  70. //PPT
  71. GPIO_InitStructure.GPIO_Pin=KEY_PPT_PIN;
  72. GPIO_Init(KEY_PPT_PORT, &GPIO_InitStructure);
  73. // INFO VOL+
  74. GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN;
  75. GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure);
  76. // FUNC VOL-
  77. GPIO_InitStructure.GPIO_Pin=KEY_FUNC_PIN;
  78. GPIO_Init(KEY_FUNC_PORT, &GPIO_InitStructure);
  79. //KEY_POWER 电源检测几秒
  80. /*GPIO设置为浮空*/
  81. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  82. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  83. GPIO_InitStructure.GPIO_Pin=KEY_POWER_PIN;
  84. GPIO_Init(KEY_POWER_PORT, &GPIO_InitStructure);
  85. //HEADSET_PIN 耳机检测
  86. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  87. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  88. GPIO_InitStructure.GPIO_Pin=HEADSET_PIN;
  89. GPIO_Init(HEADSET_PORT, &GPIO_InitStructure);
  90. CheckPwrKeyOn();
  91. g_ucKeyMode=0;//连续触发模式
  92. }
  93. static unsigned long KeyScanPort(void)
  94. {
  95. unsigned long k1,k2;
  96. k1=GPIOB->IDR;
  97. k1&=KEY_ALL_PORTB;
  98. k1<<=16;
  99. return (k1);
  100. }
  101. /************************************************************
  102. *GetKey
  103. 读键盘数据
  104. 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
  105. 参数:
  106. g_ucKeyMode=1时,单次模式
  107. g_ucKeyMode=0时,持续模式
  108. 返回值:
  109. 0--按键无变化
  110. 1 --按键有按下:
  111. 单次模式下,当按键第一次按下才返回一次该值;
  112. 持续模式下,按着按键不放将间隔返回该值
  113. g_ulKeyValue保存当前按键值
  114. -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
  115. 释放后,g_ulKeyValue保存上次按下的按键值
  116. *************************************************************/
  117. unsigned char PttKeyFlag;
  118. int GetKey(void)
  119. {
  120. static unsigned char sucCt=0;//连续模式下响应频率控制;
  121. static unsigned char sucFlag=0;
  122. static unsigned char sucPress=0;
  123. static unsigned char sucLastPress=0;
  124. static unsigned long Key=0;
  125. static unsigned long Key1=0;
  126. static unsigned long Key2=0;
  127. //去抖动 0 1
  128. if(++sucFlag>2){ //1
  129. sucFlag=0;
  130. }
  131. if(sucFlag==0){
  132. Key1=KeyScanPort();
  133. }else{
  134. Key2=KeyScanPort();
  135. }
  136. if(Key1!=Key2)return 0;
  137. //按键状态发生变更
  138. if(Key!=Key1){
  139. Key=Key1;
  140. if(Key!=(KEY_ALL_PORTB<<16))
  141. {
  142. sucPress=1; //有按下
  143. printf("KEY=%lx\r\n",Key);
  144. }
  145. else
  146. {
  147. sucPress=0; //无按下
  148. }
  149. }
  150. if(sucPress){//有按键按下
  151. if(g_ucKeyMode){ //单次模式
  152. if(sucLastPress!=sucPress){
  153. sucLastPress=sucPress;
  154. g_ulKeyValue=Key;
  155. if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
  156. printf("##############\r\n");
  157. return 1;
  158. }else{
  159. return 0;
  160. }
  161. }else{ //持续模式
  162. sucLastPress=sucPress;
  163. if(0==sucCt){
  164. g_ulKeyValue=Key1;
  165. if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
  166. sucCt=100;//10ms*100=1000ms
  167. printf("************\r\n");
  168. return 1;
  169. }else{
  170. sucCt--;
  171. return 0;
  172. }
  173. }
  174. }else{ //无按键按下
  175. sucCt=0;
  176. if(sucLastPress){
  177. sucLastPress=0;
  178. return -1;
  179. }
  180. }
  181. return 0;
  182. }
  183. void KeyCount(void)
  184. {
  185. //if(g_ucDKC<255)g_ucDKC++;
  186. if(g_ucUKC<255)g_ucUKC++;
  187. }
  188. int GetKeyVBAT(void)
  189. {
  190. static unsigned char sucCt=0;//连续模式下响应频率控制;
  191. static unsigned char sucFlag=0;
  192. static unsigned char sucPress=0;
  193. static unsigned char sucLastPress=0;
  194. static unsigned long Key=0;
  195. static unsigned long Key1=0;
  196. static unsigned long Key2=0;
  197. //去抖动 0 1
  198. if(++sucFlag>2){ //1
  199. sucFlag=0;
  200. }
  201. if(sucFlag==0){
  202. Key1=GetKeyVbat();
  203. }else{
  204. Key2=GetKeyVbat();
  205. }
  206. if(Key1!=Key2)return 0;
  207. //按键状态发生变更
  208. if(Key!=Key1){
  209. Key=Key1;
  210. if(Key<290)
  211. {
  212. sucPress=1; //有按下
  213. // printf("KEY=%lx............\r\n",Key);
  214. }
  215. else
  216. {
  217. sucPress=0; //无按下
  218. }
  219. }
  220. if(sucPress){//有按键按下
  221. if(g_ucKeyMode){ //单次模式
  222. if(sucLastPress!=sucPress){
  223. sucLastPress=sucPress;
  224. if(Key==0){
  225. g_ulKeyValue=KEY_PANEL_MENU;
  226. }else if(Key<60){
  227. g_ulKeyValue=KEY_UP_DOWN_IP;
  228. }else if(Key<100){
  229. g_ulKeyValue=KEY_PANEL_UP;
  230. }else if(Key<180){
  231. g_ulKeyValue=KEY_PANEL_DOWN;
  232. }else
  233. g_ulKeyValue=KEY_PANEL_EXIT;
  234. return 1;
  235. }else{
  236. return 0;
  237. }
  238. }else{ //持续模式
  239. sucLastPress=sucPress;
  240. // printf("KEYVbat=%d............\r\n",Key);
  241. if(0==sucCt){
  242. //g_ulKeyValue=Key1;
  243. if(Key==0){
  244. g_ulKeyValue=KEY_PANEL_MENU;
  245. }else if(Key<60){
  246. g_ulKeyValue=KEY_UP_DOWN_IP;
  247. }else if(Key<100){
  248. g_ulKeyValue=KEY_PANEL_UP;
  249. }else if(Key<180){
  250. g_ulKeyValue=KEY_PANEL_DOWN;
  251. }else
  252. g_ulKeyValue=KEY_PANEL_EXIT;
  253. sucCt=100;//10ms*100=1000ms
  254. return 1;
  255. }else{
  256. sucCt--;
  257. return 0;
  258. }
  259. }
  260. }else{ //无按键按下
  261. sucCt=0;
  262. if(sucLastPress){
  263. sucLastPress=0;
  264. return -1;
  265. }
  266. }
  267. return 0;
  268. }