boilerplate_scene_2.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "../malveke_gb_photo.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #include <dolphin/dolphin.h>
  7. struct BoilerplateScene2
  8. {
  9. View *view;
  10. BoilerplateScene2Callback callback;
  11. void *context;
  12. };
  13. typedef struct
  14. {
  15. bool in_progress;
  16. int page;
  17. BoilerplateScene2 *instance;
  18. } BoilerplateScene2Model;
  19. void boilerplate_scene_2_set_callback(
  20. BoilerplateScene2 *instance,
  21. BoilerplateScene2Callback callback,
  22. void *context)
  23. {
  24. furi_assert(instance);
  25. furi_assert(callback);
  26. instance->callback = callback;
  27. instance->context = context;
  28. }
  29. void draw_thumbnail(void *context, Canvas *canvas, int page){
  30. BoilerplateScene2 *instance = context;
  31. Boilerplate *app = instance->context;
  32. UNUSED(app);
  33. // Gallery
  34. // for (int s=0;s< 8 * (page+1);s++) {
  35. int count = ((page + 2) * 0x1000) + 0x0e00;
  36. storage_file_seek(app->camera_ram_sav, count, true);
  37. for (int y = 0; y < 4; y++) {
  38. for (int x = 0; x < 4; x++) {
  39. storage_file_read(app->camera_ram_sav, app->tile_data, sizeof(app->tile_data));
  40. for (int row = 0; row < 8; row++) {
  41. uint8_t temp1 = app->tile_data[row * 2];
  42. uint8_t temp2 = app->tile_data[row * 2 + 1];
  43. for (int pixel = 7; pixel >= 0; pixel--) {
  44. int colorIndex = ((temp1 & 1)+((temp2 & 1)*2));
  45. if (colorIndex >=2) {
  46. canvas_draw_dot(canvas, (x*8)+ (pixel+47), (y*8)+row+17);
  47. }
  48. temp1 >>= 1;
  49. temp2 >>= 1;
  50. }
  51. }
  52. }
  53. }
  54. // }
  55. }
  56. void boilerplate_scene_2_draw(Canvas *canvas, BoilerplateScene2Model *model)
  57. {
  58. UNUSED(model);
  59. BoilerplateScene2 *instance = model->instance;
  60. Boilerplate *app = instance->context;
  61. UNUSED(app);
  62. canvas_clear(canvas);
  63. canvas_set_color(canvas, ColorBlack);
  64. canvas_set_font(canvas, FontPrimary);
  65. canvas_draw_str(canvas, 21, 13, "Export All Photos");
  66. canvas_set_font(canvas, FontPrimary);
  67. char totalText[32];
  68. snprintf(totalText, sizeof(totalText), "%02d/29", model->page);
  69. draw_thumbnail(instance, canvas, model->page);
  70. if (!model->in_progress)
  71. {
  72. elements_button_center(canvas, "OK");
  73. }
  74. else
  75. {
  76. float dict_progress = (float)(model->page * 1) / (float)(29);
  77. int progress_width = 128 / 2;
  78. int position_x = (128 / 2);
  79. int position_y = 52;
  80. elements_progress_bar_with_text(canvas, position_x - (progress_width / 2), position_y, progress_width, dict_progress, totalText);
  81. }
  82. }
  83. static void boilerplate_scene_2_model_init(BoilerplateScene2Model *const model, void *context)
  84. {
  85. BoilerplateScene2 *instance = context;
  86. model->instance = instance;
  87. model->page = 0;
  88. model->in_progress = false;
  89. }
  90. void save_all_image(void *context)
  91. {
  92. BoilerplateScene2 *instance = context;
  93. Boilerplate *app = instance->context;
  94. UNUSED(app);
  95. furi_assert(app);
  96. NotificationApp *notifications = furi_record_open(RECORD_NOTIFICATION);
  97. // Create MALVEKE dir
  98. if (storage_common_stat(app->storage, MALVEKE_APP_FOLDER, NULL) == FSE_NOT_EXIST)
  99. {
  100. storage_simply_mkdir(app->storage, MALVEKE_APP_FOLDER);
  101. }
  102. // Create MALVEKE Photos dir
  103. if (storage_common_stat(app->storage, MALVEKE_APP_FOLDER_PHOTOS, NULL) == FSE_NOT_EXIST)
  104. {
  105. storage_simply_mkdir(app->storage, MALVEKE_APP_FOLDER_PHOTOS);
  106. }
  107. for (int page = 0; page < 30; page++)
  108. {
  109. int count = (page + 1) * 0x1000;
  110. storage_file_seek(app->camera_ram_sav, count, true);
  111. // create file name
  112. FuriString *file_name = furi_string_alloc();
  113. get_timefilename(file_name, page);
  114. File *file = storage_file_alloc(app->storage);
  115. bool result =
  116. storage_file_open(file, furi_string_get_cstr(file_name), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  117. if (result)
  118. {
  119. static char bmp[BMP_SIZE(WIDTH, HEIGHT)];
  120. bmp_init(bmp, WIDTH, HEIGHT);
  121. // Selected Palette
  122. uint32_t palette[] = {
  123. bmp_encode(app->palette_color_hex_a),
  124. bmp_encode(app->palette_color_hex_b),
  125. bmp_encode(app->palette_color_hex_c),
  126. bmp_encode(app->palette_color_hex_d)};
  127. UNUSED(palette);
  128. for (int y = 0; y < 14; y++)
  129. {
  130. for (int x = 0; x < 16; x++)
  131. {
  132. storage_file_read(app->camera_ram_sav, app->tile_data, sizeof(app->tile_data));
  133. for (int row = 0; row < 8; row++)
  134. {
  135. uint8_t temp1 = app->tile_data[row * 2];
  136. uint8_t temp2 = app->tile_data[row * 2 + 1];
  137. for (int pixel = 7; pixel >= 0; pixel--)
  138. {
  139. bmp_set(
  140. bmp,
  141. (x * 8) + pixel,
  142. (y * 8) + row,
  143. palette[((temp1 & 1) + ((temp2 & 1) * 2))]);
  144. temp1 >>= 1;
  145. temp2 >>= 1;
  146. }
  147. }
  148. }
  149. }
  150. storage_file_write(file, bmp, sizeof(bmp));
  151. with_view_model(
  152. instance->view,
  153. BoilerplateScene2Model * model,
  154. {
  155. // UNUSED(model);
  156. model->page = page;
  157. },
  158. true);
  159. furi_delay_ms(120);
  160. }
  161. // Closing the "file descriptor"
  162. storage_file_close(file);
  163. // Freeing up memory
  164. storage_file_free(file);
  165. }
  166. notification_message(notifications, &sequence_success);
  167. with_view_model(
  168. instance->view,
  169. BoilerplateScene2Model * model,
  170. {
  171. model->in_progress = false;
  172. },
  173. true);
  174. }
  175. bool boilerplate_scene_2_input(InputEvent *event, void *context)
  176. {
  177. furi_assert(context);
  178. BoilerplateScene2 *instance = context;
  179. // Boilerplate *app = instance->context;
  180. if (event->type == InputTypeRelease)
  181. {
  182. switch (event->key)
  183. {
  184. case InputKeyOk:
  185. with_view_model(
  186. instance->view,
  187. BoilerplateScene2Model * model,
  188. {
  189. model->in_progress = true;
  190. },
  191. true);
  192. save_all_image(instance);
  193. break;
  194. case InputKeyBack:
  195. instance->callback(BoilerplateCustomEventScene2Back, instance->context);
  196. break;
  197. case InputKeyUp:
  198. case InputKeyDown:
  199. case InputKeyLeft:
  200. case InputKeyRight:
  201. case InputKeyMAX:
  202. break;
  203. }
  204. }
  205. return true;
  206. }
  207. void boilerplate_scene_2_exit(void *context)
  208. {
  209. furi_assert(context);
  210. Boilerplate *app = context;
  211. UNUSED(app);
  212. // boilerplate_stop_all_sound(app);
  213. // boilerplate_led_reset(app);
  214. }
  215. BoilerplateScene2 *boilerplate_scene_2_alloc()
  216. {
  217. BoilerplateScene2 *instance = malloc(sizeof(BoilerplateScene2));
  218. instance->view = view_alloc();
  219. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(BoilerplateScene2Model));
  220. view_set_context(instance->view, instance);
  221. view_set_draw_callback(instance->view, (ViewDrawCallback)boilerplate_scene_2_draw);
  222. view_set_input_callback(instance->view, boilerplate_scene_2_input);
  223. view_set_exit_callback(instance->view, boilerplate_scene_2_exit);
  224. with_view_model(
  225. instance->view,
  226. BoilerplateScene2Model * model,
  227. {
  228. boilerplate_scene_2_model_init(model, instance);
  229. },
  230. true);
  231. return instance;
  232. }
  233. void boilerplate_scene_2_free(BoilerplateScene2 *instance)
  234. {
  235. furi_assert(instance);
  236. view_free(instance->view);
  237. free(instance);
  238. }
  239. View *boilerplate_scene_2_get_view(BoilerplateScene2 *instance)
  240. {
  241. furi_assert(instance);
  242. return instance->view;
  243. }