1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-16 04:45:17 +03:00
opentx/radio/src/gui/common/stdlcd/radio_tools.cpp
Bertrand Songis 56a31f20d3
Bsongis/radio tools (#6484)
Lua tools now displayed in RADIO / TOOLS
Add a function to write CENTERED text with lcdDrawText
2019-06-08 10:42:29 +02:00

167 lines
No EOL
5.1 KiB
C++

/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#include <algorithm>
#include "opentx.h"
extern uint8_t g_moduleIdx;
bool addRadioTool(uint8_t index, const char * label)
{
int8_t sub = menuVerticalPosition - HEADER_LINE;
LcdFlags attr = (sub == index ? INVERS : 0);
coord_t y = MENU_HEADER_HEIGHT + 1 + index * FH;
lcdDrawNumber(3, y, index + 1, LEADING0|LEFT, 2);
lcdDrawText(3*FW, y, label, (sub == index ? INVERS : 0));
if (attr && s_editMode > 0) {
s_editMode = 0;
return true;
}
return false;
}
void addRadioModuleTool(uint8_t index, const char * label, void (* tool)(event_t), uint8_t module)
{
if (addRadioTool(index, label)) {
g_moduleIdx = module;
pushMenu(tool);
}
}
#define TOOL_NAME_MAXLEN 16
bool readToolName(const char * filename, char * name)
{
FIL file;
char buffer[1024];
UINT count;
if (f_open(&file, filename, FA_READ) != FR_OK) {
return "Error opening file";
}
if (f_read(&file, &buffer, sizeof(buffer), &count) != FR_OK) {
f_close(&file);
return false;
}
const char * tns = "TNS|";
auto * start = std::search(buffer, buffer + sizeof(buffer), tns, tns + 4);
if (start >= buffer + sizeof(buffer))
return false;
start += 4;
const char * tne = "|TNE";
auto * end = std::search(buffer, buffer + sizeof(buffer), tne, tne + 4);
if (end >= buffer + sizeof(buffer) || end <= start)
return false;
uint8_t len = end - start;
if (len > TOOL_NAME_MAXLEN)
return false;
strncpy(name, start, len);
memclear(name + len, TOOL_NAME_MAXLEN + 1 - len);
return true;
}
void addRadioScriptTool(uint8_t index, const char * filename)
{
TCHAR path[_MAX_LFN+1] = SCRIPTS_TOOLS_PATH "/";
strcat(path, filename);
char toolName[TOOL_NAME_MAXLEN + 1];
const char * label;
if (readToolName(path, toolName)) {
label = toolName;
}
else {
char * ext = (char *)getFileExtension(filename);
*ext = '\0';
label = filename;
}
if (addRadioTool(index, label)) {
luaExec(path);
}
}
bool isRadioScriptTool(const char * filename)
{
const char * ext = getFileExtension(filename);
return ext && !strcasecmp(ext, SCRIPT_EXT);
}
void menuRadioTools(event_t event)
{
if (event == EVT_ENTRY || event == EVT_ENTRY_UP) {
memclear(&reusableBuffer.radioTools, sizeof(reusableBuffer.radioTools));
#if defined(PXX2)
for (uint8_t module = 0; module < NUM_MODULES; module++) {
if (isModulePXX2(module) && (module == INTERNAL_MODULE ? IS_INTERNAL_MODULE_ON() : IS_EXTERNAL_MODULE_ON())) {
moduleState[module].readModuleInformation(&reusableBuffer.radioTools.modules[module], PXX2_HW_INFO_TX_ID, PXX2_HW_INFO_TX_ID);
}
}
#endif
}
SIMPLE_MENU(STR_MENUTOOLS, menuTabGeneral, MENU_RADIO_TOOLS, HEADER_LINE + reusableBuffer.radioTools.linesCount);
uint8_t index = 0;
#if defined(PXX2)
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[INTERNAL_MODULE].information.modelID, MODULE_OPTION_SPECTRUM_ANALYSER))
addRadioModuleTool(index++, STR_SPECTRUM_ANALYSER_INT, menuRadioSpectrumAnalyser, INTERNAL_MODULE);
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[INTERNAL_MODULE].information.modelID, MODULE_OPTION_POWER_METER))
addRadioModuleTool(index++, STR_POWER_METER_INT, menuRadioPowerMeter, INTERNAL_MODULE);
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[EXTERNAL_MODULE].information.modelID, MODULE_OPTION_SPECTRUM_ANALYSER))
addRadioModuleTool(index++, STR_SPECTRUM_ANALYSER_EXT, menuRadioSpectrumAnalyser, EXTERNAL_MODULE);
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[EXTERNAL_MODULE].information.modelID, MODULE_OPTION_POWER_METER))
addRadioModuleTool(index++, STR_POWER_METER_EXT, menuRadioPowerMeter, EXTERNAL_MODULE);
#endif
FILINFO fno;
DIR dir;
FRESULT res = f_opendir(&dir, SCRIPTS_TOOLS_PATH);
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
if (fno.fattrib & AM_DIR) continue; /* Skip subfolders */
if (fno.fattrib & AM_HID) continue; /* Skip hidden files */
if (fno.fattrib & AM_SYS) continue; /* Skip system files */
if (isRadioScriptTool(fno.fname))
addRadioScriptTool(index++, fno.fname);
}
}
if (index == 0) {
lcdDrawCenteredText(LCD_H/2, STR_NO_TOOLS);
}
reusableBuffer.radioTools.linesCount = index;
}