etch-a-sketch.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <assets_icons.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <gui/gui.h>
  5. #include <gui/elements.h>
  6. #include <gui/icon.h>
  7. #include <input/input.h>
  8. #include <notification/notification.h>
  9. #include <notification/notification_messages.h>
  10. #include <stdbool.h> // Header-file for boolean data-type.
  11. #include <stdio.h>
  12. #include <string.h>
  13. #define WIDTH 64
  14. #define HEIGHT 32
  15. const int brush_size = 2;
  16. typedef struct selected_position {
  17. int x;
  18. int y;
  19. } selected_position;
  20. typedef struct {
  21. selected_position selected;
  22. bool board[64][32];
  23. bool isDrawing;
  24. bool showWelcome;
  25. } EtchData;
  26. // Sequence to indicate that drawing is enabled.
  27. const NotificationSequence sequence_begin_draw = {
  28. &message_display_backlight_on,
  29. // Vibrate to indicate that drawing is enabled.
  30. &message_vibro_on,
  31. &message_note_g5,
  32. &message_delay_50,
  33. &message_note_c6,
  34. &message_delay_50,
  35. &message_note_e5,
  36. &message_vibro_off,
  37. &message_sound_off,
  38. NULL,
  39. };
  40. // sequence to indicate that drawing is disabled
  41. const NotificationSequence sequence_end_draw = {
  42. &message_red_0,
  43. // Indicate that drawing is disabled.
  44. &message_vibro_on,
  45. &message_note_g5,
  46. &message_delay_50,
  47. &message_note_e5,
  48. &message_delay_50,
  49. &message_vibro_off,
  50. &message_sound_off,
  51. &message_do_not_reset,
  52. NULL,
  53. };
  54. // Indicate that drawing is enabled.
  55. const NotificationSequence sequence_draw_enabled = {
  56. &message_red_255,
  57. &message_do_not_reset,
  58. NULL,
  59. };
  60. // Indicate that drawing is disabled.
  61. const NotificationSequence sequence_draw_disabled = {
  62. &message_red_0,
  63. &message_do_not_reset,
  64. NULL,
  65. };
  66. const NotificationSequence sequence_cleanup = {
  67. &message_red_0,
  68. &message_green_0,
  69. &message_blue_0,
  70. &message_sound_off,
  71. &message_vibro_off,
  72. NULL,
  73. };
  74. void etch_draw_callback(Canvas* canvas, void* ctx) {
  75. const EtchData* etch_state = acquire_mutex((ValueMutex*)ctx, 25);
  76. UNUSED(ctx);
  77. canvas_clear(canvas);
  78. // Show Welcome Message
  79. if(etch_state->showWelcome) {
  80. // Draw Etch A Sketch frame
  81. canvas_draw_frame(canvas, 5, 3, 119, 55); // Border
  82. canvas_draw_icon(canvas, 8, 50, &I_Ok_btn_pressed_13x13); // Left Knob
  83. canvas_draw_icon(canvas, 107, 50, &I_Ok_btn_pressed_13x13); // Right Knob
  84. // Draw Etch A Sketch text banner
  85. canvas_set_font(canvas, FontSecondary);
  86. canvas_draw_str(canvas, 36, 15, "Etch A Sketch");
  87. // Draw Etch A Sketch instructions "Hold Back to clear"
  88. canvas_set_font(canvas, FontSecondary);
  89. canvas_draw_str(canvas, 31, 26, "* Hold ");
  90. canvas_draw_icon(canvas, 59, 18, &I_Pin_back_arrow_10x8);
  91. canvas_set_font(canvas, FontSecondary);
  92. canvas_draw_str(canvas, 72, 26, "to clear");
  93. // Draw Etch A Sketch instructions "Hold OK button to draw"
  94. canvas_set_font(canvas, FontSecondary);
  95. canvas_draw_str(canvas, 31, 37, "* Hold");
  96. canvas_draw_icon(canvas, 61, 30, &I_ButtonCenter_7x7);
  97. canvas_set_font(canvas, FontSecondary);
  98. canvas_draw_str(canvas, 72, 37, "to draw");
  99. }
  100. canvas_set_color(canvas, ColorBlack);
  101. //draw the canvas(64x32) on screen(144x64) using brush_size*brush_size tiles
  102. for(int y = 0; y < 32; y++) {
  103. for(int x = 0; x < 64; x++) {
  104. if(etch_state->board[x][y]) {
  105. canvas_draw_box(canvas, x * brush_size, y * brush_size, 2, 2);
  106. }
  107. }
  108. }
  109. //draw cursor as a brush_size by brush_size black box
  110. canvas_set_color(canvas, ColorBlack);
  111. canvas_draw_box(
  112. canvas,
  113. etch_state->selected.x * brush_size,
  114. etch_state->selected.y * brush_size,
  115. brush_size,
  116. brush_size);
  117. //release the mutex
  118. release_mutex((ValueMutex*)ctx, etch_state);
  119. }
  120. void etch_input_callback(InputEvent* input_event, void* ctx) {
  121. furi_assert(ctx);
  122. FuriMessageQueue* event_queue = ctx;
  123. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  124. }
  125. int32_t etch_a_sketch_app(void* p) {
  126. UNUSED(p);
  127. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  128. EtchData* etch_state = malloc(sizeof(EtchData));
  129. ValueMutex etch_state_mutex;
  130. if(!init_mutex(&etch_state_mutex, etch_state, sizeof(EtchData))) {
  131. FURI_LOG_E("etch", "cannot create mutex\r\n");
  132. free(etch_state);
  133. return -1;
  134. }
  135. // Configure view port
  136. ViewPort* view_port = view_port_alloc();
  137. view_port_draw_callback_set(view_port, etch_draw_callback, &etch_state_mutex);
  138. view_port_input_callback_set(view_port, etch_input_callback, event_queue);
  139. // Register view port in GUI
  140. Gui* gui = furi_record_open(RECORD_GUI);
  141. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  142. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  143. InputEvent event;
  144. // Show Welcome Banner
  145. etch_state->showWelcome = true;
  146. while(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) {
  147. //break out of the loop if the back key is pressed
  148. if(event.key == InputKeyBack && event.type == InputTypeShort) {
  149. break;
  150. }
  151. // Clear
  152. // TODO: Do animation of shaking board
  153. if(event.key == InputKeyBack && event.type == InputTypeLong) {
  154. etch_state->showWelcome = false;
  155. etch_state->board[1][1] = true;
  156. for(int y = 0; y < 32; y++) {
  157. for(int x = 0; x < 64; x++) {
  158. etch_state->board[x][y] = false;
  159. }
  160. }
  161. view_port_update(view_port);
  162. }
  163. // Keep LED on while drawing
  164. if(etch_state->isDrawing) {
  165. notification_message(notification, &sequence_draw_enabled);
  166. } else {
  167. notification_message(notification, &sequence_draw_disabled);
  168. }
  169. // Single Dot Select
  170. if(event.key == InputKeyOk && event.type == InputTypeShort) {
  171. etch_state->board[etch_state->selected.x][etch_state->selected.y] =
  172. !etch_state->board[etch_state->selected.x][etch_state->selected.y];
  173. }
  174. // Start Drawing
  175. if(event.key == InputKeyOk && event.type == InputTypeLong) {
  176. // notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_begin_draw);
  177. notification_message(notification, &sequence_begin_draw);
  178. if(etch_state->isDrawing) {
  179. // We're ending the drawing
  180. notification_message(notification, &sequence_end_draw);
  181. }
  182. etch_state->isDrawing = !etch_state->isDrawing;
  183. etch_state->board[etch_state->selected.x][etch_state->selected.y] = true;
  184. view_port_update(view_port);
  185. }
  186. //check the key pressed and change x and y accordingly
  187. if(event.type == InputTypeShort || event.type == InputTypeRepeat ||
  188. event.type == InputTypeLong) {
  189. switch(event.key) {
  190. case InputKeyUp:
  191. etch_state->selected.y -= 1;
  192. break;
  193. case InputKeyDown:
  194. etch_state->selected.y += 1;
  195. break;
  196. case InputKeyLeft:
  197. etch_state->selected.x -= 1;
  198. break;
  199. case InputKeyRight:
  200. etch_state->selected.x += 1;
  201. break;
  202. default:
  203. break;
  204. }
  205. //check if cursor position is out of bounds and reset it to the closest position
  206. if(etch_state->selected.x < 0) {
  207. etch_state->selected.x = 0;
  208. }
  209. if(etch_state->selected.x > 61) {
  210. etch_state->selected.x = 61;
  211. }
  212. if(etch_state->selected.y < 0) {
  213. etch_state->selected.y = 0;
  214. }
  215. if(etch_state->selected.y > 31) {
  216. etch_state->selected.y = 31;
  217. }
  218. if(etch_state->isDrawing == true) {
  219. etch_state->board[etch_state->selected.x][etch_state->selected.y] = true;
  220. }
  221. view_port_update(view_port);
  222. }
  223. }
  224. notification_message(notification, &sequence_cleanup);
  225. gui_remove_view_port(gui, view_port);
  226. view_port_free(view_port);
  227. furi_message_queue_free(event_queue);
  228. free(etch_state);
  229. furi_record_close(RECORD_NOTIFICATION);
  230. furi_record_close(RECORD_GUI);
  231. return 0;
  232. }