app(754).c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. #include "includes.h"
  2. #include "lsapi_tts.h"
  3. #include "osi_api.h"
  4. #define APP_IDLE_TIME 3 //如果POC已休眿APP没有操作一定秒数后休眠
  5. int keyNum=0;
  6. APP_DEF sutApp;
  7. static void appDataInit(void){
  8. memset((unsigned char *)&sutApp, 0, sizeof(APP_DEF));
  9. sutApp.updateStatusBar=1;
  10. sutApp.g_iBAT=-1;
  11. sutApp.copstype=-1;
  12. sutApp.pcant=4;
  13. sutApp.enableKey=1;
  14. appSleepCtl(ASLEEP_POC, 1);
  15. appSleepCtl(ASLEEP_LCD, 1);
  16. readInfoNow();
  17. sysIniRead();
  18. }
  19. void dataInit(void){
  20. appDataInit();
  21. pocDataInit();
  22. gpsDataInit();
  23. uiDataInit();
  24. }
  25. static void ledsProCtl(void){
  26. if(sutApp.gtMode!=0) return;
  27. if(0==sutPocStatus.logined) uioStatusSet(UIO_INDOFFLINE);
  28. else if(sutPocStatus.spk>0)uioStatusSet(UIO_INDRX);
  29. else if(sutPocStatus.mic>0 )uioStatusSet(UIO_INDTX);
  30. else uioStatusSet(UIO_INDONLINE);
  31. }
  32. static void adcRead(unsigned int interval);
  33. char isKeyPress(void){
  34. if(keyNum) return keyNum--;
  35. else return 0;
  36. }
  37. /*APP休眠处理接口
  38. ctlType, 操作对象
  39. status,操作值,0 清除 else 设置
  40. */
  41. void appSleepCtl(ASLEEP_ENUM ctlType, char status){
  42. if(status==0) sutApp.appSleepStatus &= ~(1<<ctlType);
  43. else sutApp.appSleepStatus |= 1<<ctlType;
  44. }
  45. /*APP休眠状态接叿*/
  46. char isAppSleepReady(void){
  47. if(sutApp.appSleepStatus==0) return 1;
  48. else return 0;
  49. }
  50. /*获取指定休眠控制成员锁定的状怿
  51. 0 未锁宿否则锁定,锁定即可以休眠
  52. */
  53. char getAppObjStatus(ASLEEP_ENUM ctlType){
  54. if(sutApp.appSleepStatus & (1<<ctlType)) return 1;
  55. else return 0;
  56. }
  57. /*检测是否APP可以休眠操作*/
  58. char isSleepReady(void){
  59. static unsigned int idleCnt=0;
  60. #ifdef DISABLE_SLEEP
  61. return 0;//不休眿
  62. #endif
  63. if(isAppSleepReady()==0){
  64. idleCnt=0;
  65. return 0;
  66. }
  67. if(++idleCnt>=(APP_IDLE_TIME*1000/APP_SUB_DIV_TIME)){
  68. idleCnt=0;
  69. return 1;
  70. }else return 0;
  71. }
  72. void usbCmdHandler(const char *data,unsigned int length){
  73. static char buf[256];
  74. static int len=0;
  75. static char lach;
  76. char ch;
  77. int i;
  78. if(sutApp.authReady==0){
  79. //authRecvPro(data, length);
  80. return;
  81. }
  82. for(i=0;i<length;i++){
  83. ch=data[i];
  84. if(len<sizeof(buf)-1) buf[len++]=ch;
  85. else{
  86. MSG_WARN(1, "USB Buf Over");
  87. len=0;
  88. buf[len++]=ch;
  89. }
  90. if(lach=='\r' && ch=='\n'){
  91. buf[len-2]=0; //GWSD 新指令去掉回车换行
  92. cmdSetting(buf);
  93. len=0;
  94. }
  95. lach=ch;
  96. }
  97. }
  98. static void pttHandler(char type, char ctl){
  99. #if 1//主PTT, 耳机PTT以及组合PTT都可以触发,即矩阵驱动时,任何一个按下如果没发请麦,则发请麦,只要有一个放开,则直接发放麦指令。因为组合按放后,放开,只有一个放开键值出来
  100. static char micPttPressed=0;
  101. static char pttPressed=0;
  102. static char maxPressed=0;
  103. if(type==0){//ptt
  104. if(ctl==0){//free
  105. pttPressed=0;
  106. }else{//press
  107. pttPressed=1;
  108. }
  109. }else if(type==1){//mic ptt
  110. if(ctl==0){//free
  111. micPttPressed=0;
  112. }else{//press
  113. micPttPressed=1;
  114. }
  115. }else if(type==2){//mic ptt
  116. if(ctl==0){//free
  117. maxPressed=0;
  118. }else{//press
  119. maxPressed=1;
  120. }
  121. }else return;
  122. if(ctl==0){//free
  123. if(pttPressed==0 || micPttPressed==0 || maxPressed==0){
  124. sutApp.pttReq=0;
  125. pttPressed=0;micPttPressed=0;maxPressed=0;
  126. msgAtSend("AT+POC=relspk00");
  127. }
  128. }else{//press
  129. if(pttPressed || micPttPressed || maxPressed){
  130. if(sutApp.pttReq==0){
  131. sutApp.pttReq=1;
  132. msgAtSend("AT+POC=reqspk00");
  133. }
  134. }
  135. }
  136. #else
  137. static char micPttPressed=0;
  138. static char pttPressed=0;
  139. char needSend=0;
  140. char mic_spk;
  141. if(type==0){//ptt
  142. if(ctl==0){//放开
  143. if(micPttPressed==0){
  144. needSend=1;mic_spk=0;
  145. }
  146. pttPressed=0;
  147. }else{//按下
  148. if(pttPressed==0 && micPttPressed==0){
  149. needSend=1;mic_spk=1;
  150. }
  151. pttPressed=1;
  152. }
  153. }else if(type==1){//mic ptt
  154. if(ctl==0){//放开
  155. if(pttPressed==0){
  156. needSend=1;mic_spk=0;
  157. }
  158. micPttPressed=0;
  159. }else{//按下
  160. if(pttPressed==0 && micPttPressed==0){
  161. needSend=1;mic_spk=1;
  162. }
  163. micPttPressed=1;
  164. }
  165. }
  166. if(needSend!=0){
  167. if(mic_spk==0) msgAtSend("AT+POC=0C0000\r\n");
  168. else msgAtSend("AT+POC=0B0000\r\n");
  169. }
  170. #endif
  171. }
  172. /*打印键信恿*/
  173. const char freeSeg[]="Free";
  174. const char pressSeg[]="Press";
  175. void showKeyMessage(unsigned short key,unsigned char status){
  176. char buf[50]="[Key_";
  177. switch(key){
  178. case MKEY_VALUE_F2: strcat(buf, "F2]");break;
  179. case MKEY_VALUE_F1: strcat(buf, "F1]");break;
  180. case MKEY_VALUE_PTT: strcat(buf, "PTT]");break;
  181. case MKEY_VALUE_MENU: strcat(buf, "MENU]");break;
  182. case MKEY_VALUE_UP: strcat(buf, "UP]");break;
  183. case MKEY_VALUE_DOWN: strcat(buf, "DOWN]");break;
  184. case MKEY_VALUE_ESC: strcat(buf, "ESC]");break;
  185. case MKEY_VALUE_CB_GT: strcat(buf, "CB_GT]");break;
  186. case MKEY_VALUE_CB_IP: strcat(buf, "CB_IP]");break;
  187. case MKEY_VALUE_MIC_PTT: strcat(buf, "MIC_PTT]");break;
  188. case MKEY_VALUE_CB_PTT: strcat(buf, "MAX_PTT]");break;
  189. default: return;
  190. }
  191. if(status==0) strcat(buf, freeSeg);
  192. else strcat(buf, pressSeg);
  193. MSG_INFO(1,buf);
  194. }
  195. /*
  196. 龙尚矩阵驱动有一个问题:先按A不放,检测到A按下,再按B不放,检测到B按下,同时放开,概率只检测到B放开,A没检测到
  197. 规避方式:键值发生变化时,如果上次的键值大于等于2,直接清掉键值。
  198. */
  199. void assistantCheck(unsigned short key){
  200. unsigned int i,j,w;
  201. static unsigned short lkey=0;
  202. if(lkey==key) return;
  203. j=0;
  204. for(i=0;i<16;i++){//上次键值置位的个数
  205. if(lkey & (1<<i)) j++;
  206. }
  207. w=0;
  208. for(i=0;i<16;i++){//本次键值置位的个数
  209. if(key & (1<<i)) w++;
  210. }
  211. if(j>=2 && j>w){//上一次至少两个键按下,现在发生变化,至少一个键放开了,全键清除
  212. lkey=0;
  213. clearKeyValue();
  214. }
  215. lkey=key;
  216. }
  217. unsigned char keyInArow=0;
  218. void keyCheck(unsigned short keyStatus){
  219. static unsigned short lkey=0;
  220. static unsigned char keyCtl=0,keyCnt=0;
  221. unsigned char keyType,needSendKey=0;
  222. assistantCheck(keyStatus);
  223. if(keyStatus==KS_ALL_IDLE){
  224. keyType=0;
  225. if(keyCtl==1){
  226. keyCtl=0;
  227. needSendKey=1;
  228. }
  229. }else{
  230. keyType=1;
  231. if(lkey != keyStatus) keyCtl=0;//值变匿
  232. if(keyCtl==0){
  233. lkey=keyStatus;
  234. keyCtl=1;
  235. keyCnt=0;
  236. needSendKey=1;
  237. }else if(keyInArow!=0){//是否输出持续倿
  238. if(++keyCnt>=(1000/KEY_TIMER_TICK)){//每秒产生持续倿
  239. keyCnt=0;
  240. needSendKey=1;
  241. }
  242. }
  243. }
  244. if(needSendKey){
  245. if(sutApp.enableKey!=0)//延时期间不检测按锿
  246. keySendEvent(lkey,keyType);
  247. }
  248. }
  249. bool lcdBackLightStatus=false;
  250. void pwrDetRead(char type){
  251. static char cnt=0;
  252. if(type==0){
  253. cnt++;
  254. if(cnt==3){
  255. sutApp.waitEscReleased=1;
  256. uISetNextStatus(UIS_MENU_SHUT_PWR);
  257. }
  258. }else{
  259. sutApp.waitEscReleased=0;
  260. cnt=0;
  261. }
  262. #if 0
  263. if(sutUIstatus.Status == UIS_MENU_SHUT_PWR) return;//关机界面不处理
  264. if(type==0) return;//放开时处理
  265. //处理按顶部键切换UI
  266. uiTimeOutToStandby(1,APP_SUB_DIV_TIME);
  267. //处理按顶部键切换背光
  268. if(sutUIstatus.Status != UIS_STANDBY) return;//待机页面时才反转背光
  269. if(lcdBackLightStatus==false) backLightReset();
  270. else lcdBackLightApi(0);
  271. #endif
  272. }
  273. void lcdBackLightApi(char value){
  274. CTL_LCD_BL(value);
  275. if(value==0) lcdBackLightStatus=false;
  276. else lcdBackLightStatus=true;
  277. }
  278. static void lockProcess(void){
  279. if(sutApp.keyLock==0){
  280. sutApp.keyLock=1;
  281. appSleepCtl(ASLEEP_GGROUP, 0);
  282. appSleepCtl(ASLEEP_GUSER, 0);
  283. uISetNextStatus(UIS_STANDBY);
  284. }else{
  285. sutApp.keyLock=0;
  286. sutApp.lockShow=0;
  287. }
  288. }
  289. //key:1 跳过
  290. void keyToneStart(char type){
  291. if(newPara.KeySound==0&&type==0) return;
  292. if(sutPocStatus.TTS)return;
  293. if(type==2)sutPocStatus.beep=2;
  294. else sutPocStatus.beep=1;
  295. paControl(true);
  296. MSG_INFO(1, "Ptt:%d\r\n",sutPocStatus.PttFail);
  297. if(ToneThreadPtr!=NULL)threadSendEvent(ToneThreadPtr,OHPOC_EVENT_MAIN,NULL,NULL,NULL);//0702Send("AT+TONE\r\n");
  298. }
  299. void quickUiSwitch(int uistatus){
  300. if(true==isUiSwitchReady()) return;
  301. uISetNextStatus(uistatus);
  302. getStackStruct()->FastUiChange=1;
  303. }
  304. /*进入GT模式*/
  305. static void enterGTMode(void){
  306. #ifndef ENABLE_PWM_BEEP
  307. msgAtSend("AT+TONES=1\r\n");
  308. #endif
  309. sutApp.gtMode=1;
  310. uioStatusSet(UIO_INDGT);
  311. paControl(true);//GT下常开喇叭
  312. #ifdef ENABLE_PWM_BEEP
  313. pwmBeepStart(868,50);
  314. LSAPI_OSI_ThreadSleep(100);
  315. pwmBeepStart(500,50);
  316. LSAPI_OSI_ThreadSleep(100);
  317. pwmBeepStart(868,50);
  318. #else
  319. msgAtSend("AT+TONE\r\n");
  320. LSAPI_OSI_ThreadSleep(200);
  321. msgAtSend("AT+TONE\r\n");
  322. #endif
  323. MSG_WARN(1, "Enter GT Mode");
  324. }
  325. void micPttHandler(char status){
  326. keySendEvent(MKEY_VALUE_MIC_PTT,status);
  327. }
  328. void keyHandler(unsigned short key,unsigned char status){//普通按键值处琿
  329. static unsigned char gtModeCnt=0;
  330. static unsigned char lockCnt=0;
  331. if(newPara.lockType!=0){
  332. if(sutApp.keyLock!=0 && MKEY_VALUE_MENU!=key){
  333. sutApp.lockShow=1;
  334. return;
  335. }
  336. }else{
  337. if(sutApp.keyLock!=0 && (MKEY_VALUE_DOWN==key || MKEY_VALUE_ESC==key)){
  338. sutApp.lockShow=1;
  339. return;
  340. }
  341. }
  342. MSG_INFO(1, "KEY:%d,%d", key,status);
  343. showKeyMessage(key,status);
  344. if(status==0){//key free
  345. switch(key){
  346. case MKEY_VALUE_MIC_PTT:
  347. pttHandler(0,0);
  348. break;
  349. case MKEY_VALUE_PTT:
  350. pttHandler(1,0);
  351. break;
  352. case MKEY_VALUE_CB_PTT:
  353. pttHandler(2,0);
  354. break;
  355. case MKEY_VALUE_ESC:
  356. pwrDetRead(1);
  357. break;
  358. case MKEY_VALUE_F1:
  359. if(sutApp.key_f1_cnt==1 && sutUIstatus.Status!=UIS_MENU_SYS_SERVERPASS) sutApp.voluemUpdate=1;
  360. sutApp.key_f1_cnt=0;
  361. break;
  362. case MKEY_VALUE_F2:
  363. if(sutApp.key_f2_cnt==1 && sutUIstatus.Status!=UIS_MENU_SYS_SERVERPASS) sutApp.voluemUpdate=2;
  364. sutApp.key_f2_cnt=0;
  365. break;
  366. case MKEY_VALUE_CB_GT:
  367. gtModeCnt=0;
  368. break;
  369. case MKEY_VALUE_MENU:
  370. if(sutApp.keyLock != 0 && lockCnt==1)//实际无法实现锁短按一下,因为按下去键值就被使用了,不过锁键盘后,都是在待机界面,不影响
  371. sutApp.lockShow=1;
  372. lockCnt=0;
  373. break;
  374. default:
  375. break;
  376. }
  377. sutApp.timeOutCnt=0;
  378. keyInArow=0;
  379. appSleepCtl(ASLEEP_MKEY, 0);
  380. }else{//key press
  381. if(key!=MKEY_VALUE_PTT && key!=MKEY_VALUE_CB_GT&&key!=MKEY_VALUE_MIC_PTT) keyToneStart(0);
  382. backLightReset();//其它所有键都点亮(接口里面有调阻止休眠接口)
  383. appSleepCtl(ASLEEP_MKEY, 1);
  384. keyNum++;
  385. switch(key){
  386. case MKEY_VALUE_MIC_PTT:
  387. pttHandler(0,1);
  388. break;
  389. case MKEY_VALUE_PTT:
  390. pttHandler(1,1);
  391. break;
  392. case MKEY_VALUE_CB_PTT:
  393. pttHandler(2,1);
  394. break;
  395. case MKEY_VALUE_ESC:
  396. keyInArow=1;
  397. pwrDetRead(0);
  398. break;
  399. case MKEY_VALUE_F1:
  400. keyInArow=1;
  401. sutApp.key_f1_cnt++;
  402. if(sutApp.keyLock==0){
  403. if(sutApp.key_f1_cnt==3) quickUiSwitch(UIS_MENU_GROUP_SEL);
  404. }else if(sutApp.key_f1_cnt!=1) sutApp.lockShow=1;
  405. break;
  406. case MKEY_VALUE_F2:
  407. keyInArow=1;
  408. sutApp.key_f2_cnt++;
  409. if(sutApp.keyLock==0){
  410. if(sutApp.key_f2_cnt==3) quickUiSwitch(UIS_MENU_USER_SEL);
  411. }else if(sutApp.key_f2_cnt!=1) sutApp.lockShow=1;
  412. break;
  413. case MKEY_VALUE_CB_GT:
  414. keyInArow=1;
  415. gtModeCnt++;
  416. if(sutApp.keyLock==0){
  417. if(gtModeCnt==3){
  418. if(sutApp.gtMode==0){
  419. enterGTMode();
  420. }
  421. }
  422. }else sutApp.lockShow=1;
  423. break;
  424. case MKEY_VALUE_CB_IP:
  425. keyInArow=1;
  426. break;
  427. case MKEY_VALUE_MENU:
  428. keyInArow=1;
  429. if(++lockCnt==3) lockProcess();
  430. break;
  431. default:
  432. break;
  433. }
  434. }
  435. }
  436. /////////////////////////////事件发送接叿//////////////////////////
  437. void threadSendEvent(LSAPI_OSI_Thread_t *threadID, unsigned int id, unsigned int param1,unsigned int param2,unsigned int param3){
  438. LSAPI_OSI_Event_t pEventSend;
  439. pEventSend.id=id;
  440. pEventSend.param1=param1;
  441. pEventSend.param2=param2;
  442. pEventSend.param3=param3;
  443. osiEventTrySend(threadID,&pEventSend,0);
  444. }
  445. //////////////////////////////其它处理接口//////////////////////////
  446. static void adcRead(unsigned int interval){
  447. #if 1
  448. static unsigned char earStatus=0xff;
  449. unsigned char tmpEar;
  450. int32_t chargeVol;
  451. static unsigned short cnt=0,lcnt=0;
  452. static unsigned int laSiVbat;
  453. unsigned int adcValueTmp;
  454. if(++cnt<(1000/interval)) return;//1秒读一次ADC即可
  455. cnt=0;
  456. LSAPI_SYS_BattGetVol(&adcValueTmp);
  457. sutApp.g_iBAT=adcValueTmp/10;
  458. if(sutApp.g_iBAT == laSiVbat){
  459. laSiVbat=sutApp.g_iBAT;
  460. if(sutApp.g_iBAT<=MIN_PWR_LEVEL){
  461. MSG_WARN(1,"Low power:%d", lcnt++);
  462. if(lcnt>=10){
  463. MSG_INFO(1, "Low power ,shut");
  464. sutApp.lowPwrMsg=1;
  465. appSleepCtl(ASLEEP_PWR, 1);
  466. }
  467. }else lcnt=0;
  468. }
  469. //读取充电/耳机接入状态 //不知道为啥是LSAPI_Device_ADC_CHANNEL_3,而不是LSAPI_Device_ADC_CHANNEL_1
  470. chargeVol=LSAPI_Device_AdcGetChannelVolt(LSAPI_Device_ADC_CHANNEL_3, LSAPI_Device_ADC_SCALE_3V233);
  471. if(chargeVol<=800){//耳机插入,没在充电
  472. tmpEar=0;
  473. sutApp.chargeStatus=0;
  474. }else if(chargeVol<=1000){//耳机插入,正在充电
  475. tmpEar=0;
  476. sutApp.chargeStatus=1;
  477. }else if(chargeVol<=1400){//耳机拔出,没在充电
  478. tmpEar=1;
  479. sutApp.chargeStatus=0;
  480. }else{//耳机拔出,正在充电
  481. tmpEar=1;
  482. sutApp.chargeStatus=1;
  483. }
  484. if(tmpEar != earStatus){//耳机状态发生变化
  485. earStatus=tmpEar;
  486. tmpEar += 0x30;
  487. proHeadSet(&tmpEar);
  488. }
  489. #else//这里ADC太频繁调用ADC接口,有概率会影响poc播放tone音效果
  490. #define VBAT_READ_TIME 20
  491. unsigned int adcValueTmp;
  492. static unsigned int siVbat=0,laSiVbat;
  493. static unsigned char index=0;
  494. LSAPI_SYS_BattGetVol(&adcValueTmp);
  495. siVbat += adcValueTmp;
  496. if(++index>=VBAT_READ_TIME){
  497. sutApp.g_iBAT=siVbat / VBAT_READ_TIME / 10;
  498. if(laSiVbat!=sutApp.g_iBAT){
  499. //MSG_INFO(1, "bat:%d", sutApp.g_iBAT);
  500. laSiVbat=sutApp.g_iBAT;
  501. if(sutApp.g_iBAT<=MIN_PWR_LEVEL){
  502. MSG_INFO(1, "Low power ,shut");
  503. sutApp.lowPwrMsg=1;
  504. appSleepCtl(ASLEEP_PWR, 1);
  505. }
  506. }
  507. siVbat=0;
  508. index=0;
  509. }
  510. #endif
  511. }
  512. /*处理UI操作*/
  513. static void uiProcess(unsigned int exeInterval){
  514. static int cnt=0;
  515. if(getAppObjStatus(ASLEEP_PWR) != 0) return;//关机后,不切换菜单,只显示关机
  516. //uiLoop(exeInterval);
  517. uiResponse(exeInterval);
  518. }
  519. /*触发关机操作*/
  520. static void pwrShutPro(void){
  521. static char shutFlag=0;
  522. if(getAppObjStatus(ASLEEP_PWR) == 0) return;
  523. lcdBackLightApi(1);
  524. msgAtSend("AT+POC=050000\r\n");
  525. msgAtSend("AT+CFUN=0\r\n");
  526. if(shutFlag==0){
  527. //显示关机界面
  528. if(sutApp.lowPwrMsg==0) guiShowMessageBox("正在关机");
  529. else guiShowMessageBox("低压关机");
  530. shutFlag=1;
  531. LSAPI_OSI_ThreadSleep(1000);
  532. guiShowBmp(0,0,"BYEBYE.bmp");
  533. LSAPI_OSI_ThreadSleep(1000);
  534. }
  535. MSG_INFO(1, "Normal PwrOff");
  536. //msgAtSend("AT+TRB=1\r\n");//normal power off 当前固件先不发模块关机指令,否则断电再上电有可能是起不来皿
  537. //LSAPI_OSI_ThreadSleep(1000);//一秒后释放电源锿防止软关机不成功
  538. lcdBackLightApi(0);
  539. MSG_INFO(1, "Release pwr");
  540. guiFillRect(0,0,GLCD_WIDTH-1,GLCD_HEIGHT-1,guiGetBackColor());//防止下次开机能看到残留
  541. msgAtSend("AT+CPOF\r\n");//软件关机模块
  542. LSAPI_OSI_ThreadSleep(100);
  543. CTL_POWER_HOLD(false);
  544. }
  545. static void NoteCheck(int interval){
  546. int dlyMs;
  547. if(sutApp.lowPwrWarn!=0){
  548. sutApp.lowPwrCnt += interval;
  549. if(sutPocStatus.spk==0 && sutPocStatus.mic==0){//对计状态下不播放请充电
  550. if(sutApp.lowPwrCnt>=WARN_TTS_INTERVAL){
  551. sutApp.lowPwrCnt=0;
  552. ttsPlay(ENCODE_USER_GBK, "C7EBB3E4B5E7");
  553. }
  554. }
  555. }
  556. }
  557. void AntModeRun(void){
  558. #ifdef ENABLE_ANT_MODE
  559. guiShowMessageBox("调天线模式");
  560. for(;;){
  561. LSAPI_OSI_ThreadSleep(2000);
  562. }
  563. #endif
  564. }
  565. static void paProCtl(void);
  566. static void subTimerCtl(unsigned char sleep_or_not);
  567. static void configureVoiceParam(void);
  568. static void startPocLogin(void){
  569. //if(sutApp.startPoc==0) return;
  570. MSG_INFO(1, "Start poc");
  571. sutApp.startPoc=0;
  572. msgAtSend("AT+POC=startup011");//打开对讲,关闭POC播放TTS //msgAtSend("AT+POC=0000000101\r\n");
  573. }
  574. void pocSetDomain(void){//设置域名前缀
  575. const char platform[]="platform2";
  576. const char pFormatMark[]="chn-gwsd-";
  577. char info[100]={0};
  578. char tbuf[50];
  579. char len =0;
  580. // char* platform=0;
  581. // char* pFormatMark=0;
  582. // platform=sutApp.UserInfo.platform;
  583. // pFormatMark=sutApp.UserInfo.pFormatMark;
  584. snprintf(tbuf, sizeof(tbuf), "%s&%s", platform,pFormatMark);
  585. len =strlen(tbuf);
  586. snprintf(info, sizeof(info), "AT+POC=setdns%02x%s",len ,tbuf);
  587. msgAtSend(info);
  588. MSG_INFO(0, "%s",info);
  589. }
  590. void appRun(void){
  591. char cnt=0;
  592. LSAPI_OSI_Event_t event={0};
  593. //等待lcd初始化完房
  594. MSG_INFO(1, "wait lcd init");
  595. while(sutApp.guiStatus==0){LSAPI_OSI_ThreadSleep(50);}//是否检测超时?
  596. // AntModeRun();
  597. // localAuthNow();
  598. sutApp.authReady=1;
  599. newPara.lcdParaDefaultIndex=4;
  600. //等待poc启动完成
  601. MSG_INFO(1, "lcd init done, wait poc start");
  602. // while(sutApp.pocInitStatus==0){
  603. // msgAtSend("ATE0\r\n");
  604. // LSAPI_OSI_ThreadSleep(1000);
  605. // }//是否检测超时?
  606. MSG_INFO(1, "app loop start");
  607. msgToModem("AT+AUDCH=0,0\r\n");//此机型只使用主通道
  608. //设置一下TTS语音速度
  609. msgToModem("AT+LSHTTSPARAM=3000,106,100\r\n");//设置TTS参数
  610. //查询一下POC版本叿
  611. msgToModem("AT+SIMCROSS?\r\n");
  612. msgAtSend("AT+POC=version\r\n");//发指令后等待POC启动
  613. // configureVoiceParam();
  614. // adcRead(APP_SUB_DIV_TIME);//在开机播放前设置下SPK
  615. // splVolumeSet(newPara.spkVol*10);//更新音量
  616. backLightReset();
  617. newGuiNoteBoot(1);
  618. // //检测卡是否存在
  619. // while(sutApp.cardStatus==0){
  620. // msgAtSend("AT+CCID\r\n");
  621. // LSAPI_OSI_ThreadSleep(500);
  622. // if(++cnt>=10){
  623. // MSG_INFO(1, "Check card timeout");
  624. // break;
  625. // }
  626. // }
  627. sutApp.cardStatus=1;
  628. LSAPI_OSI_ThreadSleep(3000);
  629. msgAtSend("AT+POC=poccmd1");
  630. LSAPI_OSI_ThreadSleep(3000);
  631. pocSetDomain();
  632. setPocPara();
  633. LSAPI_OSI_ThreadSleep(1000);
  634. startPocLogin();
  635. msgToModem("AT+CLVL=100\r\n");
  636. for(;;){
  637. ledsProCtl();
  638. adcRead(APP_SUB_DIV_TIME);
  639. uiProcess(APP_SUB_DIV_TIME);
  640. pocProCtl(APP_SUB_DIV_TIME);
  641. NoteCheck(APP_SUB_DIV_TIME);
  642. uiTimeOutToStandby(0, APP_SUB_DIV_TIME);
  643. HookGroupUserInfo();
  644. LogoutOutTimeCheck(APP_SUB_DIV_TIME);
  645. if(isSleepReady()==0) LSAPI_OSI_ThreadSleep(APP_TASK_SLEEP_TIME);
  646. else if(sutApp.gtMode==0){//正常模式下可以休眿
  647. MSG_WARN(1, "APP SLEEP");
  648. uiTimeOutToStandby(1, APP_SUB_DIV_TIME);
  649. stopKeyTimer();
  650. subTimerCtl(1);//切换为休眠状态定时器
  651. sutApp.appStatus=1;
  652. redLedCtl(false);//休眠后保证灯没亮,否则有可能灯亮10ms,按理10ms后灯能灭,然后10ms时休眠了,应用就短时间内不会灭灯
  653. greenLedCtl(false);//休眠后保证灯没亮
  654. sutApp.updateStatusBar=1;
  655. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  656. LSAPI_OSI_ThreadSleep(150);//添加这个后,休眠时按键或被呼叫或本机呼叫,tone音能是完整的
  657. lcdDrv_Init(1);//深度休眠唤醒后重新初始化SPI-LCD
  658. if(sutApp.lcdDlyLightUp){
  659. sutApp.lcdDlyLightUp=0;
  660. lcdBackLightApi(1);
  661. }
  662. sutApp.forceUiFlash=1;
  663. sutApp.appStatus=0;
  664. subTimerCtl(0);//切换为唤醒状态定时器
  665. MSG_WARN(1, "APP WAKEUP");
  666. }
  667. //检测是否关机,这里要放在唤醒后操作
  668. pwrShutPro();
  669. }
  670. }
  671. unsigned int subTimerInterval=100;//定时器定时的时间
  672. LSAPI_OSI_Timer_t *pSubtimer_t = NULL;
  673. /*
  674. 为了做低功耗,此定时器在休眠时,设置为长时间定旿
  675. 非休眠时,设置为短时间定旿
  676. sleep_or_not:0 未休眠, else 休眠
  677. */
  678. static void subTimerCtl(unsigned char sleep_or_not){
  679. if(NULL==pSubtimer_t){
  680. MSG_WARN(1, "subTimer null");
  681. return;
  682. }
  683. LSAPI_OSI_TimerStop(pSubtimer_t);
  684. //打开定时噿
  685. if(sleep_or_not==0) subTimerInterval=100;//未休眠时_00ms
  686. else subTimerInterval=5000;//休眠旿5000ms
  687. LSAPI_OSI_TimerStart(pSubtimer_t,subTimerInterval);
  688. }
  689. static void subTimeroutcallback(void *param){
  690. unsigned int *dlyTime=(unsigned int *)param;
  691. //处理灯的显示
  692. uioProctl(*dlyTime);
  693. //控制功放的关闿
  694. paProCtl();
  695. if(NULL!=pSubtimer_t) LSAPI_OSI_TimerStart(pSubtimer_t,subTimerInterval);
  696. }
  697. void subTask(void *param){
  698. LSAPI_OSI_Event_t event={0};
  699. if(NULL==pSubtimer_t) pSubtimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), subTimeroutcallback, (void *)&subTimerInterval);
  700. if(NULL==pSubtimer_t) MSG_WARN(1,"sub timer init failed");
  701. else LSAPI_OSI_TimerStart(pSubtimer_t,subTimerInterval);
  702. for(;;){LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);}
  703. }
  704. static void paProCtl(void){
  705. //会被定时调用
  706. if(sutPocStatus.TTS!=0) return;
  707. if(sutPocStatus.spk!=0) return;
  708. if(sutPocStatus.TONE!=0) return;
  709. if(sutPocStatus.beep!=0) return;
  710. if(sutApp.gtMode!=0) return;
  711. paControl(false);
  712. //MSG_INFO(1,"spk off");
  713. }
  714. void tryWakeupApp(void){
  715. if(sutApp.appStatus!=0){
  716. if(mainThreadPtr!=NULL){
  717. threadSendEvent(mainThreadPtr,OHPOC_EVENT_MAIN,NULL,NULL,NULL);
  718. }
  719. }
  720. }
  721. void ttsPlay(ENCODE_USER_ENUM type, char *tts){
  722. int len,i;
  723. char ttsMessage[20*4];
  724. char Playbuf[100]={0};
  725. unsigned char codeType=ENCODE_USER_GBK;
  726. if(newPara.ttsMessage[0]==0) return;
  727. len=strlen(newPara.ttsMessage);
  728. len = len+(8-len%8);//
  729. MSG_INFO(1,"tts->");
  730. if(type==ENCODE_USER_UNICODE_BE)
  731. {
  732. codeType=LSAPI_TTS_UNICODE_BIG;
  733. for(i=0;i<len;i+=4){
  734. ttsMessage[i]=newPara.ttsMessage[i];
  735. ttsMessage[i+1]=newPara.ttsMessage[i+1];
  736. ttsMessage[i+2]=newPara.ttsMessage[i+2];
  737. ttsMessage[i+3]=newPara.ttsMessage[i+3];
  738. MSG_INFO(0,"%c",ttsMessage[i]);
  739. MSG_INFO(0,"%c",ttsMessage[i+1]);
  740. MSG_INFO(0,"%c",ttsMessage[i+2]);
  741. MSG_INFO(0,"%c",ttsMessage[i+3]);
  742. }
  743. ttsMessage[len-1]=0;
  744. StrAsciiToHex(ttsMessage,Playbuf);
  745. //MSG_INFO(1,"");
  746. }else if(type==ENCODE_USER_GBK) {
  747. codeType=LSAPI_TTS_GBK;
  748. //StrAsciiToHex(newPara.ttsMessage,Playbuf);
  749. StrAsciiToHex(tts,Playbuf);
  750. MSG_INFO(1, "tts Play:%d,%s",codeType,tts);
  751. }else if(type==ENCODE_USER_ASCII){
  752. codeType=LSAPI_TTS_GBK;
  753. strncpy(Playbuf, tts, strlen(tts));
  754. }
  755. paControl(true);
  756. sutPocStatus.TTS=1;
  757. // OEM_TTS_Spk(int type, char * atxt);
  758. LSAPI_TTS_Play(Playbuf,strlen(Playbuf),codeType);
  759. }
  760. /*背光时间到,熄灭*/
  761. void backLightCb(void *param){
  762. if(sutPocStatus.firstLogin==0) return;//未登录过,不灭屏
  763. if(newPara.lcdParaList[newPara.lcdParaDefaultIndex]==0) return;//常亮
  764. lcdBackLightApi(0);
  765. appSleepCtl(ASLEEP_LCD, 0);//可以休眠
  766. }
  767. /*点亮背光*/
  768. void backLightReset(void){
  769. unsigned short tmp;
  770. appSleepCtl(ASLEEP_LCD, 1);//不让休眠
  771. tmp=newPara.lcdParaList[newPara.lcdParaDefaultIndex];
  772. if(sutApp.appStatus==0) lcdBackLightApi(1);//未休眠时,直接点亮
  773. else sutApp.lcdDlyLightUp=1;//休眠时不马上亮亮,会在LCD初始化完后点亮
  774. startBackLightTimer(tmp*1000);
  775. }
  776. void sysPwrLock(void){
  777. sutApp.pwrLock=1;
  778. }
  779. void sysPwrRlease(void){
  780. sutApp.pwrLock=0;
  781. }
  782. //"3132" --> 0x31,0x32
  783. void StrAsciiToHex(char *src, unsigned char *des){
  784. unsigned char temp[2],i;
  785. if(strlen(src)%2) return;
  786. while(0!=*src)
  787. {
  788. for(i=0;i<2;i++)
  789. {
  790. temp[i] = *src++;
  791. if(temp[i] >= '0' && temp[i] <= '9') temp[i] -= 0x30;
  792. else if(temp[i] >= 'A' && temp[i] <= 'F') temp[i] -= 0x37;
  793. else temp[i] -= 0x57;
  794. }
  795. temp[0] <<= 4;
  796. temp[0] &= 0xf0;
  797. *des++=temp[0]|temp[1];
  798. }
  799. *des=0;
  800. }
  801. void splVolumeSet(unsigned char level){
  802. char info[30];
  803. snprintf(info, sizeof(info), "AT+CLVL=%d\r\nAT+CRSL=%d\r\n", level,level);
  804. msgAtSend(info);
  805. MSG_INFO(1,"SPK_VOL:%d",level);
  806. }
  807. const unsigned char spkGainTable[SPK_GAIN_NUM];
  808. /*调节音量,刷新音量进度条*/
  809. void volAdjAction(char direction,unsigned char UIStatus){
  810. unsigned char vol;
  811. if(sutApp.earLev==0){//不接耳机
  812. if(direction==0){
  813. if(newPara.spkVol<MAX_SPK_VOL) newPara.spkVol++;
  814. else goto FLASH;
  815. }else{
  816. if(newPara.spkVol>0) newPara.spkVol--;
  817. else goto FLASH;
  818. }
  819. vol=newPara.spkVol*10;
  820. splVolumeSet(vol);
  821. }else{//接了耳机
  822. if(direction==0){
  823. if(newPara.spkGain<SPK_GAIN_NUM) newPara.spkGain++;
  824. else goto FLASH;
  825. }else{
  826. if(newPara.spkGain>0) newPara.spkGain--;
  827. else goto FLASH;
  828. }
  829. if(newPara.spkGain==0) vol=0;
  830. else vol=spkGainTable[newPara.spkGain-1];
  831. splVolumeSet(vol);
  832. vol=newPara.spkGain;
  833. }
  834. sysIniSave();
  835. //刷新进度条
  836. FLASH:
  837. if(sutApp.earLev==0) vol=newPara.spkVol;
  838. else vol=newPara.spkGain;
  839. //if(UIS_STANDBY==UIStatus) volUiFlash(vol);
  840. }
  841. void volUpdateNeed(void){
  842. if(sutApp.voluemUpdate==0) return;
  843. if(sutApp.voluemUpdate==1) volAdjAction(0, sutUIstatus.Status);
  844. else if(sutApp.voluemUpdate==2) volAdjAction(1, sutUIstatus.Status);
  845. sutApp.voluemUpdate=0;
  846. }
  847. const unsigned char spkGainTable[SPK_GAIN_NUM]={
  848. 24,32,40,48,56,64,72,80
  849. };
  850. void VolSpkUpdate(void){
  851. char cmd[30];
  852. unsigned char volLev;
  853. if(sutApp.earLev==0){//耳机拔掉后,恢复正常的SPK增益
  854. volLev=newPara.spkVol*10;
  855. }else{//耳机接入,应用特定值
  856. if(newPara.spkGain>=SPK_GAIN_NUM) volLev=SPK_GAIN_DEFAULT;
  857. else volLev=newPara.spkGain;
  858. if(volLev>0) volLev=spkGainTable[newPara.spkGain-1];
  859. }
  860. splVolumeSet(volLev);
  861. }
  862. //对都使用主音频通道设计来说,耳机模式时,需要按比例降低输入输出增益
  863. const unsigned short micGainTable[MIC_GAIN_NUM][2]={
  864. {2,6},{2,10},{3,10},{4,10},{5,10},{6,10},{6,13}
  865. };
  866. void VolMicUpdate(void){
  867. #if 1
  868. char cmd[50];
  869. unsigned char hana,lana,hadc,ladc;
  870. if(sutApp.earLev==0){//耳机拔掉,恢复正常MIC增益
  871. hana=sutApp.mic_ana>>8;
  872. lana=sutApp.mic_ana;
  873. hadc=sutApp.mic_adc>>8;
  874. ladc=sutApp.mic_adc;
  875. }else{//耳机接入
  876. hana=micGainTable[newPara.micGain][0]>>8;
  877. lana=micGainTable[newPara.micGain][0];
  878. hadc=micGainTable[newPara.micGain][1]>>8;
  879. ladc=micGainTable[newPara.micGain][1];
  880. }
  881. snprintf(cmd, sizeof(cmd), "AT+CACCP=0,0,0,\"%02x%02x%02x%02x\"\r\n",lana,hana,ladc,hadc);
  882. msgAtSend(cmd);
  883. #else
  884. unsigned char tableIndex;
  885. char cmd[50];
  886. int vol=newPara.spkVol*10;
  887. splVolumeSet(vol);
  888. if(sutApp.earLev==0){//耳机拔掉,恢复正常MIC增益
  889. tableIndex=newPara.micGain_onspk;
  890. //msgAtSend("AT+CACCP=0,0,0,\"04000a00\"\r\n");
  891. }else{//耳机接入
  892. tableIndex=newPara.micGain_onear;
  893. //msgAtSend("AT+CACCP=0,0,0,\"02000f00\"\r\n");
  894. }
  895. if(tableIndex>=MIC_GAIN_NUM){
  896. MSG_WARN(1, "Index invalid:%d", tableIndex);
  897. return;
  898. }
  899. snprintf(cmd, sizeof(cmd), "AT+CACCP=0,0,0,\"%02x00%02x00\"\r\n", micGainTable[tableIndex][0],micGainTable[tableIndex][1]);
  900. msgAtSend(cmd);
  901. #endif
  902. }
  903. //开机配置设定好的模块音频参数文件
  904. #define READ_PER_SIZE 400 //每次从Bin文件读取最多400字节来发送,AT时长度翻倍
  905. static void configureVoiceParam(void){
  906. //const char *confiFile="ZT-MINI101HGS.bin";
  907. const char *confiFile="ZT-MINI201-202HGS-20210703.bin";
  908. unsigned char sendbuf[40+2*400];//AT+CAIET=0,0,9600,400,""
  909. unsigned char *dataPtr=sendbuf+sizeof(sendbuf)-READ_PER_SIZE;
  910. char buf[3];
  911. char status=0;
  912. int fd,offset,readsize,i;
  913. LSAPI_FS_Stat_info_t pBuf;
  914. MSG_INFO(1, "config audio file'%s'",confiFile);
  915. fd=LSAPI_FS_Open(confiFile, LSAPI_FS_O_RDONLY,0x0);
  916. if(fd>0){
  917. memset(&pBuf,0,sizeof(pBuf));
  918. LSAPI_FS_Fstat(fd,&pBuf);
  919. offset=0;
  920. LSAPI_FS_Seek(fd, offset, LSAPI_FS_SEEK_SET);
  921. while(offset<pBuf.st_size){
  922. readsize=pBuf.st_size-offset;
  923. if(readsize>400) readsize=400;
  924. readsize=LSAPI_FS_Read(fd, dataPtr, readsize);
  925. if(readsize==0) break;
  926. else if(readsize<0){
  927. status=1;
  928. break;
  929. }
  930. sprintf(sendbuf, "AT+CAIET=0,0,%d,%d,\"", offset, readsize);
  931. for(i=0;i<readsize;i++){
  932. sprintf(buf, "%02x", dataPtr[i]);
  933. strcat(sendbuf, buf);
  934. }
  935. strcat(sendbuf,"\"\r\n");
  936. msgAtSend(sendbuf);
  937. offset += readsize;
  938. LSAPI_OSI_ThreadSleep(10);
  939. }
  940. LSAPI_FS_Close(fd);
  941. if(status==0) MSG_INFO(1, "config audio file succ:%d",offset);
  942. else MSG_ERR(1, "config audio failed %d",status);
  943. }else MSG_ERR(1, "'%s' open failed",confiFile);
  944. if(status==0){//配置成功后,回读一下默认的Mic,这个值在不接耳机时是固定值
  945. LSAPI_OSI_ThreadSleep(50);
  946. msgAtSend("AT+CACCP=0,0,1,\"\"\r\n");
  947. LSAPI_OSI_ThreadSleep(50);
  948. }else{
  949. //如果配置失败了,直接使用默认值
  950. sutApp.mic_ana=4;
  951. sutApp.mic_adc=4;//12;
  952. }
  953. VolMicUpdate();
  954. }
  955. //+CACCP:
  956. //+CACCP: "04000C00" ana=0x0004 adc=0x000c
  957. void proCaccpMsg(char *msg){
  958. char tmp[10],*pName;
  959. char i=0,j=0;
  960. if(msg[0]=='"'){
  961. memcpy(tmp+j, msg+7,2);j += 2;
  962. memcpy(tmp+j, msg+5,2);j += 2;
  963. memcpy(tmp+j, msg+3,2);j += 2;
  964. memcpy(tmp+j, msg+1,2);j += 2;
  965. tmp[j]=0;//000C0004
  966. sutApp.mic_ana=strtol(tmp+4, &pName,16);
  967. tmp[4]=0;
  968. sutApp.mic_adc=strtol(tmp, &pName,16);
  969. }
  970. }