Skip to content

Commit

Permalink
fix: Prevent functional degradation if SD has less than 50MB free spa…
Browse files Browse the repository at this point in the history
…ce (#3739)

* 2.8 port of #3730 (PR_SDCard_low_warning_2.9)

* Popup:

- turns on backlight if dimmed due to inactivity setting
- if backlight is dimmed due to inactivity setting backlight can turnd on again by any controls

* remove test supporting code

* fix b&w UI

* updated languages CN, CZ, DA, DE, FR, IT, PL, PT, SE, TW

* chore: Update translations
  • Loading branch information
mha1 authored Jul 14, 2023
1 parent 67e30e3 commit cafce5a
Show file tree
Hide file tree
Showing 29 changed files with 208 additions and 33 deletions.
61 changes: 61 additions & 0 deletions radio/src/gui/colorlcd/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,43 @@
* GNU General Public License for more details.
*/

#include "opentx.h"
#include "popups.h"
#include "libopenui.h"
#include "pwr.h"

static void _run_popup_dialog(const char* title, const char* msg,
const char* info = nullptr)
{
bool running = true;

resetBacklightTimeout();

// reset input devices to avoid
// RELEASED/CLICKED to be called in a loop
lv_indev_reset(nullptr, nullptr);

auto md = new MessageDialog(MainWindow::instance(), title, msg);
md->setCloseHandler([&]() { running = false; });
if (info) {
md->setInfoText(std::string(info));
}
while (running) {
// Allow power off while showing popup
auto check = pwrCheck();
if (check == e_power_off) {
boardOff();
#if defined(SIMU)
// Required so simulator exits cleanly when window closed
return;
#endif
} else if (check == e_power_press) {
WDG_RESET();
RTOS_WAIT_MS(1);
continue;
}

checkBacklight();
WDG_RESET();
MainWindow::instance()->run();
LvglWrapper::runNested();
Expand All @@ -48,3 +72,40 @@ void POPUP_WARNING(const char * message, const char * info)
{
_run_popup_dialog("Warning", message, info);
}

static const char* ui_popup_title = nullptr;
static const char* ui_popup_msg = nullptr;
static const char* ui_popup_info = nullptr;
static bool ui_popup_active = false;

// Allow UI task to show a popup deferred from another task.
void show_ui_popup()
{
if (ui_popup_active) {
_run_popup_dialog(ui_popup_title, ui_popup_msg, ui_popup_info);
ui_popup_active = false;
}
}

void POPUP_WARNING_ON_UI_TASK(const char * message, const char * info, bool waitForClose)
{
// if already in a popup, and we don't want to wait, ignore call
if (!waitForClose && ui_popup_active)
return;

// Wait in case already in popup.
while (ui_popup_active) {
RTOS_WAIT_MS(20);
}
ui_popup_title = "Warning";
ui_popup_msg = message;
ui_popup_info = info;
ui_popup_active = true;

// Wait until closed
if (waitForClose) {
while (ui_popup_active) {
RTOS_WAIT_MS(20);
}
}
}
3 changes: 3 additions & 0 deletions radio/src/gui/colorlcd/popups.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ typedef std::function<void(const char *, const char *, int, int)> ProgressHandle

void POPUP_INFORMATION(const char * message);
void POPUP_WARNING(const char * message, const char * info = nullptr);
void POPUP_WARNING_ON_UI_TASK(const char * message, const char * info = nullptr, bool waitForClose = true);

void show_ui_popup();
6 changes: 3 additions & 3 deletions radio/src/gui/common/stdlcd/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ void runPopupWarning(event_t event)
return;

case WARNING_TYPE_INFO:
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+2*FH+2, STR_OK);
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+4*FH+2, STR_OK);
break;

case WARNING_TYPE_ASTERISK:
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+2*FH+2, BUTTON(TR_EXIT));
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+4*FH+2, BUTTON(TR_EXIT));
break;

default:
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+2*FH+2, STR_POPUPS_ENTER_EXIT);
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+4*FH+2, STR_POPUPS_ENTER_EXIT);
break;
}

Expand Down
11 changes: 8 additions & 3 deletions radio/src/gui/common/stdlcd/popups.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum
#if !defined(GUI)
#define DISPLAY_WARNING(...)
inline void POPUP_WAIT(const char * s) { }
inline void POPUP_WARNING(const char *, const char * = nullptr) { }
inline void POPUP_WARNING(const char *, const char * = nullptr, bool waitForClose) { }
inline void POPUP_CONFIRMATION(const char * s, PopupMenuHandler handler) { }
inline void POPUP_INPUT(const char * s, PopupFunc func) { }
inline void SET_WARNING_INFO(const char * info, uint8_t length, uint8_t flags) { }
Expand Down Expand Up @@ -105,8 +105,10 @@ enum
popupFunc = runPopupWarning;
}

inline void POPUP_WARNING(const char * message, const char * info = nullptr)
inline void POPUP_WARNING(const char * message, const char * info = nullptr, bool waitForClose = true)
{
(void)waitForClose;

warningText = message;
warningInfoText = info;
warningInfoLength = info ? strlen(info) : 0;
Expand Down Expand Up @@ -187,4 +189,7 @@ inline void POPUP_MENU_START(PopupMenuHandler handler)
}
}

