endpoint C207 radio support
Dependents: mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular
Diff: MBEDUbloxGPS.cpp
- Revision:
- 3:6acc6c8143b7
- Parent:
- 1:76d2da9e2b84
- Child:
- 5:a8fd1fa0f0d0
diff -r a28f19f39f2b -r 6acc6c8143b7 MBEDUbloxGPS.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MBEDUbloxGPS.cpp Sun Mar 30 16:16:38 2014 +0000 @@ -0,0 +1,106 @@ +/* Copyright C2013 Doug Anson, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files the "Software", to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + // class support + #include "MBEDUbloxGPS.h" + + // MBEDEndpoint support + #include "MBEDEndpoint.h" + + // our instance + MBEDUbloxGPS *_instance = NULL; + + // threaded task + static void read_gps(void const *args) { + while (_instance != NULL) { + _instance->update(); + wait_ms(UBLOX_GPS_POLL_MS); + } + } + + // default constructor + MBEDUbloxGPS::MBEDUbloxGPS(ErrorHandler *error_handler, void *endpoint) { + this->m_error_handler = error_handler; + this->m_endpoint = endpoint; + this->m_c207 = new C207(); + this->m_gps = new I2C(GPSSDA, GPSSCL); + this->m_gps->frequency(UBLOX_GPS_FREQ); + this->m_latitude = 0; + this->m_longitude = 0; + _instance = this; + this->m_gps_poll_thread = new Thread(read_gps); + } + + // default destructor + MBEDUbloxGPS::~MBEDUbloxGPS() { + if (this->m_gps_poll_thread != NULL) { this->m_gps_poll_thread->terminate(); delete this->m_gps_poll_thread; } + if (this->m_c207 != NULL) delete this->m_c207; + if (this->m_gps != NULL) delete this->m_gps; + } + + // update our GPS info + void MBEDUbloxGPS::update() { + int s = 0; + int i = 0; + int o = 1; + char in[1024]; + char out[1024] = { 0xFF/*STREAM_REG*/, 0x00 /* ... */ }; + + memset(in,0,1024); + memset(out,0,1024); + + // read the GPS input + const char r = 0xFD /*LENGTH_REG*/; + unsigned char sz[2]; + if (0 == this->m_gps->write(GPSADR,&r,sizeof(r), true)) + { + if (0 == this->m_gps->read(GPSADR,(char*)sz,sizeof(sz),true)) + { + int b = (int)sz[0]; + b *= 256; + b += sz[1]; + if (i == s) + i = s = 0; + if (b > sizeof(in)-s) + b = sizeof(in)-s; + if (b > 0) + { + if (0 == this->m_gps->read(GPSADR,&in[s],b,true)) + s += b; + } + } + } + this->m_gps->stop(); + if (o > 1) + { + if (0 == this->m_gps->write(GPSADR,out,o)) + o = 0; + } + + // convert the input to latitude/longitude decimal values + this->logger()->log("GPS: Output: %s",out); + } + + // get latitude + float MBEDUbloxGPS::getLatitude() { return this->m_latitude; } + + // get longitude + float MBEDUbloxGPS::getLongitude() { return this->m_longitude; } + + // ErrorHandler + ErrorHandler *Ubloxgps::logger() { return this->m_error_handler; } \ No newline at end of file