etch-a-sketch.c 7.3 KB

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