-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
157 lines (135 loc) · 3.69 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <tonc.h>
#include <string>
#include <vector>
extern "C" {
// (0) Include the header
#include "../lib/flashcartio.h"
}
#define MAX_ENTRIES 20
void listDir(std::vector<FILINFO> items,
u32 selected,
u32 offset = 0,
u32 count = MAX_ENTRIES);
std::vector<FILINFO> readDir(std::string path);
void log(std::string text);
void halt(std::string text);
u16 waitFor(u16 key);
const std::string names[] = {"-", "Everdrive", "EZ Flash"};
void init() {
REG_DISPCNT = DCNT_MODE0 | DCNT_BG0;
tte_init_se_default(0, BG_CBB(0) | BG_SBB(31));
irq_init(NULL);
irq_add(II_VBLANK, NULL);
REG_KEYCNT = 0b1100000000001111;
irq_add(II_KEYPAD, SoftReset);
// (1) Initialize the library
log("Activating...");
if (!flashcartio_activate())
halt("No flashcart detected!");
}
int main() {
init();
log("Detected flashcart: " + names[active_flashcart] +
"\n\nPress A and open a text file!\nPress B to go back!");
waitFor(KEY_A);
// (2) Use FatFs functions
FATFS fatfs;
FRESULT fr = f_mount(&fatfs, "", 1);
if (fr > 0)
halt("mount failed!");
std::string path = "";
u32 selected = 0;
std::vector<FILINFO> files = readDir(path);
listDir(files, selected);
while (true) {
u16 keys = waitFor(KEY_DOWN | KEY_UP | KEY_A | KEY_B);
if (keys & KEY_DOWN) {
// Cursor down
selected = (selected + 1) % files.size();
} else if (keys & KEY_UP) {
// Cursor up
selected = selected == 0 ? files.size() - 1 : selected - 1;
} else if (keys & KEY_A) {
// Read file/directory
auto selectedItem = files[selected];
if (selectedItem.fattrib & AM_DIR) {
// Navigate to directory
path += "/" + std::string(selectedItem.fname);
files = readDir(path);
selected = 0;
} else {
// Read file
if (selectedItem.fsize < 1024 * 10) {
auto filePath = path + "/" + selectedItem.fname;
FIL fil;
f_open(&fil, filePath.c_str(), FA_READ);
if (fr > 0)
halt("open failed!");
std::string content = "";
char line[128];
while (f_gets(line, sizeof(line), &fil))
content += std::string(line) + "\n";
log(content);
} else {
log("Nah, too big! Use a text file!");
}
waitFor(KEY_B);
}
} else if (keys & KEY_B) {
// Go back
if (path != "/") {
path = "/";
files = readDir(path);
selected = 0;
}
}
VBlankIntrWait();
listDir(files, selected, max(selected - MAX_ENTRIES / 2, 0));
}
return 0;
}
void listDir(std::vector<FILINFO> items, u32 selected, u32 offset, u32 count) {
std::string output = "";
for (u32 i = offset; i < items.size(); i++) {
auto item = items[i];
output += std::string(selected == i ? ">>" : "") +
(item.fattrib & AM_DIR ? "[" : "") + std::string(item.fname) +
(item.fattrib & AM_DIR ? "]" : "") + "\n";
if (i - offset + 1 >= count)
break;
}
log(output);
}
std::vector<FILINFO> readDir(std::string path) {
auto items = std::vector<FILINFO>{};
DIR dir;
FRESULT fr = f_opendir(&dir, path.c_str());
if (fr > 0)
halt("opendir failed!");
FILINFO fno;
while (f_readdir(&dir, &fno) == FR_OK && fno.fname[0] != 0)
items.push_back(fno);
fr = f_closedir(&dir);
if (fr > 0)
halt("closedir failed!");
return items;
}
void log(std::string text) {
tte_erase_screen();
tte_write("#{P:0,0}");
tte_write(text.c_str());
}
void halt(std::string text) {
log(text);
while (true)
;
}
u16 waitFor(u16 key) {
u32 hits;
do {
VBlankIntrWait();
key_poll();
hits = key_hit(key);
} while (hits == 0);
return hits;
}