#endif // _STDLCD_POPUPS_H_
// For compatability with color LCD code base, not (currently) required for B&W
#define POPUP_WARNING_ON_UI_TASK POPUP_WARNING

#endif // _STDLCD_POPUPS_H_
9 changes: 7 additions & 2 deletions radio/src/gui/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const char * writeScreenshot()
UINT written;
char filename[42]; // /SCREENSHOTS/screen-2013-01-01-123540.bmp

if (IS_SDCARD_FULL()) {
POPUP_WARNING(STR_SDCARD_FULL_EXT);
return STR_SDCARD_FULL_EXT;
}

// check and create folder here
strcpy(filename, SCREENSHOTS_PATH);
const char * error = sdCheckAndCreateDirectory(filename);
Expand Down Expand Up @@ -88,7 +93,7 @@ const char * writeScreenshot()
auto h = snapshot->header.h;

for (int y = h - 1; y >= 0; y--) {
for (int x = 0; x < w; x++) {
for (uint32_t x = 0; x < w; x++) {

lv_color_t pixel = lv_img_buf_get_px_color(snapshot, x, y, {});

Expand Down Expand Up @@ -123,4 +128,4 @@ const char * writeScreenshot()
f_close(&bmpFile);

return nullptr;
}
}
35 changes: 23 additions & 12 deletions radio/src/logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ const char * logsOpen()
if (!sdMounted())
return STR_NO_SDCARD;

if (sdGetFreeSectors() == 0)
return STR_SDCARD_FULL;

// check and create folder here
strcpy(filename, STR_LOGS_PATH);
const char * error = sdCheckAndCreateDirectory(filename);
Expand Down Expand Up @@ -189,9 +186,7 @@ void logsClose()
}
lastLogTime = 0;
}
#if !defined(SIMU)
loggingTimerStop();
#endif

}

void writeHeader()
Expand Down Expand Up @@ -283,17 +278,31 @@ void logsWrite()
{
#endif

bool sdCardFull = IS_SDCARD_FULL();

// check if file needs to be opened
if (!g_oLogFile.obj.fs) {
const char * result = logsOpen();
const char *result = sdCardFull ? STR_SDCARD_FULL_EXT : logsOpen();

// SD card is full or file open failed
if (result) {
if (result != error_displayed) {
error_displayed = result;
POPUP_WARNING(result);
POPUP_WARNING_ON_UI_TASK(result, nullptr, false);
}
return;
}
}

// check at every write cycle
if (sdCardFull) {
logsClose(); // timer is still running and code above will try to
// open the file again but will fail with error
// which will trigger the warning popup
return;
}


#if defined(RTCLOCK)
{
static struct gtm utm;
Expand Down Expand Up @@ -380,15 +389,17 @@ void logsWrite()

if (result<0 && !error_displayed) {
error_displayed = STR_SDCARD_ERROR;
POPUP_WARNING(STR_SDCARD_ERROR);
POPUP_WARNING_ON_UI_TASK(STR_SDCARD_ERROR, nullptr, false);
logsClose();
}
}
}
else {
error_displayed = nullptr;
if (g_oLogFile.obj.fs) {
logsClose();
}
logsClose();

#if !defined(SIMU)
loggingTimerStop();
#endif
}
}
2 changes: 2 additions & 0 deletions radio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ void perMain()
DEBUG_TIMER_START(debugTimerGuiMain);
#if defined(LIBOPENUI)
guiMain(0);
// For color screens show a popup deferred from another task
show_ui_popup();
#else
guiMain(evt);
#endif
Expand Down
7 changes: 7 additions & 0 deletions radio/src/opentx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,11 @@ static void checkRTCBattery()
}
}

void checkSDfreeStorage() {
if(IS_SDCARD_FULL())
ALERT(STR_SD_CARD, STR_SDCARD_FULL, AU_ERROR);
}

