Fahrplan: Letzte Position merken.
authorWintermate <wintermute@hannover.ccc.de>
Wed, 23 Oct 2013 20:01:08 +0000 (22:01 +0200)
committerWintermate <wintermute@hannover.ccc.de>
Wed, 23 Oct 2013 20:01:08 +0000 (22:01 +0200)
Makefile
badge/fahrplan.c [new file with mode: 0644]
badge/fahrplan.h [new file with mode: 0644]
badge/main.c
badge/ui/browser.c
badge/ui/browser.h
badge/ui/menu.c

index 67c7fb5..a989b7c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -31,8 +31,9 @@ OPTDEFINES = -D __NEWLIB__ -DHOB_REV2
 ##########################################################################\r
 \r
 SRCS = \\r
-  badge/main.c \\r
+  badge/fahrplan.c \\r
   badge/init.c \\r
+  badge/main.c \\r
   badge/jumpnrun/collision.c \\r
   badge/jumpnrun/enemies.c \\r
   badge/jumpnrun/game_state.c \\r
diff --git a/badge/fahrplan.c b/badge/fahrplan.c
new file mode 100644 (file)
index 0000000..e6127be
--- /dev/null
@@ -0,0 +1,48 @@
+#include "fahrplan.h"
+#include "ui/browser.h"
+
+#include <drivers/fatfs/ff.h>
+
+#define FAHRPLAN_PATH     "/fahrplan"
+#define FAHRPLAN_MENUFILE "fahrplan.lst"
+#define FAHRPLAN_POSFILE  "selected.dat"
+
+static size_t badge_load_fahrplan_pos(void) {
+  size_t pos = 0;
+  FIL fd;
+
+  if(FR_OK == f_open(&fd, FAHRPLAN_POSFILE, FA_OPEN_EXISTING | FA_READ)) {
+    UINT bytes;
+    size_t buf;
+    if(FR_OK == f_read(&fd, &buf, sizeof(buf), &bytes) && bytes == sizeof(buf)) {
+      pos = buf;
+    }
+
+    f_close(&fd);
+  }
+
+  return pos;
+}
+
+static void badge_save_fahrplan_pos(size_t pos) {
+  FIL fd;
+
+  if(FR_OK == f_open(&fd, FAHRPLAN_POSFILE, FA_CREATE_ALWAYS | FA_WRITE)) {
+    UINT bytes;
+    f_write(&fd, &pos, sizeof(pos), &bytes);
+    f_close(&fd);
+  }
+}
+
+void badge_fahrplan(void) {
+  f_chdir(FAHRPLAN_PATH);
+
+  size_t oldpos = badge_load_fahrplan_pos();
+  size_t pos = oldpos;
+
+  badge_browse_textfiles(FAHRPLAN_MENUFILE, &pos);
+
+  if(oldpos != pos) {
+    badge_save_fahrplan_pos(pos);
+  }
+}
diff --git a/badge/fahrplan.h b/badge/fahrplan.h
new file mode 100644 (file)
index 0000000..f1ecccf
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef INCLUDED_BADGE_FAHRPLAN_H
+#define INCLUDED_BADGE_FAHRPLAN_H
+
+void badge_fahrplan(void);
+
+#endif
index 466ae3e..e7d30a3 100644 (file)
@@ -53,6 +53,7 @@
   #include "core/cmd/cmd.h"
 #endif
 
+#include "fahrplan.h"
 #include "init.h"
 #include "ui/browser.h"
 #include "ui/display.h"
@@ -202,11 +203,6 @@ static void usbmode(void) {
   for(;;);
 }
 
-static void fahrplan(void) {
-  f_chdir("/fahrplan");
-  badge_browse_textfiles("fahrplan.lst");
-}
-
 uint8_t main_menu_show(uint8_t selected) {
   char const menu_buf[][15] = {
     "Vanity-Screen",
@@ -234,7 +230,7 @@ void main_menu(void) {
     switch(selected) {
     case 0: badge_vanity_show(); break;
     case 1: jumpnrun_play    (); break;
-    case 2: fahrplan         (); break;
+    case 2: badge_fahrplan   (); break;
     case 3: usbmode          (); break;
     }
   }
index 1f5fe24..5a2d79c 100644 (file)
@@ -107,12 +107,11 @@ void badge_browse_textfile(char const *fname) {
   }
 }
 
-void badge_browse_textfiles(char const *menufile) {
+void badge_browse_textfiles(char const *menufile, size_t *selected) {
   char buf[FNAME_MAX + 1];
   size_t first_visible = 0;
-  size_t selected = 0;
 
-  while(0 == badge_pick_filename(buf, menufile, &first_visible, &selected)) {
+  while(0 == badge_pick_filename(buf, menufile, &first_visible, selected)) {
     badge_browse_textfile(buf);
   }
 }
index 3fc35b5..3e9f433 100644 (file)
@@ -1,7 +1,9 @@
 #ifndef INCLUDED_BADGE_UI_BROWSER_H
 #define INCLUDED_BADGE_UI_BROWSER_H
 
+#include <stddef.h>
+
 void badge_browse_textfile(char const *fname);
-void badge_browse_textfiles(char const *menufile);
+void badge_browse_textfiles(char const *menufile, size_t *selected);
 
 #endif
index 79e2e8a..c67471d 100644 (file)
@@ -39,6 +39,10 @@ static void badge_menu_show(char const *const *menu,
   size_t first_used_row = 0;
   size_t used_rows = MENU_ENTRIES_VISIBLE;
 
+  if(selected >= n) {
+    selected = n - 1;
+  }
+
   if(n <= MENU_ENTRIES_VISIBLE) {
     *first_visible = 0;
     used_rows      = n;
This page took 0.028135 seconds and 4 git commands to generate.