boilerplate_scene_1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 + 1) * 0x1000;
  73. if(app->camera_ram_sav) {
  74. storage_file_seek(app->camera_ram_sav, count - 0x1b1, true);
  75. uint8_t status;
  76. storage_file_read(app->camera_ram_sav, &status, 1);
  77. storage_file_seek(app->camera_ram_sav, count, true);
  78. for(int y = app->pos_y; y < 14; y++) {
  79. for(int x = app->pos_x; x < 16; x++) {
  80. storage_file_read(app->camera_ram_sav, app->tile_data, sizeof(app->tile_data));
  81. for(int row = 0; row < 8; row++) {
  82. uint8_t temp1 = app->tile_data[row * 2];
  83. uint8_t temp2 = app->tile_data[row * 2 + 1];
  84. for(int pixel = 7; pixel >= 0; pixel--) {
  85. if(((temp1 & 1) + ((temp2 & 1) * 2)) >= 2) {
  86. canvas_draw_dot(canvas, (x * 8) + pixel, (y * 8) + row);
  87. }
  88. temp1 >>= 1;
  89. temp2 >>= 1;
  90. }
  91. }
  92. }
  93. }
  94. if(app->info) {
  95. if(status == 0xFF) { // DELETED
  96. canvas_draw_rbox(canvas, 100, 4, 20, 11, 4);
  97. canvas_invert_color(canvas);
  98. canvas_draw_str_aligned(canvas, 110, 10, AlignCenter, AlignCenter, "D");
  99. canvas_invert_color(canvas);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. static void boilerplate_scene_1_model_init(BoilerplateScene1Model* const model, void* context) {
  106. BoilerplateScene1* instance = context;
  107. UNUSED(model);
  108. UNUSED(instance);
  109. model->instance = context;
  110. }
  111. void save_image(void* context) {
  112. Boilerplate* app = context;
  113. furi_assert(app);
  114. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  115. // Create MALVEKE dir
  116. if(storage_common_stat(app->storage, MALVEKE_APP_FOLDER, NULL) == FSE_NOT_EXIST) {
  117. storage_simply_mkdir(app->storage, MALVEKE_APP_FOLDER);
  118. }
  119. // Create MALVEKE Photos dir
  120. if(storage_common_stat(app->storage, MALVEKE_APP_FOLDER_PHOTOS, NULL) == FSE_NOT_EXIST) {
  121. storage_simply_mkdir(app->storage, MALVEKE_APP_FOLDER_PHOTOS);
  122. }
  123. int count = (app->page + 1) * 0x1000;
  124. storage_file_seek(app->camera_ram_sav, count, true);
  125. // create file name
  126. FuriString* file_name = furi_string_alloc();
  127. get_timefilename(file_name, app->page);
  128. File* file = storage_file_alloc(app->storage);
  129. bool result =
  130. storage_file_open(file, furi_string_get_cstr(file_name), FSAM_WRITE, FSOM_OPEN_ALWAYS);
  131. if(result) {
  132. static char bmp[BMP_SIZE(WIDTH, HEIGHT)];
  133. bmp_init(bmp, WIDTH, HEIGHT);
  134. // Selected Palette
  135. uint32_t palette[] = {
  136. bmp_encode(app->palette_color_hex_a),
  137. bmp_encode(app->palette_color_hex_b),
  138. bmp_encode(app->palette_color_hex_c),
  139. bmp_encode(app->palette_color_hex_d)};
  140. UNUSED(palette);
  141. for(int y = 0; y < 14; y++) {
  142. for(int x = 0; x < 16; x++) {
  143. storage_file_read(app->camera_ram_sav, app->tile_data, sizeof(app->tile_data));
  144. for(int row = 0; row < 8; row++) {
  145. uint8_t temp1 = app->tile_data[row * 2];
  146. uint8_t temp2 = app->tile_data[row * 2 + 1];
  147. for(int pixel = 7; pixel >= 0; pixel--) {
  148. bmp_set(
  149. bmp,
  150. (x * 8) + pixel,
  151. (y * 8) + row,
  152. palette[((temp1 & 1) + ((temp2 & 1) * 2))]);
  153. temp1 >>= 1;
  154. temp2 >>= 1;
  155. }
  156. }
  157. }
  158. }
  159. storage_file_write(file, bmp, sizeof(bmp));
  160. storage_file_close(file);
  161. }
  162. // Closing the "file descriptor"
  163. storage_file_close(file);
  164. // Freeing up memory
  165. storage_file_free(file);
  166. notification_message(notifications, result ? &sequence_success : &sequence_error);
  167. }
  168. bool boilerplate_scene_1_input(InputEvent* event, void* context) {
  169. furi_assert(context);
  170. BoilerplateScene1* instance = context;
  171. Boilerplate* app = instance->context;
  172. if(event->type == InputTypeRelease) {
  173. switch(event->key) {
  174. case InputKeyBack:
  175. with_view_model(
  176. instance->view,
  177. BoilerplateScene1Model * model,
  178. {
  179. UNUSED(model);
  180. instance->callback(BoilerplateCustomEventScene1Back, instance->context);
  181. },
  182. true);
  183. break;
  184. case InputKeyLeft:
  185. with_view_model(
  186. instance->view,
  187. BoilerplateScene1Model * model,
  188. {
  189. UNUSED(model);
  190. app->pos_y = 0;
  191. app->pos_x = 0;
  192. if(app->page > 0) {
  193. app->page--;
  194. } else {
  195. app->page = 29;
  196. }
  197. },
  198. true);
  199. break;
  200. case InputKeyRight:
  201. with_view_model(
  202. instance->view,
  203. BoilerplateScene1Model * model,
  204. {
  205. UNUSED(model);
  206. app->pos_y = 0;
  207. app->pos_x = 0;
  208. if(app->page < 29) {
  209. app->page++;
  210. } else {
  211. app->page = 0;
  212. }
  213. },
  214. true);
  215. break;
  216. case InputKeyUp:
  217. with_view_model(
  218. instance->view,
  219. BoilerplateScene1Model * model,
  220. {
  221. UNUSED(model);
  222. if(app->pos_y < 0) {
  223. app->pos_y++;
  224. }
  225. },
  226. true);
  227. break;
  228. case InputKeyDown:
  229. with_view_model(
  230. instance->view,
  231. BoilerplateScene1Model * model,
  232. {
  233. UNUSED(model);
  234. if(app->pos_y > -6) {
  235. app->pos_y--;
  236. }
  237. },
  238. true);
  239. break;
  240. case InputKeyOk:
  241. with_view_model(
  242. instance->view,
  243. BoilerplateScene1Model * model,
  244. {
  245. UNUSED(model);
  246. if(app->show_instructions) {
  247. app->show_instructions = false;
  248. } else {
  249. save_image(app);
  250. }
  251. },
  252. true);
  253. break;
  254. case InputKeyMAX:
  255. break;
  256. }
  257. }
  258. return true;
  259. }
  260. void boilerplate_scene_1_exit(void* context) {
  261. furi_assert(context);
  262. }
  263. void boilerplate_scene_1_enter(void* context) {
  264. furi_assert(context);
  265. BoilerplateScene1* instance = (BoilerplateScene1*)context;
  266. with_view_model(
  267. instance->view,
  268. BoilerplateScene1Model * model,
  269. { boilerplate_scene_1_model_init(model, instance); },
  270. true);
  271. }
  272. BoilerplateScene1* boilerplate_scene_1_alloc() {
  273. BoilerplateScene1* instance = malloc(sizeof(BoilerplateScene1));
  274. instance->view = view_alloc();
  275. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(BoilerplateScene1Model));
  276. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  277. view_set_draw_callback(instance->view, (ViewDrawCallback)boilerplate_scene_1_draw);
  278. view_set_input_callback(instance->view, boilerplate_scene_1_input);
  279. view_set_enter_callback(instance->view, boilerplate_scene_1_enter);
  280. view_set_exit_callback(instance->view, boilerplate_scene_1_exit);
  281. with_view_model(
  282. instance->view,
  283. BoilerplateScene1Model * model,
  284. { boilerplate_scene_1_model_init(model, instance); },
  285. true);
  286. return instance;
  287. }
  288. void boilerplate_scene_1_free(BoilerplateScene1* instance) {
  289. furi_assert(instance);
  290. with_view_model(
  291. instance->view, BoilerplateScene1Model * model, { UNUSED(model); }, true);
  292. view_free(instance->view);
  293. free(instance);
  294. }
  295. View* boilerplate_scene_1_get_view(BoilerplateScene1* instance) {
  296. furi_assert(instance);
  297. return instance->view;
  298. }