Skip to content

Commit

Permalink
fix(color): Broken search for model specific audio files for switches (
Browse files Browse the repository at this point in the history
…#4162)

Co-authored-by: raphaelcoeffic <[email protected]>
Co-authored-by: Peter Feerick <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2023
1 parent 745bb8d commit 1bb624c
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 118 deletions.
9 changes: 9 additions & 0 deletions radio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ option(RAS "RAS (SWR) enabled" ON)
option(TEMPLATES "Model templates menu" OFF)
option(TRACE_SIMPGMSPACE "Turn on traces in simpgmspace.cpp" ON)
option(TRACE_LUA_INTERNALS "Turn on traces for Lua internals" OFF)
option(TRACE_AUDIO "Traces audio enabled" OFF)
option(DEBUG_SEGGER_RTT "Debug output to Segger RTT" OFF)
option(DEBUG_WINDOWS "Turn on windows traces" OFF)
option(DEBUG_YAML "Turn on YAML traces" OFF)
Expand Down Expand Up @@ -284,6 +285,12 @@ if(TRACE_LUA_INTERNALS)
add_definitions(-DTRACE_LUA_INTERNALS_ENABLED)
endif()

if(TRACE_AUDIO)
add_definitions(-DTRACE_AUDIO)
set(DEBUG ON)
set(DEBUG_TRACE_BUFFER ON)
endif()

if(DEBUG_SEGGER_RTT)
add_definitions(-DDEBUG_SEGGER_RTT)
set(SRC ${SRC} ${THIRDPARTY_DIR}/Segger_RTT/RTT/SEGGER_RTT.c)
Expand Down Expand Up @@ -386,6 +393,8 @@ set(SRC
trainer.cpp
model_init.cpp
serial.cpp
audio.cpp
model_audio.cpp
sbus.cpp
input_mapping.cpp
inactivity_timer.cpp
Expand Down
141 changes: 31 additions & 110 deletions radio/src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
* GNU General Public License for more details.
*/

#include "opentx.h"
#include <math.h>

#include "opentx.h"
#include "strhelpers.h"
#include "switches.h"

#if defined(LIBOPENUI)
#include "libopenui.h"
#include "libopenui.h"
#endif

#include "model_audio.h"

extern RTOS_MUTEX_HANDLE audioMutex;

const int16_t sineValues[] =
Expand Down Expand Up @@ -306,133 +309,51 @@ void referenceSystemAudioFiles()
}
}

const char * const suffixes[] = { "-off", "-on" };

char *getModelAudioPath(char *path)
{
strcpy(path, SOUNDS_PATH "/");
strncpy(path + SOUNDS_PATH_LNG_OFS, currentLanguagePack->id, 2);
char *buf = strcat_currentmodelname(path + sizeof(SOUNDS_PATH), ' ');

if (!isFileAvailable(path)) {
buf = strcat_currentmodelname(path + sizeof(SOUNDS_PATH), 0);
}

*buf++ = '/';
*buf = '\0';
return buf;
}

void getFlightmodeAudioFile(char * filename, int index, unsigned int event)
{
char * str = getModelAudioPath(filename);
char * tmp = strcatFlightmodeName(str, index);
strcpy(tmp, suffixes[event]);
strcat(tmp, SOUNDS_EXT);
}

void getSwitchAudioFile(char * filename, swsrc_t index)
{
char * str = getModelAudioPath(filename);

if (index <= MAX_SWITCHES * 3) {
div_t swinfo = switchInfo(index);
*str++ = 'S';
*str++ = switchGetLetter(swinfo.quot);
const char * positions[] = { "-up", "-mid", "-down" };
strcpy(str, positions[swinfo.rem]);
}
else {
index -= MAX_SWITCHES * 3;
div_t swinfo = div((int)index, XPOTS_MULTIPOS_COUNT);
*str++ = 'S';
*str++ = '1' + swinfo.quot;
*str++ = '1' + swinfo.rem;
*str = '\0';
}
strcat(str, SOUNDS_EXT);
}

