فهرست منبع

* Heltec tracker: new 'periph_power' shared pin (between Display & GPS)

Scott Powell 1 سال پیش
والد
کامیت
810fc8b8f0

+ 8 - 2
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 <helpers/ui/ST7735Display.h>
@@ -71,7 +71,13 @@
   #else
     #include <helpers/ui/SSD1306Display.h>
   #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
 

+ 6 - 3
src/helpers/HeltecV3Board.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <Arduino.h>
+#include <helpers/RefCountedDigitalPin.h>
 
 // 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) {

+ 29 - 0
src/helpers/RefCountedDigitalPin.h

@@ -0,0 +1,29 @@
+#pragma once
+
+#include <Arduino.h>
+
+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);
+    }
+  }
+};

+ 11 - 11
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() {

+ 14 - 2
src/helpers/ui/ST7735Display.h

@@ -5,18 +5,30 @@
 #include <SPI.h>
 #include <Adafruit_GFX.h>
 #include <Adafruit_ST7735.h>
+#include <helpers/RefCountedDigitalPin.h>
 
 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();
 

+ 1 - 1
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

+ 16 - 8
variants/heltec_tracker/target.cpp

@@ -1,5 +1,6 @@
 #include <Arduino.h>
 #include "target.h"
+
 #include <helpers/sensors/MicroNMEALocationProvider.h>
 
 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;
   }

+ 1 - 0
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!