portable version of the cumulocity demo

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
ublox
Date:
Fri Oct 03 07:54:13 2014 +0000
Revision:
56:2da813cc2f47
Parent:
49:ac0ba9d54ebc
update dependency

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 47:89ae46d5c466 1 #include "GPSTracker.h"
Cumulocity 47:89ae46d5c466 2 #include <stdlib.h>
Cumulocity 47:89ae46d5c466 3 #include <string.h>
Cumulocity 47:89ae46d5c466 4
Cumulocity 47:89ae46d5c466 5 GPSTracker::GPSTracker(GPSI2C& gps) :
Cumulocity 47:89ae46d5c466 6 _gps(gps),
Cumulocity 47:89ae46d5c466 7 _thread(GPSTracker::thread_func, this),
Cumulocity 47:89ae46d5c466 8 _positionSet(false)
Cumulocity 47:89ae46d5c466 9 {
Cumulocity 47:89ae46d5c466 10 }
Cumulocity 47:89ae46d5c466 11
Cumulocity 47:89ae46d5c466 12 bool GPSTracker::position(GPSTracker::Position *position)
Cumulocity 47:89ae46d5c466 13 {
Cumulocity 47:89ae46d5c466 14 bool result;
Cumulocity 47:89ae46d5c466 15
Cumulocity 47:89ae46d5c466 16 _mutex.lock();
Cumulocity 47:89ae46d5c466 17 if (_positionSet) {
Cumulocity 47:89ae46d5c466 18 memcpy(position, &_position, sizeof(GPSTracker::Position));
Cumulocity 47:89ae46d5c466 19 _positionSet = false;
Cumulocity 47:89ae46d5c466 20 result = true;
Cumulocity 47:89ae46d5c466 21 } else {
Cumulocity 47:89ae46d5c466 22 result = false;
Cumulocity 47:89ae46d5c466 23 }
Cumulocity 47:89ae46d5c466 24 _mutex.unlock();
Cumulocity 47:89ae46d5c466 25
Cumulocity 47:89ae46d5c466 26 return result;
Cumulocity 47:89ae46d5c466 27 }
Cumulocity 47:89ae46d5c466 28
Cumulocity 47:89ae46d5c466 29 void GPSTracker::thread()
Cumulocity 47:89ae46d5c466 30 {
Cumulocity 47:89ae46d5c466 31 char buf[256], chr; // needs to be that big otherwise mdm isn't working
Cumulocity 47:89ae46d5c466 32 int ret, len, n;
Cumulocity 47:89ae46d5c466 33 double altitude, latitude, longitude;
Cumulocity 47:89ae46d5c466 34
Cumulocity 47:89ae46d5c466 35 while (true) {
Cumulocity 47:89ae46d5c466 36 ret = _gps.getMessage(buf, sizeof(buf));
Cumulocity 47:89ae46d5c466 37 if (ret <= 0) {
Cumulocity 47:89ae46d5c466 38 Thread::wait(100);
Cumulocity 47:89ae46d5c466 39 continue;
Cumulocity 47:89ae46d5c466 40 }
Cumulocity 47:89ae46d5c466 41
Cumulocity 47:89ae46d5c466 42 len = LENGTH(ret);
Cumulocity 47:89ae46d5c466 43 if ((PROTOCOL(ret) != GPSParser::NMEA) || (len <= 6))
Cumulocity 47:89ae46d5c466 44 continue;
Cumulocity 47:89ae46d5c466 45
Cumulocity 47:89ae46d5c466 46 // we're only interested in fixed GPS positions
Cumulocity 47:89ae46d5c466 47 // we are not interested in invalid data
Cumulocity 47:89ae46d5c466 48 if ((strncmp("$GPGGA", buf, 6) != 0) ||
Cumulocity 47:89ae46d5c466 49 (!_gps.getNmeaItem(6, buf, len, n, 10)) || (n == 0))
Cumulocity 47:89ae46d5c466 50 continue;
Cumulocity 47:89ae46d5c466 51
Cumulocity 47:89ae46d5c466 52 // get altitude, latitude and longitude
Cumulocity 47:89ae46d5c466 53 if ((!_gps.getNmeaAngle(2, buf, len, latitude)) ||
Cumulocity 47:89ae46d5c466 54 (!_gps.getNmeaAngle(4, buf, len, longitude)) ||
Cumulocity 47:89ae46d5c466 55 (!_gps.getNmeaItem(9, buf, len, altitude)) ||
Cumulocity 47:89ae46d5c466 56 (!_gps.getNmeaItem(10, buf, len, chr)) ||
Cumulocity 47:89ae46d5c466 57 (chr != 'M'))
Cumulocity 47:89ae46d5c466 58 continue;
Cumulocity 47:89ae46d5c466 59
Cumulocity 47:89ae46d5c466 60 _mutex.lock();
Cumulocity 47:89ae46d5c466 61 _position.altitude = altitude;
Cumulocity 47:89ae46d5c466 62 _position.latitude = latitude;
Cumulocity 47:89ae46d5c466 63 _position.longitude = longitude;
Cumulocity 47:89ae46d5c466 64 _positionSet = true;
Cumulocity 47:89ae46d5c466 65 _mutex.unlock();
Cumulocity 47:89ae46d5c466 66 }
Cumulocity 47:89ae46d5c466 67 }
Cumulocity 47:89ae46d5c466 68
Cumulocity 47:89ae46d5c466 69 void GPSTracker::thread_func(void const *arg)
Cumulocity 47:89ae46d5c466 70 {
Cumulocity 47:89ae46d5c466 71 GPSTracker *that;
Cumulocity 47:89ae46d5c466 72 that = (GPSTracker*)arg;
Cumulocity 47:89ae46d5c466 73 that->thread();
Cumulocity 47:89ae46d5c466 74 }