etch-a-sketch.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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->showWelcome = false;
  135. etch_state->board[1][1] = true;
  136. for(int y = 0; y < 32; y++) {
  137. for(int x = 0; x < 64; x++) {
  138. etch_state->board[x][y] = false;
  139. }
  140. }
  141. view_port_update(view_port);
  142. }
  143. // Keep LED on while drawing
  144. if(etch_state->isDrawing) {
  145. notification_message(notification, &sequence_draw_enabled);
  146. } else {
  147. notification_message(notification, &sequence_draw_disabled);
  148. }
  149. // Single Dot Select
  150. if(event.key == InputKeyOk && event.type == InputTypeShort) {
  151. etch_state->board[etch_state->selected.x][etch_state->selected.y] =
  152. !etch_state->board[etch_state->selected.x][etch_state->selected.y];
  153. }
  154. // Start Drawing
  155. if(event.key == InputKeyOk && event.type == InputTypeLong) {
  156. // notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_begin_draw);
  157. notification_message(notification, &sequence_begin_draw);
  158. if(etch_state->isDrawing) {
  159. // We're ending the drawing
  160. notification_message(notification, &sequence_end_draw);
  161. }
  162. etch_state->isDrawing = !etch_state->isDrawing;
  163. etch_state->board[etch_state->selected.x][etch_state->selected.y] = true;
  164. view_port_update(view_port);
  165. }
  166. //check the key pressed and change x and y accordingly
  167. if(event.type == InputTypeShort || event.type == InputTypeRepeat ||
  168. event.type == InputTypeLong) {
  169. switch(event.key) {
  170. case InputKeyUp:
  171. etch_state->selected.y -= 1;
  172. break;
  173. case InputKeyDown:
  174. etch_state->selected.y += 1;
  175. break;
  176. case InputKeyLeft:
  177. etch_state->selected.x -= 1;
  178. break;
  179. case InputKeyRight:
  180. etch_state->selected.x += 1;
  181. break;
  182. default:
  183. break;
  184. }
  185. //check if cursor position is out of bounds and reset it to the closest position
  186. if(etch_state->selected.x < 0) {
  187. etch_state->selected.x = 0;
  188. }
  189. if(etch_state->selected.x > 61) {
  190. etch_state->selected.x = 61;
  191. }
  192. if(etch_state->selected.y < 0) {
  193. etch_state->selected.y = 0;
  194. }
  195. if(etch_state->selected.y > 31) {
  196. etch_state->selected.y = 31;
  197. }
  198. if(etch_state->isDrawing == true) {
  199. etch_state->board[etch_state->selected.x][etch_state->selected.y] = true;
  200. }
  201. view_port_update(view_port);
  202. }
  203. }
  204. notification_message(notification, &sequence_cleanup);
  205. gui_remove_view_port(gui, view_port);
  206. view_port_free(view_port);
  207. furi_message_queue_free(event_queue);
  208. free(etch_state);
  209. furi_record_close(RECORD_NOTIFICATION);
  210. furi_record_close(RECORD_GUI);
  211. return 0;
  212. }