luat_lv_fs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "luat_base.h"
  2. #include "lvgl.h"
  3. #include "luat_fs.h"
  4. #define LUAT_LOG_TAG "lvgl.fs"
  5. #include "luat_log.h"
  6. typedef FILE * file_t;
  7. static bool luat_lv_fs_ready(struct _lv_fs_drv_t * drv);
  8. static lv_fs_res_t luat_lv_fs_open(struct _lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode);
  9. static lv_fs_res_t luat_lv_fs_close(struct _lv_fs_drv_t * drv, void * file_p);
  10. static lv_fs_res_t luat_lv_fs_remove(struct _lv_fs_drv_t * drv, const char * fn);
  11. static lv_fs_res_t luat_lv_fs_read(struct _lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br);
  12. static lv_fs_res_t luat_lv_fs_write(struct _lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw);
  13. static lv_fs_res_t luat_lv_fs_seek(struct _lv_fs_drv_t * drv, void * file_p, uint32_t pos);
  14. static lv_fs_res_t luat_lv_fs_tell(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p);
  15. static lv_fs_res_t luat_lv_fs_trunc(struct _lv_fs_drv_t * drv, void * file_p);
  16. static lv_fs_res_t luat_lv_fs_size(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * size_p);
  17. static lv_fs_res_t luat_lv_fs_rename(struct _lv_fs_drv_t * drv, const char * oldname, const char * newname);
  18. static lv_fs_res_t luat_lv_fs_free_space(struct _lv_fs_drv_t * drv, uint32_t * total_p, uint32_t * free_p);
  19. static lv_fs_res_t luat_lv_fs_dir_open(struct _lv_fs_drv_t * drv, void * rddir_p, const char * path);
  20. static lv_fs_res_t luat_lv_fs_dir_read(struct _lv_fs_drv_t * drv, void * rddir_p, char * fn);
  21. static lv_fs_res_t luat_lv_fs_dir_close(struct _lv_fs_drv_t * drv, void * rddir_p);
  22. void luat_lv_fs_init(void) {
  23. lv_fs_drv_t fs_drv = {
  24. .letter = '/',
  25. .file_size = sizeof(FILE*),
  26. .rddir_size = sizeof(FILE*),
  27. .ready_cb = luat_lv_fs_ready,
  28. .open_cb = luat_lv_fs_open,
  29. .close_cb = luat_lv_fs_close,
  30. .remove_cb = luat_lv_fs_remove,
  31. .read_cb = luat_lv_fs_read,
  32. .write_cb = NULL,
  33. .seek_cb = luat_lv_fs_seek,
  34. .tell_cb = luat_lv_fs_tell,
  35. .trunc_cb = NULL,
  36. .size_cb = luat_lv_fs_size,
  37. .rename_cb = NULL,
  38. .free_space_cb = NULL,
  39. .dir_open_cb = NULL,
  40. .dir_read_cb = NULL,
  41. .dir_close_cb = NULL,
  42. };
  43. lv_fs_drv_register(&fs_drv);
  44. };
  45. static bool luat_lv_fs_ready(struct _lv_fs_drv_t * drv) {
  46. return true;
  47. }
  48. static lv_fs_res_t luat_lv_fs_open(struct _lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode) {
  49. char rpath[128] = {0};
  50. if (*path != '/') {
  51. rpath[0] = '/';
  52. memcpy(rpath + 1, path, strlen(path) + 1);
  53. }
  54. else
  55. memcpy(rpath, path, strlen(path) + 1);
  56. FILE* fd = NULL;
  57. if (mode == LV_FS_MODE_WR)
  58. fd = luat_fs_fopen(rpath, "wb");
  59. else
  60. fd = luat_fs_fopen(rpath, "rb");
  61. if (fd == NULL) {
  62. return LV_FS_RES_NOT_EX;
  63. };
  64. file_t* fp = file_p;
  65. *fp = fd;
  66. return LV_FS_RES_OK;
  67. }
  68. static lv_fs_res_t luat_lv_fs_close(struct _lv_fs_drv_t * drv, void * file_p) {
  69. if (file_p != NULL) {
  70. file_t* fp = file_p;
  71. luat_fs_fclose(*fp);
  72. return LV_FS_RES_OK;
  73. }
  74. return LV_FS_RES_NOT_EX;
  75. }
  76. static lv_fs_res_t luat_lv_fs_remove(struct _lv_fs_drv_t * drv, const char * fn) {
  77. luat_fs_remove(fn);
  78. return LV_FS_RES_OK;
  79. }
  80. static lv_fs_res_t luat_lv_fs_read(struct _lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) {
  81. file_t* fp = file_p;
  82. *br = luat_fs_fread(buf, 1, btr, *fp);
  83. //LLOGD("luat_fs_fread expect %ld act %ld", btr, *br);
  84. if (*br > 0)
  85. return LV_FS_RES_OK;
  86. return LV_FS_RES_FS_ERR;
  87. }
  88. // static lv_fs_res_t luat_lv_fs_write(struct _lv_fs_drv_t * drv, void * file_p, const void * buf, uint32_t btw, uint32_t * bw) {
  89. // file_t* fp = file_p;
  90. // *bw = luat_fs_fwrite(buf, btw, 1, *fp);
  91. // if (*bw > 0)
  92. // return LV_FS_RES_OK;
  93. // return LV_FS_RES_FS_ERR;
  94. // }
  95. static lv_fs_res_t luat_lv_fs_seek(struct _lv_fs_drv_t * drv, void * file_p, uint32_t pos) {
  96. file_t* fp = file_p;
  97. int ret = luat_fs_fseek(*fp, pos, SEEK_SET);
  98. if (ret == 0)
  99. return LV_FS_RES_OK;
  100. return LV_FS_RES_FS_ERR;
  101. }
  102. static lv_fs_res_t luat_lv_fs_tell(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p) {
  103. file_t* fp = file_p;
  104. int ret = luat_fs_ftell(*fp);
  105. if (ret >= 0) {
  106. *pos_p = ret;
  107. return LV_FS_RES_OK;
  108. }
  109. return LV_FS_RES_FS_ERR;
  110. }
  111. // static lv_fs_res_t luat_lv_fs_trunc(struct _lv_fs_drv_t * drv, void * file_p) {
  112. // return LV_FS_RES_NOT_IMP;
  113. // }
  114. static lv_fs_res_t luat_lv_fs_size(struct _lv_fs_drv_t * drv, void * file_p, uint32_t * size_p) {
  115. file_t* fp = file_p;
  116. int curr = luat_fs_ftell(*fp);
  117. luat_fs_fseek(*fp, 0, SEEK_END);
  118. *size_p = luat_fs_ftell(*fp);
  119. luat_fs_fseek(*fp, curr, SEEK_SET);
  120. return LV_FS_RES_OK;
  121. }
  122. // static lv_fs_res_t luat_lv_fs_rename(struct _lv_fs_drv_t * drv, const char * oldname, const char * newname) {
  123. // int ret = luat_fs_rename(oldname, newname);
  124. // if (ret == 0)
  125. // return LV_FS_RES_OK;
  126. // return LV_FS_RES_FS_ERR;
  127. // }
  128. // static lv_fs_res_t luat_lv_fs_free_space(struct _lv_fs_drv_t * drv, uint32_t * total_p, uint32_t * free_p) {
  129. // return LV_FS_RES_NOT_IMP;
  130. // }
  131. // static lv_fs_res_t luat_lv_fs_dir_open(struct _lv_fs_drv_t * drv, void * rddir_p, const char * path) {
  132. // return LV_FS_RES_NOT_IMP;
  133. // }
  134. // static lv_fs_res_t luat_lv_fs_dir_read(struct _lv_fs_drv_t * drv, void * rddir_p, char * fn) {
  135. // return LV_FS_RES_NOT_IMP;
  136. // }
  137. // static lv_fs_res_t luat_lv_fs_dir_close(struct _lv_fs_drv_t * drv, void * rddir_p) {
  138. // return LV_FS_RES_NOT_IMP;
  139. // }