Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of dgps by
main.cpp
- Committer:
- dylanembed123
- Date:
- 2014-02-21
- Revision:
- 6:434d20e99e49
- Parent:
- 0:9c001c4e7bf4
- Child:
- 7:c75d5e5e6bfc
File content as of revision 6:434d20e99e49:
#include "mbed.h" #include <string> #include <sstream> #include "usb.h" #include "camera.h" /* Serial pc(USBTX,USBRX); Serial xbee(p9,p10);//tx, rx Serial gps(p28,p27); Serial camera(p13,p14); typedef struct { int latitude; //in .0001 minutes int longitude; //in .0001 minutes int altitude; //in decimeters int time; //in milliseconds } GpsData; GpsData gpsData; GpsData otherGps; void readSerial(Serial &s, char str[], int size){ for (int i = 0; i < size; i++){ str[i] = s.getc(); } } //sends: "$<command>*<checksum>\r\l" void sendGpsCommand(string command){ uint8_t checksum = 0; pc.printf("Sending command to gps: "); gps.putc('$'); pc.putc('$'); char c; for (int i = 0; i < command.length(); i++){ c = command[i]; checksum ^= c; gps.putc(c); pc.putc(c); } gps.putc('*'); pc.putc('*'); string checkSumString; while (checksum > 0){ uint8_t checksumChar = checksum & 0x0F; if (checksumChar >= 10){ checksumChar -= 10; checksumChar += 'A'; } else { checksumChar += '0'; } checkSumString.push_back((char) checksumChar); checksum = checksum >> 4; } for (int i = checkSumString.length() - 1; i >= 0; i--){ gps.putc(checkSumString[i]); pc.putc(checkSumString[i]); } gps.putc('\r'); pc.putc('\r'); gps.putc('\n'); pc.putc('\n'); } long parseDec(string s){ int mult = 1; int result = 0; for (int i = s.length() - 1; i >=0; i--){ if (s[i] != '.'){ result += (s[i] - '0') * mult; mult *= 10; } } return result; } //cs: little endian parsing int nextInt(char *data, int i){ i |= data[i]; i |= (data[i + 1] << 8); i |= (data[i + 2] << 16); i |= (data[i + 3] << 24); return i; } void handleXbeeGps(){ static bool reading = false; static char packet[16]; static int i = 0; char c = xbee.getc(); if (reading){ packet[i] = c; i++; if (i == 16){ i = 0; otherGps.latitude = nextInt(packet, 0); otherGps.longitude = nextInt(packet, 4); otherGps.altitude = nextInt(packet, 8); otherGps.time = nextInt(packet, 12); pc.printf("His GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n", otherGps.latitude, otherGps.longitude, otherGps.altitude, otherGps.time ); reading = false; } } else if (c == 'X'){ reading = true; } } void handleGpsData(){ static bool reading = false; static stringstream line; char c = gps.getc(); if (reading){ if (c == '*'){ //sentence buffer complete; we're ignoring the checksum string field; std::getline(line, field, ','); //GPGGA std::getline(line, field, ','); //time gpsData.time = parseDec(field); std::getline(line, field, ','); //latitude gpsData.latitude = parseDec(field); std::getline(line, field, ','); //N or S std::getline(line, field, ','); //longitude gpsData.longitude = parseDec(field); std::getline(line, field, ','); //E or W std::getline(line, field, ','); //skip std::getline(line, field, ','); //skip std::getline(line, field, ','); //skip std::getline(line, field, ','); //altitude gpsData.altitude = parseDec(field); //update whatever needs updating when gps updates pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n", gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time ); line.str(string("")); reading = false; } else { line.put(c); } } else if (c == '$') { reading = true; } } */ int main(){ //USB::getSerial().printf("------Starting Program------\n"); Camera cam; char* version = cam.getVersion(); //USB::getSerial().printf("Camera Version:\n%s\n",version); uint8_t targetSize=VC0706_640x480;//VC0706_160x120; cam.setImageSize(targetSize); uint8_t realSize=cam.getImageSize(); //USB::getSerial().printf("Image Size | Target: %d, Real %d",targetSize,realSize); if (! cam.takePicture()) { USB::getSerial().printf("Failed to snap!\n"); while(1){} } else { //USB::getSerial().printf("Picture taken!\n"); } int size=cam.frameLength(); //USB::getSerial().printf("Image Size %d\n",size); int i; for(i=0;i<size;){ // read 32 bytes at a time; uint8_t bytesToRead = min(64, size-i); // change 32 to 64 for a speedup but may not work with all setups! uint8_t bytesRead=0; uint8_t* buffer = cam.readPicture(bytesToRead,&bytesRead); for(int a=0;a<bytesRead;a++){USB::getSerial().putc(buffer[a]);} //USB::getSerial().write(buffer,bytesToRead); //USB::getSerial().printf("Read: %d/%d - %d/%d\n",i,size,bytesRead,bytesToRead); i+=bytesRead; } //USB::getSerial().printf("Done(%d)\n",i); while(1){ } // gps.baud(57600); // xbee.baud(9600); // pc.baud(57600); // sendGpsCommand("PMTK301,1"); // while(true){pc.putc(gps.getc());} // gps.attach(&handleGpsData, Serial::RxIrq); // xbee.attach(&handleXbeeGps, Serial::RxIrq); }