Import of CSR's demo for SirfV. Has minor cleanup.
Dependencies: CsrLocation mbed GPSProvider
Fork of CsrLocationDemo by
CsrLocationDemo.cpp
- Committer:
- zhjcpi
- Date:
- 2014-03-24
- Revision:
- 0:789f6b6f710d
- Child:
- 1:e6c96c984e2b
File content as of revision 0:789f6b6f710d:
/* CsrLocation class for mbed Microcontroller * Copyright 2014 CSR plc */ #include "mbed.h" #include "CsrLocation.h" /* appliation commands */ typedef enum AppCmd { APP_CMD_IDLE, // No special command APP_CMD_HELP, // Show the supported commands APP_CMD_START, // Start location APP_CMD_STOP, // Stop location APP_CMD_PM_FULL, // Set full power mode APP_CMD_PM_PTF, // Set low power PTF mode APP_CMD_PTF_GETPOS, // Get location immediately in low power PTF mode APP_CMD_START_FAILED, // Process start failed case APP_CMD_STOP_FAILED, // Process stop failed case APP_CMD_NMEA, // Debug command, switch chip to NMEA protocol at 4800bps APP_CMD_ONOFF_ON, // Debug command, pull onoff pin high level APP_CMD_ONOFF_OFF, // Debug command, pull onoff pin low level APP_CMD_RESET_ON, // Debug command, pull reset pin high level APP_CMD_RESET_OFF // Debug command, pull reset pin low level }eAppCmd; static void _AppShowCmd(void); static void _AppOutputCallback(CsrUint32 msgId, void * const pMsgData, CsrUint32 msgLength); static void _AppEventCallback(eCsrLocEventType event, CsrUint32 data); static void _ConsoleRxHandler(void); static void _AppCmdProcess(char *pCmd); static int sAppCmd = APP_CMD_IDLE; ePowerMode sPwrMode = PWR_FULL; static Serial sSerialDebug(USBTX, USBRX); static Serial sSerialLoc(p9, p10);; static DigitalOut sPinOnoff(p5); static DigitalOut sPinReset(p6); static DigitalOut sLedLocOn(LED1); static DigitalOut sLedPosReport(LED2); int main(void) { CsrLocation *pCsrLoc; tCsrLocConfig locConfig; /* initialize the debug serial port */ sSerialDebug.baud(115200); sSerialDebug.attach(&_ConsoleRxHandler); /* initialize the CsrLocConfig */ locConfig.pSerialDebug = &sSerialDebug; locConfig.pSerialLoc = &sSerialLoc; locConfig.pPinOnoff = &sPinOnoff; locConfig.pPinReset = &sPinReset; /* new the CsrLocation instance */ pCsrLoc = new CsrLocation(&locConfig); if(pCsrLoc == NULL) { sSerialDebug.printf("Failed to new csrLocation.\r\n"); sSerialDebug.attach(NULL); return -1; } /* Register output callback and event callback functions */ pCsrLoc->CsrLocRegOutput(_AppOutputCallback, _AppEventCallback); sSerialDebug.printf("mbed initialized OK.\r\n"); _AppShowCmd(); while(1) { switch(sAppCmd) { case APP_CMD_HELP: sAppCmd = APP_CMD_IDLE; _AppShowCmd(); break; case APP_CMD_START: sAppCmd = APP_CMD_IDLE; sSerialDebug.printf("start location.\r\n"); sLedLocOn = 1; pCsrLoc->CsrLocStart(sPwrMode); break; case APP_CMD_STOP: sAppCmd = APP_CMD_IDLE; sSerialDebug.printf("stop location.\r\n"); sLedLocOn = 0; pCsrLoc->CsrLocStop(); break; case APP_CMD_START_FAILED: sAppCmd = APP_CMD_IDLE; sSerialDebug.printf("reset as start failed.\r\n"); sLedLocOn = 0; pCsrLoc->CsrLocStop(); pCsrLoc->CsrLocReset(); break; case APP_CMD_STOP_FAILED: sAppCmd = APP_CMD_IDLE; sSerialDebug.printf("reset as stop failed.\r\n"); sLedLocOn = 0; pCsrLoc->CsrLocStop(); pCsrLoc->CsrLocReset(); break; case APP_CMD_IDLE: pCsrLoc->CsrLocUpdate(); break; case APP_CMD_ONOFF_ON: sSerialDebug.printf("onoff on.\r\n"); sAppCmd = APP_CMD_IDLE; sPinOnoff = 1; break; case APP_CMD_ONOFF_OFF: sSerialDebug.printf("onoff off.\r\n"); sAppCmd = APP_CMD_IDLE; sPinOnoff = 0; break; case APP_CMD_RESET_ON: sSerialDebug.printf("reset on.\r\n"); sAppCmd = APP_CMD_IDLE; sPinReset = 1; break; case APP_CMD_RESET_OFF: sSerialDebug.printf("reset off.\r\n"); sAppCmd = APP_CMD_IDLE; sPinReset = 0; break; case APP_CMD_PTF_GETPOS: sSerialDebug.printf("lpm get pos.\r\n"); sAppCmd = APP_CMD_IDLE; pCsrLoc->CsrLocLpmGetPos(); break; case APP_CMD_NMEA: sSerialDebug.printf("switch to nmea.\r\n"); sAppCmd = APP_CMD_IDLE; pCsrLoc->CsrLocDebugSwitch2Nmea(); break; case APP_CMD_PM_FULL: sSerialDebug.printf("fpm set.\r\n"); sAppCmd = APP_CMD_IDLE; sPwrMode = PWR_FULL; break; case APP_CMD_PM_PTF: sSerialDebug.printf("lpm ptf set.\r\n"); sAppCmd = APP_CMD_IDLE; sPwrMode = PWR_PTF; break; } } delete pCsrLoc; } static void _AppShowCmd(void) { sSerialDebug.printf("Location commands:\r\n"); sSerialDebug.printf(" help - help to show supported commands\r\n"); sSerialDebug.printf(" start - begin location\r\n"); sSerialDebug.printf(" stop - end location\r\n"); sSerialDebug.printf(" fpm - full power mode\r\n"); sSerialDebug.printf(" ptf - ptf low power mode\r\n"); sSerialDebug.printf(" getpos - get location immediately in low power ptf mode\r\n"); } static void _AppOutputCallback(CsrUint32 msgId, void * const pMsgData, CsrUint32 msgLength) { switch(msgId) { case LOC_OUTPUT_LOCATION: { tLocPosResp *pPosRsp = (tLocPosResp *)pMsgData; sSerialDebug.printf("Loc: lat=%f, lon=%f, alt=%f\r\n", pPosRsp->lat, pPosRsp->lon, pPosRsp->alt); sLedPosReport = !sLedPosReport; break; } case LOC_OUTPUT_SV_STATUS: { tLocSvStatus *pSvStatus = (tLocSvStatus *)pMsgData; sSerialDebug.printf("SV:week=%u, tow=%lu, GPS Num=%u, GLO Num=%u, GPS Mask=0x%lx, GLO Mask=0x%lx\r\n", pSvStatus->gps_week, pSvStatus->tow, pSvStatus->numOfSVs, pSvStatus->numOfGloSVs, pSvStatus->svUsedInFixMask, pSvStatus->gloSvUsedInFixMask); break; } default : break; } } static void _AppEventCallback(eCsrLocEventType event, CsrUint32 data) { switch(event) { case CSR_LOC_EVENT_START_RESULT: if(data != 0) { sSerialDebug.printf("start failed.\r\n"); sAppCmd = APP_CMD_START_FAILED; } else { sSerialDebug.printf("start OK.\r\n"); } break; case CSR_LOC_EVENT_STOP_RESULT: if(data != 0) { sSerialDebug.printf("stop failed.\r\n"); sAppCmd = APP_CMD_STOP_FAILED; } else { sSerialDebug.printf("stop OK.\r\n"); } break; default: break; } } static void _ConsoleRxHandler(void) { static char cmd[32] = {0}; char ch; ch = sSerialDebug.getc(); sSerialDebug.putc(ch); if(ch == '\r') { sSerialDebug.putc('\n'); if(strlen(cmd) > 0) { _AppCmdProcess(cmd); memset(cmd, 0, sizeof(cmd)); } } else { cmd[strlen(cmd)] = ch; } } static void _AppCmdProcess(char *pCmd) { if(strcmp(pCmd, "help") == 0) { sAppCmd = APP_CMD_HELP; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "start") == 0) { sAppCmd = APP_CMD_START; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "stop") == 0) { sAppCmd = APP_CMD_STOP; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "fpm") == 0) { sAppCmd = APP_CMD_PM_FULL; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "ptf") == 0) { sAppCmd = APP_CMD_PM_PTF; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "getpos") == 0) { sAppCmd = APP_CMD_PTF_GETPOS; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "nmea") == 0) { sAppCmd = APP_CMD_NMEA; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "onoffon") == 0) { sAppCmd = APP_CMD_ONOFF_ON; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "onoffoff") == 0) { sAppCmd = APP_CMD_ONOFF_OFF; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "reseton") == 0) { sAppCmd = APP_CMD_RESET_ON; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else if(strcmp(pCmd, "resetoff") == 0) { sAppCmd = APP_CMD_RESET_OFF; sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); } else { sSerialDebug.putc('\r'); sSerialDebug.putc('\n'); sSerialDebug.printf("Unknown command %s\r\n", pCmd); } }