Skip to content

Commit

Permalink
for meshtastic#4154 use a binary gpio transformer to manage vext on h…
Browse files Browse the repository at this point in the history
…eltec-tracker (saves power)
  • Loading branch information
geeksville committed Aug 28, 2024
1 parent cdafa87 commit 8a9cc72
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
16 changes: 15 additions & 1 deletion src/GpioLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void GpioVirtPin::set(bool value)

void GpioHwPin::set(bool value)
{
// if (num == 3) LOG_DEBUG("Setting pin %d to %d\n", num, value);
pinMode(num, OUTPUT);
digitalWrite(num, value);
}
Expand All @@ -23,7 +24,7 @@ void GpioTransformer::set(bool value)
outPin->set(value);
}

GpioNotTransformer::GpioNotTransformer(GpioVirtPin *inPin, GpioPin *outPin) : GpioTransformer(outPin), inPin(inPin)
GpioUnaryTransformer::GpioUnaryTransformer(GpioVirtPin *inPin, GpioPin *outPin) : GpioTransformer(outPin), inPin(inPin)
{
assert(!inPin->dependentPin); // We only allow one dependent pin
inPin->dependentPin = this;
Expand All @@ -33,6 +34,18 @@ GpioNotTransformer::GpioNotTransformer(GpioVirtPin *inPin, GpioPin *outPin) : Gp
// update();
}

/**
* Update the output pin based on the current state of the input pin.
*/
void GpioUnaryTransformer::update()
{
auto p = inPin->get();
if (p == GpioVirtPin::PinState::Unset)
return; // Not yet fully initialized

set(p);
}

/**
* Update the output pin based on the current state of the input pin.
*/
Expand Down Expand Up @@ -75,6 +88,7 @@ void GpioBinaryTransformer::update()
newValue = (GpioVirtPin::PinState)(p1 && p2);
break;
case Or:
// LOG_DEBUG("Doing GPIO OR\n");
newValue = (GpioVirtPin::PinState)(p1 || p2);
break;
case Xor:
Expand Down
28 changes: 22 additions & 6 deletions src/GpioLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GpioBinaryTransformer;
class GpioVirtPin : public GpioPin
{
friend class GpioBinaryTransformer;
friend class GpioNotTransformer;
friend class GpioUnaryTransformer;

public:
enum PinState { On = true, Off = false, Unset = 2 };
Expand Down Expand Up @@ -79,25 +79,41 @@ class GpioTransformer
};

/**
* A transformer that performs a unary NOT operation from an input.
* A transformer that just drives a hw pin based on a virtual pin.
*/
class GpioNotTransformer : public GpioTransformer
class GpioUnaryTransformer : public GpioTransformer
{
public:
GpioNotTransformer(GpioVirtPin *inPin, GpioPin *outPin);
GpioUnaryTransformer(GpioVirtPin *inPin, GpioPin *outPin);

protected:
friend class GpioVirtPin;

/**
* Update the output pin based on the current state of the input pin.
*/
void update();
virtual void update();

private:
GpioVirtPin *inPin;
};

/**
* A transformer that performs a unary NOT operation from an input.
*/
class GpioNotTransformer : public GpioUnaryTransformer
{
public:
GpioNotTransformer(GpioVirtPin *inPin, GpioPin *outPin) : GpioUnaryTransformer(inPin, outPin) {}

protected:
friend class GpioVirtPin;

/**
* Update the output pin based on the current state of the input pin.
*/
void update();
};

/**
* A transformer that combines multiple virtual pins to drive an output pin
*/
Expand Down
11 changes: 5 additions & 6 deletions src/gps/GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1415,19 +1415,18 @@ GPS *GPS::createGps()
new_gps->rx_gpio = _rx_gpio;
new_gps->tx_gpio = _tx_gpio;

GpioVirtPin *virtPin = new GpioVirtPin();
new_gps->enablePin = virtPin; // Always at least populate a virtual pin
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;
} else {
new GpioUnaryTransformer(
virtPin, p); // We just leave this created object on the heap so it can stay watching virtPin and driving en_gpio
}
new_gps->enablePin = p;
} else {
// Just use a simulated pin
new_gps->enablePin = new GpioVirtPin();
}

#ifdef PIN_GPS_PPS
Expand Down
2 changes: 1 addition & 1 deletion src/gps/GPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class GPS : private concurrency::OSThread
*
* Normally set by GPS::createGPS()
*/
GpioPin *enablePin;
GpioVirtPin *enablePin;

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

Expand Down
4 changes: 4 additions & 0 deletions src/graphics/TFTDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ extern SX1509 gpioExtender;
#define TFT_INVERT true
#endif

GpioPin *TFTDisplay::backlightEnable;

class LGFX : public lgfx::LGFX_Device
{
lgfx::Panel_ST7735S _panel_instance;
Expand Down Expand Up @@ -584,6 +586,7 @@ void TFTDisplay::sendCommand(uint8_t com)
// handle display on/off directly
switch (com) {
case DISPLAYON: {
// LOG_DEBUG("Display on\n");
backlightEnable->set(true);
#if ARCH_PORTDUINO
display(true);
Expand All @@ -607,6 +610,7 @@ void TFTDisplay::sendCommand(uint8_t com)
break;
}
case DISPLAYOFF: {
// LOG_DEBUG("Display off\n");
backlightEnable->set(false);
#if ARCH_PORTDUINO
tft->clear();
Expand Down
4 changes: 3 additions & 1 deletion src/graphics/TFTDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class TFTDisplay : public OLEDDisplay
/**
* This is normally managed entirely by TFTDisplay, but some rare applications (heltec tracker) might need to replace the
* default GPIO behavior with something a bit more complex.
*
* We (cruftily) make it static so that variant.cpp can access it without needing a ptr to the TFTDisplay instance.
*/
GpioPin *backlightEnable;
static GpioPin *backlightEnable;

protected:
// the header size of the buffer used, e.g. for the SPI command header
Expand Down

0 comments on commit 8a9cc72

Please sign in to comment.