Added filesystem module, tests and implementation for it in emulator.
This commit is contained in:
@@ -3,12 +3,13 @@
|
||||
#include "system.h"
|
||||
#include "touch.h"
|
||||
#include "screen_main.h"
|
||||
|
||||
#include "filesystem.h"
|
||||
|
||||
void app_init() {
|
||||
system_init();
|
||||
tft_init();
|
||||
touch_init();
|
||||
filesystem_init();
|
||||
|
||||
gui_screen_navigate(get_screen_main());
|
||||
}
|
||||
|
||||
128
common/app/screen_filetest.c
Normal file
128
common/app/screen_filetest.c
Normal file
@@ -0,0 +1,128 @@
|
||||
#include "screen_filetest.h"
|
||||
#include "button.h"
|
||||
#include "tft.h"
|
||||
#include "filesystem.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
static BUTTON_STRUCT b_back;
|
||||
|
||||
|
||||
static void b_back_cb(void* button) {
|
||||
gui_screen_back();
|
||||
}
|
||||
|
||||
static void enter(void* screen) {
|
||||
tft_clear(HEX(0xBABECD));
|
||||
|
||||
//Back button
|
||||
b_back.base.x1=10; //Start X of Button
|
||||
b_back.base.y1=200; //Start Y of Button
|
||||
b_back.base.x2=AUTO; //b_back.base.x1+160; //Auto Calculate X2 with String Width
|
||||
b_back.base.y2=AUTO; //Auto Calculate Y2 with String Height
|
||||
b_back.txtcolor=WHITE; //Set foreground color
|
||||
b_back.bgcolor=HEX(0xAE1010); //Set background color (Don't take 255 or 0 on at least one channel, to make shadows possible)
|
||||
b_back.font=0; //Select Font
|
||||
b_back.text="Back"; //Set Text (For formatted strings take sprintf)
|
||||
b_back.callback=b_back_cb; //Call b_back_cb as Callback
|
||||
gui_button_add(&b_back); //Register Button (and run the callback from now on)
|
||||
|
||||
|
||||
/*
|
||||
//tft test
|
||||
tft_draw_pixel(0,0,BLACK);
|
||||
tft_draw_pixel(319,239,BLACK);
|
||||
tft_draw_pixel(10,210,BLUE);
|
||||
tft_draw_pixel(12,210,BLUE);
|
||||
tft_draw_rectangle(40,100,60,235,BLUE);
|
||||
tft_fill_rectangle(100,215,200,225,GREEN);
|
||||
tft_draw_line(10,50,310,225,RGB(0xFF,0,0xFF));
|
||||
tft_draw_circle(10,10,100, RED);
|
||||
tft_print_line(30, 130, RED, BLUE, 0, "Hallo");
|
||||
*/
|
||||
|
||||
tft_draw_line(10,30,310,30,BLACK);
|
||||
tft_print_line(10,18,BLUE,TRANSPARENT,0,"Name D H RO Date Time Size");
|
||||
|
||||
int y = 33;
|
||||
|
||||
DIRECTORY_STRUCT* dir = filesystem_dir_open(".");
|
||||
if(dir==NULL) return;
|
||||
|
||||
for(int i=0; i<dir->num_files; i++) {
|
||||
FILE_STRUCT* file = &(dir->files[i]);
|
||||
tft_print_formatted(10,y,
|
||||
(file->fattrib&F_DIR)?GREEN:RED,
|
||||
TRANSPARENT,0,"%-13s%c %c %s %02u%02u%02u %02u:%02u:%02u %u",
|
||||
file->fname,
|
||||
(file->fattrib&F_DIR)?'D':' ',
|
||||
(file->fattrib&F_HID)?'H':' ',
|
||||
(file->fattrib&F_RDO)?"R ":"RW",
|
||||
file->fdate.day,
|
||||
file->fdate.month,
|
||||
(file->fdate.year+1980)%100,
|
||||
file->ftime.hour,
|
||||
file->ftime.min,
|
||||
file->ftime.sec*2,
|
||||
file->fsize);
|
||||
y+=14;
|
||||
}
|
||||
|
||||
|
||||
y+=14;
|
||||
|
||||
FILE_HANDLE* file = filesystem_file_open("test.txt");
|
||||
if(file==NULL) {
|
||||
tft_print_line(10,y,BLUE,TRANSPARENT,0,"Could not open test.txt");
|
||||
} else {
|
||||
char buf [30];
|
||||
int size = (file->fsize>30)?29:file->fsize-1;
|
||||
FILE_STATUS st = filesystem_file_read(file,buf,size);
|
||||
|
||||
if(st==F_OK) {
|
||||
buf[file->fpos]='\0';
|
||||
tft_print_formatted(10,y,BLUE,TRANSPARENT,0,"test.txt contains \"%s\"",buf);
|
||||
long num = strtol(&(buf[file->fpos-4]),NULL,0);
|
||||
num++;
|
||||
|
||||
y+=14;
|
||||
|
||||
if(filesystem_file_seek(file,file->fpos-4)!=F_OK) {
|
||||
tft_print_formatted(10,y,BLUE,TRANSPARENT,0,"Could not seek to %d",file->fpos-4);
|
||||
} else {
|
||||
sprintf(buf,"%04d",num);
|
||||
if(filesystem_file_write(file,buf,4) != F_OK) {
|
||||
tft_print_formatted(10,y,BLUE,TRANSPARENT,0,"Could not write new number %d",num);
|
||||
} else {
|
||||
tft_print_formatted(10,y,BLUE,TRANSPARENT,0,"New number written %d",num);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tft_print_line(10,y,BLUE,TRANSPARENT,0,"Could not read from test.txt");
|
||||
}
|
||||
|
||||
}
|
||||
filesystem_file_close(file);
|
||||
|
||||
}
|
||||
|
||||
static void leave(void* screen) {
|
||||
gui_button_remove(&b_back);
|
||||
}
|
||||
|
||||
static void update(void* screen) {
|
||||
}
|
||||
|
||||
|
||||
static SCREEN_STRUCT screen = {
|
||||
enter,
|
||||
leave,
|
||||
update
|
||||
};
|
||||
|
||||
|
||||
SCREEN_STRUCT* get_screen_filetest() {
|
||||
return &screen;
|
||||
}
|
||||
|
||||
|
||||
3
common/app/screen_filetest.h
Normal file
3
common/app/screen_filetest.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "screen.h"
|
||||
|
||||
SCREEN_STRUCT* get_screen_filetest();
|
||||
@@ -1,17 +1,22 @@
|
||||
#include "screen_main.h"
|
||||
#include "screen_guitest.h"
|
||||
#include "screen_pixytest.h"
|
||||
#include "screen_filetest.h"
|
||||
#include "button.h"
|
||||
#include "tft.h"
|
||||
|
||||
BUTTON_STRUCT b_guitest;
|
||||
BUTTON_STRUCT b_pixytest;
|
||||
|
||||
BUTTON_STRUCT b_filetest;
|
||||
|
||||
static void b_guitest_cb(void* button) {
|
||||
gui_screen_navigate(get_screen_guitest());
|
||||
}
|
||||
|
||||
static void b_filetest_cb(void* button) {
|
||||
gui_screen_navigate(get_screen_filetest());
|
||||
}
|
||||
|
||||
static void b_pixytest_cb(void* button) {
|
||||
gui_screen_navigate(get_screen_pixytest());
|
||||
}
|
||||
@@ -33,7 +38,7 @@ static void enter(void* screen) {
|
||||
gui_button_add(&b_guitest); //Register Button (and run the callback from now on)
|
||||
|
||||
//button to reach pixy test
|
||||
b_pixytest.base.x1=200; //Start X of Button
|
||||
b_pixytest.base.x1=150; //Start X of Button
|
||||
b_pixytest.base.y1=45; //Start Y of Button
|
||||
b_pixytest.base.x2=AUTO; //b_pixytest.base.x1+160; //Auto Calculate X2 with String Width
|
||||
b_pixytest.base.y2=AUTO; //Auto Calculate Y2 with String Height
|
||||
@@ -44,6 +49,18 @@ static void enter(void* screen) {
|
||||
b_pixytest.callback=b_pixytest_cb; //Call b_pixytest_cb as Callback
|
||||
gui_button_add(&b_pixytest); //Register Button (and run the callback from now on)
|
||||
|
||||
//button to reach filesystem test
|
||||
b_filetest.base.x1=240; //Start X of Button
|
||||
b_filetest.base.y1=45; //Start Y of Button
|
||||
b_filetest.base.x2=AUTO; //b_filetest.base.x1+160; //Auto Calculate X2 with String Width
|
||||
b_filetest.base.y2=AUTO; //Auto Calculate Y2 with String Height
|
||||
b_filetest.txtcolor=WHITE; //Set foreground color
|
||||
b_filetest.bgcolor=HEX(0x501EA0); //Set background color (Don't take 255 or 0 on at least one channel, to make shadows possible)
|
||||
b_filetest.font=0; //Select Font
|
||||
b_filetest.text="File Test"; //Set Text (For formatted strings take sprintf)
|
||||
b_filetest.callback=b_filetest_cb; //Call b_filetest_cb as Callback
|
||||
gui_button_add(&b_filetest); //Register Button (and run the callback from now on)
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -51,6 +68,7 @@ static void enter(void* screen) {
|
||||
static void leave(void* screen) {
|
||||
gui_button_remove(&b_guitest);
|
||||
gui_button_remove(&b_pixytest);
|
||||
gui_button_remove(&b_filetest);
|
||||
}
|
||||
|
||||
static void update(void* screen) {
|
||||
|
||||
38
common/filesystem/filesystem.c
Normal file
38
common/filesystem/filesystem.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "filesystem.h"
|
||||
#include "ll_filesystem.h"
|
||||
|
||||
bool filesystem_init() {
|
||||
return ll_filesystem_init();
|
||||
}
|
||||
|
||||
DIRECTORY_STRUCT* filesystem_dir_open(const char* path) {
|
||||
return ll_filesystem_dir_open(path);
|
||||
}
|
||||
|
||||
void filesystem_dir_close(DIRECTORY_STRUCT* dir) {
|
||||
filesystem_dir_close(dir);
|
||||
}
|
||||
|
||||
FILE_HANDLE* filesystem_file_open(const char* filename) {
|
||||
return ll_filesystem_file_open(filename);
|
||||
}
|
||||
|
||||
void filesystem_file_close(FILE_HANDLE* handle) {
|
||||
ll_filesystem_file_close(handle);
|
||||
}
|
||||
|
||||
FILE_STATUS filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset) {
|
||||
return ll_filesystem_file_seek(handle,offset);
|
||||
}
|
||||
|
||||
FILE_STATUS filesystem_file_read(FILE_HANDLE* handle, uint8_t* buf, uint32_t size) {
|
||||
return ll_filesystem_file_read(handle,buf,size);
|
||||
}
|
||||
|
||||
FILE_STATUS filesystem_file_write(FILE_HANDLE* handle, uint8_t* buf, uint32_t size) {
|
||||
return ll_filesystem_file_write(handle,buf,size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#ifndef FILESYSTEM_H
|
||||
#define FILESYSTEM_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
F_DIR=1,
|
||||
F_RDO=2,
|
||||
F_HID=4,
|
||||
F_SYS=8,
|
||||
F_ARC=16
|
||||
} FILE_ATTRIBUTES;
|
||||
|
||||
|
||||
typedef struct {
|
||||
unsigned year : 7; //year from 1980 (0..127)
|
||||
unsigned month: 4; //month (1..12)
|
||||
unsigned day: 5; //day (1..31)
|
||||
} FILE_DATE_STRUCT;
|
||||
|
||||
|
||||
typedef struct {
|
||||
unsigned hour : 5; //hour (0..23)
|
||||
unsigned min: 6; //minute (0..59
|
||||
unsigned sec: 5; //second/2 (0..29)
|
||||
} FILE_TIME_STRUCT;
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint32_t fsize; /* File size */
|
||||
FILE_DATE_STRUCT fdate; /* Last modified date */
|
||||
FILE_TIME_STRUCT ftime; /* Last modified time */
|
||||
uint8_t fattrib; /* Attribute */
|
||||
char* fname; /* File name */
|
||||
} FILE_STRUCT;
|
||||
|
||||
typedef struct {
|
||||
const char* path;
|
||||
uint16_t num_files;
|
||||
FILE_STRUCT* files;
|
||||
} DIRECTORY_STRUCT;
|
||||
|
||||
typedef struct {
|
||||
const char* fname;
|
||||
uint32_t fpos;
|
||||
uint32_t fsize;
|
||||
} FILE_HANDLE;
|
||||
|
||||
typedef enum {
|
||||
F_OK,
|
||||
F_EOF,
|
||||
F_EACCESS,
|
||||
F_INVALIDPARAM,
|
||||
F_DISKERROR
|
||||
} FILE_STATUS;
|
||||
|
||||
|
||||
bool filesystem_init();
|
||||
|
||||
DIRECTORY_STRUCT* filesystem_dir_open(const char* path);
|
||||
void filesystem_dir_close(DIRECTORY_STRUCT* dir);
|
||||
FILE_HANDLE* filesystem_file_open(const char* filename);
|
||||
void filesystem_file_close(FILE_HANDLE* handle);
|
||||
FILE_STATUS filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset);
|
||||
FILE_STATUS filesystem_file_read(FILE_HANDLE* handle, uint8_t* buf, uint32_t size);
|
||||
FILE_STATUS filesystem_file_write(FILE_HANDLE* handle, uint8_t* buf, uint32_t size);
|
||||
|
||||
|
||||
#endif /* FILESYSTEM_H */
|
||||
|
||||
|
||||
11
common/lowlevel/ll_filesystem.h
Normal file
11
common/lowlevel/ll_filesystem.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "filesystem.h"
|
||||
|
||||
bool ll_filesystem_init();
|
||||
|
||||
DIRECTORY_STRUCT* ll_filesystem_dir_open(const char* path);
|
||||
void ll_filesystem_dir_close(DIRECTORY_STRUCT* dir);
|
||||
FILE_HANDLE* ll_filesystem_file_open(const char* filename);
|
||||
void ll_filesystem_file_close(FILE_HANDLE* handle);
|
||||
FILE_STATUS ll_filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset);
|
||||
FILE_STATUS ll_filesystem_file_read(FILE_HANDLE* handle, uint8_t* buf, uint32_t size);
|
||||
FILE_STATUS ll_filesystem_file_write(FILE_HANDLE* handle, uint8_t* buf, uint32_t size);
|
||||
Reference in New Issue
Block a user