boilerplate_scene_1.c 12 KB

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