etch_a_sketch.c 8.4 KB

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