Skip to content

Commit a98ce05

Browse files
TheRealMoederPhracturedBlue
authored andcommitted
Strip RAM needs for voice (#554)
* Read labels only in voiceconfig menu * fix branch * cleanup * More cleanup * Address comments part 1 * Simplify voice.ini parsing * Cleanup * Remove unused variable * Add comments for clarification * Cleanup and small bugfix for voicemenu * Fix linter errors * Fix linter errors
1 parent ac8c59a commit a98ce05

9 files changed

Lines changed: 69 additions & 66 deletions

File tree

src/config/tx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ void CONFIG_LoadTx()
350350
CONFIG_IniParse("tx.ini", ini_handler, (void *)&Transmitter);
351351
crc32 = Crc(&Transmitter, sizeof(Transmitter));
352352
#if HAS_EXTENDED_AUDIO
353-
CONFIG_VoiceParse();
353+
CONFIG_VoiceParse(MAX_VOICEMAP_ENTRIES);
354354
#endif
355355
return;
356356
}

src/config/voice.c

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "common.h"
1919
#include "model.h"
2020
#include "../music.h"
21+
#include "extended_audio.h"
2122
#include "tx.h"
23+
#include "voice.h"
2224

2325
#define MATCH_SECTION(s) (strcasecmp(section, s) == 0)
2426
#define MATCH_KEY(s) (strcasecmp(name, s) == 0)
@@ -28,55 +30,55 @@
2830
const char SECTION_VOICE_GLOBAL[] = "global";
2931
const char SECTION_VOICE_CUSTOM[] = "custom";
3032