void getLogicalSwitchAudioFile(char * filename, int index, unsigned int event)
{
char * str = getModelAudioPath(filename);

*str++ = 'L';
if (index >= 9) {
div_t qr = div(index+1, 10);
*str++ = '0' + qr.quot;
*str++ = '0' + qr.rem;
}
else {
*str++ = '1' + index;
}

strcpy(str, suffixes[event]);
strcat(str, SOUNDS_EXT);
}

void referenceModelAudioFiles()
{
char path[AUDIO_FILENAME_MAXLEN+1];
FILINFO fno;
DIR dir;
FILINFO fno;
char path[AUDIO_FILENAME_MAXLEN + 1];

sdAvailableFlightmodeAudioFiles.reset();
sdAvailableSwitchAudioFiles.reset();
sdAvailableLogicalSwitchAudioFiles.reset();

char * filename = getModelAudioPath(path);
*(filename-1) = '\0';
getModelAudioPath(path, false);

FRESULT res = f_opendir(&dir, path); /* Open the directory */
FRESULT res = f_opendir(&dir, path); /* Open the directory */
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 */
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 */
uint8_t len = strlen(fno.fname);
bool found = false;

// Eliminates directories / non wav files
if (len < 5 || strcasecmp(fno.fname+len-4, SOUNDS_EXT) || (fno.fattrib & AM_DIR)) continue;
if (fno.fattrib & AM_DIR) continue;
if (len < sizeof(SOUNDS_EXT)) continue;

char *ext = fno.fname + len - (sizeof(SOUNDS_EXT) - 1);
if (strcasecmp(ext, SOUNDS_EXT)) continue;

TRACE("referenceModelAudioFiles(): using file: %s", fno.fname);

// Flight modes Audio Files <flightmodename>-[on|off].wav
for (int i=0; i<MAX_FLIGHT_MODES && !found; i++) {
for (int event=0; event<2; event++) {
getFlightmodeAudioFile(path, i, event);
// TRACE("referenceModelAudioFiles(): searching for %s in %s", filename, fno.fname);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableFlightmodeAudioFiles.setBit(INDEX_PHASE_AUDIO_FILE(i, event));
found = true;
TRACE("\tfound: %s", filename);
break;
}
}
int idx, event;
if (matchModeAudioFile(fno.fname, idx, event)) {
sdAvailableFlightmodeAudioFiles.setBit(
INDEX_PHASE_AUDIO_FILE(idx, event));
continue;
}

// Switches Audio Files <switchname>-[up|mid|down].wav
for (unsigned i = 0; i <= MAX_SWITCH_POSITIONS && !found; i++) {
getSwitchAudioFile(path, i);
// TRACE("referenceModelAudioFiles(): searching for %s in %s (%d)", path, fno.fname, i);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableSwitchAudioFiles.setBit(i);
found = true;
TRACE("\tfound: %s", filename);
}
if (matchSwitchAudioFile(fno.fname, idx)) {
sdAvailableSwitchAudioFiles.setBit(idx);
continue;
}

// Logical Switches Audio Files <switchname>-[on|off].wav
for (int i=0; i<MAX_LOGICAL_SWITCHES && !found; i++) {
for (int event=0; event<2; event++) {
getLogicalSwitchAudioFile(path, i, event);
// TRACE("referenceModelAudioFiles(): searching for %s in %s", filename, fno.fname);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableLogicalSwitchAudioFiles.setBit(INDEX_LOGICAL_SWITCH_AUDIO_FILE(i, event));
found = true;
TRACE("\tfound: %s", filename);
break;
}
}
if (matchLogicalSwitchAudioFile(fno.fname, idx, event)) {
sdAvailableLogicalSwitchAudioFiles.setBit(
INDEX_LOGICAL_SWITCH_AUDIO_FILE(idx, event));
continue;
}
}
f_closedir(&dir);
Expand Down Expand Up @@ -461,7 +382,7 @@ bool isAudioFileReferenced(uint32_t i, char * filename)
}
else if (category == SWITCH_AUDIO_CATEGORY) {
if (sdAvailableSwitchAudioFiles.getBit(index)) {
getSwitchAudioFile(filename, SWSRC_FIRST_SWITCH+index);
getSwitchAudioFile(filename, SWSRC_FIRST_SWITCH + index);
return true;
}
}
Expand Down
Loading

0 comments on commit 1bb624c

Please sign in to comment.