Skip to content

Commit

Permalink
Clean up some inline functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thebentern authored and caveman99 committed Nov 26, 2024
1 parent 09286a3 commit b430aeb
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 43 deletions.
20 changes: 20 additions & 0 deletions src/gps/GeoCoord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,23 @@ const char *GeoCoord::degreesToBearing(unsigned int degrees)
else
return "N";
}

double GeoCoord::pow_neg(double base, double exponent)
{
if (exponent == 0) {
return 1;
} else if (exponent > 0) {
return pow(base, exponent);
}
return 1 / pow(base, -exponent);
}

double GeoCoord::toRadians(double deg)
{
return deg * PI / 180;
}

double GeoCoord::toDegrees(double r)
{
return r * 180 / PI;
}
29 changes: 6 additions & 23 deletions src/gps/GeoCoord.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,6 @@
#define OLC_CODE_LEN 11
#define DEG_CONVERT (180 / PI)

// Helper functions
// Raises a number to an exponent, handling negative exponents.
static inline double pow_neg(double base, double exponent)
{
if (exponent == 0) {
return 1;
} else if (exponent > 0) {
return pow(base, exponent);
}
return 1 / pow(base, -exponent);
}

static inline double toRadians(double deg)
{
return deg * PI / 180;
}

static inline double toDegrees(double r)
{
return r * 180 / PI;
}

// GeoCoord structs/classes
// A struct to hold the data for a DMS coordinate.
struct DMS {
Expand Down Expand Up @@ -120,6 +98,11 @@ class GeoCoord
static unsigned int bearingToDegrees(const char *bearing);
static const char *degreesToBearing(unsigned int degrees);

// Raises a number to an exponent, handling negative exponents.
static double pow_neg(double base, double exponent);
static double toRadians(double deg);
static double toDegrees(double r);

// Point to point conversions
int32_t distanceTo(const GeoCoord &pointB);
int32_t bearingTo(const GeoCoord &pointB);
Expand Down Expand Up @@ -162,4 +145,4 @@ class GeoCoord

// OLC getter
void getOLCCode(char *code) { strncpy(code, _olc.code, OLC_CODE_LEN + 1); } // +1 for null termination
};
};
6 changes: 3 additions & 3 deletions src/graphics/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ static void drawNodeInfo(OLEDDisplay *display, OLEDDisplayUiState *state, int16_
}
bool hasNodeHeading = false;

