endpoint C207 radio support

Dependents:   mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular

Committer:
ansond
Date:
Mon Mar 31 18:47:26 2014 +0000
Revision:
5:a8fd1fa0f0d0
Parent:
3:6acc6c8143b7
Child:
6:cd2d23300f05
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:d46b1accad7d 1 /* Copyright C2013 Doug Anson, MIT License
ansond 0:d46b1accad7d 2 *
ansond 0:d46b1accad7d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:d46b1accad7d 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:d46b1accad7d 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:d46b1accad7d 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:d46b1accad7d 7 * furnished to do so, subject to the following conditions:
ansond 0:d46b1accad7d 8 *
ansond 0:d46b1accad7d 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:d46b1accad7d 10 * substantial portions of the Software.
ansond 0:d46b1accad7d 11 *
ansond 0:d46b1accad7d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:d46b1accad7d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:d46b1accad7d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:d46b1accad7d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:d46b1accad7d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:d46b1accad7d 17 */
ansond 0:d46b1accad7d 18
ansond 0:d46b1accad7d 19 // class support
ansond 3:6acc6c8143b7 20 #include "MBEDUbloxGPS.h"
ansond 0:d46b1accad7d 21
ansond 0:d46b1accad7d 22 // MBEDEndpoint support
ansond 0:d46b1accad7d 23 #include "MBEDEndpoint.h"
ansond 0:d46b1accad7d 24
ansond 1:76d2da9e2b84 25 // our instance
ansond 5:a8fd1fa0f0d0 26 MBEDUbloxGPS *_ublox_gps_instance = NULL;
ansond 1:76d2da9e2b84 27
ansond 1:76d2da9e2b84 28 // threaded task
ansond 1:76d2da9e2b84 29 static void read_gps(void const *args) {
ansond 5:a8fd1fa0f0d0 30 while (_ublox_gps_instance != NULL) {
ansond 5:a8fd1fa0f0d0 31 _ublox_gps_instance->update();
ansond 1:76d2da9e2b84 32 wait_ms(UBLOX_GPS_POLL_MS);
ansond 1:76d2da9e2b84 33 }
ansond 1:76d2da9e2b84 34 }
ansond 1:76d2da9e2b84 35
ansond 0:d46b1accad7d 36 // default constructor
ansond 5:a8fd1fa0f0d0 37 MBEDUbloxGPS::MBEDUbloxGPS(ErrorHandler *error_handler, void *endpoint) : BaseClass(error_handler,endpoint) {
ansond 5:a8fd1fa0f0d0 38 this->m_c027 = new C027();
ansond 1:76d2da9e2b84 39 this->m_gps = new I2C(GPSSDA, GPSSCL);
ansond 1:76d2da9e2b84 40 this->m_gps->frequency(UBLOX_GPS_FREQ);
ansond 1:76d2da9e2b84 41 this->m_latitude = 0;
ansond 1:76d2da9e2b84 42 this->m_longitude = 0;
ansond 5:a8fd1fa0f0d0 43 _ublox_gps_instance = this;
ansond 1:76d2da9e2b84 44 this->m_gps_poll_thread = new Thread(read_gps);
ansond 0:d46b1accad7d 45 }
ansond 0:d46b1accad7d 46
ansond 0:d46b1accad7d 47 // default destructor
ansond 3:6acc6c8143b7 48 MBEDUbloxGPS::~MBEDUbloxGPS() {
ansond 1:76d2da9e2b84 49 if (this->m_gps_poll_thread != NULL) { this->m_gps_poll_thread->terminate(); delete this->m_gps_poll_thread; }
ansond 5:a8fd1fa0f0d0 50 if (this->m_c027 != NULL) delete this->m_c027;
ansond 1:76d2da9e2b84 51 if (this->m_gps != NULL) delete this->m_gps;
ansond 0:d46b1accad7d 52 }
ansond 0:d46b1accad7d 53
ansond 1:76d2da9e2b84 54 // update our GPS info
ansond 3:6acc6c8143b7 55 void MBEDUbloxGPS::update() {
ansond 1:76d2da9e2b84 56 int s = 0;
ansond 1:76d2da9e2b84 57 int i = 0;
ansond 1:76d2da9e2b84 58 int o = 1;
ansond 1:76d2da9e2b84 59 char in[1024];
ansond 1:76d2da9e2b84 60 char out[1024] = { 0xFF/*STREAM_REG*/, 0x00 /* ... */ };
ansond 1:76d2da9e2b84 61
ansond 1:76d2da9e2b84 62 memset(in,0,1024);
ansond 1:76d2da9e2b84 63 memset(out,0,1024);
ansond 1:76d2da9e2b84 64
ansond 1:76d2da9e2b84 65 // read the GPS input
ansond 1:76d2da9e2b84 66 const char r = 0xFD /*LENGTH_REG*/;
ansond 1:76d2da9e2b84 67 unsigned char sz[2];
ansond 1:76d2da9e2b84 68 if (0 == this->m_gps->write(GPSADR,&r,sizeof(r), true))
ansond 1:76d2da9e2b84 69 {
ansond 1:76d2da9e2b84 70 if (0 == this->m_gps->read(GPSADR,(char*)sz,sizeof(sz),true))
ansond 1:76d2da9e2b84 71 {
ansond 1:76d2da9e2b84 72 int b = (int)sz[0];
ansond 1:76d2da9e2b84 73 b *= 256;
ansond 1:76d2da9e2b84 74 b += sz[1];
ansond 1:76d2da9e2b84 75 if (i == s)
ansond 1:76d2da9e2b84 76 i = s = 0;
ansond 1:76d2da9e2b84 77 if (b > sizeof(in)-s)
ansond 1:76d2da9e2b84 78 b = sizeof(in)-s;
ansond 1:76d2da9e2b84 79 if (b > 0)
ansond 1:76d2da9e2b84 80 {
ansond 1:76d2da9e2b84 81 if (0 == this->m_gps->read(GPSADR,&in[s],b,true))
ansond 1:76d2da9e2b84 82 s += b;
ansond 1:76d2da9e2b84 83 }
ansond 1:76d2da9e2b84 84 }
ansond 1:76d2da9e2b84 85 }
ansond 1:76d2da9e2b84 86 this->m_gps->stop();
ansond 1:76d2da9e2b84 87 if (o > 1)
ansond 1:76d2da9e2b84 88 {
ansond 1:76d2da9e2b84 89 if (0 == this->m_gps->write(GPSADR,out,o))
ansond 1:76d2da9e2b84 90 o = 0;
ansond 1:76d2da9e2b84 91 }
ansond 1:76d2da9e2b84 92
ansond 1:76d2da9e2b84 93 // convert the input to latitude/longitude decimal values
ansond 1:76d2da9e2b84 94 this->logger()->log("GPS: Output: %s",out);
ansond 0:d46b1accad7d 95 }
ansond 0:d46b1accad7d 96
ansond 0:d46b1accad7d 97 // get latitude
ansond 3:6acc6c8143b7 98 float MBEDUbloxGPS::getLatitude() { return this->m_latitude; }
ansond 0:d46b1accad7d 99
ansond 0:d46b1accad7d 100 // get longitude
ansond 5:a8fd1fa0f0d0 101 float MBEDUbloxGPS::getLongitude() { return this->m_longitude; }