summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/recorder/radio.c76
1 files changed, 52 insertions, 24 deletions
diff --git a/apps/recorder/radio.c b/apps/recorder/radio.c
index dee38b5475..a6213abfb1 100644
--- a/apps/recorder/radio.c
+++ b/apps/recorder/radio.c
@@ -295,34 +295,54 @@ static int find_preset(int freq)
295 return -1; 295 return -1;
296} 296}
297 297
298/* Return the first preset encountered in the search direction with 298/* Return the closest preset encountered in the search direction with
299 wraparound. */ 299 wraparound. */
300static int find_closest_preset(int freq, int direction) 300static int find_closest_preset(int freq, int direction)
301{ 301{
302 int i; 302 int i;
303 int lowpreset = 0;
304 int highpreset = 0;
305 int closest = -1;
303 306
304 if (direction == 0) /* direction == 0 isn't really used */ 307 if (direction == 0) /* direction == 0 isn't really used */
305 return 0; 308 return 0;
306 309
307 for (i = 0; i < MAX_PRESETS; i++) 310 for (i = 0; i < num_presets; i++)
308 { 311 {
309 int preset_frequency = presets[i].frequency; 312 int f = presets[i].frequency;
310 313 if (f == freq)
311 if (preset_frequency == freq)
312 return i; /* Exact match = stop */ 314 return i; /* Exact match = stop */
313 /* Stop when the preset frequency exeeds freq so that we can 315
314 pick the correct one based on direction */ 316 /* remember the highest and lowest presets for wraparound */
315 if (preset_frequency > freq) 317 if (f < presets[lowpreset].frequency)
316 break; 318 lowpreset = i;
319 if (f > presets[highpreset].frequency)
320 highpreset = i;
321
322 /* find the closest preset in the given direction */
323 if (direction > 0 && f > freq)
324 {
325 if (closest < 0 || f < presets[closest].frequency)
326 closest = i;
327 }
328 else if (direction < 0 && f < freq)
329 {
330 if (closest < 0 || f > presets[closest].frequency)
331 closest = i;
332 }
317 } 333 }
318 334
319 /* wrap around depending on direction */ 335 if (closest < 0)
320 if (i == 0 || i >= num_presets - 1) 336 {
321 i = direction < 0 ? num_presets - 1 : 0; 337 /* no presets in the given direction */
322 else if (direction < 0) 338 /* wrap around depending on direction */
323 i--; /* use previous */ 339 if (direction < 0)
340 closest = highpreset;
341 else
342 closest = lowpreset;
343 }
324 344
325 return i; 345 return closest;
326} 346}
327 347
328static void remember_frequency(void) 348static void remember_frequency(void)
@@ -762,6 +782,7 @@ int radio_screen(void)
762 FOR_NB_SCREENS(i) 782 FOR_NB_SCREENS(i)
763 { 783 {
764 screens[i].set_viewport(&vp[i]); 784 screens[i].set_viewport(&vp[i]);
785 screens[i].stop_scroll();
765 screens[i].clear_viewport(); 786 screens[i].clear_viewport();
766 screens[i].update_viewport(); 787 screens[i].update_viewport();
767 screens[i].set_viewport(NULL); 788 screens[i].set_viewport(NULL);
@@ -1158,24 +1179,24 @@ static int radio_add_preset(void)
1158 1179
1159 if(num_presets < MAX_PRESETS) 1180 if(num_presets < MAX_PRESETS)
1160 { 1181 {
1161 memset(buf, 0, MAX_FMPRESET_LEN); 1182 buf[0] = '\0';
1162 1183
1163 if (!kbd_input(buf, MAX_FMPRESET_LEN)) 1184 if (!kbd_input(buf, MAX_FMPRESET_LEN + 1))
1164 { 1185 {
1165 struct fmstation * const fms = &presets[num_presets]; 1186 struct fmstation * const fms = &presets[num_presets];
1166 buf[MAX_FMPRESET_LEN] = '\0';
1167 strcpy(fms->name, buf); 1187 strcpy(fms->name, buf);
1168 fms->frequency = curr_freq; 1188 fms->frequency = curr_freq;
1169 num_presets++; 1189 num_presets++;
1170 presets_changed = true; 1190 presets_changed = true;
1171 presets_loaded = num_presets > 0; 1191 presets_loaded = num_presets > 0;
1192 return true;
1172 } 1193 }
1173 } 1194 }
1174 else 1195 else
1175 { 1196 {
1176 splash(HZ, ID2P(LANG_FM_NO_FREE_PRESETS)); 1197 splash(HZ, ID2P(LANG_FM_NO_FREE_PRESETS));
1177 } 1198 }
1178 return true; 1199 return false;
1179} 1200}
1180 1201
1181/* needed to know which preset we are edit/delete-ing */ 1202/* needed to know which preset we are edit/delete-ing */
@@ -1188,11 +1209,10 @@ static int radio_edit_preset(void)
1188 { 1209 {
1189 struct fmstation * const fms = &presets[selected_preset]; 1210 struct fmstation * const fms = &presets[selected_preset];
1190 1211
1191 strncpy(buf, fms->name, MAX_FMPRESET_LEN); 1212 strcpy(buf, fms->name);
1192 1213
1193 if (!kbd_input(buf, MAX_FMPRESET_LEN)) 1214 if (!kbd_input(buf, MAX_FMPRESET_LEN + 1))
1194 { 1215 {
1195 buf[MAX_FMPRESET_LEN] = '\0';
1196 strcpy(fms->name, buf); 1216 strcpy(fms->name, buf);
1197 presets_changed = true; 1217 presets_changed = true;
1198 } 1218 }
@@ -1213,6 +1233,8 @@ static int radio_delete_preset(void)
1213 memmove(fms, fms + 1, (uintptr_t)(fms + num_presets) - 1233 memmove(fms, fms + 1, (uintptr_t)(fms + num_presets) -
1214 (uintptr_t)fms); 1234 (uintptr_t)fms);
1215 1235
1236 if (curr_preset >= num_presets)
1237 --curr_preset;
1216 } 1238 }
1217 1239
1218 /* Don't ask to save when all presets are deleted. */ 1240 /* Don't ask to save when all presets are deleted. */
@@ -1222,6 +1244,7 @@ static int radio_delete_preset(void)
1222 { 1244 {
1223 /* The preset list will be cleared, switch to Scan Mode. */ 1245 /* The preset list will be cleared, switch to Scan Mode. */
1224 radio_mode = RADIO_SCAN_MODE; 1246 radio_mode = RADIO_SCAN_MODE;
1247 curr_preset = -1;
1225 presets_loaded = false; 1248 presets_loaded = false;
1226 } 1249 }
1227 1250
@@ -1295,6 +1318,7 @@ static int clear_preset_list(void)
1295 presets_loaded = false; 1318 presets_loaded = false;
1296 /* The preset list will be cleared switch to Scan Mode. */ 1319 /* The preset list will be cleared switch to Scan Mode. */
1297 radio_mode = RADIO_SCAN_MODE; 1320 radio_mode = RADIO_SCAN_MODE;
1321 curr_preset = -1;
1298 1322
1299 presets_changed = false; /* Don't ask to save when clearing the list. */ 1323 presets_changed = false; /* Don't ask to save when clearing the list. */
1300 1324
@@ -1374,12 +1398,16 @@ static int handle_radio_presets(void)
1374 { 1398 {
1375 gui_synclist_draw(&lists); 1399 gui_synclist_draw(&lists);
1376 gui_syncstatusbar_draw(&statusbars, true); 1400 gui_syncstatusbar_draw(&statusbars, true);
1377 list_do_action(CONTEXT_STD, HZ, 1401 list_do_action(CONTEXT_STD, TIMEOUT_BLOCK,
1378 &lists, &action, LIST_WRAP_UNLESS_HELD); 1402 &lists, &action, LIST_WRAP_UNLESS_HELD);
1379 switch (action) 1403 switch (action)
1380 { 1404 {
1381 case ACTION_STD_MENU: 1405 case ACTION_STD_MENU:
1382 radio_add_preset(); 1406 if (radio_add_preset())
1407 {
1408 gui_synclist_set_nb_items(&lists, num_presets);
1409 gui_synclist_select_item(&lists, num_presets - 1);
1410 }
1383 break; 1411 break;
1384 case ACTION_STD_CANCEL: 1412 case ACTION_STD_CANCEL:
1385 result = 1; 1413 result = 1;