vanity-convert: add Makefile
[hackover2013-badge-firmware.git] / badge / fahrplan.c
1 #include "fahrplan.h"
2 #include "ui/browser.h"
3
4 #include <drivers/fatfs/ff.h>
5 #include <stdint.h>
6
7 #define FAHRPLAN_PATH "/fahrplan"
8 #define FAHRPLAN_MENUFILE "fahrplan.lst"
9 #define FAHRPLAN_POSFILE "selected.dat"
10
11 static uint8_t badge_load_fahrplan_pos(void) {
12 uint8_t pos = 0;
13 FIL fd;
14
15 if(FR_OK == f_open(&fd, FAHRPLAN_POSFILE, FA_OPEN_EXISTING | FA_READ)) {
16 UINT bytes;
17 uint8_t buf;
18 if(FR_OK == f_read(&fd, &buf, sizeof(buf), &bytes) && bytes == sizeof(buf)) {
19 pos = buf;
20 }
21
22 f_close(&fd);
23 }
24
25 return pos;
26 }
27
28 static void badge_save_fahrplan_pos(uint8_t pos) {
29 FIL fd;
30
31 if(FR_OK == f_open(&fd, FAHRPLAN_POSFILE, FA_CREATE_ALWAYS | FA_WRITE)) {
32 UINT bytes;
33 f_write(&fd, &pos, sizeof(pos), &bytes);
34 f_close(&fd);
35 }
36 }
37
38 void badge_fahrplan(void) {
39 f_chdir(FAHRPLAN_PATH);
40
41 uint8_t oldpos = badge_load_fahrplan_pos();
42 uint8_t pos = oldpos;
43
44 badge_browse_textfiles(FAHRPLAN_MENUFILE, &pos);
45
46 if(oldpos != pos) {
47 badge_save_fahrplan_pos(pos);
48 }
49 }
This page took 0.05492 seconds and 5 git commands to generate.