Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
io/GPSTracker.cpp@92:0acd11870c6a, 2015-04-13 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 13 14:24:58 2015 +0000
- Revision:
- 92:0acd11870c6a
- Parent:
- 76:b07effe83fb8
- Child:
- 98:e369fc75c000
v2.1rc1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 47:89ae46d5c466 | 1 | #include <stdlib.h> |
Cumulocity | 47:89ae46d5c466 | 2 | #include <string.h> |
xinlei | 76:b07effe83fb8 | 3 | #include "GPSTracker.h" |
xinlei | 76:b07effe83fb8 | 4 | #include "logging.h" |
Cumulocity | 47:89ae46d5c466 | 5 | |
Cumulocity | 47:89ae46d5c466 | 6 | GPSTracker::GPSTracker(GPSI2C& gps) : |
Cumulocity | 47:89ae46d5c466 | 7 | _gps(gps), |
Cumulocity | 47:89ae46d5c466 | 8 | _thread(GPSTracker::thread_func, this), |
Cumulocity | 47:89ae46d5c466 | 9 | _positionSet(false) |
Cumulocity | 47:89ae46d5c466 | 10 | { |
Cumulocity | 47:89ae46d5c466 | 11 | } |
Cumulocity | 47:89ae46d5c466 | 12 | |
Cumulocity | 47:89ae46d5c466 | 13 | bool GPSTracker::position(GPSTracker::Position *position) |
Cumulocity | 47:89ae46d5c466 | 14 | { |
Cumulocity | 47:89ae46d5c466 | 15 | bool result; |
Cumulocity | 47:89ae46d5c466 | 16 | |
Cumulocity | 47:89ae46d5c466 | 17 | _mutex.lock(); |
Cumulocity | 47:89ae46d5c466 | 18 | if (_positionSet) { |
Cumulocity | 47:89ae46d5c466 | 19 | memcpy(position, &_position, sizeof(GPSTracker::Position)); |
Cumulocity | 47:89ae46d5c466 | 20 | _positionSet = false; |
Cumulocity | 47:89ae46d5c466 | 21 | result = true; |
Cumulocity | 47:89ae46d5c466 | 22 | } else { |
Cumulocity | 47:89ae46d5c466 | 23 | result = false; |
Cumulocity | 47:89ae46d5c466 | 24 | } |
Cumulocity | 47:89ae46d5c466 | 25 | _mutex.unlock(); |
Cumulocity | 47:89ae46d5c466 | 26 | |
Cumulocity | 47:89ae46d5c466 | 27 | return result; |
Cumulocity | 47:89ae46d5c466 | 28 | } |
vwochnik | 60:3c822f97fc73 | 29 | |
Cumulocity | 47:89ae46d5c466 | 30 | void GPSTracker::thread() |
Cumulocity | 47:89ae46d5c466 | 31 | { |
Cumulocity | 47:89ae46d5c466 | 32 | char buf[256], chr; // needs to be that big otherwise mdm isn't working |
Cumulocity | 47:89ae46d5c466 | 33 | int ret, len, n; |
Cumulocity | 47:89ae46d5c466 | 34 | double altitude, latitude, longitude; |
xinlei | 92:0acd11870c6a | 35 | aInfo("GPS thread: %p\r\n", Thread::gettid()); |
Cumulocity | 47:89ae46d5c466 | 36 | while (true) { |
Cumulocity | 47:89ae46d5c466 | 37 | ret = _gps.getMessage(buf, sizeof(buf)); |
Cumulocity | 47:89ae46d5c466 | 38 | if (ret <= 0) { |
Cumulocity | 47:89ae46d5c466 | 39 | Thread::wait(100); |
Cumulocity | 47:89ae46d5c466 | 40 | continue; |
Cumulocity | 47:89ae46d5c466 | 41 | } |
Cumulocity | 47:89ae46d5c466 | 42 | |
Cumulocity | 47:89ae46d5c466 | 43 | len = LENGTH(ret); |
xinlei | 76:b07effe83fb8 | 44 | if ((PROTOCOL(ret) != GPSParser::NMEA) || (len <= 6)) { |
xinlei | 76:b07effe83fb8 | 45 | aWarning("GPS data is not in NMEA protocol!\r\n"); |
Cumulocity | 47:89ae46d5c466 | 46 | continue; |
xinlei | 76:b07effe83fb8 | 47 | } |
Cumulocity | 47:89ae46d5c466 | 48 | |
xinlei | 76:b07effe83fb8 | 49 | // we're only interested in fixed GPS positions, data type: GPGGA |
xinlei | 76:b07effe83fb8 | 50 | if ((strncmp("$GPGGA", buf, 6) != 0)) { |
Cumulocity | 47:89ae46d5c466 | 51 | continue; |
xinlei | 76:b07effe83fb8 | 52 | } |
xinlei | 76:b07effe83fb8 | 53 | |
xinlei | 76:b07effe83fb8 | 54 | if (!_gps.getNmeaItem(6, buf, len, n, 10) || n == 0) { |
xinlei | 76:b07effe83fb8 | 55 | continue; |
xinlei | 76:b07effe83fb8 | 56 | } |
Cumulocity | 47:89ae46d5c466 | 57 | |
Cumulocity | 47:89ae46d5c466 | 58 | // get altitude, latitude and longitude |
Cumulocity | 47:89ae46d5c466 | 59 | if ((!_gps.getNmeaAngle(2, buf, len, latitude)) || |
Cumulocity | 47:89ae46d5c466 | 60 | (!_gps.getNmeaAngle(4, buf, len, longitude)) || |
Cumulocity | 47:89ae46d5c466 | 61 | (!_gps.getNmeaItem(9, buf, len, altitude)) || |
Cumulocity | 47:89ae46d5c466 | 62 | (!_gps.getNmeaItem(10, buf, len, chr)) || |
xinlei | 76:b07effe83fb8 | 63 | (chr != 'M')) { |
xinlei | 76:b07effe83fb8 | 64 | aWarning("Unable to retrieve GPS data!\r\n"); |
Cumulocity | 47:89ae46d5c466 | 65 | continue; |
xinlei | 76:b07effe83fb8 | 66 | } |
Cumulocity | 47:89ae46d5c466 | 67 | |
Cumulocity | 47:89ae46d5c466 | 68 | _mutex.lock(); |
Cumulocity | 47:89ae46d5c466 | 69 | _position.altitude = altitude; |
Cumulocity | 47:89ae46d5c466 | 70 | _position.latitude = latitude; |
Cumulocity | 47:89ae46d5c466 | 71 | _position.longitude = longitude; |
Cumulocity | 47:89ae46d5c466 | 72 | _positionSet = true; |
Cumulocity | 47:89ae46d5c466 | 73 | _mutex.unlock(); |
Cumulocity | 47:89ae46d5c466 | 74 | } |
Cumulocity | 47:89ae46d5c466 | 75 | } |
Cumulocity | 47:89ae46d5c466 | 76 | |
Cumulocity | 47:89ae46d5c466 | 77 | void GPSTracker::thread_func(void const *arg) |
Cumulocity | 47:89ae46d5c466 | 78 | { |
Cumulocity | 47:89ae46d5c466 | 79 | GPSTracker *that; |
Cumulocity | 47:89ae46d5c466 | 80 | that = (GPSTracker*)arg; |
Cumulocity | 47:89ae46d5c466 | 81 | that->thread(); |
Cumulocity | 47:89ae46d5c466 | 82 | } |