slideshow.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "slideshow.h"
  2. #include <stddef.h>
  3. #include <storage/storage.h>
  4. #include <gui/icon.h>
  5. #include <gui/icon_i.h>
  6. #include <furi/dangerous_defines.h>
  7. #define SLIDESHOW_MAGIC 0x72676468
  8. #define SLIDESHOW_MAX_SUPPORTED_VERSION 1
  9. struct Slideshow {
  10. Icon icon;
  11. uint32_t current_frame;
  12. };
  13. #pragma pack(push, 1)
  14. typedef struct {
  15. uint32_t magic;
  16. uint8_t version;
  17. uint8_t width;
  18. uint8_t height;
  19. uint8_t frame_count;
  20. } SlideshowFileHeader;
  21. _Static_assert(sizeof(SlideshowFileHeader) == 8, "Incorrect SlideshowFileHeader size");
  22. typedef struct {
  23. uint16_t size;
  24. } SlideshowFrameHeader;
  25. _Static_assert(sizeof(SlideshowFrameHeader) == 2, "Incorrect SlideshowFrameHeader size");
  26. #pragma pack(pop)
  27. Slideshow* slideshow_alloc() {
  28. Slideshow* ret = malloc(sizeof(Slideshow));
  29. return ret;
  30. }
  31. void slideshow_free(Slideshow* slideshow) {
  32. Icon* icon = &slideshow->icon;
  33. if(icon) {
  34. for(int frame_idx = 0; frame_idx < icon->frame_count; ++frame_idx) {
  35. uint8_t* frame_data = (uint8_t*)icon->frames[frame_idx];
  36. free(frame_data);
  37. }
  38. free((uint8_t**)icon->frames);
  39. }
  40. free(slideshow);
  41. }
  42. bool slideshow_load(Slideshow* slideshow, const char* fspath) {
  43. Storage* storage = furi_record_open("storage");
  44. File* slideshow_file = storage_file_alloc(storage);
  45. bool load_success = false;
  46. do {
  47. if(!storage_file_open(slideshow_file, fspath, FSAM_READ, FSOM_OPEN_EXISTING)) {
  48. break;
  49. }
  50. SlideshowFileHeader header;
  51. if((storage_file_read(slideshow_file, &header, sizeof(header)) != sizeof(header)) ||
  52. (header.magic != SLIDESHOW_MAGIC) ||
  53. (header.version > SLIDESHOW_MAX_SUPPORTED_VERSION)) {
  54. break;
  55. }
  56. Icon* icon = &slideshow->icon;
  57. FURI_CONST_ASSIGN(icon->frame_count, header.frame_count);
  58. FURI_CONST_ASSIGN(icon->width, header.width);
  59. FURI_CONST_ASSIGN(icon->height, header.height);
  60. icon->frames = malloc(header.frame_count * sizeof(uint8_t*));
  61. for(int frame_idx = 0; frame_idx < header.frame_count; ++frame_idx) {
  62. SlideshowFrameHeader frame_header;
  63. if(storage_file_read(slideshow_file, &frame_header, sizeof(frame_header)) !=
  64. sizeof(frame_header)) {
  65. break;
  66. }
  67. FURI_CONST_ASSIGN_PTR(icon->frames[frame_idx], malloc(frame_header.size));
  68. uint8_t* frame_data = (uint8_t*)icon->frames[frame_idx];
  69. if(storage_file_read(slideshow_file, frame_data, frame_header.size) !=
  70. frame_header.size) {
  71. break;
  72. }
  73. load_success = (frame_idx + 1) == header.frame_count;
  74. }
  75. } while(false);
  76. storage_file_free(slideshow_file);
  77. furi_record_close("storage");
  78. return load_success;
  79. }
  80. bool slideshow_advance(Slideshow* slideshow) {
  81. uint8_t next_frame = slideshow->current_frame + 1;
  82. if(next_frame < slideshow->icon.frame_count) {
  83. slideshow->current_frame = next_frame;
  84. return true;
  85. }
  86. return false;
  87. }
  88. void slideshow_goback(Slideshow* slideshow) {
  89. if(slideshow->current_frame > 0) {
  90. slideshow->current_frame--;
  91. }
  92. }
  93. void slideshow_draw(Slideshow* slideshow, Canvas* canvas, uint8_t x, uint8_t y) {
  94. furi_assert(slideshow->current_frame < slideshow->icon.frame_count);
  95. canvas_draw_bitmap(
  96. canvas,
  97. x,
  98. y,
  99. slideshow->icon.width,
  100. slideshow->icon.height,
  101. slideshow->icon.frames[slideshow->current_frame]);
  102. }