Skip to content

Commit

Permalink
change GPS to use virtual GPIOs (for meshtastic#4154)
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksville committed Aug 22, 2024
1 parent 2dda640 commit 5570b6b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/GpioLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ void GpioVirtPin::set(bool value)
}
}

void GpioHwPin::set(bool value)
{
pinMode(num, OUTPUT);
digitalWrite(num, value);
}

GpioTransformer::GpioTransformer(GpioPin *outPin) : outPin(outPin) {}

void GpioTransformer::set(bool value)
Expand Down
2 changes: 1 addition & 1 deletion src/GpioLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GpioHwPin : public GpioPin
public:
explicit GpioHwPin(uint32_t num) : num(num) {}

void set(bool value) { digitalWrite(num, value); }
void set(bool value);
};

class GpioTransformer;
Expand Down
27 changes: 17 additions & 10 deletions src/gps/GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#if !MESHTASTIC_EXCLUDE_GPS
#include "Default.h"
#include "GPS.h"
#include "GpioLogic.h"
#include "NodeDB.h"
#include "PowerMon.h"
#include "RTC.h"
Expand Down Expand Up @@ -875,16 +876,8 @@ void GPS::writePinEN(bool on)
if (HW_VENDOR == meshtastic_HardwareModel_RAK4631 && (rotaryEncoderInterruptImpl1 || upDownInterruptImpl1))
return;

// Abort: if pin unset
if (!en_gpio)
return;

// Determine new value for the pin
bool val = GPS_EN_ACTIVE ? on : !on;

// Write and log
pinMode(en_gpio, OUTPUT);
digitalWrite(en_gpio, val);
enablePin->set(on);
#ifdef GPS_EXTRAVERBOSE
LOG_DEBUG("Pin EN %s\n", val == HIGH ? "HIGH" : "LOW");
#endif
Expand Down Expand Up @@ -1421,7 +1414,21 @@ GPS *GPS::createGps()
GPS *new_gps = new GPS;
new_gps->rx_gpio = _rx_gpio;
new_gps->tx_gpio = _tx_gpio;
new_gps->en_gpio = _en_gpio;

if (_en_gpio) {
GpioPin *p = new GpioHwPin(_en_gpio);

if (!GPS_EN_ACTIVE) { // Need to invert the pin before hardware
auto virtPin = new GpioVirtPin();
new GpioNotTransformer(
virtPin, p); // We just leave this created object on the heap so it can stay watching virtPin and driving en_gpio
p = virtPin;
}
new_gps->enablePin = p;
} else {
// Just use a simulated pin
new_gps->enablePin = new GpioVirtPin();
}

#ifdef PIN_GPS_PPS
// pulse per second
Expand Down
11 changes: 9 additions & 2 deletions src/gps/GPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if !MESHTASTIC_EXCLUDE_GPS

#include "GPSStatus.h"
#include "GpioLogic.h"
#include "Observer.h"
#include "TinyGPS++.h"
#include "concurrency/OSThread.h"
Expand Down Expand Up @@ -73,7 +74,6 @@ class GPS : private concurrency::OSThread
uint32_t lastWakeStartMsec = 0, lastSleepStartMsec = 0, lastFixStartMsec = 0;
uint32_t rx_gpio = 0;
uint32_t tx_gpio = 0;
uint32_t en_gpio = 0;

int speedSelect = 0;
int probeTries = 2;
Expand Down Expand Up @@ -152,6 +152,13 @@ class GPS : private concurrency::OSThread

meshtastic_Position p = meshtastic_Position_init_default;

/** This is normally bound to config.position.gps_en_gpio but some rare boards (like heltec tracker) need more advanced
* implementations. Those boards will set this public variable to a custom implementation.
*
* Normally set by GPS::createGPS()
*/
GpioPin *enablePin;

GPS() : concurrency::OSThread("GPS") {}

virtual ~GPS();
Expand Down Expand Up @@ -303,4 +310,4 @@ class GPS : private concurrency::OSThread
};

extern GPS *gps;
#endif // Exclude GPS
#endif // Exclude GPS

0 comments on commit 5570b6b

Please sign in to comment.