diff --git a/openrtx/include/settings.h b/openrtx/include/settings.h index 6be470f9..12529d88 100644 --- a/openrtx/include/settings.h +++ b/openrtx/include/settings.h @@ -23,15 +23,37 @@ #include +typedef enum +{ + TIMER_OFF = 0, + TIMER_5S = 1, + TIMER_10S = 2, + TIMER_15S = 3, + TIMER_20S = 4, + TIMER_25S = 5, + TIMER_30S = 6, + TIMER_1M = 7, + TIMER_2M = 8, + TIMER_3M = 9, + TIMER_4M = 10, + TIMER_5M = 11, + TIMER_15M = 12, + TIMER_30M = 13, + TIMER_45M = 14, + TIMER_1H = 15 +} +display_timer_t; + typedef struct { - uint8_t brightness; // Display brightness - uint8_t contrast; // Display contrast - uint8_t sqlLevel; // Squelch level - uint8_t voxLevel; // Vox level - int8_t utc_timezone; // Timezone - bool gps_enabled; // GPS active - char callsign[10]; // Plaintext callsign, for future use + uint8_t brightness; // Display brightness + uint8_t contrast; // Display contrast + uint8_t sqlLevel; // Squelch level + uint8_t voxLevel; // Vox level + int8_t utc_timezone; // Timezone + bool gps_enabled; // GPS active + char callsign[10]; // Plaintext callsign, for future use + display_timer_t brightness_timer; // Standby timer } __attribute__((packed)) settings_t; @@ -48,7 +70,8 @@ static const settings_t default_settings = 0, // Vox level 0, // UTC Timezone false, // GPS enabled - "" // Empty callsign + "", // Empty callsign + TIMER_30S // 30 seconds }; #endif /* SETTINGS_H */ diff --git a/openrtx/src/ui/ui.c b/openrtx/src/ui/ui.c index 47b75587..eb85028f 100644 --- a/openrtx/src/ui/ui.c +++ b/openrtx/src/ui/ui.c @@ -225,7 +225,6 @@ bool macro_menu = false; bool layout_ready = false; bool redraw_needed = true; -#define STANDBY_LIMIT 30000 // 30s - TODO: move into a setting bool standby = false; long long last_event_tick = 0; @@ -645,6 +644,45 @@ void _ui_changeContrast(int variation) display_setContrast(state.settings.contrast); } +bool _ui_checkStandby(long long time_since_last_event) +{ + if (standby) + { + return false; + } + + switch (state.settings.brightness_timer) + { + case TIMER_OFF: + return false; + case TIMER_5S: + case TIMER_10S: + case TIMER_15S: + case TIMER_20S: + case TIMER_25S: + case TIMER_30S: + return time_since_last_event >= + (5000 * state.settings.brightness_timer); + case TIMER_1M: + case TIMER_2M: + case TIMER_3M: + case TIMER_4M: + case TIMER_5M: + return time_since_last_event >= + (60000 * (state.settings.brightness_timer - (TIMER_1M - 1))); + case TIMER_15M: + case TIMER_30M: + case TIMER_45M: + return time_since_last_event >= + (60000 * 15 * (state.settings.brightness_timer - (TIMER_15M - 1))); + case TIMER_1H: + return time_since_last_event >= 60 * 60 * 1000; + } + + // unreachable code + return false; +} + void _ui_enterStandby() { if(standby) @@ -1527,14 +1565,14 @@ void ui_updateFSM(event_t event, bool *sync_rtx) else if(event.type == EVENT_STATUS) { if (txOngoing || rtx_rxSquelchOpen()) - { - _ui_exitStandby(now); - return; - } - - if (!standby && (now - last_event_tick >= STANDBY_LIMIT)) { - _ui_enterStandby(); + _ui_exitStandby(now); + return; + } + + if (_ui_checkStandby(now - last_event_tick)) + { + _ui_enterStandby(); } } } diff --git a/tests/unit/ui_check_standby.c b/tests/unit/ui_check_standby.c new file mode 100644 index 00000000..12ac08c9 --- /dev/null +++ b/tests/unit/ui_check_standby.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include + +extern bool _ui_checkStandby(long long); +extern state_t state; + +void assert_display_timer(display_timer_t conf, + long long time_sec, + bool expected) +{ + state.settings.brightness_timer = conf; + + long long ticks = time_sec * 1000; + + if (_ui_checkStandby(ticks) != expected) + { + printf("FAILED! enum value %d - time %lld sec - expected %d\n", + conf, time_sec, expected); + exit(1); + } +} + +void test_timer_threshold(display_timer_t conf, long long time_sec) +{ + assert_display_timer(conf, time_sec -1, false); + assert_display_timer(conf, time_sec, true); +} + +int main() { + printf("Backlight timer test\n"); + + test_timer_threshold(TIMER_5S, 5); + test_timer_threshold(TIMER_10S, 10); + test_timer_threshold(TIMER_15S, 15); + test_timer_threshold(TIMER_20S, 20); + test_timer_threshold(TIMER_25S, 25); + test_timer_threshold(TIMER_30S, 30); + + test_timer_threshold(TIMER_1M, 1 * 60); + test_timer_threshold(TIMER_2M, 2 * 60); + test_timer_threshold(TIMER_3M, 3 * 60); + test_timer_threshold(TIMER_4M, 4 * 60); + test_timer_threshold(TIMER_5M, 5 * 60); + + + test_timer_threshold(TIMER_15M, 15 * 60); + test_timer_threshold(TIMER_30M, 30 * 60); + test_timer_threshold(TIMER_45M, 45 * 60); + + test_timer_threshold(TIMER_1H, 60 * 60); + + assert_display_timer(TIMER_OFF, 0, false); + assert_display_timer(TIMER_OFF, 60 * 60 * 24, false); + + printf("PASS\n"); + + return 0; +}