Skip to content

Instantly share code, notes, and snippets.

@flippyhead
Created July 8, 2015 21:51
Show Gist options
  • Save flippyhead/b2c197cd5d77959aeb2f to your computer and use it in GitHub Desktop.
Save flippyhead/b2c197cd5d77959aeb2f to your computer and use it in GitHub Desktop.
native _begin(), _read(), _GPS;
native do
##include <Adafruit_GPS.h>
##include <SoftwareSerial.h>
SoftwareSerial my_Serial(8, 7);
Adafruit_GPS GPS(&my_Serial);
void begin() {
Serial.begin(9600);
Serial.println("Adafruit GPS library basic test!");
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
// uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
// Set the update rate
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
// Request updates on antenna status, comment out to keep quiet
GPS.sendCommand(PGCMD_ANTENNA);
}
void read() {
GPS.read();
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
if (GPS.fix) {
Serial.print("Location: ");
Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
Serial.print(", ");
Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
}
}
end
#include "arduino.ceu"
#include "gps.ceu"
_begin();
loop do
_read();
// ouputs unilitialized data:
// Location: 0, 0
await 2s;
end
#include "arduino.ceu"
#include "gps.ceu"
_begin();
loop do
// duplicating the update() function in gps.ceu
_GPS.read();
if _GPS.newNMEAreceived() then
if _GPS.parse(_GPS.lastNMEA()) then
await 5s;
if _GPS.fix then
_Serial.print("Location: ");
_Serial.print(_GPS.latitude, 4); _Serial.print(_GPS.lat);
_Serial.print(", ");
_Serial.print(_GPS.longitude, 4); _Serial.println(_GPS.lon);
end
// outputs valid location data:
// Location: 51.5033630, 48.10202
end
end
end
@fsantanna
Copy link

Looks like the examples above are not quite the same: you chose different timings (2s vs 5s) and the await statements are placed in between different statements (after _read() vs between parse and fix).

Anyway, I think it is a good idea to separate the sensing task from your programs:
Sensing should deal with the details in the GPS protocol and emit some higher level input event into your program. This way, sensing can be reused in any program using GPS.
The program will only deal with the awaits and the control flow of your actual problem domain.

par do
    /* This all could be in another file (gps.ceu) */

    /* input event that your program will react to */
    input (?,?,?) GPS;  // complete with the data types to expect from GPS

    /* GPS initialization (or let it be a function to be called from the program if it requires arguments) */
    <...>

    /* sensing loop */
    every <?> ms do  // what is the sensing frequency?
        _GPS.read();
        if _GPS.newNMEAreceived() and _GPS.parse(_GPS.lastNMEA()) and _GPS.fix then
            /* emit only in those special conditions */
            async do
                emit GPS => (?,?,?);  // complete with the data values from the GPS
            end
        end
    end
with
    <...>   // this is your program that will "await GPS" somewhere
end

The sensing loop can also be merged with poll.ino.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment