mirror of
https://github.com/opentx/opentx.git
synced 2025-07-17 05:15:18 +03:00
Bsongis/radio tools (#6484)
Lua tools now displayed in RADIO / TOOLS Add a function to write CENTERED text with lcdDrawText
This commit is contained in:
parent
a3cc0ee830
commit
56a31f20d3
10 changed files with 144 additions and 57 deletions
|
@ -18,11 +18,12 @@
|
|||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include "opentx.h"
|
||||
|
||||
extern uint8_t g_moduleIdx;
|
||||
|
||||
void addRadioTool(uint8_t index, const char * label, void (* tool)(event_t), uint8_t module)
|
||||
bool addRadioTool(uint8_t index, const char * label)
|
||||
{
|
||||
int8_t sub = menuVerticalPosition - HEADER_LINE;
|
||||
LcdFlags attr = (sub == index ? INVERS : 0);
|
||||
|
@ -31,54 +32,136 @@ void addRadioTool(uint8_t index, const char * label, void (* tool)(event_t), uin
|
|||
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)
|
||||
{
|
||||
uint8_t spectrum_modules = 0, power_modules = 0;
|
||||
|
||||
if (event == EVT_ENTRY || event == EVT_ENTRY_UP) {
|
||||
memclear(&reusableBuffer.hardwareAndSettings, sizeof(reusableBuffer.hardwareAndSettings));
|
||||
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.hardwareAndSettings.modules[module], PXX2_HW_INFO_TX_ID, PXX2_HW_INFO_TX_ID);
|
||||
moduleState[module].readModuleInformation(&reusableBuffer.radioTools.modules[module], PXX2_HW_INFO_TX_ID, PXX2_HW_INFO_TX_ID);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
for (uint8_t module = 0; module < NUM_MODULES; module++) {
|
||||
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[module].information.modelID, MODULE_OPTION_SPECTRUM_ANALYSER)) {
|
||||
spectrum_modules++;
|
||||
}
|
||||
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[module].information.modelID, MODULE_OPTION_POWER_METER)) {
|
||||
power_modules++;
|
||||
}
|
||||
}
|
||||
|
||||
SIMPLE_MENU(STR_MENUTOOLS, menuTabGeneral, MENU_RADIO_TOOLS, HEADER_LINE + spectrum_modules + power_modules);
|
||||
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))
|
||||
addRadioTool(index++, STR_SPECTRUM_ANALYSER_INT, menuRadioSpectrumAnalyser, INTERNAL_MODULE);
|
||||
addRadioModuleTool(index++, STR_SPECTRUM_ANALYSER_INT, menuRadioSpectrumAnalyser, INTERNAL_MODULE);
|
||||
|
||||
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[INTERNAL_MODULE].information.modelID, MODULE_OPTION_POWER_METER))
|
||||
addRadioTool(index++, STR_POWER_METER_INT, menuRadioPowerMeter, INTERNAL_MODULE);
|
||||
addRadioModuleTool(index++, STR_POWER_METER_INT, menuRadioPowerMeter, INTERNAL_MODULE);
|
||||
|
||||
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[EXTERNAL_MODULE].information.modelID, MODULE_OPTION_SPECTRUM_ANALYSER))
|
||||
addRadioTool(index++, STR_SPECTRUM_ANALYSER_EXT, menuRadioSpectrumAnalyser, EXTERNAL_MODULE);
|
||||
addRadioModuleTool(index++, STR_SPECTRUM_ANALYSER_EXT, menuRadioSpectrumAnalyser, EXTERNAL_MODULE);
|
||||
|
||||
if (isModuleOptionAvailable(reusableBuffer.hardwareAndSettings.modules[EXTERNAL_MODULE].information.modelID, MODULE_OPTION_POWER_METER))
|
||||
addRadioTool(index++, STR_POWER_METER_EXT, menuRadioPowerMeter, EXTERNAL_MODULE);
|
||||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue