etch-a-sketch.c 4.9 KB

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