Skip to content

Commit f0f8753

Browse files
committed
[Example-Cpp] Add MIT licence and some code cleanup
1 parent 0625aa0 commit f0f8753

8 files changed

Lines changed: 164 additions & 43 deletions

File tree

apps/Example-Cpp/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Damien Nicolet and contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

apps/Example-Cpp/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
API=../../api
22
CC=arm-none-eabi-gcc
33
OBJCOPY=arm-none-eabi-objcopy
4-
AR=arm-none-eabi-ar
54
CFLAGS=-DNDEBUG -ggdb3 -I$(API) -Os -mcpu=cortex-m7 -mthumb -mfpu=fpv5-sp-d16 -mfloat-abi=hard -fno-common -fdata-sections -ffunction-sections -fno-exceptions
65
LDFLAGS=-Wl,-L$(API) -Wl,--gc-sections -Wl,--entry=entrypoint --specs=nosys.specs -nostartfiles -Wl,-Ur -lapi
76

7+
OBJS = main.o peripherals.o selector.o
8+
89
%.o: %.cpp
910
$(CC) $(CFLAGS) -c $<
1011

11-
app.elf: main.o peripherals.o selector.o
12+
app.elf: $(OBJS)
1213
$(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS)
1314

1415
clean:

apps/Example-Cpp/app.elf

93.3 KB
Binary file not shown.

apps/Example-Cpp/app.icon

-437 Bytes
Binary file not shown.

apps/Example-Cpp/icon.png

-982 Bytes
Loading

apps/Example-Cpp/icon.svg

Lines changed: 129 additions & 0 deletions
Loading

