etch-a-sketch.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. void etch_draw_callback(Canvas* canvas, void* ctx) {
  19. const EtchData* etch_state = acquire_mutex((ValueMutex*)ctx, 25);
  20. UNUSED(ctx);
  21. canvas_clear(canvas);
  22. canvas_set_color(canvas, ColorBlack);
  23. //draw the canvas(32x16) on screen(144x64) using 4x4 tiles
  24. for(int y = 0; y < 32; y++) {
  25. for(int x = 0; x < 64; x++) {
  26. if(etch_state->board[x][y]) {
  27. canvas_draw_box(canvas, x * brush_size, y * brush_size, 2, 2);
  28. }
  29. }
  30. }
  31. //draw cursor as a 4x4 black box with a 2x2 white box inside
  32. canvas_set_color(canvas, ColorBlack);
  33. canvas_draw_box(
  34. canvas,
  35. etch_state->selected.x * brush_size,
  36. etch_state->selected.y * brush_size,
  37. brush_size,
  38. brush_size);
  39. // canvas_set_color(canvas, ColorWhite);
  40. // canvas_draw_box(
  41. // canvas,
  42. // etch_state->selected.x * brush_size + 1,
  43. // etch_state->selected.y * brush_size + 1,
  44. // brush_size,
  45. // brush_size);
  46. //release the mutex
  47. release_mutex((ValueMutex*)ctx, etch_state);
  48. }
  49. void etch_input_callback(InputEvent* input_event, void* ctx) {
  50. furi_assert(ctx);
  51. FuriMessageQueue* event_queue = ctx;
  52. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  53. }
  54. int32_t etch_a_sketch_app(void* p) {
  55. UNUSED(p);
  56. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  57. EtchData* etch_state = malloc(sizeof(EtchData));
  58. ValueMutex etch_state_mutex;
  59. if(!init_mutex(&etch_state_mutex, etch_state, sizeof(EtchData))) {
  60. FURI_LOG_E("etch", "cannot create mutex\r\n");
  61. free(etch_state);
  62. return -1;
  63. }
  64. // Configure view port
  65. ViewPort* view_port = view_port_alloc();
  66. view_port_draw_callback_set(view_port, etch_draw_callback, &etch_state_mutex);
  67. view_port_input_callback_set(view_port, etch_input_callback, event_queue);
  68. // Register view port in GUI
  69. Gui* gui = furi_record_open(RECORD_GUI);
  70. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  71. //NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  72. InputEvent event;
  73. while(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) {
  74. //break out of the loop if the back key is pressed
  75. if(event.type == InputTypeShort && event.key == InputKeyBack) {
  76. break;
  77. }
  78. // Single Dot Select
  79. if(event.type == InputTypeShort && event.key == InputKeyOk) {
  80. etch_state->board[etch_state->selected.x][etch_state->selected.y] =
  81. !etch_state->board[etch_state->selected.x][etch_state->selected.y];
  82. }
  83. //check the key pressed and change x and y accordingly
  84. if(event.type) {
  85. switch(event.key) {
  86. case InputKeyUp:
  87. etch_state->selected.y -= 1;
  88. break;
  89. case InputKeyDown:
  90. etch_state->selected.y += 1;
  91. break;
  92. case InputKeyLeft:
  93. etch_state->selected.x -= 1;
  94. break;
  95. case InputKeyRight:
  96. etch_state->selected.x += 1;
  97. break;
  98. default:
  99. break;
  100. }
  101. //check if cursor position is out of bounds and reset it to the closest position
  102. if(etch_state->selected.x < 0) {
  103. etch_state->selected.x = 0;
  104. }
  105. if(etch_state->selected.x > 61) {
  106. etch_state->selected.x = 61;
  107. }
  108. if(etch_state->selected.y < 0) {
  109. etch_state->selected.y = 0;
  110. }
  111. if(etch_state->selected.y > 31) {
  112. etch_state->selected.y = 31;
  113. }
  114. if(etch_state->isDrawing == true) {
  115. etch_state->board[etch_state->selected.x][etch_state->selected.y] = true;
  116. }
  117. view_port_update(view_port);
  118. }
  119. // Clear Board
  120. if(event.key == InputKeyBack && event.type == InputTypeLong) {
  121. etch_state->board[1][1] = true;
  122. for(int y = 0; y < 32; y++) {
  123. for(int x = 0; x < 64; x++) {
  124. etch_state->board[x][y] = false;
  125. }
  126. }
  127. view_port_update(view_port);
  128. }
  129. // Erase Board
  130. // TODO: Do animation of shaking board
  131. if(event.key == InputKeyOk && event.type == InputTypeLong) {
  132. etch_state->isDrawing = !etch_state->isDrawing;
  133. etch_state->board[etch_state->selected.x][etch_state->selected.y] = true;
  134. view_port_update(view_port);
  135. }
  136. }
  137. gui_remove_view_port(gui, view_port);
  138. view_port_free(view_port);
  139. furi_message_queue_free(event_queue);
  140. free(etch_state);
  141. furi_record_close(RECORD_NOTIFICATION);
  142. furi_record_close(RECORD_GUI);
  143. return 0;
  144. }