OpenRTX/openrtx/src/ui.c

543 wiersze
18 KiB
C
Czysty Zwykły widok Historia

2020-10-30 19:13:52 +00:00
/***************************************************************************
* Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, *
* Niccolò Izzo IU2KIN *
* Frederik Saraci IU2NRO *
2020-10-30 19:13:52 +00:00
* Silvano Seva IU2KWO *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
2020-10-30 19:56:48 +00:00
/*
* The graphical user interface (GUI) works by splitting the screen in
2020-10-30 19:56:48 +00:00
* horizontal rows, with row height depending on vertical resolution.
*
2020-10-30 19:56:48 +00:00
* The general screen layout is composed by an upper status bar at the
* top of the screen and a lower status bar at the bottom.
* The central portion of the screen is filled by two big text/number rows
* And a small row.
*
* Below is shown the row height for two common display densities.
*
* 160x128 display (MD380) Recommended font size
2020-10-30 19:56:48 +00:00
*
* top_status_bar (16 px) 8 pt font with 4 px vertical padding
2020-10-30 19:56:48 +00:00
*
* Line 1 (32px) 16 pt font with 8 px vertical padding
2020-10-30 19:56:48 +00:00
*
* Line 2 (32px) 16 pt font with 8 px vertical padding
2020-10-30 19:56:48 +00:00
*
* Line 3 (32px) 16 pt font with 8 px vertical padding
2020-10-30 19:56:48 +00:00
*
*
* bottom_status_bar (16 px) 8 pt font with 4 px vertical padding
2020-10-30 19:56:48 +00:00
*
*
2020-10-31 17:12:44 +00:00
* 128x64 display (GD-77)
2020-10-30 19:56:48 +00:00
*
* top_status_bar (8 px) 8 pt font without vertical padding
2020-10-30 19:56:48 +00:00
*
2020-10-31 17:12:44 +00:00
* Line 1 (20px) 16 pt font with 2 px vertical padding
* Line 2 (20px) 16 pt font with 2 px vertical padding
* Line 3 (8px) 8 pt font without vertical padding
2020-10-30 19:56:48 +00:00
*
* bottom_status_bar (8 px) 8 pt font without vertical padding
2020-10-30 19:56:48 +00:00
*
2020-10-31 17:12:44 +00:00
*
* 128x48 display (RD-5R)
*
* top_status_bar (8 px) 8 pt font without vertical padding
*
* Line 1 (15px) 16 pt font with 2 px vertical padding
* Line 2 (15px) 16 pt font with 2 px vertical padding
2020-10-31 17:12:44 +00:00
*
2020-10-30 19:56:48 +00:00
*/
2020-10-30 19:13:52 +00:00
#include <stdio.h>
2020-11-16 08:03:03 +00:00
#include <stdint.h>
#include <ui.h>
#include <rtx.h>
2020-11-16 08:03:03 +00:00
#include <delays.h>
#include <graphics.h>
#include <keyboard.h>
#include <platform.h>
#include <hwconfig.h>
#include <string.h>
#include <battery.h>
// Maximum menu entry length
#define MAX_ENTRY_LEN 12
// Number of top menu entries
#define MENU_NUM 6
// Number of settings menu entries
#define SETTINGS_NUM 1
const char *menu_items[MENU_NUM] =
2020-12-06 20:45:10 +00:00
{
"Zone",
"Channels",
"Contacts",
"Messages",
"GPS",
"Settings"
};
const char *settings_items[SETTINGS_NUM] =
{
"Time & Date"
};
2020-10-31 17:12:44 +00:00
typedef struct layout_t
{
2020-11-19 16:05:10 +00:00
uint16_t top_h;
2020-12-06 20:24:25 +00:00
uint16_t line1_h;
2020-11-19 16:05:10 +00:00
uint16_t bottom_h;
uint16_t vertical_pad;
uint16_t horizontal_pad;
2020-10-31 17:12:44 +00:00
point_t top_pos;
point_t line1_pos;
point_t line2_pos;
point_t line3_pos;
point_t bottom_pos;
fontSize_t top_font;
fontSize_t line1_font;
fontSize_t line2_font;
fontSize_t line3_font;
fontSize_t bottom_font;
} layout_t;
const color_t color_black = {0, 0, 0, 255};
const color_t color_grey = {60, 60, 60, 255};
const color_t color_white = {255, 255, 255, 255};
const color_t yellow_fab413 = {250, 180, 19, 255};
2020-10-31 17:12:44 +00:00
layout_t layout;
bool layout_ready = false;
2020-11-21 09:46:39 +00:00
bool redraw_needed = true;
uint8_t menu_selected = 0;
2020-10-31 17:12:44 +00:00
layout_t _ui_calculateLayout()
{
2020-10-31 17:12:44 +00:00
// Calculate UI layout depending on vertical resolution
// Tytera MD380, MD-UV380
2020-11-16 08:03:03 +00:00
#if SCREEN_HEIGHT > 127
// Height and padding shown in diagram at beginning of file
const uint16_t top_h = 16;
const uint16_t bottom_h = top_h;
2020-11-16 08:03:03 +00:00
const uint16_t line1_h = 32;
const uint16_t line2_h = 32;
const uint16_t line3_h = 32;
const uint16_t line_pad = 8;
const uint16_t vertical_pad = 4;
const uint16_t horizontal_pad = 4;
2020-11-16 08:03:03 +00:00
// Top bar font: 8 pt
const fontSize_t top_font = FONT_SIZE_8PT;
2020-11-16 08:03:03 +00:00
// Middle line fonts: 16 pt
const fontSize_t line1_font = FONT_SIZE_12PT;
const fontSize_t line2_font = FONT_SIZE_12PT;
const fontSize_t line3_font = FONT_SIZE_12PT;
2020-11-16 08:03:03 +00:00
// Bottom bar font: 8 pt
const fontSize_t bottom_font = FONT_SIZE_8PT;
2020-11-16 08:03:03 +00:00
2020-10-31 17:12:44 +00:00
// Radioddity GD-77
2020-11-16 08:03:03 +00:00
#elif SCREEN_HEIGHT > 63
// Height and padding shown in diagram at beginning of file
const uint16_t top_h = 13;
const uint16_t bottom_h = 18;
const uint16_t line1_h = 0;
const uint16_t line2_h = 15;
const uint16_t line3_h = 15;
const uint16_t line_pad = 2;
const uint16_t vertical_pad = 4;
const uint16_t horizontal_pad = 4;
2020-11-16 08:03:03 +00:00
// Top bar font: 8 pt
const fontSize_t top_font = FONT_SIZE_6PT;
2020-11-16 08:03:03 +00:00
// Middle line fonts: 16, 16, 8 pt
const fontSize_t line1_font = FONT_SIZE_8PT;
const fontSize_t line2_font = FONT_SIZE_8PT;
const fontSize_t line3_font = FONT_SIZE_5PT;
2020-11-16 08:03:03 +00:00
// Bottom bar font: 8 pt
const fontSize_t bottom_font = FONT_SIZE_6PT;
2020-11-16 08:03:03 +00:00
#elif SCREEN_HEIGHT > 47
// Height and padding shown in diagram at beginning of file
const uint16_t top_h = 8;
const uint16_t bottom_h = 0;
2020-11-16 08:03:03 +00:00
const uint16_t line1_h = 20;
const uint16_t line2_h = 20;
const uint16_t line3_h = 0;
const uint16_t line_pad = 2;
const uint16_t vertical_pad = 0;
const uint16_t horizontal_pad = 0;
2020-11-16 08:03:03 +00:00
// Top bar font: 8 pt
const fontSize_t top_font = FONT_SIZE_1;
// Middle line fonts: 16, 16
const fontSize_t line1_font = FONT_SIZE_3;
const fontSize_t line2_font = FONT_SIZE_3;
// Not present in this UI
const fontSize_t line3_font = 0;
const fontSize_t bottom_font = 0;
#else
#error Unsupported vertical resolution!
#endif
2020-10-31 17:12:44 +00:00
// Calculate printing positions
point_t top_pos = {horizontal_pad, top_h - vertical_pad};
point_t line1_pos = {horizontal_pad, top_h + line1_h - line_pad};
point_t line2_pos = {horizontal_pad, top_h + line1_h + line2_h - line_pad};
point_t line3_pos = {horizontal_pad, top_h + line1_h + line2_h + line3_h - line_pad};
point_t bottom_pos = {horizontal_pad, top_h + line1_h + line2_h + line3_h + bottom_h - vertical_pad};
2020-10-31 17:12:44 +00:00
2020-11-16 08:03:03 +00:00
layout_t new_layout =
{
2020-11-19 16:05:10 +00:00
top_h,
2020-12-06 20:24:25 +00:00
line1_h,
2020-11-19 16:05:10 +00:00
bottom_h,
vertical_pad,
horizontal_pad,
2020-10-31 17:12:44 +00:00
top_pos,
line1_pos,
line2_pos,
line3_pos,
bottom_pos,
top_font,
line1_font,
line2_font,
line3_font,
bottom_font
2020-10-31 17:12:44 +00:00
};
return new_layout;
}
void _ui_drawVFOBackground()
2020-11-19 16:05:10 +00:00
{
// Print top bar line of 1 pixel height
gfx_drawHLine(layout.top_h, 1, color_grey);
// Print bottom bar line of 1 pixel height
gfx_drawHLine(SCREEN_HEIGHT - layout.bottom_h - 1, 1, color_grey);
}
void _ui_drawVFOTop(state_t* last_state)
2020-10-31 17:12:44 +00:00
{
2020-10-31 12:58:09 +00:00
// Print clock on top bar
2020-11-29 12:23:21 +00:00
char clock_buf[9] = "";
snprintf(clock_buf, sizeof(clock_buf), "%02d:%02d:%02d", last_state->time.hour,
2020-12-06 19:31:06 +00:00
last_state->time.minute, last_state->time.second);
gfx_print(layout.top_pos, clock_buf, layout.top_font, TEXT_ALIGN_CENTER,
color_white);
2020-10-31 12:58:09 +00:00
2020-12-03 22:35:33 +00:00
// Print battery icon on top bar, use 4 px padding
float charge = battery_getCharge(last_state->v_bat);
uint16_t bat_width = SCREEN_WIDTH / 9;
uint16_t bat_height = layout.top_h - layout.vertical_pad;
point_t bat_pos = {SCREEN_WIDTH - bat_width - layout.horizontal_pad,
layout.vertical_pad / 2};
gfx_drawBattery(bat_pos, bat_width, bat_height, charge);
// Print radio mode on top bar
char mode[4] = "";
switch(last_state->channel.mode)
{
case FM:
strcpy(mode, "FM");
break;
case DMR:
strcpy(mode, "DMR");
break;
}
gfx_print(layout.top_pos, mode, layout.top_font, TEXT_ALIGN_LEFT,
color_white);
}
void _ui_drawVFOMiddle(state_t* last_state)
2020-10-31 13:40:02 +00:00
{
// Print VFO frequencies
char freq_buf[20] = "";
2020-12-07 20:43:00 +00:00
snprintf(freq_buf, sizeof(freq_buf), "Rx: %03lu.%05lu",
2020-12-06 19:31:06 +00:00
last_state->channel.rx_frequency/1000000,
last_state->channel.rx_frequency%1000000/10);
gfx_print(layout.line2_pos, freq_buf, layout.line1_font, TEXT_ALIGN_CENTER,
color_white);
2020-12-07 20:43:00 +00:00
snprintf(freq_buf, sizeof(freq_buf), "Tx: %03lu.%05lu",
2020-12-06 19:31:06 +00:00
last_state->channel.tx_frequency/1000000,
last_state->channel.tx_frequency%1000000/10);
gfx_print(layout.line3_pos, freq_buf, layout.line2_font, TEXT_ALIGN_CENTER,
color_white);
2020-11-18 09:12:14 +00:00
}
void _ui_drawVFOBottom()
2020-11-18 09:12:14 +00:00
{
gfx_print(layout.bottom_pos, "OpenRTX", layout.bottom_font,
TEXT_ALIGN_CENTER, color_white);
2020-10-31 13:40:02 +00:00
}
void _ui_drawMenuList(point_t pos, const char *entries[],
uint8_t num_entries, uint8_t selected)
{
char entry_buf[MAX_ENTRY_LEN] = "";
for(int item=0; (item < num_entries) && (pos.y < SCREEN_HEIGHT); item++)
{
snprintf(entry_buf, sizeof(entry_buf), "%s", entries[item]);
if(item == selected)
{
2020-12-20 08:24:57 +00:00
// Draw rectangle under selected item, compensating for text height
point_t rect_pos = {0, pos.y - layout.top_h + 3};
gfx_drawRect(rect_pos, SCREEN_WIDTH, layout.top_h, color_white, true);
gfx_print(pos, entry_buf, layout.top_font, TEXT_ALIGN_LEFT, color_black);
}
else
{
gfx_print(pos, entry_buf, layout.top_font, TEXT_ALIGN_LEFT, color_white);
}
pos.y += layout.top_h;
}
}
2020-12-06 19:31:06 +00:00
bool _ui_drawMainVFO(state_t* last_state)
{
2020-11-21 09:46:39 +00:00
bool screen_update = false;
// Total GUI redraw
if(redraw_needed)
{
gfx_clearScreen();
_ui_drawVFOBackground();
point_t splash_origin = {0, SCREEN_HEIGHT / 2 - 6};
color_t yellow = yellow_fab413;
yellow.alpha = 0.1f * 255;
gfx_print(splash_origin, "O P N\nR T X", FONT_SIZE_12PT, TEXT_ALIGN_CENTER,
yellow);
_ui_drawVFOTop(last_state);
_ui_drawVFOMiddle(last_state);
_ui_drawVFOBottom();
2020-11-21 09:46:39 +00:00
screen_update = true;
}
2020-12-06 19:31:06 +00:00
// Partial GUI page redraw
2020-11-21 09:46:39 +00:00
// TODO: until gfx_clearRows() is implemented, we need to redraw everything
else
{
gfx_clearScreen();
_ui_drawVFOBackground();
_ui_drawVFOTop(last_state);
_ui_drawVFOMiddle(last_state);
_ui_drawVFOBottom();
2020-11-21 09:46:39 +00:00
screen_update = true;
}
return screen_update;
}
2020-10-30 19:13:52 +00:00
void _ui_drawMenuTop()
2020-12-06 19:31:06 +00:00
{
// Print "Menu" on top bar
gfx_clearScreen();
gfx_print(layout.top_pos, "Menu", layout.top_font,
TEXT_ALIGN_CENTER, color_white);
// Print menu entries
point_t pos = {layout.horizontal_pad, layout.line1_h};
_ui_drawMenuList(pos, menu_items, MENU_NUM, menu_selected);
}
void _ui_drawMenuSettings()
{
// Print "Settings" on top bar
gfx_clearScreen();
gfx_print(layout.top_pos, "Settings", layout.top_font,
TEXT_ALIGN_CENTER, color_white);
// Print menu entries
point_t pos = {layout.horizontal_pad, layout.line1_h};
_ui_drawMenuList(pos, settings_items, SETTINGS_NUM, menu_selected);
2020-12-06 19:31:06 +00:00
}
2020-10-30 19:13:52 +00:00
void ui_init()
{
2020-11-21 09:46:39 +00:00
redraw_needed = true;
2020-10-31 17:12:44 +00:00
layout = _ui_calculateLayout();
layout_ready = true;
2020-10-30 19:13:52 +00:00
}
2020-11-21 09:46:39 +00:00
void ui_drawSplashScreen()
{
gfx_clearScreen();
#ifdef OLD_SPLASH
point_t splash_origin = {0, SCREEN_HEIGHT / 2 + 6};
2020-11-21 09:46:39 +00:00
gfx_print(splash_origin, "OpenRTX", FONT_SIZE_12PT, TEXT_ALIGN_CENTER,
yellow_fab413);
#else
2020-11-26 16:18:53 +00:00
point_t splash_origin = {0, SCREEN_HEIGHT / 2 - 6};
gfx_print(splash_origin, "O P N\nR T X", FONT_SIZE_12PT, TEXT_ALIGN_CENTER,
yellow_fab413);
#endif
2020-11-26 16:18:53 +00:00
}
2020-12-18 18:03:34 +00:00
bool _ui_drawLowBatteryScreen()
{
gfx_clearScreen();
uint16_t bat_width = SCREEN_WIDTH / 2;
uint16_t bat_height = SCREEN_HEIGHT / 3;
point_t bat_pos = {SCREEN_WIDTH / 4, SCREEN_HEIGHT / 8};
gfx_drawBattery(bat_pos, bat_width, bat_height, 0.1f);
point_t text_pos_1 = {0, SCREEN_HEIGHT * 2 / 3};
point_t text_pos_2 = {0, SCREEN_HEIGHT * 2 / 3 + 16};
gfx_print(text_pos_1,
"For emergency use",
FONT_SIZE_6PT,
TEXT_ALIGN_CENTER,
color_white);
gfx_print(text_pos_2,
"press any button.",
FONT_SIZE_6PT,
TEXT_ALIGN_CENTER,
color_white);
2020-12-18 18:03:34 +00:00
return true;
}
void ui_updateFSM(event_t event, bool *sync_rtx)
2020-10-30 19:13:52 +00:00
{
2020-12-18 18:03:34 +00:00
// Check if battery has enough charge to operate
float charge = battery_getCharge(state.v_bat);
if (!state.emergency && charge <= 0)
{
state.ui_screen = LOW_BAT;
if(event.type == EVENT_KBD && event.payload) {
state.ui_screen = MAIN_VFO;
state.emergency = true;
}
return;
}
2020-12-05 17:40:08 +00:00
// Process pressed keys
if(event.type == EVENT_KBD)
2020-11-26 21:44:39 +00:00
{
kbd_msg_t msg;
msg.value = event.payload;
2020-12-06 19:31:06 +00:00
switch(state.ui_screen)
2020-12-05 17:40:08 +00:00
{
2020-12-06 19:31:06 +00:00
// VFO screen
case MAIN_VFO:
if(msg.keys & KEY_UP)
2020-12-06 19:31:06 +00:00
{
// Advance TX and RX frequency of 12.5KHz
state.channel.rx_frequency += 12500;
state.channel.tx_frequency += 12500;
*sync_rtx = true;
2020-12-06 19:31:06 +00:00
}
else if(msg.keys & KEY_DOWN)
2020-12-06 19:31:06 +00:00
{
// Advance TX and RX frequency of 12.5KHz
state.channel.rx_frequency -= 12500;
state.channel.tx_frequency -= 12500;
*sync_rtx = true;
2020-12-06 19:31:06 +00:00
}
else if(msg.keys & KEY_ENTER)
2020-12-06 19:31:06 +00:00
// Open Menu
state.ui_screen = MENU_TOP;
break;
// Top menu screen
case MENU_TOP:
if(msg.keys & KEY_UP)
2020-12-06 19:31:06 +00:00
{
2020-12-20 08:24:57 +00:00
if(menu_selected > 0)
menu_selected -= 1;
2020-12-06 19:31:06 +00:00
}
else if(msg.keys & KEY_DOWN)
2020-12-06 19:31:06 +00:00
{
if(menu_selected < MENU_NUM-1)
2020-12-20 08:24:57 +00:00
menu_selected += 1;
2020-12-06 19:31:06 +00:00
}
else if(msg.keys & KEY_ENTER)
{
// Open selected menu item
switch(menu_selected)
{
// TODO: Add missing submenu states
case 5:
state.ui_screen = MENU_SETTINGS;
break;
default:
state.ui_screen = MENU_TOP;
}
// Reset menu selection
menu_selected = 0;
}
else if(msg.keys & KEY_ESC)
{
2020-12-06 19:31:06 +00:00
// Close Menu
state.ui_screen = MAIN_VFO;
// Reset menu selection
menu_selected = 0;
}
break;
// Settings menu screen
case MENU_SETTINGS:
if(msg.keys & KEY_ESC)
{
// Return to top menu
state.ui_screen = MENU_TOP;
// Reset menu selection
menu_selected = 0;
}
2020-12-06 19:31:06 +00:00
break;
2020-12-05 17:40:08 +00:00
}
2020-11-26 21:44:39 +00:00
}
}
2020-11-26 21:44:39 +00:00
bool ui_updateGUI(state_t last_state)
{
2020-10-31 17:12:44 +00:00
if(!layout_ready)
{
layout = _ui_calculateLayout();
layout_ready = true;
}
// TODO: Improve screen_update logic
2020-12-06 19:31:06 +00:00
bool screen_update = false;
// Draw current GUI page
switch(last_state.ui_screen)
{
// VFO screen
case MAIN_VFO:
screen_update = _ui_drawMainVFO(&last_state);
break;
// Top menu screen
case MENU_TOP:
_ui_drawMenuTop();
screen_update = true;
break;
// Settings menu screen
case MENU_SETTINGS:
_ui_drawMenuSettings();
screen_update = true;
2020-12-06 19:31:06 +00:00
break;
2020-12-18 18:03:34 +00:00
// Low battery screen
case LOW_BAT:
screen_update = _ui_drawLowBatteryScreen();
break;
2020-12-06 19:31:06 +00:00
}
2020-11-21 09:46:39 +00:00
return screen_update;
2020-10-30 19:13:52 +00:00
}
void ui_terminate()
{
}