boilerplate_scene_1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. extern const uint8_t u8g2_font_5x7_mf[1911]; // Declare as extern
  8. const uint8_t _I_DolphinCommon_56x48_0[] = {
  9. 0x01, 0x00, 0xdf, 0x00, 0x00, 0x1f, 0xfe, 0x0e, 0x05, 0x3f, 0x04, 0x06, 0x78, 0x06, 0x30, 0x20,
  10. 0xf8, 0x00, 0xc6, 0x12, 0x1c, 0x04, 0x0c, 0x0a, 0x38, 0x08, 0x08, 0x0c, 0x60, 0xc0, 0x21, 0xe0,
  11. 0x04, 0x0a, 0x18, 0x02, 0x1b, 0x00, 0x18, 0xa3, 0x00, 0x21, 0x90, 0x01, 0x8a, 0x20, 0x02, 0x19,
  12. 0x80, 0x18, 0x80, 0x64, 0x09, 0x20, 0x89, 0x81, 0x8c, 0x3e, 0x41, 0xe2, 0x80, 0x50, 0x00, 0x43,
  13. 0x08, 0x01, 0x0c, 0xfc, 0x68, 0x40, 0x61, 0xc0, 0x50, 0x30, 0x00, 0x63, 0xa0, 0x7f, 0x80, 0xc4,
  14. 0x41, 0x19, 0x07, 0xff, 0x02, 0x06, 0x18, 0x24, 0x03, 0x41, 0xf3, 0x2b, 0x10, 0x19, 0x38, 0x10,
  15. 0x30, 0x31, 0x7f, 0xe0, 0x34, 0x08, 0x30, 0x19, 0x60, 0x80, 0x65, 0x86, 0x0a, 0x4c, 0x0c, 0x30,
  16. 0x81, 0xb9, 0x41, 0xa0, 0x54, 0x08, 0xc7, 0xe2, 0x06, 0x8a, 0x18, 0x25, 0x02, 0x21, 0x0f, 0x19,
  17. 0x88, 0xd8, 0x6e, 0x1b, 0x01, 0xd1, 0x1b, 0x86, 0x39, 0x66, 0x3a, 0xa4, 0x1a, 0x50, 0x06, 0x48,
  18. 0x18, 0x18, 0xd0, 0x03, 0x01, 0x41, 0x98, 0xcc, 0x60, 0x39, 0x01, 0x49, 0x2d, 0x06, 0x03, 0x50,
  19. 0xf8, 0x40, 0x3e, 0x02, 0xc1, 0x82, 0x86, 0xc7, 0xfe, 0x0f, 0x28, 0x2c, 0x91, 0xd2, 0x90, 0x9a,
  20. 0x18, 0x19, 0x3e, 0x6d, 0x73, 0x12, 0x16, 0x00, 0x32, 0x49, 0x72, 0xc0, 0x7e, 0x5d, 0x44, 0xba,
  21. 0x2c, 0x08, 0xa4, 0xc8, 0x82, 0x06, 0x17, 0xe0, 0x81, 0x90, 0x2a, 0x40, 0x61, 0xe1, 0xa2, 0x44,
  22. 0x0c, 0x76, 0x2b, 0xe8, 0x89, 0x26, 0x43, 0x83, 0x31, 0x8c, 0x78, 0x0c, 0xb0, 0x48, 0x10, 0x1a,
  23. 0xe0, 0x00, 0x63,
  24. };
  25. const uint8_t* const _I_DolphinCommon_56x48[] = {_I_DolphinCommon_56x48_0};
  26. const Icon I_DolphinCommon_56x48 = {
  27. .width = 56,
  28. .height = 48,
  29. .frame_count = 1,
  30. .frame_rate = 0,
  31. .frames = _I_DolphinCommon_56x48};
  32. struct BoilerplateScene1 {
  33. View* view;
  34. BoilerplateScene1Callback callback;
  35. void* context;
  36. };
  37. typedef struct {
  38. BoilerplateScene1* instance;
  39. } BoilerplateScene1Model;
  40. void boilerplate_scene_1_set_callback(
  41. BoilerplateScene1* instance,
  42. BoilerplateScene1Callback callback,
  43. void* context) {
  44. furi_assert(instance);
  45. furi_assert(callback);
  46. instance->callback = callback;
  47. instance->context = context;
  48. }
  49. void boilerplate_scene_1_draw(Canvas* canvas, BoilerplateScene1Model* model) {
  50. UNUSED(model);
  51. BoilerplateScene1* instance = model->instance;
  52. Boilerplate* app = instance->context;
  53. canvas_clear(canvas);
  54. // Prepare canvas
  55. canvas_set_color(canvas, ColorBlack);
  56. canvas_draw_frame(canvas, 0, 0, FRAME_WIDTH, FRAME_HEIGTH);
  57. if(app->show_instructions) {
  58. canvas_draw_icon(canvas, 71, 15, &I_DolphinCommon_56x48);
  59. canvas_set_font(canvas, FontSecondary);
  60. canvas_draw_str_aligned(canvas, (128 / 2), 4, AlignCenter, AlignTop, "INSTRUCTIONS");
  61. canvas_set_custom_u8g2_font(canvas, u8g2_font_5x7_mf); // 5x7 font, 9 lines
  62. canvas_draw_icon_ex(canvas, 15, 18, &I_arrow_13x6, IconRotation180); // DOWN
  63. canvas_draw_icon(canvas, 28, 18, &I_arrow_13x6); // UP
  64. canvas_draw_str_aligned(canvas, 47, 21, AlignLeft, AlignCenter, "SCROLL");
  65. canvas_draw_icon_ex(canvas, 33, 26, &I_arrow_13x6, IconRotation270); // PREV
  66. canvas_draw_icon_ex(canvas, 22, 36, &I_arrow_13x6, IconRotation90); // NEXT
  67. canvas_draw_str_aligned(canvas, 47, 32, AlignLeft, AlignCenter, "LEFT");
  68. canvas_draw_str_aligned(canvas, 47, 43, AlignLeft, AlignCenter, "RIGHT");
  69. canvas_set_font(canvas, FontPrimary);
  70. elements_button_center(canvas, "OK");
  71. } else {
  72. // int count = (app->page) * 0x1000;
  73. int count = GB_FIRST_PHOTO_OFFSET + app->page * GB_PHOTO_SIZE;
  74. FURI_LOG_I(TAG, "Page \"%d\"\n", app->page);
  75. FURI_LOG_I(TAG, "Read Index \"%d\" \n", count);
  76. if(app->camera_ram_sav) {
  77. storage_file_seek(app->camera_ram_sav, (app->page) + 0x11B2, true);
  78. uint8_t status;
  79. storage_file_read(app->camera_ram_sav, &status, 1);
  80. FURI_LOG_I(TAG, "Status/Nº Photo \"%x\"\n", status);
  81. storage_file_seek(app->camera_ram_sav, count, true);
  82. for(int y = app->pos_y; y < 14; y++) {
  83. for(int x = app->pos_x; x < 16; x++) {
  84. storage_file_read(app->camera_ram_sav, app->tile_data, sizeof(app->tile_data));
  85. for(int row = 0; row < 8; row++) {
  86. uint8_t temp1 = app->tile_data[row * 2];
  87. uint8_t temp2 = app->tile_data[row * 2 + 1];
  88. for(int pixel = 7; pixel >= 0; pixel--) {
  89. if(((temp1 & 1) + ((temp2 & 1) * 2)) >= 2) {
  90. canvas_draw_dot(canvas, (x * 8) + pixel, (y * 8) + row);
  91. }
  92. temp1 >>= 1;
  93. temp2 >>= 1;
  94. }
  95. }
  96. }
  97. }
  98. if(app->info) {
  99. if(status == 0xFF) { // DELETED
  100. canvas_draw_rbox(canvas, 100, 4, 20, 11, 4);
  101. canvas_invert_color(canvas);
  102. canvas_draw_str_aligned(canvas, 110, 10, AlignCenter, AlignCenter, "D");
  103. canvas_invert_color(canvas);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. static void boilerplate_scene_1_model_init(BoilerplateScene1Model* const model, void* context) {
  110. BoilerplateScene1* instance = context;
  111. UNUSED(model);
  112. UNUSED(instance);
  113. model->instance = context;
  114. }
  115. void save_image(void* context) {
  116. Boilerplate* app = context;
  117. furi_assert(app);
  118. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  119. // Create MALVEKE dir
  120. if(storage_common_stat(app->storage, MALVEKE_APP_FOLDER, NULL) == FSE_NOT_EXIST) {
  121. storage_simply_mkdir(app->storage, MALVEKE_APP_FOLDER);
  122. }
  123. // Create MALVEKE Photos dir
  124. if(storage_common_stat(app->storage, MALVEKE_APP_FOLDER_PHOTOS, NULL) == FSE_NOT_EXIST) {
  125. storage_simply_mkdir(app->storage, MALVEKE_APP_FOLDER_PHOTOS);
  126. }
  127. int count = GB_FIRST_PHOTO_OFFSET + app->page * GB_PHOTO_SIZE;
  128. storage_file_seek(app->camera_ram_sav, count, true);
  129. // create file name
  130. FuriString* file_name = furi_string_alloc();
  131. get_timefilename(file_name, app->page);
  132. File* file = storage_file_alloc(app->storage);
  133. bool result =
  134. storage_file_open(file, furi_string_get_cstr(file_name), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  135. if(result) {
  136. static char bmp[BMP_SIZE(WIDTH, HEIGHT)];
  137. bmp_init(bmp, WIDTH, HEIGHT);
  138. // Selected Palette
  139. uint32_t palette[] = {
  140. bmp_encode(app->palette_color_hex_a),
  141. bmp_encode(app->palette_color_hex_b),
  142. bmp_encode(app->palette_color_hex_c),
  143. bmp_encode(app->palette_color_hex_d)};
  144. UNUSED(palette);
  145. for(int y = 0; y < 14; y++) {
  146. for(int x = 0; x < 16; x++) {
  147. storage_file_read(app->camera_ram_sav, app->tile_data, sizeof(app->tile_data));
  148. for(int row = 0; row < 8; row++) {
  149. uint8_t temp1 = app->tile_data[row * 2];
  150. uint8_t temp2 = app->tile_data[row * 2 + 1];
  151. for(int pixel = 7; pixel >= 0; pixel--) {
  152. bmp_set(
  153. bmp,
  154. (x * 8) + pixel,
  155. (y * 8) + row,
  156. palette[((temp1 & 1) + ((temp2 & 1) * 2))]);
  157. temp1 >>= 1;
  158. temp2 >>= 1;
  159. }
  160. }
  161. }
  162. }
  163. storage_file_write(file, bmp, sizeof(bmp));
  164. storage_file_close(file);
  165. }
  166. // Closing the "file descriptor"
  167. storage_file_close(file);
  168. // Freeing up memory
  169. storage_file_free(file);
  170. notification_message(notifications, result ? &sequence_success : &sequence_error);
  171. }
  172. bool boilerplate_scene_1_input(InputEvent* event, void* context) {
  173. furi_assert(context);
  174. BoilerplateScene1* instance = context;
  175. Boilerplate* app = instance->context;
  176. if(event->type == InputTypeRelease) {
  177. switch(event->key) {
  178. case InputKeyBack:
  179. with_view_model(
  180. instance->view,
  181. BoilerplateScene1Model * model,
  182. {
  183. UNUSED(model);
  184. instance->callback(BoilerplateCustomEventScene1Back, instance->context);
  185. },
  186. true);
  187. break;
  188. case InputKeyLeft:
  189. with_view_model(
  190. instance->view,
  191. BoilerplateScene1Model * model,
  192. {
  193. UNUSED(model);
  194. app->pos_y = 0;
  195. app->pos_x = 0;
  196. if(app->page > 0) {
  197. app->page--;
  198. } else {
  199. app->page = 29;
  200. }
  201. },
  202. true);
  203. break;
  204. case InputKeyRight:
  205. with_view_model(
  206. instance->view,
  207. BoilerplateScene1Model * model,
  208. {
  209. UNUSED(model);
  210. app->pos_y = 0;
  211. app->pos_x = 0;
  212. if(app->page < 29) {
  213. app->page++;
  214. } else {
  215. app->page = 0;
  216. }
  217. },
  218. true);
  219. break;
  220. case InputKeyUp:
  221. with_view_model(
  222. instance->view,
  223. BoilerplateScene1Model * model,
  224. {
  225. UNUSED(model);
  226. if(app->pos_y < 0) {
  227. app->pos_y++;
  228. }
  229. },
  230. true);
  231. break;
  232. case InputKeyDown:
  233. with_view_model(
  234. instance->view,
  235. BoilerplateScene1Model * model,
  236. {
  237. UNUSED(model);
  238. if(app->pos_y > -6) {
  239. app->pos_y--;
  240. }
  241. },
  242. true);
  243. break;
  244. case InputKeyOk:
  245. with_view_model(
  246. instance->view,
  247. BoilerplateScene1Model * model,
  248. {
  249. UNUSED(model);
  250. if(app->show_instructions) {
  251. app->show_instructions = false;
  252. } else {
  253. save_image(app);
  254. }
  255. },
  256. true);
  257. break;
  258. case InputKeyMAX:
  259. break;
  260. }
  261. }
  262. return true;
  263. }
  264. void boilerplate_scene_1_exit(void* context) {
  265. furi_assert(context);
  266. }
  267. void boilerplate_scene_1_enter(void* context) {
  268. furi_assert(context);
  269. BoilerplateScene1* instance = (BoilerplateScene1*)context;
  270. with_view_model(
  271. instance->view,
  272. BoilerplateScene1Model * model,
  273. { boilerplate_scene_1_model_init(model, instance); },
  274. true);
  275. }
  276. BoilerplateScene1* boilerplate_scene_1_alloc() {
  277. BoilerplateScene1* instance = malloc(sizeof(BoilerplateScene1));
  278. instance->view = view_alloc();
  279. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(BoilerplateScene1Model));
  280. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  281. view_set_draw_callback(instance->view, (ViewDrawCallback)boilerplate_scene_1_draw);
  282. view_set_input_callback(instance->view, boilerplate_scene_1_input);
  283. view_set_enter_callback(instance->view, boilerplate_scene_1_enter);
  284. view_set_exit_callback(instance->view, boilerplate_scene_1_exit);
  285. with_view_model(
  286. instance->view,
  287. BoilerplateScene1Model * model,
  288. { boilerplate_scene_1_model_init(model, instance); },
  289. true);
  290. return instance;
  291. }
  292. void boilerplate_scene_1_free(BoilerplateScene1* instance) {
  293. furi_assert(instance);
  294. with_view_model(instance->view, BoilerplateScene1Model * model, { UNUSED(model); }, true);
  295. view_free(instance->view);
  296. free(instance);
  297. }
  298. View* boilerplate_scene_1_get_view(BoilerplateScene1* instance) {
  299. furi_assert(instance);
  300. return instance->view;
  301. }