etch-a-sketch.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. const NotificationSequence sequence_cleanup = {
  59. &message_red_0,
  60. &message_green_0,
  61. &message_blue_0,
  62. &message_sound_off,
  63. &message_vibro_off,
  64. NULL,
  65. };
  66. void etch_draw_callback(Canvas* canvas, void* ctx) {
  67. const EtchData* etch_state = acquire_mutex((ValueMutex*)ctx, 25);
  68. UNUSED(ctx);
  69. canvas_clear(canvas);
  70. canvas_set_color(canvas, ColorBlack);
  71. //draw the canvas(64x32) on screen(144x64) using brush_size*brush_size tiles
  72. for(int y = 0; y < 32; y++) {
  73. for(int x = 0; x < 64; x++) {
  74. if(etch_state->board[x][y]) {
  75. canvas_draw_box(canvas, x * brush_size, y * brush_size, 2, 2);
  76. }
  77. }
  78. }
  79. //draw cursor as a brush_size by brush_size black box
  80. canvas_set_color(canvas, ColorBlack);
  81. canvas_draw_box(
  82. canvas,
  83. etch_state->selected.x * brush_size,
  84. etch_state->selected.y * brush_size,
  85. brush_size,
  86. brush_size);
  87. //release the mutex
  88. release_mutex((ValueMutex*)ctx, etch_state);
  89. }
  90. void etch_input_callback(InputEvent* input_event, void* ctx) {
  91. furi_assert(ctx);
  92. FuriMessageQueue* event_queue = ctx;
  93. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  94. }
  95. int32_t etch_a_sketch_app(void* p) {
  96. UNUSED(p);
  97. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  98. EtchData* etch_state = malloc(sizeof(EtchData));
  99. ValueMutex etch_state_mutex;
  100. if(!init_mutex(&etch_state_mutex, etch_state, sizeof(EtchData))) {
  101. FURI_LOG_E("etch", "cannot create mutex\r\n");
  102. free(etch_state);
  103. return -1;
  104. }
  105. // Configure view port
  106. ViewPort* view_port = view_port_alloc();
  107. view_port_draw_callback_set(view_port, etch_draw_callback, &etch_state_mutex);
  108. view_port_input_callback_set(view_port, etch_input_callback, event_queue);
  109. // Register view port in GUI
  110. Gui* gui = furi_record_open(RECORD_GUI);
  111. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  112. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  113. InputEvent event;
  114. while(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) {
  115. //break out of the loop if the back key is pressed
  116. if(event.key == InputKeyBack && event.type == InputTypeShort) {
  117. break;
  118. }
  119. // Clear
  120. // TODO: Do animation of shaking board
  121. if(event.key == InputKeyBack && event.type == InputTypeLong) {
  122. etch_state->board[1][1] = true;
  123. for(int y = 0; y < 32; y++) {
  124. for(int x = 0; x < 64; x++) {
  125. etch_state->board[x][y] = false;
  126. }
  127. }
  128. view_port_update(view_port);
  129. }
  130. // Keep LED on while drawing
  131. if(etch_state->isDrawing) {
  132. notification_message(notification, &sequence_draw_enabled);
  133. } else {
  134. notification_message(notification, &sequence_draw_disabled);
  135. }
  136. // Single Dot Select
  137. if(event.key == InputKeyOk && event.type == InputTypeShort) {
  138. etch_state->board[etch_state->selected.x][etch_state->selected.y] =
  139. !etch_state->board[etch_state->selected.x][etch_state->selected.y];
  140. }
  141. // Start Drawing
  142. if(event.key == InputKeyOk && event.type == InputTypeLong) {
  143. // notification_message(furi_record_open(RECORD_NOTIFICATION), &sequence_begin_draw);
  144. notification_message(notification, &sequence_begin_draw);
  145. if(etch_state->isDrawing) {
  146. // We're ending the drawing
  147. notification_message(notification, &sequence_end_draw);
  148. }
  149. etch_state->isDrawing = !etch_state->isDrawing;
  150. etch_state->board[etch_state->selected.x][etch_state->selected.y] = true;
  151. view_port_update(view_port);
  152. }
  153. //check the key pressed and change x and y accordingly
  154. if(event.type == InputTypeShort || event.type == InputTypeRepeat ||
  155. event.type == InputTypeLong) {
  156. switch(event.key) {
  157. case InputKeyUp:
  158. etch_state->selected.y -= 1;
  159. break;
  160. case InputKeyDown:
  161. etch_state->selected.y += 1;
  162. break;
  163. case InputKeyLeft:
  164. etch_state->selected.x -= 1;
  165. break;
  166. case InputKeyRight:
  167. etch_state->selected.x += 1;
  168. break;
  169. default:
  170. break;
  171. }
  172. //check if cursor position is out of bounds and reset it to the closest position
  173. if(etch_state->selected.x < 0) {
  174. etch_state->selected.x = 0;
  175. }
  176. if(etch_state->selected.x > 61) {
  177. etch_state->selected.x = 61;
  178. }
  179. if(etch_state->selected.y < 0) {
  180. etch_state->selected.y = 0;
  181. }
  182. if(etch_state->selected.y > 31) {
  183. etch_state->selected.y = 31;
  184. }
  185. if(etch_state->isDrawing == true) {
  186. etch_state->board[etch_state->selected.x][etch_state->selected.y] = true;
  187. }
  188. view_port_update(view_port);
  189. }
  190. }
  191. notification_message(notification, &sequence_cleanup);
  192. gui_remove_view_port(gui, view_port);
  193. view_port_free(view_port);
  194. furi_message_queue_free(event_queue);
  195. free(etch_state);
  196. furi_record_close(RECORD_NOTIFICATION);
  197. furi_record_close(RECORD_GUI);
  198. return 0;
  199. }