From cd2fa5136035a935257356bf53d2c80ad9db7bb1 Mon Sep 17 00:00:00 2001 From: "Hansi, dl9rdz" Date: Fri, 31 Jan 2020 19:33:55 +0100 Subject: [PATCH] use PWR button as button2 on T-Beam 1.0 --- RX_FSK/RX_FSK.ino | 67 ++++++++++++++++++++++--------- RX_FSK/data/config.txt | 1 + libraries/SX1278FSK/SX1278FSK.cpp | 3 -- libraries/SondeLib/Display.cpp | 5 +++ libraries/SondeLib/Sonde.cpp | 6 +++ libraries/SondeLib/Sonde.h | 1 + 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/RX_FSK/RX_FSK.ino b/RX_FSK/RX_FSK.ino index d9d3e70..bbd0293 100644 --- a/RX_FSK/RX_FSK.ino +++ b/RX_FSK/RX_FSK.ino @@ -32,7 +32,8 @@ AsyncWebSocket ws("/ws"); AXP20X_Class axp; #define PMU_IRQ 35 - +SemaphoreHandle_t axpSemaphore; +bool pmu_irq = false; String updateHost = "rdzsonde.mooo.com"; int updatePort = 80; @@ -359,9 +360,9 @@ void addSondeStatus(char *ptr, int i) ts = *gmtime(&t); sprintf(ptr + strlen(ptr), "Frame# %d, Sats=%d, %04d-%02d-%02d %02d:%02d:%02d", s->frame, s->sats, ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec + s->sec); - if(s->type == STYPE_RS41) { - sprintf(ptr + strlen(ptr), "Burst-KT=%d Launch-KT=%d Countdown=%d (vor %ds)\n", - s->burstKT, s->launchKT, s->countKT, ((uint16_t)s->frame-s->crefKT)); + if (s->type == STYPE_RS41) { + sprintf(ptr + strlen(ptr), "Burst-KT=%d Launch-KT=%d Countdown=%d (vor %ds)\n", + s->burstKT, s->launchKT, s->countKT, ((uint16_t)s->frame - s->crefKT)); } sprintf(ptr + strlen(ptr), "GEO-App - ", s->lat, s->lon); sprintf(ptr + strlen(ptr), "WX.DL2MF.de - ", s->id); @@ -459,6 +460,7 @@ struct st_configitems config_list[] = { {"tft_orient", "TFT orientation (0/1/2/3), OLED flip: 3", 0, &sonde.config.tft_orient}, {"button_pin", "Button input port", -4, &sonde.config.button_pin}, {"button2_pin", "Button 2 input port", -4, &sonde.config.button2_pin}, + {"button2_axp", "Use AXP192 PWR as Button 2", 0, &sonde.config.button2_axp}, {"touch_thresh", "Touch button threshold
(0 for calib mode)", 0, &sonde.config.touch_thresh}, {"power_pout", "Power control port", 0, &sonde.config.power_pout}, {"led_pout", "LED output port", 0, &sonde.config.led_pout}, @@ -477,9 +479,16 @@ void addConfigNumEntry(char *ptr, int idx, const char *label, int *value) { label, idx, *value); } void addConfigButtonEntry(char *ptr, int idx, const char *label, int *value) { + int v = *value, ck = 0; + if (v == 255) v = -1; + if (v != -1) { + if (v & 128) ck = 1; + v = v & 127; + } + sprintf(ptr + strlen(ptr), "%s", - label, idx, 127 & *value); - sprintf(ptr + strlen(ptr), " Touch \n", idx, 128 & *value ? " checked" : ""); + label, idx, v); + sprintf(ptr + strlen(ptr), " Touch \n", idx, ck ? " checked" : ""); } void addConfigTypeEntry(char *ptr, int idx, const char *label, int *value) { // TODO @@ -583,7 +592,8 @@ const char *handleConfigPost(AsyncWebServerRequest *request) { snprintf(tmp, 10, "TO%d", idx); AsyncWebParameter *touch = request->getParam(tmp, true); if (touch) { - int i = atoi(strvalue.c_str()) + 128; + int i = atoi(strvalue.c_str()); + if (i != -1 && i != 255) i += 128; strvalue = String(i); } } @@ -747,7 +757,7 @@ const char *sendGPX(AsyncWebServerRequest * request) { } SondeInfo *si = &sonde.sondeList[index]; strcpy(si->id, "test"); - si->lat=48; si->lon=11; si->alt=500; + si->lat = 48; si->lon = 11; si->alt = 500; snprintf(ptr, 10240, "\n" "text("Hello from ESP32 Server"); - } else if(type == WS_EVT_DISCONNECT){ + } else if (type == WS_EVT_DISCONNECT) { Serial.println("Client disconnected"); - } + } } #if 0 void onWebSocketEvent(uint8_t clientNum, WStype_t type, uint8_t *payload, size_t length) { - switch(type) { + switch (type) { case WStype_DISCONNECTED: Serial.printf("[%u] WS client disconnected\n", clientNum); break; @@ -1227,14 +1237,31 @@ int getKeyPress() { } int getKey2Press() { + if (sonde.config.button2_axp) { + // Use AXP power button as second button + if (pmu_irq) { + Serial.println("PMU_IRQ is set\n"); + xSemaphoreTake( axpSemaphore, portMAX_DELAY ); + axp.readIRQ(); + if (axp.isPEKShortPressIRQ()) { + button2.pressed = KP_SHORT; + button2.keydownTime = my_millis(); + } + if (axp.isPEKLongtPressIRQ()) { + button2.pressed = KP_MID; + button2.keydownTime = my_millis(); + } + pmu_irq = false; + axp.clearIRQ(); + xSemaphoreGive( axpSemaphore ); + } + } KeyPress p = button2.pressed; button2.pressed = KP_NONE; //Serial.printf("button2 press: %d at %ld (%d)\n", p, button2.keydownTime, button2.numberKeyPresses); return p; } -int hasKeyPress() { - return button1.pressed || button2.pressed; -} + int getKeyPressEvent() { int p = getKeyPress(); if (p == KP_NONE) { @@ -1290,8 +1317,6 @@ int scanI2Cdevice(void) } extern int initlevels[40]; -bool pmu_irq = false; - void setup() { @@ -1303,6 +1328,9 @@ void setup() Serial.printf("%d:%d ", i, v); } Serial.println(""); + axpSemaphore = xSemaphoreCreateBinary(); + xSemaphoreGive(axpSemaphore); + #if 0 delay(2000); // temporary test @@ -1369,7 +1397,8 @@ void setup() pmu_irq = true; }, FALLING); axp.adc1Enable(AXP202_BATT_CUR_ADC1, 1); - axp.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ, 1); + //axp.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ, 1); + axp.enableIRQ( AXP202_PEK_LONGPRESS_IRQ | AXP202_PEK_SHORTPRESS_IRQ, 1 ); axp.clearIRQ(); int ndevices = scanI2Cdevice(); if (sonde.fingerprint != 17 || ndevices > 0) break; // only retry for fingerprint 17 (startup problems of new t-beam with oled) diff --git a/RX_FSK/data/config.txt b/RX_FSK/data/config.txt index 4d3039f..850aa8d 100644 --- a/RX_FSK/data/config.txt +++ b/RX_FSK/data/config.txt @@ -7,6 +7,7 @@ # No specification in config file: try autodetection (gpio4 pin level at startup) #button_pin=0 #button2_pin=255 +#button2_axp=0 # LED port led_pout=9 # OLED Setup is depending on hardware of LoRa board diff --git a/libraries/SX1278FSK/SX1278FSK.cpp b/libraries/SX1278FSK/SX1278FSK.cpp index abf9c93..fa25d87 100644 --- a/libraries/SX1278FSK/SX1278FSK.cpp +++ b/libraries/SX1278FSK/SX1278FSK.cpp @@ -679,9 +679,6 @@ uint8_t SX1278FSK::receive() return state; } -// ugly. shouldn't be here in a nice software design -extern int hasKeyPress(); - /* Function: Configures the module to receive a packet Returns: Integer that determines if there has been any error diff --git a/libraries/SondeLib/Display.cpp b/libraries/SondeLib/Display.cpp index d8ea0ec..1a6b14f 100644 --- a/libraries/SondeLib/Display.cpp +++ b/libraries/SondeLib/Display.cpp @@ -23,6 +23,8 @@ extern MicroNMEA nmea; extern AXP20X_Class axp; extern bool axp192_found; +extern SemaphoreHandle_t axpSemaphore; + SPIClass spiDisp(HSPI); @@ -1353,6 +1355,8 @@ void Display::drawBatt(DispEntry *de) { float val; char buf[30]; if(!axp192_found) return; + + xSemaphoreTake( axpSemaphore, portMAX_DELAY ); switch(de->extra[0]) { case 'S': if(!axp.isBatteryConnect()) { @@ -1389,6 +1393,7 @@ void Display::drawBatt(DispEntry *de) { default: *buf=0; } + xSemaphoreGive( axpSemaphore ); rdis->setFont(de->fmt); drawString(de, buf); } diff --git a/libraries/SondeLib/Sonde.cpp b/libraries/SondeLib/Sonde.cpp index 9f9e519..5a5ab0f 100644 --- a/libraries/SondeLib/Sonde.cpp +++ b/libraries/SondeLib/Sonde.cpp @@ -78,6 +78,7 @@ void Sonde::defaultConfig() { config.oled_rst = 16; config.disptype = 0; config.tft_orient = 1; + config.button2_axp = 0; if(initlevels[16]==0) { config.oled_sda = 4; config.oled_scl = 15; @@ -94,6 +95,9 @@ void Sonde::defaultConfig() { Serial.println("Autoconfig: looks like T-Beam 1.0 board"); config.button_pin = 38; config.button2_pin = 15 + 128; //T4 + 128; // T4 = GPIO13 + // Maybe in future use as default only PWR as button2? + //config.button2_pin = 255; + config.button2_axp = 1; config.gps_rxd = 34; // Check for I2C-Display@21,22 #define SSD1306_ADDRESS 0x3c @@ -204,6 +208,8 @@ void Sonde::setConfig(const char *cfg) { config.button_pin = atoi(val); } else if(strcmp(cfg,"button2_pin")==0) { config.button2_pin = atoi(val); + } else if(strcmp(cfg,"button2_axp")==0) { + config.button2_axp = atoi(val); } else if(strcmp(cfg,"touch_thresh")==0) { config.touch_thresh = atoi(val); } else if(strcmp(cfg,"led_pout")==0) { diff --git a/libraries/SondeLib/Sonde.h b/libraries/SondeLib/Sonde.h index dc19733..1dc1f44 100644 --- a/libraries/SondeLib/Sonde.h +++ b/libraries/SondeLib/Sonde.h @@ -149,6 +149,7 @@ typedef struct st_rdzconfig { // hardware configuration int button_pin; // PIN port number menu button (+128 for touch mode) int button2_pin; // PIN port number menu button (+128 for touch mode) + int button2_axp; // Use AXP192 power button as button2 int touch_thresh; // Threshold value (0..100) for touch input button int led_pout; // POUT port number of LED (used as serial monitor) int power_pout; // Power control pin (for Heltec v2)