dvdbounce.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <string.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <gui/gui.h>
  5. #include <input/input.h>
  6. #include "dvd_bounce_icons.h"
  7. //init some variables
  8. int x = 0;
  9. int y = 0;
  10. int mode = 0;
  11. bool bounce_up = false;
  12. bool bounce_right = true;
  13. char mode_str[12];
  14. //the thing to draw to the screen
  15. static void app_draw_callback(Canvas* canvas, void* ctx) {
  16. UNUSED(ctx);
  17. canvas_clear(canvas);
  18. //draws the ball to positions x and y
  19. canvas_draw_icon(canvas, x, y, &I_Ok_btn_pressed_13x13);
  20. //displays the current mode
  21. canvas_set_font(canvas, FontSecondary);
  22. canvas_draw_str(canvas, 2, 8, "Mode:");
  23. //converts mode int to string
  24. itoa(mode, mode_str, 10);
  25. canvas_draw_str(canvas, 28, 8, mode_str);
  26. switch(mode){
  27. case 1:
  28. canvas_draw_str(canvas, 2, 16, "Left/Right");
  29. break;
  30. case 2:
  31. canvas_draw_str(canvas, 2, 16, "Up/Down");
  32. break;
  33. default:
  34. canvas_draw_str(canvas, 2, 16, "Normal");
  35. break;
  36. }
  37. }
  38. static void app_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 bounce_moment(void* p){
  44. UNUSED(p);
  45. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  46. // Configure viewport
  47. ViewPort* view_port = view_port_alloc();
  48. view_port_draw_callback_set(view_port, app_draw_callback, view_port);
  49. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  50. // Register viewport in GUI
  51. Gui* gui = furi_record_open(RECORD_GUI);
  52. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  53. InputEvent event;
  54. bool running = true;
  55. while(running) {
  56. if (furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
  57. if ((event.type == InputTypePress) || (event.type == InputTypeRepeat)) {
  58. //arrows move the ball by 10 in their respective directions
  59. switch (event.key) {
  60. case InputKeyUp:
  61. y += -10;
  62. break;
  63. case InputKeyDown:
  64. y += 10;
  65. break;
  66. case InputKeyLeft:
  67. x += -10;
  68. break;
  69. case InputKeyRight:
  70. x += 10;
  71. break;
  72. //sets the ball to the middle of the screen and sets the current mode
  73. case InputKeyOk:
  74. x = 51;
  75. y = 19;
  76. if(mode == 2){
  77. mode = 0;
  78. }
  79. else{
  80. mode += 1;
  81. }
  82. break;
  83. //exits the program if back is pressed
  84. default:
  85. running = false;
  86. break;
  87. }
  88. }
  89. }
  90. //bunch of conditionals determining how the ball should move
  91. if (x <= 0) {
  92. bounce_up = false;
  93. }
  94. if (x >= 115) {
  95. bounce_up = true;
  96. }
  97. if (y <= 0) {
  98. bounce_right = true;
  99. }
  100. if (y >= 51) {
  101. bounce_right = false;
  102. }
  103. if((bounce_up) && (mode != 2)){
  104. x += -1;
  105. }
  106. if((!bounce_up) && (mode != 2)){
  107. x += 1;
  108. }
  109. if((bounce_right) && (mode != 1)){
  110. y += 1;
  111. }
  112. if((!bounce_right) && (mode != 1)){
  113. y += -1;
  114. }
  115. view_port_update(view_port);
  116. }
  117. //cleanup go brrrrr
  118. view_port_enabled_set(view_port, false);
  119. gui_remove_view_port(gui, view_port);
  120. view_port_free(view_port);
  121. furi_message_queue_free(event_queue);
  122. furi_record_close(RECORD_GUI);
  123. return 0;
  124. }