#if defined(PCBFRSKY) || defined(PCBFLYSKY)
static void checkFailsafe()
{
Expand Down Expand Up @@ -722,6 +727,8 @@ void checkAll()
#if defined(EEPROM_RLC) && !defined(SDCARD_RAW) && !defined(SDCARD_YAML)
checkLowEEPROM();
#endif

checkSDfreeStorage();

// we don't check the throttle stick if the radio is not calibrated
if (g_eeGeneral.chkSum == evalChkSum()) {
Expand Down
4 changes: 4 additions & 0 deletions radio/src/opentx.h
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,10 @@ inline getvalue_t convertTelemValue(source_t channel, ls_telemetry_value_t value
return convert16bitsTelemValue(channel, value);
}

inline bool IS_SDCARD_FULL() {
return sdGetFreeSectors() < ((50 *1024*1024) / BLOCK_SIZE); // 50MB safety margin
}

extern uint8_t g_vbat100mV;

inline uint8_t GET_TXBATT_BARS(uint8_t barsMax)
Expand Down
2 changes: 1 addition & 1 deletion radio/src/sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ uint32_t sdGetSize()

uint32_t sdGetFreeSectors()
{
return 10;
return ((50 *1024*1024)/BLOCK_SIZE)+1; // SIMU SD card is always above threshold
}

#endif // #if !defined(SIMU) || defined(SIMU_DISKIO)
2 changes: 1 addition & 1 deletion radio/src/targets/horus/diskio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ DRESULT __disk_read(BYTE drv, BYTE * buff, DWORD sector, UINT count)

if ((DWORD)buff < 0x20000000 || ((DWORD)buff & 3)) {
// buffer is not aligned, use scratch buffer that is aligned
TRACE("disk_read bad alignment (%p)", buff);
//TRACE("disk_read bad alignment (%p)", buff);
while (count--) {
res = disk_read_dma(drv, (BYTE *)scratch, sector++, 1);
if (res != RES_OK) break;
Expand Down
4 changes: 2 additions & 2 deletions radio/src/targets/horus/lcd_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ static void startLcdRefresh(lv_disp_drv_t *disp_drv, uint16_t *buffer,
lv_area_t refr_area;
lv_area_copy(&refr_area, &disp->inv_areas[i]);

TRACE("{%d,%d,%d,%d}", refr_area.x1,
refr_area.y1, refr_area.x2, refr_area.y2);
//TRACE("{%d,%d,%d,%d}", refr_area.x1,
// refr_area.y1, refr_area.x2, refr_area.y2);

_rotate_area_180(refr_area);

Expand Down
1 change: 1 addition & 0 deletions radio/src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ const char STR_SDCARD_ERROR[] = TR_SDCARD_ERROR;
#define STR_CLEAR TR_CLEAR
const char STR_NO_SDCARD[] = TR_NO_SDCARD;
const char STR_SDCARD_FULL[] = TR_SDCARD_FULL;
const char STR_SDCARD_FULL_EXT[] = TR_SDCARD_FULL_EXT;
const char STR_INCOMPATIBLE[] = TR_INCOMPATIBLE;
const char STR_LOGS_PATH[] = LOGS_PATH;
const char STR_LOGS_EXT[] = LOGS_EXT;
Expand Down
1 change: 1 addition & 0 deletions radio/src/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ extern const char STR_DELETE_ERROR[];
extern const char STR_SDCARD_ERROR[];
extern const char STR_NO_SDCARD[];
extern const char STR_SDCARD_FULL[];
extern const char STR_SDCARD_FULL_EXT[];
extern const char STR_INCOMPATIBLE[];
extern const char STR_LOGS_PATH[];
extern const char STR_LOGS_EXT[];
Expand Down
7 changes: 6 additions & 1 deletion radio/src/translations/cn.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,12 @@
#define TR_POWER_METER_INT "功率计 (内置)"
#define TR_SPECTRUM_ANALYSER_EXT "频谱仪 (外置)"
#define TR_SPECTRUM_ANALYSER_INT "频谱仪 (内置)"
#define TR_SDCARD_FULL "存储卡已满"
#define TR_SDCARD_FULL "SD卡已满"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\n日志和截屏功能将被禁用"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036日志和 " LCDW_128_480_LINEBREAK "截屏功能将被禁用"
#endif
#define TR_NEEDS_FILE "需要文件名包含"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
7 changes: 6 additions & 1 deletion radio/src/translations/cz.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,12 @@
#define TR_POWER_METER_INT "Měřič výkonu (INT)"
#define TR_SPECTRUM_ANALYSER_EXT "Spektální an. (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spektální an. (INT)"
#define TR_SDCARD_FULL "Plná SD karta"
#define TR_SDCARD_FULL "Plná karta SD"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLogování dat a snímky obrazovky vypnuty"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Logy a " LCDW_128_480_LINEBREAK " Snímky obrazovky vypnuty"
#endif
#define TR_NEEDS_FILE "Vyžadován soubor"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
5 changes: 5 additions & 0 deletions radio/src/translations/da.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,11 @@
#define TR_SPECTRUM_ANALYSER_EXT "Spectrum (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spectrum (INT)"
#define TR_SDCARD_FULL "SD kort fuldt"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLog & skærmklip deaktiveret"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Log & skærmklip" LCDW_128_480_LINEBREAK "deaktiveret"
#endif
#define TR_NEEDS_FILE "MANGLER FIL"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
7 changes: 6 additions & 1 deletion radio/src/translations/de.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,12 @@
#define TR_POWER_METER_INT "Power Meter (INT)"
#define TR_SPECTRUM_ANALYSER_EXT "Spectrum (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spectrum (INT)"
#define TR_SDCARD_FULL "SD-Karte voll"
#define TR_SDCARD_FULL "SD-Karte voll"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLogs und Screenshots deaktiviert"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Logs und " LCDW_128_480_LINEBREAK "Screenshots deaktiviert"
#endif
#define TR_NEEDS_FILE "Datei benötigt"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
Loading

0 comments on commit cafce5a

Please sign in to comment.