etch_a_sketch.c 8.4 KB

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