etch-a-sketch.c 6.7 KB

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