Skip to content

Commit

Permalink
added "remember first valid time"
Browse files Browse the repository at this point in the history
  • Loading branch information
barry-ha committed Apr 9, 2023
1 parent d44e49f commit 996982e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 64 deletions.
7 changes: 4 additions & 3 deletions Griduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,6 @@ void loop() {
// assuming "class Adafruit_GPS" contains 2000-01-01 00:00 until
// it receives an update via NMEA sentences
// the next step (1 second timer) will actually set the clock
//redrawGraph = true;
waitingForRTC = false;

char msg[128]; // debug
Expand All @@ -864,8 +863,10 @@ void loop() {
GPS.hour,GPS.minute,GPS.seconds);
Serial.println(msg); // debug

// write this to the GPS breadcrumb trail as indication of "power up" event
// trail.remember(); // todo: create new event type "PUP" to save in history buffer
// write this to the breadcrumb trail as preliminary indication of acquiring satellites
TimeElements tm{GPS.seconds, GPS.minute, GPS.hour, 0, GPS.day, GPS.month, (byte)(2000-1970+GPS.year)};
time_t firstTime = makeTime(tm);
trail.rememberFirstValidTime(firstTime, GPS.satellites);
}

// every 1 second update the realtime clock
Expand Down
107 changes: 65 additions & 42 deletions model_breadcrumbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class Breadcrumbs {
// 3000 48 bytes 144,000 bytes crash on "list files" command
const int numHistory = sizeof(history) / sizeof(Location);
int saveInterval = 2;
PointGPS noLocation{-1.0, -1.0}; // eye-catching value, and nonzero for "isEmpty()"
const float noSpeed = -1.0;
const float noDirection = -1.0;
const float noAltitude = -1.0;

protected:
public:
Expand All @@ -59,8 +63,16 @@ class Breadcrumbs {
// todo: 4. add rememberPDN(), rememberTOD()

void rememberPUP() {
PointGPS whereAmI{-1.0, -1.0}; // eye-catching value, and nonzero for "isEmpty()"
Location pup{rPOWERUP, whereAmI, now(), 0, -1.0, -1.0, -1.0};
Location pup{rPOWERUP, noLocation, now(), 0, noSpeed, noDirection, noAltitude};

history[nextHistoryItem] = pup;
nextHistoryItem = (++nextHistoryItem % numHistory);
}

void rememberFirstValidTime(time_t vTime, uint8_t vSats) {
// "first valid time" can happen _without_ a satellite fix,
// so all we store is the GMT timestamp
Location pup{rVALIDTIME, noLocation, vTime, vSats, noSpeed, noDirection, noAltitude};

history[nextHistoryItem] = pup;
nextHistoryItem = (++nextHistoryItem % numHistory);
Expand Down Expand Up @@ -369,13 +381,22 @@ class Breadcrumbs {
uint8_t nSats = item.numSatellites;

char out[128];
if (item.isGPS()) {
if (item.isPUP()) {
snprintf(out, sizeof(out), "%d, %s, %s, %s",
ii, item.recordType, sDate, sTime);
} else {
ii, item.recordType, sDate, sTime);

} else if (item.isFirstValidTime()) {
snprintf(out, sizeof(out), "%d, %s, %s, %s, , , , , , , %d",
ii, item.recordType, sDate, sTime, nSats);

} else if (item.isGPS()) {
// 1 2 3 4 5 6 7 8 9 10
snprintf(out, sizeof(out), "%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d",
ii, item.recordType, sDate, sTime, grid6, sLat, sLng, sSpeed, sDirection, sAltitude, nSats);
ii, item.recordType, sDate, sTime, grid6, sLat, sLng, sSpeed, sDirection, sAltitude, nSats);

} else {
snprintf(out, sizeof(out), "%d, Record type '%s' not recognized",
ii, item.recordType);
}
Serial.println(out);
}
Expand Down Expand Up @@ -478,43 +499,45 @@ class Breadcrumbs {
// loop through the entire GPS history buffer
for (int ii = 0; ii < numHistory; ii++) {
Location item = history[index];
if (!item.isEmpty()) {
if (!startFound) {
// this is the first non-empty lat/long found,
// so this must be chronologically the oldest entry recorded
// Remember it for the "Start" pushpin
startIndex = index;
startFound = true;
if (item.isGPS()) {
if (!item.isEmpty()) {
if (!startFound) {
// this is the first non-empty lat/long found,
// so this must be chronologically the oldest entry recorded
// Remember it for the "Start" pushpin
startIndex = index;
startFound = true;
}

// PlaceMark with timestamp
TimeElements time; // https://github.com/PaulStoffregen/Time
breakTime(item.timestamp, time);

char sLat[12], sLng[12];
floatToCharArray(sLat, sizeof(sLat), item.loc.lat, 5);
floatToCharArray(sLng, sizeof(sLng), item.loc.lng, 5);

char grid6[7];
grid.calcLocator(grid6, item.loc.lat, item.loc.lng, 6);

char sSpeed[12], sDirection[12], sAltitude[12];
floatToCharArray(sSpeed, sizeof(sSpeed), item.speed, 1);
floatToCharArray(sDirection, sizeof(sDirection), item.direction, 1);
floatToCharArray(sAltitude, sizeof(sAltitude), item.altitude, 1);
int numSats = item.numSatellites;

char msg[500];
snprintf(msg, sizeof(msg), PLACEMARK_WITH_TIMESTAMP,
sLat, sLng, // humans prefer "latitude,longitude"
grid6,
time.Year + 1970, time.Month, time.Day, time.Hour, time.Minute, time.Second, // human readable time
time.Year + 1970, time.Month, time.Day, time.Hour, time.Minute, time.Second, // kml timestamp
sLng, sLat, // kml requires "longitude,latitude"
sSpeed, sDirection, sAltitude, // mph, degrees from North, meters
numSats // number of satellites
);
Serial.print(msg);
}

// PlaceMark with timestamp
TimeElements time; // https://github.com/PaulStoffregen/Time
breakTime(item.timestamp, time);

char sLat[12], sLng[12];
floatToCharArray(sLat, sizeof(sLat), item.loc.lat, 5);
floatToCharArray(sLng, sizeof(sLng), item.loc.lng, 5);

char grid6[7];
grid.calcLocator(grid6, item.loc.lat, item.loc.lng, 6);

char sSpeed[12], sDirection[12], sAltitude[12];
floatToCharArray(sSpeed, sizeof(sSpeed), item.speed, 1);
floatToCharArray(sDirection, sizeof(sDirection), item.direction, 1);
floatToCharArray(sAltitude, sizeof(sAltitude), item.altitude, 1);
int numSats = item.numSatellites;

char msg[500];
snprintf(msg, sizeof(msg), PLACEMARK_WITH_TIMESTAMP,
sLat, sLng, // humans prefer "latitude,longitude"
grid6,
time.Year + 1970, time.Month, time.Day, time.Hour, time.Minute, time.Second, // human readable time
time.Year + 1970, time.Month, time.Day, time.Hour, time.Minute, time.Second, // kml timestamp
sLng, sLat, // kml requires "longitude,latitude"
sSpeed, sDirection, sAltitude, // mph, degrees from North, meters
numSats // number of satellites
);
Serial.print(msg);
}
index = (index + 1) % numHistory;
}
Expand Down
36 changes: 17 additions & 19 deletions unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ int testNextGridLineEast(float fExpected, double fLongitude) {
// unit test helper for finding grid line crossings
int r = 0;
float result = grid.nextGridLineEast(fLongitude);
// clang-format off
Serial.print("Grid Crossing East: given = "); //~Serial.print(fLongitude);
Serial.print(", expected = "); //~Serial.print(fExpected);
Serial.print(", result = "); //~Serial.print(result);
// clang on
// clang-format on
if (result == fExpected) {
~Serial.println("");
} else {
Expand All @@ -221,14 +222,11 @@ int testNextGridLineWest(float fExpected, double fLongitude) {
// unit test helper for finding grid line crossings
int r = 0;
float result = grid.nextGridLineWest(fLongitude);
// clang off
Serial.print("Grid Crossing West: given = ");
Serial.print(fLongitude);
Serial.print(", expected = ");
Serial.print(fExpected);
Serial.print(", result = ");
Serial.print(result);
// clang on
// clang-format off
Serial.print("Grid Crossing West: given = "); Serial.print(fLongitude);
Serial.print(", expected = "); Serial.print(fExpected);
Serial.print(", result = "); Serial.print(result);
// clang-format on
if (result == fExpected) {
Serial.println("");
} else {
Expand Down Expand Up @@ -493,7 +491,7 @@ int verifyBreadCrumbs() {
model->gLongitude = -122.274711; // CN87

// reduce the frequency of saving to memory
trail.saveInterval = 20; // default 2 is too often, 100 is not often enough
trail.saveInterval = 20; // default 2 is too often, 100 is not often enough

// initialize the canvas that we will draw upon
gridView.startScreen(); // clear and draw normal screen
Expand Down Expand Up @@ -764,20 +762,20 @@ void runUnitTest() {
countDown(5); //
f += verifyBreadCrumbs(); // verify pushpins near the four corners
countDown(5); //
f += verifyBreadCrumbTrail1(); // verify painting the bread crumb trail
countDown(5); //
f += verifyBreadCrumbTrail2(); // verify painting the bread crumb trail
countDown(5); //
f += verifySaveTrail(); // save GPS route to non-volatile memory
countDown(5); //
f += verifyRestoreTrail(); // restore GPS route from non-volatile memory
countDown(5); //
f += verifyBreadCrumbTrail1(); // verify painting the bread crumb trail
countDown(5); //
f += verifyBreadCrumbTrail2(); // verify painting the bread crumb trail
countDown(5); //
f += verifySaveTrail(); // save GPS route to non-volatile memory
countDown(5); //
f += verifyRestoreTrail(); // restore GPS route from non-volatile memory
countDown(5); //
f += verifyDerivingGridSquare(); // verify deriving grid square from lat-long coordinates
countDown(5); //
f += verifyComputingDistance(); // verify computing distance
f += verifyComputingGridLines(); // verify finding grid lines on E and W
countDown(5); // give user time to inspect display appearance for unit test problems
trail.clearHistory(); // clean up our mess after unit test
trail.clearHistory(); // clean up our mess after unit test
trail.rememberPUP();

logger.fencepost("unittest.cpp", "End Unit Test", __LINE__);
Expand Down

0 comments on commit 996982e

Please sign in to comment.