if (ourNode && (hasValidPosition(ourNode) || screen->hasHeading())) {
if (ourNode && (nodeDB->hasValidPosition(ourNode) || screen->hasHeading())) {
const meshtastic_PositionLite &op = ourNode->position;
float myHeading;
if (screen->hasHeading())
Expand All @@ -1429,7 +1429,7 @@ static void drawNodeInfo(OLEDDisplay *display, OLEDDisplayUiState *state, int16_
myHeading = screen->estimatedHeading(DegD(op.latitude_i), DegD(op.longitude_i));
screen->drawCompassNorth(display, compassX, compassY, myHeading);

if (hasValidPosition(node)) {
if (nodeDB->hasValidPosition(node)) {
// display direction toward node
hasNodeHeading = true;
const meshtastic_PositionLite &p = node->position;
Expand Down Expand Up @@ -2757,4 +2757,4 @@ int Screen::handleAdminMessage(const meshtastic_AdminMessage *arg)
} // namespace graphics
#else
graphics::Screen::Screen(ScanI2C::DeviceAddress, meshtastic_Config_DisplayConfig_OledType, OLEDDISPLAY_GEOMETRY) {}
#endif // HAS_SCREEN
#endif // HAS_SCREEN
2 changes: 1 addition & 1 deletion src/mesh/MeshService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ bool MeshService::trySendPosition(NodeNum dest, bool wantReplies)

assert(node);

if (hasValidPosition(node)) {
if (nodeDB->hasValidPosition(node)) {
#if HAS_GPS && !MESHTASTIC_EXCLUDE_GPS
if (positionModule) {
LOG_INFO("Send position ping to 0x%x, wantReplies=%d, channel=%d", dest, wantReplies, node->channel);
Expand Down
7 changes: 7 additions & 0 deletions src/mesh/NodeDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,13 @@ meshtastic_NodeInfoLite *NodeDB::getOrCreateMeshNode(NodeNum n)
return lite;
}

/// Sometimes we will have Position objects that only have a time, so check for
/// valid lat/lon
bool NodeDB::hasValidPosition(const meshtastic_NodeInfoLite *n)
{
return n->has_position && (n->position.latitude_i != 0 || n->position.longitude_i != 0);
}

/// Record an error that should be reported via analytics
void recordCriticalError(meshtastic_CriticalErrorCode code, uint32_t address, const char *filename)
{
Expand Down
9 changes: 2 additions & 7 deletions src/mesh/NodeDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ class NodeDB
localPosition = position;
}

bool hasValidPosition(const meshtastic_NodeInfoLite *n);

private:
uint32_t lastNodeDbSave = 0; // when we last saved our db to flash
/// Find a node in our DB, create an empty NodeInfoLite if missing
Expand Down Expand Up @@ -217,13 +219,6 @@ extern NodeDB *nodeDB;
prefs.is_power_saving = True
*/

/// Sometimes we will have Position objects that only have a time, so check for
/// valid lat/lon
static inline bool hasValidPosition(const meshtastic_NodeInfoLite *n)
{
return n->has_position && (n->position.latitude_i != 0 || n->position.longitude_i != 0);
}

/** The current change # for radio settings. Starts at 0 on boot and any time the radio settings
* might have changed is incremented. Allows others to detect they might now be on a new channel.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/aes-ccm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "aes-ccm.h"
#if !MESHTASTIC_EXCLUDE_PKI

static inline void WPA_PUT_BE16(uint8_t *a, uint16_t val)
static void WPA_PUT_BE16(uint8_t *a, uint16_t val)
{
a[0] = val >> 8;
a[1] = val & 0xff;
Expand Down
8 changes: 4 additions & 4 deletions src/modules/PositionModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ int32_t PositionModule::runOnce()
}

if (lastGpsSend == 0 || msSinceLastSend >= intervalMs) {
if (hasValidPosition(node)) {
if (nodeDB->hasValidPosition(node)) {
lastGpsSend = now;

lastGpsLatitude = node->position.latitude_i;
Expand All @@ -403,7 +403,7 @@ int32_t PositionModule::runOnce()
} else if (config.position.position_broadcast_smart_enabled) {
const meshtastic_NodeInfoLite *node2 = service->refreshLocalMeshNode(); // should guarantee there is now a position

if (hasValidPosition(node2)) {
if (nodeDB->hasValidPosition(node2)) {
// The minimum time (in seconds) that would pass before we are able to send a new position packet.

auto smartPosition = getDistanceTraveledSinceLastSend(node->position);
Expand Down Expand Up @@ -464,7 +464,7 @@ void PositionModule::handleNewPosition()
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(nodeDB->getNodeNum());
const meshtastic_NodeInfoLite *node2 = service->refreshLocalMeshNode(); // should guarantee there is now a position
// We limit our GPS broadcasts to a max rate
if (hasValidPosition(node2)) {
if (nodeDB->hasValidPosition(node2)) {
auto smartPosition = getDistanceTraveledSinceLastSend(node->position);
uint32_t msSinceLastSend = millis() - lastGpsSend;
if (smartPosition.hasTraveledOverThreshold &&
Expand All @@ -483,4 +483,4 @@ void PositionModule::handleNewPosition()
}
}

#endif
#endif
6 changes: 3 additions & 3 deletions src/modules/SerialModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ int32_t SerialModule::runOnce()
uint32_t readIndex = 0;
const meshtastic_NodeInfoLite *tempNodeInfo = nodeDB->readNextMeshNode(readIndex);
while (tempNodeInfo != NULL) {
if (tempNodeInfo->has_user && hasValidPosition(tempNodeInfo)) {
if (tempNodeInfo->has_user && nodeDB->hasValidPosition(tempNodeInfo)) {
printWPL(outbuf, sizeof(outbuf), tempNodeInfo->position, tempNodeInfo->user.long_name, true);
serialPrint->printf("%s", outbuf);
}
Expand Down Expand Up @@ -474,7 +474,7 @@ void SerialModule::processWXSerial()
if (windDirPos != NULL) {
// Extract data after "=" for WindDir
strcpy(windDir, windDirPos + 15); // Add 15 to skip "WindDir = "
double radians = toRadians(strtof(windDir, nullptr));
double radians = GeoCoord::toRadians(strtof(windDir, nullptr));
dir_sum_sin += sin(radians);
dir_sum_cos += cos(radians);
dirCount++;
Expand Down Expand Up @@ -541,7 +541,7 @@ void SerialModule::processWXSerial()
double avgCos = dir_sum_cos / dirCount;

double avgRadians = atan2(avgSin, avgCos);
float dirAvg = toDegrees(avgRadians);
float dirAvg = GeoCoord::toDegrees(avgRadians);

if (dirAvg < 0) {
dirAvg += 360.0;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/WaypointModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void WaypointModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state,
}

// If our node has a position:
if (ourNode && (hasValidPosition(ourNode) || screen->hasHeading())) {
if (ourNode && (nodeDB->hasValidPosition(ourNode) || screen->hasHeading())) {
const meshtastic_PositionLite &op = ourNode->position;
float myHeading;
if (screen->hasHeading())
Expand Down

0 comments on commit b430aeb

Please sign in to comment.