31-
static const char* getfield(char* value, int num) {
32-
const char* tok;
33-
for (tok = strtok(value, ","); tok && *tok; tok = strtok(NULL, ",\n")) {
34-
if (!--num)
35-
return tok;
36-
}
37-
return "";
38-
}
39-
4033
static int ini_handler(void* user, const char* section, const char* name, const char* value)
4134
{
42-
(void) user;
43-
char tmp[100];
44-
strlcpy(tmp, value, 100);
45-
int duration = atoi(getfield(tmp,2));
46-
#if HAS_MUSIC_CONFIG
47-
char label[MAX_VOICE_LABEL];
48-
strlcpy(label,getfield(tmp, 1), MAX_VOICE_LABEL);
49-
#endif
35+
// ini_handler returns a requested mp3 label passed at *user to tempstring
36+
u16 req_id = *((long*)user);
37+
u16 id = atoi(name);
38+
const char* ptr = value;
39+
u8 k = 0;
40+
u16 duration = 0;
41+
42+
while (*ptr && *ptr != ',') {
43+
ptr++;
44+
k++;
45+
}
46+
if (*ptr == ',') {
47+
duration = atoi(ptr + 1);
48+
}
5049

51-
if (MATCH_SECTION(SECTION_VOICE_GLOBAL)) {
52-
for (int i = 0; i < CUSTOM_ALARM_ID; i++) {
53-
snprintf(tempstring, 4, "%d", i);
54-
if (MATCH_KEY(tempstring)) {
55-
voice_map[i].duration = duration;
56-
voice_map[i].id = i;
5750
#if HAS_MUSIC_CONFIG
58-
strcpy(voice_map[i].label, label);
51+
if ( k && (req_id != MAX_VOICEMAP_ENTRIES) && (req_id == id) ) {
52+
strlcpy(tempstring, value, k+1);
53+
tempstring[MAX_VOICE_LABEL] = '\0'; // limit label length
54+
return 1;
55+
}
5956
#endif
60-
return 1;
57+
if ( req_id == MAX_VOICEMAP_ENTRIES ) {
58+
if (MATCH_SECTION(SECTION_VOICE_GLOBAL)) {
59+
for (int i = 0; i < CUSTOM_ALARM_ID; i++) {
60+
snprintf(tempstring, 4, "%d", i);
61+
if (MATCH_KEY(tempstring)) {
62+
voice_map[i].duration = duration;
63+
voice_map[i].id = i;
64+
return 1;
65+
}
6166
}
6267
}
68+
if (MATCH_SECTION(SECTION_VOICE_CUSTOM)) {
69+
voice_map[voice_map_entries].duration = duration;
70+
voice_map[voice_map_entries].id = id;
71+
voice_map_entries++;
72+
return 1;
73+
}
74+
printf("Unknown entry in voice.ini: %s\n", value);
75+
return 0;
6376
}
64-
if (MATCH_SECTION(SECTION_VOICE_CUSTOM)) {
65-
voice_map[voice_map_entries].duration = duration;
66-
voice_map[voice_map_entries].id = atoi(name);
67-
#if HAS_MUSIC_CONFIG
68-
strcpy(voice_map[voice_map_entries].label, label);
69-
#endif
70-
voice_map_entries++;
71-
return 1;
72-
}
73-
printf("Unknown entry in voice.ini: %s\n", value);
74-
return 0;
77+
return 1; // voice label ignored
7578
}
7679

77-
void CONFIG_VoiceParse()
80+
const char* CONFIG_VoiceParse(unsigned id)
7881
{
79-
voice_map_entries = CUSTOM_ALARM_ID;
8082
#ifdef _DEVO12_TARGET_H_
8183
static char filename[] = "media/voice.ini\0\0\0"; // placeholder for longer folder name
8284
static u8 checked;
@@ -92,9 +94,19 @@ void CONFIG_VoiceParse()
9294
#else
9395
char filename[] = "media/voice.ini";
9496
#endif
95-
if (CONFIG_IniParse(filename, ini_handler, NULL)) {
96-
printf("Failed to parse voice.ini\n");
97-
Transmitter.audio_player = AUDIO_NONE; // disable external voice output
97+
if (id == MAX_VOICEMAP_ENTRIES) { // initial parse of voice.ini
98+
voice_map_entries = CUSTOM_ALARM_ID; // Reserve space in map for global alerts
99+
if (CONFIG_IniParse(filename, ini_handler, &id)) {
100+
printf("Failed to parse voice.ini\n");
101+
Transmitter.audio_player = AUDIO_NONE; // disable external voice output
102+
tempstring[0] = '\0';
103+
}
104+
}
105+
if ( (id < MAX_VOICEMAP_ENTRIES) && (id >= CUSTOM_ALARM_ID) ) {
106+
if (CONFIG_IniParse(filename, ini_handler, &id)) {
107+
// ini handler will return tempstring with label of id
108+
}
98109
}
110+
return tempstring;
99111
}
100112
#endif

src/config/voice.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
2-
void CONFIG_VoiceParse();
1+
const char* CONFIG_VoiceParse();

src/extended_audio.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
#if HAS_EXTENDED_AUDIO
2828
u16 voice_map_entries;
2929
struct VoiceMap voice_map[MAX_VOICEMAP_ENTRIES];
30-
3130
u16 audio_queue[AUDIO_QUEUE_LENGTH];
3231
u8 next_audio;
3332
u8 num_audio;
3433
u32 audio_queue_time;
3534

35+
#ifndef EMULATOR
3636
static u8 player_buffer[] = {0x7E, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF};
37+
#endif
3738

3839
// Initialize UART for extended-audio
3940
void AUDIO_Init() {
@@ -103,12 +104,7 @@ int AUDIO_Play(u16 music) {
103104
printf("Voice: beep only\n");
104105
return 0;
105106
}
106-
107-
#if HAS_MUSIC_CONFIG
108-
printf("Voice: Playing mp3 #%d (%s)\n", voice_map[music].id, voice_map[music].label);
109-
#else
110107
printf("Voice: Playing mp3 #%d\n", voice_map[music].id);
111-
#endif
112108

113109
#ifdef EMULATOR // On emulators call mpg123 to play mp3s
114110
char cmd[70];

src/extended_audio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _EXTENDED_AUDIO_H_
22
#define _EXTENDED_AUDIO_H_
33

4-
#ifdef HAS_EXTENDED_AUDIO
4+
#if HAS_EXTENDED_AUDIO
55

66
#define AUDIO_QUEUE_LENGTH 20 // arbitraty chosen, do we need more?
77

src/music.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ struct CustomVoice {
9595
u16 music;
9696
};
9797

98-
struct Voice {
98+
struct Voice {
9999
struct CustomVoice switches[NUM_SWITCHES]; //Switch array to point to music file number, no pots
100100
struct CustomVoice telemetry[TELEM_NUM_ALARMS]; //Telemetry Alarm array to point to music file number
101101
struct CustomVoice timer[NUM_TIMERS]; //Timer Alarm array to point to music file number
@@ -111,9 +111,6 @@ extern u16 voice_map_entries;
111111
struct VoiceMap {
112112
u16 id;
113113
u16 duration;
114-
#if HAS_MUSIC_CONFIG
115-
char label[MAX_VOICE_LABEL];
116-
#endif
117114
};
118115

119116
extern struct VoiceMap voice_map[MAX_VOICEMAP_ENTRIES];

src/pages/320x240x16/voiceconfig_page.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ enum {
3232
static int row_cb(int absrow, int relrow, int y, void *data)
3333
{
3434
(void)data;
35-
int row = y;
36-
int voice_num = absrow;
3735
//Row 1
38-
GUI_CreateLabelBox(&gui->name[relrow], COL1, row, COL2-COL1, 18, &LABEL_FONT,voiceconfig_str_cb, NULL, (void *)(long)voice_num);
39-
GUI_CreateTextSelect(&gui->voiceidx[relrow], COL2, row, TEXTSELECT_64, voice_test_cb, voiceid_cb, (void *)(long)voice_num);
40-
GUI_CreateLabelBox(&gui->voicelbl[relrow], COL3, row, LCD_WIDTH - COL3 - ARROW_WIDTH, 18, &TINY_FONT, voicelbl_cb, NULL, (void *)(long)voice_num);
36+
GUI_CreateLabelBox(&gui->name[relrow], COL1, y, COL2-COL1, 18, &LABEL_FONT, voiceconfig_str_cb, NULL, (void *)(long)absrow);
37+
GUI_CreateTextSelect(&gui->voiceidx[relrow], COL2, y, TEXTSELECT_64, voice_test_cb, voiceid_cb, (void *)(long)absrow);
38+
GUI_CreateLabelBox(&gui->voicelbl[relrow], COL3, y, LCD_WIDTH - COL3 - ARROW_WIDTH, 18, &TINY_FONT, voicelbl_cb, NULL, (void *)(long)absrow);
4139
return 1;
4240
}
4341

@@ -52,7 +50,7 @@ void PAGE_VoiceconfigInit(int page)
5250
_tr("External voice\ncurrently not\navailable"));
5351
return;
5452
}
55-
53+
5654
GUI_CreateScrollable(&gui->scrollable, 0, init_y, LCD_WIDTH, LCD_HEIGHT-init_y,
5755
VOICEROWSPACER, MODEL_CUSTOM_ALARMS, row_cb, NULL, NULL, NULL);
5856
PAGE_SetScrollable(&gui->scrollable, &current_selected);

src/pages/common/_voiceconfig_page.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,25 @@ static const char *voicelbl_cb(guiObject_t *obj, const void *data)
123123
switch (voiceconfig_getsrctype(idx)) {
124124
case VOICE_SRC_SWITCH:
125125
if (Model.voice.switches[idx - VOICE_SRC_SWITCH].music)
126-
return voice_map[Model.voice.switches[idx - VOICE_SRC_SWITCH].music].label;
126+
return CONFIG_VoiceParse(voice_map[Model.voice.switches[idx - VOICE_SRC_SWITCH].music].id);
127127
break;
128128
#if NUM_AUX_KNOBS
129129
case VOICE_SRC_AUX:
130130
if(Model.voice.aux[idx - VOICE_SRC_AUX].music)
131-
return voice_map[Model.voice.aux[idx - VOICE_SRC_AUX].music].label;
131+
return CONFIG_VoiceParse(voice_map[Model.voice.aux[idx - VOICE_SRC_AUX].music].id);
132132
break;
133133
#endif
134134
case VOICE_SRC_TIMER:
135135
if (Model.voice.timer[idx - VOICE_SRC_TIMER].music)
136-
return voice_map[Model.voice.timer[idx - VOICE_SRC_TIMER].music].label;
136+
return CONFIG_VoiceParse(voice_map[Model.voice.timer[idx - VOICE_SRC_TIMER].music].id);
137137
break;
138138
case VOICE_SRC_TELEMETRY:
139139
if (Model.voice.telemetry[idx - VOICE_SRC_TELEMETRY].music)
140-
return voice_map[Model.voice.telemetry[idx - VOICE_SRC_TELEMETRY].music].label;
140+
return CONFIG_VoiceParse(voice_map[Model.voice.telemetry[idx - VOICE_SRC_TELEMETRY].music].id);
141141
break;
142142
case VOICE_SRC_MIXER:
143143
if (Model.voice.mixer[idx - VOICE_SRC_MIXER].music)
144-
return voice_map[Model.voice.mixer[idx - VOICE_SRC_MIXER].music].label;
144+
return CONFIG_VoiceParse(voice_map[Model.voice.mixer[idx - VOICE_SRC_MIXER].music].id);
145145
break;
146146
}
147147
return strcpy(tempstring, "");

src/pages/common/voiceconfig_page.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef _MUSICCONFIG_PAGE_H_
22
#define _MUSICCONFIG_PAGE_H_
3+
#include "config/voice.h"
34

45
#if HAS_MUSIC_CONFIG
56
struct voiceconfig_page {

0 commit comments

Comments
 (0)