#include "includes.h" //LSAPI_OSI_Timer_t *BleTimer_t=NULL; void gpioConfigInput(); //////////////////////封装一下AT接口///////////////////////////////// void msgAtSend(char *at){ ModemSendAt(at); } void msgAtSends(char *at ,int len){ ModemSendAt(at); } //////日志打印口配置///////////////// void my_usb_evt_cb(lsUSBEvent_t event, unsigned long p){ LSAPI_OSI_Event_t usb_event; usb_event.id = OHPOC_EVENT_USB; usb_event.param1=event; //wlog_info("my_usb_evt_cb:%d",event); osiEventTrySend((LSAPI_OSI_Thread_t *)p, &usb_event, 0); } LSAPI_Device_USBConfig_t usb_cfg = { .name = LSAPI_DEV_USB_COM7, .event_cb = my_usb_evt_cb, }; bool usbThreadExit=false; LSAPI_Device_t *usbdev=NULL; LSAPI_OSI_Thread_t *gUsbWRTd; static void usbPortHandler(void *param){ int ret; unsigned char buffer[100]; LSAPI_OSI_Event_t waitevent; LSAPI_Log_Debug("usb create ok"); while(usbThreadExit==false){ LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &waitevent); if(waitevent.param1==LS_USB_EVENT_RX_ARRIVED){ while(LSAPI_Device_ReadAvail(usbdev)>0){ ret = LSAPI_Device_Read(usbdev, buffer, sizeof(buffer)); if(ret< sizeof(buffer)) buffer[ret]=0; usbCmdHandler(buffer,ret); } //WakeupNow("usbCallBack"); } } LSAPI_Log_Debug("usbPortHandler exit"); LSAPI_Device_Close(usbdev); gUsbWRTd=NULL; LSAPI_OSI_ThreadExit(); } static bool logPortInit(void){ gUsbWRTd=LSAPI_OSI_ThreadCreate("usb", usbPortHandler, NULL, LSAPI_OSI_PRIORITY_NORMAL, 1024*32, 4); if(gUsbWRTd==NULL) return false; usb_cfg.event_cb_ctx = gUsbWRTd; usbdev = LSAPI_Device_USBCreateEx(&usb_cfg); if(usbdev==NULL){ usbThreadExit=true; LSAPI_Log_Debug("usbex create err"); return false; } if(false==LSAPI_Device_Open(usbdev)){ LSAPI_Log_Debug("usb LSAPI_Device_Open failed"); return false; } LSAPI_Device_SetAutoSleep(usbdev, 500); LSAPI_Log_Debug("usbComInit done"); return true; } void usbOutPut(unsigned char *info, int len){ if(NULL==usbdev) return; if(LSAPI_Device_WriteAvail(usbdev) != -1) LSAPI_Device_Write(usbdev, info,len); } void usbOutPutStr(unsigned char *info){ if(NULL==usbdev) return; if(LSAPI_Device_WriteAvail(usbdev) != -1) LSAPI_Device_Write(usbdev, info,strlen(info)); } static void lcdInitTask(void *param){ //MSG_WARN(1,"lcd init thread start"); if(0==lcdDrv_Init(0)) MSG_INFO(1, "lcd init ok"); else MSG_INFO(1, "lcd init failed"); guiInit(); MSG_WARN(1,"lcd init thread end"); sutApp.guiStatus=1; LSAPI_OSI_ThreadExit(); } static void lcdsInit(void){ if(NULL==LSAPI_OSI_ThreadCreate("lcdinit", lcdInitTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, LCD_INIT_TASK_THREAD_STACK, 2)){ MSG_WARN(1,"lcd init thread error"); return; } } //////////////////gpio及定时器///////////////////////////// LSAPI_OSI_Timer_t *pKeytimer_t=NULL; LSAPI_OSI_Timer_t *pBackLightTimer_t=NULL; LSAPI_OSI_Timer_t *pVibTimer_t=NULL; bool pkeyTimerCtl=false; unsigned short keyStatus=0; unsigned short getKeyValue(){ unsigned char encodekey=getEncodeStatus(); have_encode=1; if(ENCODE_SHUN==encodekey){ keyStatus=MKEY_VALUE_DOWN; } else if(ENCODE_NI==encodekey){ keyStatus=MKEY_VALUE_UP; } else have_encode=0; return keyStatus; } unsigned short SetZeroKeyValue(){ keyStatus=0; } //规避龙尚驱动矩阵不足问题 void clearKeyValue(){ keyStatus=0; if(keyStatus | MKEY_VALUE_PTT){//规避组合键出现PTT一直按下状态问题 if(sutApp.pttReq){ sutApp.pttReq=0; msgAtSend("AT+POC=0C0000\r\n"); } } } static void startKeyTimer(void){ if(NULL==pKeytimer_t) return; if(pkeyTimerCtl==true) return; pkeyTimerCtl=true; LSAPI_OSI_TimerStart(pKeytimer_t,KEY_TIMER_TICK); } void stopKeyTimer(void){ pkeyTimerCtl=false; LSAPI_OSI_TimerStop(pKeytimer_t); } static unsigned int msTick=0;//用于做应用层的非休眠时定时器 unsigned int getAppTick(void){return msTick;} static void AppTimeroutcallback(void *param){ //毿00秒去检测键值状怿去抖势 //检测按锿 keyCheck(keyStatus); msTick ++; //EarDlyCheck(); if(pkeyTimerCtl==true) LSAPI_OSI_TimerStart(pKeytimer_t,KEY_TIMER_TICK); } LSAPI_Device_t *SpkGpio; LSAPI_Device_t *PwrHoldGpio; static LSAPI_Device_t * RedGpio = NULL; static LSAPI_Device_t * GreenGpio = NULL; //#define RED_GPIO_ID 23 //#define GREEN_GPIO_ID 18 #define SPK_GPIO_ID 19 //19 #define PWR_HOLD_ID 22 void gpioConfigOutput(void){ bool write_value = false; LSAPI_GpioConfig_t gpioConfig = {0}; // config property gpioConfig.mode = LS_GPIO_OUTPUT; gpioConfig.out_level=true; // create instance RedGpio=LSAPI_Device_LedCreate(LS_LED_2); GreenGpio=LSAPI_Device_LedCreate(LS_LED_1); if(LSAPI_Device_Open(RedGpio)==false){ MSG_INFO(1,"RedGpio---fail"); } if(LSAPI_Device_Open(GreenGpio)==false){ MSG_INFO(1,"GreenGpio----fail"); } gpioConfig.id = SPK_GPIO_ID; SpkGpio = LSAPI_Device_GPIOCreate(&gpioConfig); gpioConfig.id = PWR_HOLD_ID; PwrHoldGpio = LSAPI_Device_GPIOCreate(&gpioConfig); LSAPI_Device_Open(SpkGpio); LSAPI_Device_Write(SpkGpio, (void *)&write_value, 1); LSAPI_Device_Open(PwrHoldGpio); write_value=true;//启动成功后,默认把电源给锁稳 LSAPI_Device_Write(PwrHoldGpio, (void *)&write_value, 1); gpioBleConfigOutput(); gpioConfigInput(); redLedCtl(write_value); } #define ENCODE1_GPIO_ID 18 #define ENCODE2_GPIO_ID 23 static LSAPI_Device_t * Encode1Ctl = NULL; static LSAPI_Device_t * Encode2Ctl = NULL; unsigned char g_iEncode=ENCODE_IDLE; unsigned char g_iStatus=0; unsigned char g_iLastStatus=0; void encodeReleased(void){ g_iEncode=ENCODE_IDLE; } LSAPI_GpioIntrCB_t inputCB_Encode(void *ctx){ bool encode1,encode2; bool encodeValid1=false,encodeValid2=false; if(NULL != Encode1Ctl){ if (1 == LSAPI_Device_Read(Encode1Ctl, (void *)&encode1, 1)) encodeValid1=true; } if(NULL != Encode2Ctl){ if (1 == LSAPI_Device_Read(Encode2Ctl, (void *)&encode2, 1)) encodeValid2=true; } if(encodeValid1==true){ if(encode1==true) g_iStatus |= (1<<0); else g_iStatus &= ~(1<<0); } if(encodeValid2==true){ if(encode2==true) g_iStatus |= (1<<1); else g_iStatus &= ~(1<<1); } MSG_INFO(0, "g_iStatus======%d",g_iStatus); if(encodeValid1==true || encodeValid2==true){ if(g_iEncode==ENCODE_IDLE){ switch(g_iStatus){ case 0: if(g_iLastStatus==1) g_iEncode=ENCODE_NI; else if(g_iLastStatus==2) g_iEncode=ENCODE_SHUN; else if(g_iLastStatus==0) g_iEncode=ENCODE_SHUN; break; case 3: if(g_iLastStatus==1) g_iEncode=ENCODE_SHUN; else if(g_iLastStatus==2) g_iEncode=ENCODE_NI; else if(g_iLastStatus==3) g_iEncode=ENCODE_NI; break; } if(g_iEncode) encodeSendEvent(g_iEncode); } MSG_INFO(0, "\r\n",g_iEncode);//这个不要去掉,否则按一个方向,偶尔还是会反向一下 g_iLastStatus=g_iStatus; } } void gpioConfigInput(){ LSAPI_GpioConfig_t gpioConfig = {0}; gpioConfig.mode = LS_GPIO_INPUT; gpioConfig.intr_enabled=true; gpioConfig.intr_level=false; gpioConfig.rising=true; gpioConfig.falling=true; gpioConfig.cb_ctx = NULL; gpioConfig.id = ENCODE1_GPIO_ID; gpioConfig.cb=inputCB_Encode; Encode1Ctl = LSAPI_Device_GPIOCreate(&gpioConfig); LSAPI_Device_Open(Encode1Ctl); gpioConfig.id = ENCODE2_GPIO_ID; gpioConfig.cb=inputCB_Encode; Encode2Ctl = LSAPI_Device_GPIOCreate(&gpioConfig); LSAPI_Device_Open(Encode2Ctl); } void pwrHoldCtl(bool onoff){ bool write_value = onoff; LSAPI_Device_Write(PwrHoldGpio, (void *)&write_value, 1); } void redLedCtl(bool onoff){ bool write_value = onoff; LSAPI_Device_Write(RedGpio, (void *)&write_value, 1); } void greenLedCtl(bool onoff){ bool write_value = onoff; LSAPI_Device_Write(GreenGpio, (void *)&write_value, 1); } void spkCtl(bool onoff){ bool write_value = onoff; LSAPI_Device_Write(SpkGpio, (void *)&write_value, 1); } //void lcdBackList(char onoff){ // bool write_value = onoff; // //LSAPI_Device_Write(LcdBlGpio, (void *)&write_value, 1); // LSAPI_PmuSwitchPower(OSI_MAKE_TAG('C', 'A', 'M', 'D '), onoff, false); //} void VibCb(void *param){ VibCtrl(0); LSAPI_OSI_TimerStop(pVibTimer_t); } void startBackLightTimer(int time){ if(NULL==pBackLightTimer_t) return; LSAPI_OSI_TimerStart(pBackLightTimer_t,time); } static void gpioTask(void *param){ unsigned int param1,param2,param3; LSAPI_OSI_Event_t event = {}; //创建一个定时器 if(NULL == pKeytimer_t) pKeytimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), AppTimeroutcallback, NULL); if(NULL == pKeytimer_t) MSG_WARN(1,"gpio timer init failed"); if(NULL == pBackLightTimer_t) pBackLightTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), backLightCb, NULL); if(NULL == pBackLightTimer_t) MSG_WARN(1,"backlight timer init failed"); if(NULL == pVibTimer_t) pVibTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), VibCb, NULL); if(NULL == pVibTimer_t) MSG_WARN(1,"Vib timer init failed"); // if(NULL == BleTimer_t) BleTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), bleCb, NULL); // if(NULL == BleTimer_t) MSG_WARN(1,"ble timer init failed"); startKeyTimer(); gpioConfigOutput(); uioStatusInit(); for(;;){ LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event); param1=event.param1; param2=event.param2; param3=event.param3; if(event.id==OHPOC_EVENT_KEY_NOR) keyHandler((unsigned short)param1,(unsigned char)param2); else if(event.id==OHPOC_EVENT_ENCODE) encodeHandler((unsigned short)param1); } } LSAPI_OSI_Thread_t *gGpioTd=NULL; static void gpiosInit(void){ gGpioTd=LSAPI_OSI_ThreadCreate("gpio", gpioTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, 1024*32, 5); if(NULL==gGpioTd) MSG_WARN(1,"gpioThread Error"); else MSG_WARN(1,"gpioThread ok"); } void keySendEvent(unsigned short key, unsigned char keyType){ threadSendEvent(gGpioTd,OHPOC_EVENT_KEY_NOR, key,keyType,NULL); tryWakeupApp(); } void encodeSendEvent(unsigned short encode){ threadSendEvent(gGpioTd,OHPOC_EVENT_ENCODE, encode,NULL,NULL); tryWakeupApp(); } //////////////////按键实始化////////////////////////// #define POWER_KEY_PRESSED_TIMEOUT (3000) typedef struct ls_key{ bool ready_shutdown; LSAPI_OSI_Timer_t *key_timer; }lsKey_t; static void _lsKeyTimer(void *p){ lsKey_t *key = (lsKey_t *)p; key->ready_shutdown = true; } static void _lsKeyCB(lskeyMap_t id, lskeyState_t evt, void *p){ lsKey_t *key = (lsKey_t *)p; uint8_t status; if (evt & LS_KEY_STATE_PRESS) keyStatus |= (1<key_timer = LSAPI_OSI_TimerCreate(NULL, _lsKeyTimer, key); if (key->key_timer == NULL){ LSAPI_OSI_Free(key); return; } LSAPI_Device_KeypadCreate(_lsKeyCB, key); } static LSAPI_Device_t *vibrator = NULL; static void ls_vibrator_Init(void){ LSAPI_VibrConfig_t cfg = { .type = LS_VIBR_PMIC, }; vibrator = LSAPI_Device_VibrCreate(&cfg); if (!vibrator) { LSAPI_Log_Debug("ls_vibrator_demo: create vibrator faild!\n"); }else { LSAPI_Log_Debug("ls_vibrator_demo: start test vibrator!!!\n"); } } void VibCtrl(bool statue){ if(statue)LSAPI_Device_Open(vibrator); else LSAPI_Device_Close(vibrator); } void VibStart(char time){ VibCtrl(1); LSAPI_OSI_TimerStart(pVibTimer_t,time); } //////////////////////////PA控制接口(外放使用免提,耳机FM)/////////////////////////// //8735 //#define PA_MODE_AB //#define PA_MODE_D //#define PA_MODE_FM //#define PA_MODE_K #define PA_MODE_MIANTI void delayUs(int64_t time){ int64_t i; i=LSAPI_OSI_UpTimeUs()+time; while(LSAPI_OSI_UpTimeUs()