Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #5193: Optimise GPS probe code - one message per family #5244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/gps/GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ GPS *gps = nullptr;

GPSUpdateScheduling scheduling;

static const char *lastcmd = nullptr;
static GPS_RESPONSE cachedResponseStatus = GNSS_RESPONSE_NONE;

/// Multiple GPS instances might use the same serial port (in sequence), but we can
/// only init that port once.
static bool didSerialInit;
Expand Down Expand Up @@ -185,6 +188,7 @@ GPS_RESPONSE GPS::getACK(const char *message, uint32_t waitMillis)
#ifdef GPS_DEBUG
LOG_DEBUG("Found: %s", message); // Log the found message
#endif
cachedResponseStatus = GNSS_RESPONSE_OK;
return GNSS_RESPONSE_OK;
} else {
bytesRead = 0;
Expand All @@ -195,6 +199,7 @@ GPS_RESPONSE GPS::getACK(const char *message, uint32_t waitMillis)
}
}
}
cachedResponseStatus = GNSS_RESPONSE_NONE;
return GNSS_RESPONSE_NONE;
}

Expand Down Expand Up @@ -1099,15 +1104,28 @@ int GPS::prepareDeepSleep(void *unused)
}

const char *PROBE_MESSAGE = "Trying %s (%s)...";
const char *PROBE_MESSAGE_PREVIOUS = "Using previous buffer for command: %s";
const char *DETECTED_MESSAGE = "%s detected, using %s Module";

#define PROBE_SIMPLE(CHIP, TOWRITE, RESPONSE, DRIVER, TIMEOUT, ...) \
LOG_DEBUG(PROBE_MESSAGE, TOWRITE, CHIP); \
clearBuffer(); \
_serial_gps->write(TOWRITE "\r\n"); \
if (getACK(RESPONSE, TIMEOUT) == GNSS_RESPONSE_OK) { \
LOG_INFO(DETECTED_MESSAGE, CHIP, #DRIVER); \
return DRIVER; \
#define PROBE_SIMPLE(CHIP, TOWRITE, RESPONSE, DRIVER, TIMEOUT, ...) \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you implement this as a (lambda-) function? This macro code is copied 10 times into the source and consumes unnecessarily additional space.

if (lastcmd && strcmp(TOWRITE, lastcmd) == 0) \
{ \
LOG_DEBUG(PROBE_MESSAGE_PREVIOUS, TOWRITE); \
if (cachedResponseStatus) \
{ \
LOG_INFO(DETECTED_MESSAGE, CHIP, #DRIVER); \
return DRIVER; \
} \
} else { \
LOG_DEBUG(PROBE_MESSAGE, TOWRITE, CHIP); \
clearBuffer(); \
_serial_gps->write(TOWRITE "\r\n"); \
lastcmd = TOWRITE; \
} \
if (getACK(RESPONSE, TIMEOUT) == GNSS_RESPONSE_OK) \
{ \
LOG_INFO(DETECTED_MESSAGE, CHIP, #DRIVER); \
return DRIVER; \
}

GnssModel_t GPS::probe(int serialSpeed)
Expand Down
Loading