diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 7a5921cf..83afe4fb 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -60,7 +60,7 @@ #define PUBLIC_GROUP_PSK "izOH6cXN6mrJ5e26oRXNcg==" -#ifdef DISPLAY_CLASS +#ifdef DISPLAY_CLASS // TODO: refactor this -- move to variants/*/target #include "UITask.h" #ifdef ST7735 #include @@ -71,7 +71,13 @@ #else #include #endif - static DISPLAY_CLASS display; + + #if defined(HELTEC_LORA_V3) && defined(ST7735) + static DISPLAY_CLASS display(&board.periph_power); // peripheral power pin is shared + #else + static DISPLAY_CLASS display; + #endif + #define HAS_UI #endif diff --git a/src/helpers/HeltecV3Board.h b/src/helpers/HeltecV3Board.h index fd6352ec..988e66d9 100644 --- a/src/helpers/HeltecV3Board.h +++ b/src/helpers/HeltecV3Board.h @@ -1,6 +1,7 @@ #pragma once #include +#include // LoRa radio module pins for Heltec V3 // Also for Heltec Wireless Tracker @@ -20,7 +21,6 @@ #define PIN_ADC_CTRL_ACTIVE LOW #define PIN_ADC_CTRL_INACTIVE HIGH //#define PIN_LED_BUILTIN 35 -#define PIN_VEXT_EN 36 #include "ESP32Board.h" @@ -31,6 +31,10 @@ private: bool adc_active_state; public: + RefCountedDigitalPin periph_power; + + HeltecV3Board() : periph_power(PIN_VEXT_EN) { } + void begin() { ESP32Board::begin(); @@ -41,8 +45,7 @@ public: pinMode(PIN_ADC_CTRL, OUTPUT); digitalWrite(PIN_ADC_CTRL, !adc_active_state); // Initially inactive - pinMode(PIN_VEXT_EN, OUTPUT); - digitalWrite(PIN_VEXT_EN, LOW); // for V3.2 boards + periph_power.begin(); esp_reset_reason_t reason = esp_reset_reason(); if (reason == ESP_RST_DEEPSLEEP) { diff --git a/src/helpers/RefCountedDigitalPin.h b/src/helpers/RefCountedDigitalPin.h new file mode 100644 index 00000000..14b67fb1 --- /dev/null +++ b/src/helpers/RefCountedDigitalPin.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +class RefCountedDigitalPin { + uint8_t _pin; + int8_t _claims = 0; + +public: + RefCountedDigitalPin(uint8_t pin): _pin(pin) { } + + void begin() { + pinMode(_pin, OUTPUT); + digitalWrite(_pin, LOW); // initial state + } + + void claim() { + _claims++; + if (_claims > 0) { + digitalWrite(_pin, HIGH); + } + } + void release() { + _claims--; + if (_claims == 0) { + digitalWrite(_pin, LOW); + } + } +}; diff --git a/src/helpers/ui/ST7735Display.cpp b/src/helpers/ui/ST7735Display.cpp index bb3c6473..91c40ea5 100644 --- a/src/helpers/ui/ST7735Display.cpp +++ b/src/helpers/ui/ST7735Display.cpp @@ -19,10 +19,10 @@ bool ST7735Display::i2c_probe(TwoWire& wire, uint8_t addr) { } bool ST7735Display::begin() { - if(!_isOn) { - pinMode(PIN_TFT_VDD_CTL, OUTPUT); + if (!_isOn) { + if (_peripher_power) _peripher_power->claim(); + pinMode(PIN_TFT_LEDA_CTL, OUTPUT); - digitalWrite(PIN_TFT_VDD_CTL, HIGH); digitalWrite(PIN_TFT_LEDA_CTL, HIGH); digitalWrite(PIN_TFT_RST, HIGH); @@ -40,18 +40,18 @@ bool ST7735Display::begin() { } void ST7735Display::turnOn() { - ST7735Display::begin(); - } void ST7735Display::turnOff() { - digitalWrite(PIN_TFT_VDD_CTL, HIGH); - digitalWrite(PIN_TFT_LEDA_CTL, HIGH); - digitalWrite(PIN_TFT_RST, LOW); - digitalWrite(PIN_TFT_VDD_CTL, LOW); - digitalWrite(PIN_TFT_LEDA_CTL, LOW); - _isOn = false; + if (_isOn) { + digitalWrite(PIN_TFT_LEDA_CTL, HIGH); + digitalWrite(PIN_TFT_RST, LOW); + digitalWrite(PIN_TFT_LEDA_CTL, LOW); + _isOn = false; + + if (_peripher_power) _peripher_power->release(); + } } void ST7735Display::clear() { diff --git a/src/helpers/ui/ST7735Display.h b/src/helpers/ui/ST7735Display.h index 8e9bd04c..94797503 100644 --- a/src/helpers/ui/ST7735Display.h +++ b/src/helpers/ui/ST7735Display.h @@ -5,18 +5,30 @@ #include #include #include +#include class ST7735Display : public DisplayDriver { Adafruit_ST7735 display; bool _isOn; uint16_t _color; + RefCountedDigitalPin* _peripher_power; bool i2c_probe(TwoWire& wire, uint8_t addr); public: #ifdef USE_PIN_TFT - ST7735Display() : DisplayDriver(128, 64), display(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_SDA, PIN_TFT_SCL, PIN_TFT_RST) { _isOn = false; } + ST7735Display(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64), + display(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_SDA, PIN_TFT_SCL, PIN_TFT_RST), + _peripher_power(peripher_power) + { + _isOn = false; + } #else - ST7735Display() : DisplayDriver(128, 64), display(&SPI1, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST) { _isOn = false; } + ST7735Display(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64), + display(&SPI1, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST), + _peripher_power(peripher_power) + { + _isOn = false; + } #endif bool begin(); diff --git a/variants/heltec_tracker/platformio.ini b/variants/heltec_tracker/platformio.ini index fdf3089c..679fe163 100644 --- a/variants/heltec_tracker/platformio.ini +++ b/variants/heltec_tracker/platformio.ini @@ -19,7 +19,7 @@ build_flags = -D PIN_TFT_RST=39 ; RES -D PIN_TFT_CS=38 -D USE_PIN_TFT=1 - -D PIN_TFT_VDD_CTL=3 ; Vext is connected to VDD which is also connected to LEDA + -D PIN_VEXT_EN=3 ; Vext is connected to VDD which is also connected to OLED & GPS -D PIN_TFT_LEDA_CTL=21 ; LEDK (switches on/off via mosfet to create the ground) -D PIN_GPS_RX=33 -D PIN_GPS_TX=34 diff --git a/variants/heltec_tracker/target.cpp b/variants/heltec_tracker/target.cpp index 792ce45b..c82b70ad 100644 --- a/variants/heltec_tracker/target.cpp +++ b/variants/heltec_tracker/target.cpp @@ -1,5 +1,6 @@ #include #include "target.h" + #include HeltecV3Board board; @@ -78,18 +79,25 @@ mesh::LocalIdentity radio_new_identity() { } void HWTSensorManager::start_gps() { - gps_active = true; - // init GPS - Serial1.begin(115200, SERIAL_8N1, PIN_GPS_RX, PIN_GPS_TX); - Serial1.println("$CFGSYS,h35155*68"); + if (!gps_active) { + board.periph_power.claim(); + + gps_active = true; + Serial1.println("$CFGSYS,h35155*68"); + } } void HWTSensorManager::stop_gps() { - gps_active = false; - Serial1.end(); + if (gps_active) { + gps_active = false; + + board.periph_power.release(); + } } bool HWTSensorManager::begin() { + // init GPS port + Serial1.begin(115200, SERIAL_8N1, PIN_GPS_RX, PIN_GPS_TX); return true; } @@ -106,10 +114,10 @@ void HWTSensorManager::loop() { _location->loop(); if (millis() > next_gps_update) { - if (_location->isValid()) { + if (gps_active && _location->isValid()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; - //Serial.printf("lat %f lon %f\r\n", _lat, _lon); + MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon); } next_gps_update = millis() + 1000; } diff --git a/variants/heltec_v3/platformio.ini b/variants/heltec_v3/platformio.ini index 8452fbf5..0dbf0917 100644 --- a/variants/heltec_v3/platformio.ini +++ b/variants/heltec_v3/platformio.ini @@ -12,6 +12,7 @@ build_flags = -D PIN_BOARD_SDA=17 -D PIN_BOARD_SCL=18 -D PIN_USER_BTN=0 + -D PIN_VEXT_EN=36 -D SX126X_DIO2_AS_RF_SWITCH=true -D SX126X_DIO3_TCXO_VOLTAGE=1.8 -D SX126X_CURRENT_LIMIT=130.0f ; for best TX power!