app.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. #include "includes.h"
  2. #define APP_IDLE_TIME 3 //如果POC已休眠,APP没有操作一定秒数后休眠
  3. nwy_osiThread_t *mainThreadPtr=NULL;
  4. int keyNum=0;
  5. APP_DEF sutApp;
  6. static void appDataInit(void){
  7. memset((unsigned char *)&sutApp, 0, sizeof(APP_DEF));
  8. sutApp.pcant=4;//默认免提
  9. appSleepCtl(ASLEEP_POC, 1);
  10. appSleepCtl(ASLEEP_LCD, 1);
  11. readInfoNow();
  12. sysIniRead();
  13. }
  14. void dataInit(void){
  15. appDataInit();
  16. pocDataInit();
  17. gpsDataInit();
  18. uiDataInit();
  19. }
  20. static void ledsProCtl(void){
  21. if(sutApp.gtMode!=0) return;
  22. if(0==sutPocStatus.logined) uioStatusSet(UIO_INDOFFLINE);
  23. else if(sutPocStatus.spk>0)uioStatusSet(UIO_INDRX);
  24. else if(sutPocStatus.mic>0 )uioStatusSet(UIO_INDTX);
  25. else uioStatusSet(UIO_INDONLINE);
  26. }
  27. static void adcRead(void);
  28. char isKeyPress(void){
  29. if(keyNum) return keyNum--;
  30. else return 0;
  31. }
  32. static void subTimerCtl(unsigned char sleep_or_not);
  33. /*APP休眠处理接口
  34. ctlType, 操作对象
  35. status,操作值,0 清除 else 设置
  36. */
  37. void appSleepCtl(ASLEEP_ENUM ctlType, char status){
  38. if(status==0) sutApp.appSleepStatus &= ~(1<<ctlType);
  39. else sutApp.appSleepStatus |= 1<<ctlType;
  40. }
  41. /*APP休眠状态接口*/
  42. char isAppSleepReady(void){
  43. if(sutApp.appSleepStatus==0) return 1;
  44. else return 0;
  45. }
  46. /*获取指定休眠控制成员锁定的状态
  47. 0 未锁定 否则锁定,锁定即可以休眠
  48. */
  49. char getAppObjStatus(ASLEEP_ENUM ctlType){
  50. if(sutApp.appSleepStatus & (1<<ctlType)) return 1;
  51. else return 0;
  52. }
  53. /*检测是否APP可以休眠操作*/
  54. char isSleepReady(void){
  55. static unsigned int idleCnt=0;
  56. #ifdef DISABLE_SLEEP
  57. return 0;//不休眠
  58. #endif
  59. if(isAppSleepReady()==0){
  60. idleCnt=0;
  61. return 0;
  62. }
  63. if(++idleCnt>=(APP_IDLE_TIME*1000/APP_SUB_DIV_TIME)){
  64. idleCnt=0;
  65. return 1;
  66. }else return 0;
  67. }
  68. /*处理UI操作*/
  69. static void uiProcess(unsigned int exeInterval){
  70. if(getAppObjStatus(ASLEEP_PWR) != 0) return;//关机后,不切换菜单,只显示关机页面
  71. if(sutApp.gtMode!=0) return;//GT模式后按键无效
  72. uiLoop(exeInterval);
  73. uiResponse(exeInterval);
  74. }
  75. /*触发关机操作*/
  76. static void pwrShutPro(void){
  77. static char shutFlag=0;
  78. if(getAppObjStatus(ASLEEP_PWR) == 0) return;
  79. CTL_LCD_BL(1);
  80. msgAtSend("AT+POC=050000\r\n");
  81. msgAtSend("AT+CFUN=0\r\n");
  82. if(shutFlag==0){
  83. //显示关机界面
  84. guiShowMessageBox("正在关机");
  85. shutFlag=1;
  86. nwy_sleep(1000);
  87. }
  88. MSG_INFO(1, "Normal PwrOff");
  89. //msgAtSend("AT+TRB=1\r\n");//normal power off 当前固件先不发模块关机指令,否则断电再上电有可能是起不来的
  90. nwy_sleep(1000);//一秒后释放电源锁,防止软关机不成功
  91. CTL_LCD_BL(0);
  92. MSG_INFO(1, "Release pwr");
  93. guiFillRect(0,0,GLCD_WIDTH-1,GLCD_HEIGHT-1,guiGetBackColor());//防止下次开机能看到残留
  94. CTL_POWER_HOLD(0);
  95. nwy_sleep(100);
  96. msgAtSend("AT+TRB=2\r\n");//软件复位模块
  97. }
  98. void AntModeRun(void){
  99. #ifdef ENABLE_ANT_MODE
  100. guiShowMessageBox("调天线模式");
  101. for(;;){
  102. nwy_sleep(2000);
  103. }
  104. #endif
  105. }
  106. void lcdReConfig(void){
  107. if(sutApp.lcdReconfigCnt<=3) sutApp.lcdReconfigCnt++;
  108. if(sutApp.lcdReconfigCnt==3) recoverLightColor();
  109. }
  110. static void paProCtl(void);
  111. /*主要应用程序入口*/
  112. void appRun(void){
  113. char cnt=0;
  114. nwy_osiEvent_t event = {};
  115. //等待lcd初始化完成
  116. MSG_INFO(1, "wait lcd init");
  117. while(sutApp.guiStatus==0){nwy_sleep(50);}//是否检测超时?
  118. AntModeRun();
  119. localAuthNow();
  120. //等待poc启动完成
  121. MSG_INFO(1, "lcd init done, wait poc start");
  122. while(sutApp.pocInitStatus==0){
  123. msgAtSend("AT\r\n");
  124. nwy_sleep(50);
  125. }//是否检测超时?
  126. MSG_INFO(1, "app loop start");
  127. //设置EQ数据
  128. modifyEqData();
  129. //修改音频输出增益配置
  130. modifyAudioOutGainConfig();
  131. //设置一下TTS
  132. msgAtSend("AT+HTTSSPEED=1284,-20000\r\n");//设置TTS音量
  133. msgAtSend("AT+HTTSSPEED=1282,-5000\r\n");//设置TTS语速
  134. //查询一下POC版本号
  135. msgAtSend("AT+POC_VER\r\n");
  136. msgAtSend("AT+GMR\r\n");//查询模块版本
  137. msgAtSend("AT+MIC=6\r\n");
  138. msgAtSend("AT+VSW=1\r\n");//打开留音功能
  139. //msgAtSend("AT+CACCP=0,1,0,\"04000B00\"\r\n");
  140. #ifndef ENABLE_PWM_BEEP
  141. if(newPara.KeySound!=0) msgAtSend("AT+TONES=1\r\n");
  142. #endif
  143. splVolumeSet(newPara.spkVol*10);
  144. backLightReset();
  145. if(newPara.ttsMessage[0]!=0){
  146. if(newPara.ttsCodeType==0) ttsPlay(ENCODE_USER_UNICODE_BE, newPara.ttsMessage);
  147. else if(newPara.ttsCodeType==1) ttsPlay(ENCODE_USER_GBK, newPara.ttsMessage);
  148. else MSG_INFO(1, "ttsCodeType:%d not support", newPara.ttsCodeType);
  149. }else MSG_INFO(1, "tts null");
  150. //检测卡是否存在
  151. while(sutApp.cardStatus==0){
  152. msgAtSend("AT+CPIN?\r\n");
  153. nwy_sleep(1000);
  154. if(++cnt>=5){
  155. MSG_INFO(1, "Check card timeout");
  156. break;
  157. }
  158. }
  159. for(;;){
  160. lcdReConfig();
  161. ledsProCtl();
  162. adcRead();
  163. uiProcess(APP_SUB_DIV_TIME);
  164. gpsProCtl(APP_SUB_DIV_TIME);
  165. pocProCtl(APP_SUB_DIV_TIME);
  166. HookGroupUserInfo();
  167. HookVolToutCheck();
  168. LearnUsrInfoCheck(APP_SUB_DIV_TIME);
  169. IncomingSMS();
  170. if(isSleepReady()==0) nwy_sleep(APP_TASK_SLEEP_TIME);
  171. else if(sutApp.gtMode==0){//正常模式下可以休眠
  172. MSG_WARN(1, "APP SLEEP");
  173. stopKeyTimer();
  174. subTimerCtl(1);//切换为休眠状态定时器
  175. sutApp.appStatus=1;
  176. CTL_LED2_GREEN(0);
  177. CTL_LED1_RED(0);
  178. #ifdef ENABLE_DEEP_SLEEP
  179. pwmDeInitToSleep();
  180. lcdDrvPowerCtl(false);
  181. nwy_pm_state_set(NWY_ENTRY_SLEEP);
  182. #endif
  183. nwy_wait_thead_event(nwy_get_current_thread(), &event,0);
  184. lcdDrv_Init(1);//深度休眠唤醒后重新初始化SPI-LCD
  185. sutApp.appStatus=0;
  186. subTimerCtl(0);//切换为唤醒状态定时器
  187. MSG_WARN(1, "APP WAKEUP");
  188. }
  189. //检测是否关机,这里要放在唤醒后操作
  190. pwrShutPro();
  191. }
  192. }
  193. //////////////////////////////////////用于处理非耗时操作,如LED///////////////////////
  194. nwy_osiTimer_t *pSubtimer_t = NULL;
  195. unsigned int subTimerInterval=100;//定时器定时的时间
  196. static void subTimeroutcallback(void *param){
  197. unsigned int *dlyTime=(unsigned int *)param;
  198. //处理灯的显示
  199. uioProctl(*dlyTime);
  200. //控制功放的关闭
  201. paProCtl();
  202. }
  203. /*创建定时器*/
  204. void subTask(void *param){
  205. nwy_osiEvent_t event = {};
  206. if(NULL == pSubtimer_t) pSubtimer_t = nwy_timer_init(nwy_get_current_thread(), subTimeroutcallback, (void *)&subTimerInterval);
  207. if(NULL == pSubtimer_t) MSG_WARN(1,"sub timer init failed");
  208. else nwy_start_timer_periodic(pSubtimer_t,subTimerInterval);
  209. for(;;){nwy_wait_thead_event(nwy_get_current_thread(), &event,0);}
  210. }
  211. /*
  212. 为了做低功耗,此定时器在休眠时,设置为长时间定时
  213. 非休眠时,设置为短时间定时
  214. sleep_or_not:0 未休眠, else 休眠
  215. */
  216. static void subTimerCtl(unsigned char sleep_or_not){
  217. if(NULL==pSubtimer_t){
  218. MSG_WARN(1, "subTimer null");
  219. return;
  220. }
  221. nwy_stop_timer(pSubtimer_t);
  222. //打开定时器
  223. if(sleep_or_not==0) subTimerInterval=100;//未休眠时,100ms
  224. else subTimerInterval=5000;//休眠时 5000ms
  225. nwy_start_timer_periodic(pSubtimer_t,subTimerInterval);
  226. }
  227. //////////////////////////////////////////////////////////////////////////////////////
  228. void usbCmdHandler(const char *data,uint32 length){
  229. static char buf[256];
  230. static int len=0;
  231. static char lach;
  232. char ch;
  233. int i;
  234. if(sutApp.authReady==0){
  235. authRecvPro(data, length);
  236. return;
  237. }
  238. for(i=0;i<length;i++){
  239. ch=data[i];
  240. if(len<sizeof(buf)-1) buf[len++]=ch;
  241. else{
  242. MSG_WARN(1, "USB Buf Over");
  243. len=0;
  244. buf[len++]=ch;
  245. }
  246. if(lach=='\r' && ch=='\n'){
  247. buf[len]=0;
  248. cmdSetting(buf);
  249. len=0;
  250. }
  251. lach=ch;
  252. }
  253. }
  254. //////////////////////////////按键处理接口//////////////////////////
  255. //普通按键处理
  256. unsigned char singleKeyInArow=0;
  257. void keyCheck(unsigned short keyStatus){//定时被调用
  258. static unsigned short lkey=0;
  259. static unsigned char keyCtl=0,keyCnt=0;
  260. unsigned char keyType,needSendKey=0;
  261. if(keyStatus==KS_ALL_IDLE){
  262. keyType=0;
  263. if(keyCtl==1){
  264. keyCtl=0;
  265. needSendKey=1;
  266. }
  267. }else{
  268. keyType=1;
  269. if(lkey != keyStatus) keyCtl=0;//值变化
  270. if(keyCtl==0){
  271. lkey=keyStatus;
  272. keyCtl=1;
  273. keyCnt=0;
  274. needSendKey=1;
  275. }else if(singleKeyInArow!=0){//是否输出持续值
  276. if(++keyCnt>=(1000/KEY_TIMER_TICK)){//每秒产生持续值
  277. keyCnt=0;
  278. needSendKey=1;
  279. }
  280. }
  281. }
  282. if(needSendKey){
  283. if(sutApp.enableKey!=0)//延时期间不检测按键
  284. threadSendEvent(nwy_get_current_thread(),OHPOC_EVENT_KEY_NOR, lkey,keyType,NULL);
  285. }
  286. }
  287. static char micPttPressed=0;
  288. static char pttPressed=0;
  289. static void pttHandler(char type, char ctl){
  290. char needSend=0;
  291. char mic_spk;
  292. if(type==0){//ptt
  293. if(ctl==0){//放开
  294. if(micPttPressed==0){
  295. needSend=1;mic_spk=0;
  296. }
  297. pttPressed=0;
  298. }else{//按下
  299. if(pttPressed==0 && micPttPressed==0){
  300. needSend=1;mic_spk=1;
  301. }
  302. pttPressed=1;
  303. }
  304. }else{//mic ptt
  305. if(ctl==0){//放开
  306. if(pttPressed==0){
  307. needSend=1;mic_spk=0;
  308. }
  309. micPttPressed=0;
  310. }else{//按下
  311. if(pttPressed==0 && micPttPressed==0){
  312. needSend=1;mic_spk=1;
  313. }
  314. micPttPressed=1;
  315. }
  316. }
  317. if(isLearnBusy()==true){
  318. MSG_WARN(1, "Learn task is busy, skip ptt");
  319. return;//学习任务正在使用音频,POC不录音
  320. }
  321. if(needSend!=0){
  322. if(mic_spk==0) msgAtSend("AT+POC=0C0000\r\n");
  323. else msgAtSend("AT+POC=0B0000\r\n");
  324. }
  325. }
  326. /*打印键信息*/
  327. const char freeSeg[]="Free";
  328. const char pressSeg[]="Press";
  329. void showKeyMessage(unsigned short key,unsigned char status){
  330. char buf[50]="[Key_";
  331. switch(key){
  332. case KV_KEY_MIC_PTT: strcat(buf, "MIC_PTT]");break;
  333. default:return;
  334. }
  335. if(status==0) strcat(buf, freeSeg);
  336. else strcat(buf, pressSeg);
  337. MSG_INFO(1,buf);
  338. }
  339. void keyHandler(unsigned short key,unsigned char status){//普通按键值处理
  340. tryWakeupApp();//有普通键按下或放开,唤醒休眠的系统
  341. showKeyMessage(key,status);
  342. if(status==0){//key free
  343. switch(key){
  344. case KV_KEY_MIC_PTT:
  345. pttHandler(1,0);
  346. break;
  347. default:
  348. break;
  349. }
  350. singleKeyInArow=0;
  351. appSleepCtl(ASLEEP_KEY, 0);
  352. }else{//key press
  353. appSleepCtl(ASLEEP_KEY, 1);
  354. if(sutApp.keyLock!=0){
  355. sutApp.lockShow=1;
  356. return;
  357. }
  358. switch(key){
  359. case KV_KEY_MIC_PTT:
  360. singleKeyInArow=1;
  361. pttHandler(1,1);
  362. break;
  363. default:
  364. break;
  365. }
  366. }
  367. }
  368. //扫描矩阵按键
  369. #define MATRIX_KEY_NUMS 2 //最多检测同时两个组合键
  370. void keyStatusCtlAll(char high_low){
  371. CTL_KEY_OUT1(high_low);
  372. CTL_KEY_OUT2(high_low);
  373. CTL_KEY_OUT3(high_low);
  374. }
  375. void keyStatusCtlOne(char outIndex){
  376. switch(outIndex){
  377. case 0:CTL_KEY_OUT1(0);
  378. CTL_KEY_OUT2(1);
  379. CTL_KEY_OUT3(1);
  380. break;
  381. case 1:CTL_KEY_OUT2(0);
  382. CTL_KEY_OUT1(1);
  383. CTL_KEY_OUT3(1);
  384. break;
  385. case 2:CTL_KEY_OUT3(0);
  386. CTL_KEY_OUT2(1);
  387. CTL_KEY_OUT1(1);
  388. break;
  389. }
  390. }
  391. int keyStatusGetOne(char outIndex){
  392. int ret=2;
  393. switch(outIndex){
  394. case 0:ret=INT_KEY_IN1();break;
  395. case 1:ret=INT_KEY_IN2();break;
  396. case 2:ret=INT_KEY_IN3();break;
  397. }
  398. return ret;
  399. }
  400. unsigned short matrixGetKey(unsigned char out, unsigned char in){
  401. unsigned short vlaue=0;
  402. if(out >= MKS_KEY_OUS || in >=MKS_KEY_INS) return 0xffff;
  403. switch(in){
  404. case 0:
  405. switch(out){
  406. case 0:vlaue |= (1<<MKEY_VALUE_1);break;
  407. case 1:vlaue |= (1<<MKEY_VALUE_2);break;
  408. case 2:vlaue |= (1<<MKEY_VALUE_3);break;
  409. default:vlaue=0xffff;break;
  410. }
  411. break;
  412. case 1:
  413. switch(out){
  414. case 0:vlaue |= (1<<MKEY_VALUE_4);break;
  415. case 1:vlaue |= (1<<MKEY_VALUE_5);break;
  416. case 2:vlaue |= (1<<MKEY_VALUE_6);break;
  417. default:vlaue=0xffff;break;
  418. }
  419. break;
  420. case 2:
  421. switch(out){
  422. case 0:vlaue |= (1<<MKEY_VALUE_7);break;
  423. case 1:vlaue |= (1<<MKEY_VALUE_8);break;
  424. case 2:vlaue |= (1<<MKEY_VALUE_9);break;
  425. default:vlaue=0xffff;break;
  426. }
  427. break;
  428. default:vlaue=0xffff;break;
  429. }
  430. return vlaue;
  431. }
  432. /*进入GT模式*/
  433. static void enterGTMode(void){
  434. if(sutApp.gtMode!=0) return;
  435. #ifndef ENABLE_PWM_BEEP
  436. msgAtSend("AT+TONES=1\r\n");
  437. #endif
  438. sutApp.gtMode=1;
  439. uioStatusSet(UIO_INDGT);
  440. paControl(true);//GT下常开喇叭
  441. #ifdef ENABLE_PWM_BEEP
  442. beepStart(TONE_SPECIAL);
  443. nwy_sleep(50);
  444. beepStart(TONE_NORMAL);
  445. nwy_sleep(50);
  446. beepStart(TONE_SPECIAL);
  447. #else
  448. msgAtSend("AT+TONE\r\n");
  449. nwy_sleep(200);
  450. msgAtSend("AT+TONE\r\n");
  451. #endif
  452. MSG_WARN(1, "Enter GT Mode");
  453. guiShowMessageBox("GT模式");
  454. }
  455. static unsigned short g_usKeyValue=0;
  456. unsigned short getKeyValue(void){return g_usKeyValue;}
  457. unsigned char keyInArow=0;//键值是否持续产生
  458. void mKeyCheck(void){//定时被调用
  459. unsigned char matrix[MATRIX_KEY_NUMS][2],num;
  460. unsigned char in,out,i,j,update;
  461. int value;
  462. unsigned short keys[MATRIX_KEY_NUMS];
  463. static unsigned short finalKey=0;
  464. unsigned short tKey;
  465. static unsigned char keyCtl=0;
  466. static unsigned char keyCnt=0;
  467. unsigned char needSendKey=0;
  468. for(i=0;i<MATRIX_KEY_NUMS;i++){
  469. for(j=0;j<2;j++) matrix[i][j]=0xff;
  470. }
  471. num=0;
  472. for(out=0;out<MKS_KEY_OUS;out++){
  473. keyStatusCtlOne(out);
  474. for(in=0;in<MKS_KEY_INS;in++){
  475. if(keyStatusGetOne(in)==0){
  476. matrix[num][0]=out;
  477. matrix[num][1]=in;
  478. if(++num>=MATRIX_KEY_NUMS) goto MATRIX_END;
  479. }
  480. }
  481. }
  482. MATRIX_END:
  483. keyStatusCtlAll(0);
  484. if(num==0){//无键按下
  485. goto MKEY_NO_KEY;
  486. }
  487. for(int w=0;w<num;w++)
  488. //检测值
  489. for(i=0;i<MATRIX_KEY_NUMS;i++) keys[i]=matrixGetKey(matrix[i][0],matrix[i][1]);
  490. tKey=0;
  491. for(i=0;i<MATRIX_KEY_NUMS;i++){
  492. if(keys[i]!=0xffff) tKey |= keys[i];
  493. }
  494. needSendKey=0;
  495. //得出键值
  496. if(tKey==MKEY_VALUE_IDLE){//无键按键
  497. MKEY_NO_KEY:
  498. if(keyCtl==1){
  499. keyCtl=0;
  500. needSendKey=1;
  501. }
  502. }else{//有键按下
  503. if(tKey != g_usKeyValue) keyCtl=0;//值变化
  504. if(keyCtl==0){
  505. g_usKeyValue=tKey;
  506. keyCtl=1;
  507. keyCnt=0;
  508. needSendKey=1;
  509. }else if(keyInArow!=0){//是否输出持续值
  510. if(++keyCnt>=(1000/KEY_TIMER_TICK)){//每秒产生持续值
  511. keyCnt=0;
  512. needSendKey=1;
  513. }
  514. }
  515. }
  516. if(needSendKey){
  517. if(sutApp.enableKey!=0)
  518. threadSendEvent(nwy_get_current_thread(),OHPOC_EVENT_KEY_MAT, keyCtl,NULL,NULL);
  519. }
  520. }
  521. void showKeyMsg(char *info, unsigned char keyCtl){
  522. char buf[40];
  523. char *ctl;
  524. if(keyCtl==0) ctl=freeSeg;
  525. else ctl=pressSeg;
  526. snprintf(buf, sizeof(buf),"[Key %s]%s",ctl,info);
  527. MSG_INFO(1, buf);
  528. }
  529. static void lockProcess(void){
  530. if(sutApp.keyLock==0){
  531. sutApp.keyLock=1;
  532. appSleepCtl(ASLEEP_GGROUP, 0);
  533. appSleepCtl(ASLEEP_GUSER, 0);
  534. if(sutUIstatus.Status==UIS_STANDBY) guiShowBmp(72,110, "lock.bmp");//锁关/锁开触发时更新状态
  535. uISetNextStatus(UIS_STANDBY);
  536. }else{
  537. sutApp.keyLock=0;
  538. sutApp.lockShow=0;
  539. if(sutUIstatus.Status==UIS_STANDBY) guiClearRect(72,110,72+17,110+17,guiGetBackColor());//锁关/锁开触发时更新状态
  540. }
  541. }
  542. void keyToneStart(void){
  543. if(newPara.KeySound==0) return;
  544. #ifdef ENABLE_PWM_BEEP
  545. beepStart(TONE_NORMAL);
  546. #else
  547. msgAtSend("AT+TONE\r\n");
  548. #endif
  549. }
  550. void mKeyHandler(unsigned char keyCtl){
  551. static unsigned char gtModeCnt=0;
  552. static unsigned char lockCnt=0;
  553. if(sutApp.keyLock!=0 && MKEY_VALUE_P2!=g_usKeyValue){
  554. sutApp.lockShow=1;
  555. return;
  556. }
  557. if(keyCtl==0){//释放
  558. switch(g_usKeyValue){
  559. case MKEY_VALUE_PTT:showKeyMsg("PTT",keyCtl);
  560. pttHandler(0,0);
  561. break;
  562. case MKEY_VALUE_P1:showKeyMsg("P1",keyCtl);
  563. break;
  564. case MKEY_VALUE_P2:showKeyMsg("P2",keyCtl);
  565. lockCnt=0;
  566. break;
  567. case MKEY_VALUE_MENU:showKeyMsg("MENU",keyCtl);
  568. break;
  569. case MKEY_VALUE_ESC:showKeyMsg("ESC",keyCtl);
  570. break;
  571. case MKEY_VALUE_UP:showKeyMsg("UP",keyCtl);
  572. break;
  573. case MKEY_VALUE_DOWN:showKeyMsg("DOWN",keyCtl);
  574. break;
  575. case MKEY_VALUE_F1:showKeyMsg("F1",keyCtl);
  576. break;
  577. case MKEY_VALUE_F2:showKeyMsg("F2",keyCtl);
  578. break;
  579. case MKEY_VALUE_CB_GT:showKeyMsg("CB_GT",keyCtl);
  580. gtModeCnt=0;
  581. break;
  582. case MKEY_VALUE_CB_IP:showKeyMsg("CB_IP",keyCtl);
  583. break;
  584. default:MSG_WARN(1,"UnHande Key:%d Free",g_usKeyValue);
  585. break;
  586. }
  587. keyInArow=0;
  588. appSleepCtl(ASLEEP_MKEY, 0);
  589. }else{//按下
  590. if(g_usKeyValue!=MKEY_VALUE_PTT && g_usKeyValue!=MKEY_VALUE_CB_GT) keyToneStart();
  591. backLightReset();
  592. appSleepCtl(ASLEEP_MKEY, 1);
  593. keyNum++;
  594. switch(g_usKeyValue){
  595. case MKEY_VALUE_PTT:showKeyMsg("PTT",keyCtl);
  596. pttHandler(0,1);
  597. break;
  598. case MKEY_VALUE_P1:showKeyMsg("P1",keyCtl);
  599. break;
  600. case MKEY_VALUE_P2:showKeyMsg("P2",keyCtl);
  601. keyInArow=1;
  602. if(++lockCnt==3) lockProcess();
  603. break;
  604. case MKEY_VALUE_MENU:showKeyMsg("MENU",keyCtl);
  605. break;
  606. case MKEY_VALUE_ESC:showKeyMsg("ESC",keyCtl);
  607. break;
  608. case MKEY_VALUE_UP:showKeyMsg("UP",keyCtl);
  609. break;
  610. case MKEY_VALUE_DOWN:showKeyMsg("DOWN",keyCtl);
  611. break;
  612. case MKEY_VALUE_F1:showKeyMsg("F1",keyCtl);
  613. break;
  614. case MKEY_VALUE_F2:showKeyMsg("F2",keyCtl);
  615. break;
  616. case MKEY_VALUE_CB_GT:showKeyMsg("CB_GT",keyCtl);
  617. keyInArow=1;
  618. if(++gtModeCnt==3){
  619. if(sutApp.gtMode==0){
  620. enterGTMode();
  621. }
  622. }
  623. break;
  624. case MKEY_VALUE_CB_IP:
  625. keyInArow=1;
  626. showKeyMsg("CB_IP",keyCtl);
  627. break;
  628. default:MSG_WARN(1,"UnHande Key:%d Press",g_usKeyValue);
  629. break;
  630. }
  631. }
  632. }
  633. //////////////////////////////按键处理结束//////////////////////////
  634. //////////////////////////////其它处理接口//////////////////////////
  635. void hpDetStatusChanged(unsigned char hpStatus){
  636. if(0==hpStatus){
  637. MSG_WARN(1,"HP plug in");
  638. }else{
  639. MSG_WARN(1,"HP plug out");
  640. }
  641. }
  642. //测试主板ADC为:150~1190范围
  643. //实际使用ADC取:170~1210范围
  644. /*输入adc值,输出音量等级*/
  645. #define VOL_LEV_NUM 11 //档位数
  646. #define VOL_LEV_DEF 10 //抖动上下限
  647. unsigned char wisdomAdcProcess(int adcValueTmp){
  648. const unsigned short volAdcTable[VOL_LEV_NUM]={170,200,240,300,400,500,700,900,1100,1140,1170};//中间ADC变化快,两端ADC变化慢
  649. int i;
  650. static unsigned char ucVolLev=0;
  651. for(i=0;i<VOL_LEV_NUM;i++){
  652. if(ucVolLev==0){
  653. if(adcValueTmp >= volAdcTable[ucVolLev]+VOL_LEV_DEF) ucVolLev++;
  654. }else if(ucVolLev<VOL_LEV_NUM-1){
  655. if(adcValueTmp >= volAdcTable[ucVolLev]+VOL_LEV_DEF) ucVolLev++;
  656. else if(adcValueTmp < volAdcTable[ucVolLev-1]-VOL_LEV_DEF) ucVolLev--;
  657. }else{
  658. if(ucVolLev==VOL_LEV_NUM-1) if(adcValueTmp < volAdcTable[ucVolLev-1]-VOL_LEV_DEF) ucVolLev--;
  659. }
  660. }
  661. return ucVolLev;
  662. }
  663. static void adcRead(void){
  664. #define VBAT_READ_TIME 20
  665. int adcValueTmp;
  666. static int siVbat=0;
  667. static int laSiVbat;
  668. static unsigned char index=0;
  669. int x=(GLCD_WIDTH-16)/2;
  670. int y=GLCD_HEIGHT-12;
  671. //检测音量旋钮电压
  672. adcValueTmp=nwy_adc_get(NWY_ADC_CHANNEL3, NWY_ADC_SCALE_1V250);
  673. if(adcValueTmp>0){//15 1227
  674. if(sutUIstatus.Status==UIS_STANDBY){
  675. if(adcValueTmp > 1000){
  676. if(sutApp.earStatus==0){
  677. sutApp.pcant=2;//FM
  678. sutApp.earStatus=1;
  679. MSG_INFO(1, "FM MODE");
  680. guiShowBmp(x, y, "ear.bmp");
  681. }
  682. }else{
  683. if(sutApp.earStatus!=0){
  684. sutApp.pcant=4;//mianti
  685. sutApp.earStatus=0;
  686. MSG_INFO(1, "MIANTI MODE");
  687. guiClearRect(x, y, x+16,y+12, guiGetBackColor());
  688. }
  689. }
  690. }else if(sutApp.earStatus != 0) sutApp.earStatus=0;//切到其它菜单后再切回来还能显示
  691. }
  692. //检测电池电压
  693. adcValueTmp=nwy_adc_get(NWY_ADC_CHANNEL4, NWY_ADC_SCALE_5V000);
  694. siVbat += adcValueTmp;
  695. if(++index>=VBAT_READ_TIME){
  696. sutApp.g_iBAT=siVbat / VBAT_READ_TIME / 10;
  697. if(laSiVbat!=sutApp.g_iBAT){
  698. //MSG_INFO(1, "bat:%d", sutApp.g_iBAT);
  699. laSiVbat=sutApp.g_iBAT;
  700. }
  701. siVbat=0;
  702. index=0;
  703. }
  704. }
  705. void tryWakeupApp(void){
  706. if(sutApp.appStatus!=0){
  707. if(mainThreadPtr!=NULL){
  708. sutApp.lcdReconfigCnt=0;
  709. #ifdef ENABLE_DEEP_SLEEP
  710. nwy_pm_state_set(NWY_WAKEUP);
  711. #endif
  712. threadSendEvent(mainThreadPtr,OHPOC_EVENT_MAIN,NULL,NULL,NULL);
  713. }
  714. }
  715. }
  716. /*背光时间到,熄灭*/
  717. void backLightCb(void *param){
  718. if(sutApp.gtMode!=0){
  719. CTL_LCD_BL(1);
  720. return;
  721. }
  722. if(sutPocStatus.firstLogin==0) return;//未登录过,不灭屏
  723. if(newPara.lcdParaList[newPara.lcdParaDefaultIndex]==0) return;//常亮
  724. CTL_LCD_BL(0);
  725. appSleepCtl(ASLEEP_LCD, 0);//可以休眠
  726. }
  727. /*点亮背光*/
  728. void backLightReset(void){
  729. unsigned short tmp;
  730. appSleepCtl(ASLEEP_LCD, 1);//不让休眠
  731. tmp=newPara.lcdParaList[newPara.lcdParaDefaultIndex];
  732. CTL_LCD_BL(1);
  733. startBackLightTimer(tmp*1000);
  734. }
  735. void sysPwrLock(void){
  736. sutApp.pwrLock=1;
  737. }
  738. void sysPwrRlease(void){
  739. sutApp.pwrLock=0;
  740. }
  741. //"3132" --> 0x31,0x32
  742. void StrAsciiToHex(char *src, unsigned char *des){
  743. unsigned char temp[2],i;
  744. if(strlen(src)%2) return;
  745. while(0!=*src)
  746. {
  747. for(i=0;i<2;i++)
  748. {
  749. temp[i] = *src++;
  750. if(temp[i] >= '0' && temp[i] <= '9') temp[i] -= 0x30;
  751. else if(temp[i] >= 'A' && temp[i] <= 'F') temp[i] -= 0x37;
  752. else temp[i] -= 0x57;
  753. }
  754. temp[0] <<= 4;
  755. temp[0] &= 0xf0;
  756. *des++=temp[0]|temp[1];
  757. }
  758. *des=0;
  759. }
  760. /*调节音量,刷新音量进度条*/
  761. void volAdjAction(char direction){
  762. if(direction==0){
  763. if(newPara.spkVol<10) newPara.spkVol++;
  764. else goto FLASH;
  765. }else{
  766. if(newPara.spkVol>0) newPara.spkVol--;
  767. else goto FLASH;
  768. }
  769. splVolumeSet(newPara.spkVol*10);
  770. MSG_INFO(1,"SPK_VOL:%d",newPara.spkVol*10);
  771. sysIniSave();
  772. //刷新进度条
  773. FLASH:
  774. volUiFlash(newPara.spkVol);
  775. }
  776. //////////////////////////////其它处理结束//////////////////////////
  777. /////////////////////////////事件发送接口///////////////////////////
  778. void threadSendEvent(nwy_osiThread_t *threadID, uint32 id, uint32 param1,uint32 param2,uint32 param3){
  779. nwy_osiEvent_t pEventSend;
  780. pEventSend.id=id;
  781. pEventSend.param1=param1;
  782. pEventSend.param2=param2;
  783. pEventSend.param3=param3;
  784. nwy_send_thead_event(threadID, &pEventSend,0);
  785. }
  786. void ttsPlay(ENCODE_USER_ENUM type, char *tts){
  787. char buf[20];
  788. if(sutApp.skipTTS!=0){
  789. MSG_WARN(1, "TTS is disable by learning task");
  790. return;
  791. }
  792. MSG_INFO(1, "tts Play:%s",tts);
  793. paControl(true);
  794. snprintf(buf, sizeof(buf), "AT+HTTS=%d,\"", type);
  795. msgAtSend(buf);
  796. msgAtSend(tts);
  797. msgAtSend("\"\r\n");
  798. sutPocStatus.TTS=1;
  799. }
  800. static void paProCtl(void){
  801. //会被定时调用
  802. if(sutPocStatus.TTS!=0) return;
  803. if(sutPocStatus.spk!=0) return;
  804. if(sutPocStatus.TONE!=0) return;
  805. if(sutPocStatus.beep!=0) return;
  806. if(sutApp.gtMode!=0) return;
  807. paControl(false);
  808. //MSG_INFO(1,"spk off");
  809. }
  810. void splVolumeSet(unsigned char level){
  811. char info[30];
  812. snprintf(info, sizeof(info), "AT+SPK=%d\r\n", level);
  813. msgAtSend(info);
  814. }
  815. /////////////////////////////事件发送结束///////////////////////////