reaction.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. uint8_t state = 0;
  8. int time = 42;
  9. FuriTimer* timer;
  10. FuriTimer* timer_stop;
  11. static void beep() {
  12. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  13. notification_message(notification, &sequence_success);
  14. furi_record_close(RECORD_NOTIFICATION);
  15. }
  16. static void tick(void* context) {
  17. UNUSED(context);
  18. time++;
  19. }
  20. static void stop(void* context) {
  21. UNUSED(context);
  22. furi_timer_stop(timer_stop);
  23. furi_timer_start(timer, 1);
  24. beep();
  25. state = 2;
  26. }
  27. // Screen is 128x64 px
  28. static void app_draw_callback(Canvas* canvas, void* ctx) {
  29. UNUSED(ctx);
  30. canvas_clear(canvas);
  31. canvas_set_font(canvas, FontPrimary);
  32. if(state == 0) {
  33. canvas_draw_str(canvas, 16, 36, "Press OK to start!");
  34. } else if(state == 1) {
  35. canvas_draw_str(canvas, 44, 36, "Wait...");
  36. } else if(state == 2) {
  37. canvas_draw_str(canvas, 35, 36, "Press OK!");
  38. } else if(state == 3) {
  39. char* time_str = "??????";
  40. snprintf(time_str, 6, "%d", time);
  41. canvas_draw_str(canvas, 56, 36, time_str);
  42. canvas_set_font(canvas, FontSecondary);
  43. canvas_draw_str(canvas, 40, 24, "Time (ms):");
  44. canvas_draw_str(canvas, 18, 48, "Press OK to play again");
  45. }
  46. }
  47. static void app_input_callback(InputEvent* input_event, void* ctx) {
  48. furi_assert(ctx);
  49. FuriMessageQueue* event_queue = ctx;
  50. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  51. }
  52. int32_t reaction_main(void* p) {
  53. UNUSED(p);
  54. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  55. // Configure view port
  56. ViewPort* view_port = view_port_alloc();
  57. view_port_draw_callback_set(view_port, app_draw_callback, view_port);
  58. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  59. // Register view port in GUI
  60. Gui* gui = furi_record_open(RECORD_GUI);
  61. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  62. InputEvent event;
  63. //TImer
  64. timer = furi_timer_alloc(tick, FuriTimerTypePeriodic, NULL);
  65. timer_stop = furi_timer_alloc(stop, FuriTimerTypeOnce, NULL);
  66. bool running = true;
  67. while(running) {
  68. if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
  69. if(event.type == InputTypePress && event.key == InputKeyBack) running = false;
  70. if(state == 0 && event.type == InputTypePress && event.key == InputKeyOk) {
  71. state = 1;
  72. time = 0;
  73. view_port_update(view_port);
  74. int t = rand() % 6000 + 1000;
  75. furi_delay_ms(t);
  76. furi_timer_start(timer_stop, t);
  77. continue;
  78. }
  79. if(state == 2 && event.type == InputTypePress && event.key == InputKeyOk) {
  80. state = 3;
  81. furi_timer_stop(timer);
  82. continue;
  83. }
  84. if(state == 3 && event.type == InputTypePress && event.key == InputKeyOk) {
  85. state = 0;
  86. continue;
  87. }
  88. }
  89. view_port_update(view_port);
  90. }
  91. furi_timer_stop(timer);
  92. furi_timer_free(timer);
  93. furi_timer_stop(timer_stop);
  94. furi_timer_free(timer_stop);
  95. view_port_enabled_set(view_port, false);
  96. gui_remove_view_port(gui, view_port);
  97. view_port_free(view_port);
  98. furi_message_queue_free(event_queue);
  99. furi_record_close(RECORD_GUI);
  100. return 0;
  101. }