From a1a1dca5c3683fec98894495b8f6826a252898ac Mon Sep 17 00:00:00 2001 From: Pawel Jalocha Date: Mon, 29 Apr 2019 14:26:12 +0100 Subject: [PATCH] Attempt to enter sleep/shutdown when button pressed for 4sec --- main/config.h | 14 ++++---- main/ctrl.cpp | 21 +++++++----- main/gps.cpp | 6 ++++ main/hal.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++------ main/hal.h | 4 +++ main/rf.cpp | 3 ++ 6 files changed, 114 insertions(+), 27 deletions(-) diff --git a/main/config.h b/main/config.h index 5af80f6..b895eef 100644 --- a/main/config.h +++ b/main/config.h @@ -5,21 +5,21 @@ #define DEFAULT_FreqPlan 0 // #define WITH_HELTEC // HELTEC module: PCB LED on GPI025 -// #define WITH_TTGO // TTGO module: PCB LED on GPIO2, GPIO25 free to use as DAC2 output -#define WITH_TBEAM // T-Beam module +#define WITH_TTGO // TTGO module: PCB LED on GPIO2, GPIO25 free to use as DAC2 output +// #define WITH_TBEAM // T-Beam module // #define WITH_FollowMe // by Avionix #define WITH_RFM95 // #define WITH_RFM69 -// #define WITH_OLED // OLED display on the I2C: some TTGO modules are without OLED display +#define WITH_OLED // OLED display on the I2C: some TTGO modules are without OLED display // #define WITH_OLED2 // 2nd OLED display, I2C address next higher // #define WITH_LED_RX // #define WITH_LED_TX // #define WITH_GPS_ENABLE // use GPS_ENABLE control line to turn the GPS ON/OFF -// #define WITH_GPS_PPS // use the PPS signal from GPS for precise time-sync. +#define WITH_GPS_PPS // use the PPS signal from GPS for precise time-sync. #define WITH_GPS_CONFIG // attempt to configure higher GPS baud rate and airborne mode #define WITH_GPS_UBX // GPS understands UBX // #define WITH_GPS_MTK // GPS understands MTK @@ -31,7 +31,7 @@ // #define WITH_BMP180 // BMP180 pressure sensor // #define WITH_BMP280 // BMP280 pressure sensor -#define WITH_BME280 // BMP280 with humidity (but still works with BMP280) +// #define WITH_BME280 // BMP280 with humidity (but still works with BMP280) // #define WITH_MS5607 // MS5607 pressure sensor #define WITH_PFLAA // PFLAU and PFLAA for compatibility with XCsoar and LK8000 @@ -41,8 +41,8 @@ // #define WITH_BEEPER // #define WITH_SD // use the SD card in SPI mode and FAT file system -#define WITH_SPIFFS // use SPIFFS file system in Flash -#define WITH_LOG // log own positions and other received to SPIFFS and possibly to uSD +// #define WITH_SPIFFS // use SPIFFS file system in Flash +// #define WITH_LOG // log own positions and other received to SPIFFS and possibly to uSD #define WITH_BT_SPP // Bluetooth serial port for smartphone/tablet link // #define WITH_WIFI // attempt to connect to the wifi router for uploading the log files diff --git a/main/ctrl.cpp b/main/ctrl.cpp index 0b4b992..9cda693 100644 --- a/main/ctrl.cpp +++ b/main/ctrl.cpp @@ -245,8 +245,6 @@ static void ProcessCtrlL(void) // print syste #endif } - - static void ProcessInput(void) { for( ; ; ) { uint8_t Byte; int Err=CONS_UART_Read(Byte); if(Err<=0) break; // get byte from console, if none: exit the loop @@ -286,8 +284,9 @@ void vTaskCTRL(void* pvParameters) LED_TimerCheck(1); // update the LED flashes #ifdef WITH_BEEPER - Play_TimerCheck(); // update the LED flashes + Play_TimerCheck(1); // read the button(s) #endif + Button_TimerCheck(); uint32_t Time=TimeSync_Time(); GPS_Position *GPS = GPS_getPosition(); @@ -297,12 +296,16 @@ void vTaskCTRL(void* pvParameters) PrevTime=Time; PrevGPS=GPS; #ifdef WITH_OLED - esp_err_t StatErr=ESP_OK; - esp_err_t PosErr=ESP_OK; - if(TimeChange) - { StatErr = OLED_DisplayStatus(Time, 0); } - if(GPSchange) - { PosErr = OLED_DisplayPosition(GPS, 2); } + if(Button_SleepRequest) + { OLED_DisplayON(0); } + else + { esp_err_t StatErr=ESP_OK; + esp_err_t PosErr=ESP_OK; + if(TimeChange) + { StatErr = OLED_DisplayStatus(Time, 0); } + if(GPSchange) + { PosErr = OLED_DisplayPosition(GPS, 2); } + } #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); if(TimeChange) diff --git a/main/gps.cpp b/main/gps.cpp index 8847995..d213f55 100644 --- a/main/gps.cpp +++ b/main/gps.cpp @@ -404,6 +404,12 @@ static void GPS_NMEA(void) // wh if(NMEA.isGxRMC()) GPS_Burst.GxRMC=1; if(NMEA.isGxGGA()) GPS_Burst.GxGGA=1; if(NMEA.isGxGSA()) GPS_Burst.GxGSA=1; + if(Button_SleepRequest) + { +#ifdef WITH_GPS_MTK + Format_String(GPS_UART_Write, "$PMTK225,4*2F\r\n", 15, 0); // request to the GPS to enter sleep +#endif + } #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_UnsDec(CONS_UART_Write, TimeSync_Time()%60); diff --git a/main/hal.cpp b/main/hal.cpp index 6471d47..3434b67 100644 --- a/main/hal.cpp +++ b/main/hal.cpp @@ -123,7 +123,7 @@ PSRM32 = SDIO ? GPIO HELTEC TTGO JACEK T-Beam FollowMe Restrictions - 0 . + 0 Button 1 CONS/TxD CONS/TxD CONS/TxD CONS/TxD Console/Program 2 SD/MISO . LED Bootstrap: LOW to enter UART download mode 3 CONS/RxD CONS/RxD CONS/RxD CONS/RxD Console/Program @@ -288,6 +288,12 @@ uint8_t BARO_I2C = (uint8_t)I2C_BUS; #define PIN_SD_SCK GPIO_NUM_14 #define PIN_SD_CS GPIO_NUM_15 +#ifdef WITH_FollowMe +#define PIN_BUTTON GPIO_NUM_39 +#else +#define PIN_BUTTON GPIO_NUM_0 +#endif + // ====================================================================================================== // 48-bit unique ID of the chip @@ -337,6 +343,11 @@ void LED_PCB_On (void) { } void LED_PCB_Off (void) { } #endif +#ifdef PIN_BUTTON +void Button_Dir (void) { gpio_set_direction(PIN_BUTTON, GPIO_MODE_INPUT); } +bool Button_isPressed(void) { return !gpio_get_level(PIN_BUTTON); } +#endif + // ======================================================================================================== // ~/esp-idf/components/bt/bluedroid/api/include/esp_spp_api.h @@ -742,6 +753,17 @@ esp_err_t OLED_Init(uint8_t DispIdx) i2c_cmd_link_delete(cmd); return espRc; } +esp_err_t OLED_DisplayON(uint8_t ON, uint8_t DispIdx) +{ i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, ((OLED_I2C_ADDR+DispIdx)<<1) | I2C_MASTER_WRITE, true); + i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true); + i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_OFF+ON, true); + i2c_master_stop(cmd); + esp_err_t espRc = i2c_master_cmd_begin(I2C_BUS, cmd, 10); + i2c_cmd_link_delete(cmd); + return espRc; } + esp_err_t OLED_SetContrast(uint8_t Contrast, uint8_t DispIdx) { i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); @@ -874,6 +896,62 @@ void LED_TimerCheck(uint8_t Ticks) #endif } +bool Button_SleepRequest = 0; + +const int8_t Button_FilterTime = 20; // [ms] +const int8_t Button_FilterThres = Button_FilterTime/2; +static int8_t Button_Filter=(-Button_FilterTime); + +void Sleep(void) +{ +// #ifdef PIN_PERIPH_RST +// gpio_set_level(PIN_PERIPH_RST, 0); +// #endif +#ifdef PIN_GPS_ENA + gpio_set_level(PIN_GPS_ENA, 0); +#endif + esp_light_sleep_start(); + Button_SleepRequest = 0; +// #ifdef PIN_PERIPH_RST +// gpio_set_level(PIN_PERIPH_RST, 0); +// +// gpio_set_level(PIN_PERIPH_RST, 1); +// #endif +} + +uint32_t Button_PressTime=0; // [ms] counts for how long the button is kept pressed +void Button_keptPressed(uint8_t Ticks) +{ Button_PressTime+=Ticks; + Button_SleepRequest = Button_PressTime>=4000; } // [ms] setup SleepRequest if button pressed for >= 4sec + +void Button_keptReleased(uint8_t Ticks) +{ + if(Button_PressTime) + { Format_String(CONS_UART_Write, "Button released:"); + Format_UnsDec(CONS_UART_Write, Button_PressTime); + Format_String(CONS_UART_Write, "\n"); + if(Button_SleepRequest) + { Format_String(CONS_UART_Write, "Sleep in 1 sec\n"); + vTaskDelay(1000); + Sleep(); } + } + Button_PressTime=0; } + +void Button_TimerCheck(uint8_t Ticks) +{ +#ifdef PIN_BUTTON + if(Button_isPressed()) + { Button_Filter+=Ticks; if(Button_Filter>Button_FilterTime) Button_Filter=Button_FilterTime; + if(Button_Filter>=Button_FilterThres) { Button_keptPressed(Ticks); } + } + else + { Button_Filter-=Ticks; if(Button_Filter<(-Button_FilterTime)) Button_Filter=(-Button_FilterTime); + if(Button_Filter<=(-Button_FilterThres)) { Button_keptReleased(Ticks); } + } +#endif + +} + /* extern "C" void vApplicationIdleHook(void) // when RTOS is idle: should call "sleep until an interrupt" @@ -925,6 +1003,9 @@ void IO_Configuration(void) LED_PCB_Dir(); LED_PCB_Off(); #endif +#ifdef PIN_BUTTON + Button_Dir(); +#endif RFM_IRQ_Dir(); RFM_RESET_Dir(); @@ -1050,16 +1131,6 @@ void IO_Configuration(void) // esp_register_freertos_tick_hook(&vApplicationTickHook); -/* -#ifdef PIN_PERIPH_RST - gpio_set_level(PIN_PERIPH_RST, 0); -#endif -#ifdef PIN_GPS_ENA - gpio_set_level(PIN_GPS_ENA, 0); -#endif - esp_light_sleep_start(); -*/ - } // ====================================================================================================== diff --git a/main/hal.h b/main/hal.h index fac6cb9..4630de9 100644 --- a/main/hal.h +++ b/main/hal.h @@ -117,6 +117,7 @@ void RFM_RESET(uint8_t On); // RF module reset bool RFM_IRQ_isOn(void); // query the IRQ state #ifdef WITH_OLED +int OLED_DisplayON(uint8_t ON, uint8_t DispIdx=0); int OLED_SetContrast(uint8_t Contrast, uint8_t DispIdx=0); int OLED_PutLine(uint8_t Line, const char *Text, uint8_t DispIdx=0); #endif @@ -169,6 +170,9 @@ void LED_RX_Flash(uint8_t Time=100); void LED_TimerCheck(uint8_t Ticks=1); +extern bool Button_SleepRequest; +void Button_TimerCheck(uint8_t Ticks=1); + void IO_Configuration(void); // Configure I/O int NVS_Init(void); // initialize non-volatile-storage in the Flash diff --git a/main/rf.cpp b/main/rf.cpp index ac8ad5d..6d16f2a 100644 --- a/main/rf.cpp +++ b/main/rf.cpp @@ -207,6 +207,9 @@ extern "C" for( ; ; ) { + while(Button_SleepRequest) + { TRX.WriteMode(RF_OPMODE_SLEEP); + vTaskDelay(100); } uint32_t RxRssiSum=0; uint16_t RxRssiCount=0; // measure the average RSSI for lower frequency do