apps/Example-Cpp/main.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@ void extapp_main(void) {
1414
Peripherals::waitForKeyReleased();
1515
// Draw a white background
1616
Peripherals::init_display();
17-
// Draw hello world message
17+
1818
extapp_drawTextLarge("Hello World !", 0, 20 * 1, 0x0000, 0xFFFF, false);
1919
extapp_drawTextLarge("Press any key to continue", 0, 20 * 2, 0x0000, 0xFFFF, false);
2020

21-
// Just wait for a key to be pressed (defined in peripherals.c)
2221
Peripherals::waitForKeyPressed();
2322
Peripherals::waitForKeyReleased();
2423

2524
// Select a file (The code is in selector.c)
26-
// The code is a bit more complicated, but you can see how to use the file system API to read a file
2725
const char * filename = Selector::select_file("", 10);
28-
// Wait for the key to be released
2926
Peripherals::waitForKeyReleased();
3027

3128
// Clear the screen
@@ -81,17 +78,16 @@ void extapp_main(void) {
8178

8279
// Initialize the filename to write to the ram filesystem
8380
const char * filename_to_write = "hello_world.py";
84-
// Initialize the file content to write to the ram filesystem, add three spaces at the beginning
81+
// Initialize the file content to write to the ram filesystem, add 1 spaces at the beginning
8582
// because the Python app use the first character to store the auto importation status
8683
const char * filecontent_to_write = " hello world";
8784

8885
// Write the file into the ram filesystem
8986
extapp_fileWrite(filename_to_write, filecontent_to_write, strlen(filecontent_to_write) + 1, EXTAPP_RAM_FILE_SYSTEM);
9087
extapp_drawTextLarge("File written to ram filesystem", 0, 20 * 2, 0x0000, 0xFFFF, false);
9188
extapp_drawTextLarge("Press any key to exit", 0, 20 * 3, 0x0000, 0xFFFF, false);
89+
9290
// Wait for a key to be pressed before exiting the application
9391
Peripherals::waitForKeyPressed();
9492
Peripherals::waitForKeyReleased();
95-
96-
return;
9793
}

apps/Example-Cpp/selector.cpp

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@ namespace Selector {
1414
* @param nb int, the number of files in the list
1515
* @param selected_file int, the selected file to highlight
1616
*/
17-
static void drawfileList(char ** filenames, int nb, int selected_file) {
18-
// Initialize the name buffer
17+
static void drawfileList(char **filenames, int nb, int selected_file) {
1918
char name_buffer[FILENAME_LENGTH_MAX];
20-
// Iterate through the file list
19+
// Iterate through the file list and display them
2120
for (int i = 0; i < nb; i++) {
22-
// Copy the filename into the buffer
2321
strncpy(name_buffer, filenames[i], FILENAME_LENGTH_MAX);
24-
// Force the string to terminate with \0 because the filename can be longer than the length of the buffer
22+
// Truncate the list if needed
2523
name_buffer[26] = '\0';
26-
// Draw the filename, highlighted if selected
2724
extapp_drawTextLarge(name_buffer, 0, (i + 1) * 20, selected_file == i ? SELECTOR_COLOR_FG_SELECT : SELECTOR_COLOR_FG, SELECTOR_COLOR_BG, false);
28-
// TODO: I don't understand it
25+
26+
// Shouldn't be necessary
2927
strncpy(name_buffer, filenames[i], FILENAME_LENGTH_MAX);
3028
}
3129
}
3230

31+
// Remove element from the list
3332
static char ** remove(char ** first, char ** last) {
3433
char ** result = first;
3534
while (first != last) {
@@ -49,90 +48,65 @@ static char ** remove(char ** first, char ** last) {
4948
* @return const char *, the name of the selected file, or NULL if no file is selected
5049
*/
5150
const char * select_file(const char * extension_to_match, int max_files) {
52-
// The file list
5351
char * filenames[max_files];
54-
// The selected file index in the order of the file list
5552
int selected_file = 0;
5653

57-
// Wait for the last key to be released
5854
Peripherals::waitForKeyReleased();
5955

60-
// Clear the display
6156
extapp_pushRectUniform(0, 0, LCD_WIDTH, LCD_HEIGHT, SELECTOR_COLOR_BG);
62-
// Draw title bar
6357
extapp_drawTextLarge(" Select a file ", 0, 0, SELECTOR_COLOR_HEAD_FG, SELECTOR_COLOR_HEAD_BG, false);
6458

6559
// Get the list of files from the flash memory
6660
int nb = extapp_fileListWithExtension((const char **)filenames, max_files, "", EXTAPP_FLASH_FILE_SYSTEM);
6761

68-
// The extension of the file
6962
char * extension;
7063

7164
// If the extension to match isn't empty, filter the file list
72-
// Checking without strcomp, because it's also a string littery when it is empty
7365
if (extension_to_match[0] != '\0') {
74-
// Iterate through the file list
7566
for (int i = 0; i < nb; i++) {
76-
// Get the file extension by splitting his name by a dot
7767
extension = strrchr(filenames[i], '.');
7868

79-
// Get if the file's extension doesn't match the requested extension
8069
if (strcmp(extension, extension_to_match) != 0) {
8170
// If the extension doesn't match, delete the file from the list
8271
filenames[i] = NULL;
8372
}
8473
}
85-
// Update the number of files
8674
nb = remove(filenames, filenames + nb) - filenames;
8775
}
8876

8977
// If no files match, draw a no file found message
9078
if (nb == 0) {
91-
// Draw the text
9279
extapp_drawTextLarge(" No file found ", 0, 120, SELECTOR_COLOR_FG, SELECTOR_COLOR_BG, false);
93-
// Sleep for 10 milliseconds
9480
extapp_msleep(10);
95-
// Wait for a key to be pressed
9681
Peripherals::waitForKeyPressed();
97-
// Return nothing
9882
return NULL;
9983
} else {
10084
// Draw the file list
10185
drawfileList(filenames, nb, selected_file);
102-
// Infinite loop
10386
for (;;) {
104-
// Sleep for 10 milliseconds
10587
extapp_msleep(10);
88+
10689
// Scan the keyboard
10790
uint64_t scancode = extapp_scanKeyboard();
10891
// If the down arrow is pressed, select the next file
10992
if (scancode & SCANCODE_Down) {
110-
// Select the next file, and go to the first file if the file is out of range
11193
selected_file = (selected_file + 1) % nb;
112-
// Redraw the file list
11394
drawfileList(filenames, nb, selected_file);
114-
// Wait for the key to be released, or 200 milliseconds if the key sill pressed
11595
Peripherals::waitForKeyReleasedTimeout(200);
11696
} else if (scancode & SCANCODE_Up) {
117-
// Select the previous file
11897
selected_file = (selected_file - 1) % nb;
119-
// If the file is out of range, select the last file
12098
if (selected_file < 0) {
12199
selected_file = nb + selected_file;
122100
}
123-
// Redraw the file list
124101
drawfileList(filenames, nb, selected_file);
125-
// Wait for the key to be released, or 200 milliseconds if the key sill pressed
126102
Peripherals::waitForKeyReleasedTimeout(200);
127103
} else if (scancode & (SCANCODE_OK | SCANCODE_EXE)) {
128104
// If OK or EXE is pressed, exit the loop
129105
break;
130-
} else if (scancode & SCANCODE_Back) {
131-
// If back key is pressed, exit the menu and return nothing
106+
} else if (scancode & (SCANCODE_Back | SCANCODE_Home | SCANCODE_OnOff)) {
132107
return NULL;
133108
}
134109
}
135-
// Redraw the selected file
136110
return filenames[selected_file];
137111
}
138112
}

0 commit comments

Comments
 (0)