app.c 22 KB

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