rubiks_cube_scrambler.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #include <furi_hal.h>
  7. #include "scrambler.h"
  8. #include "furi_hal_random.h"
  9. int scrambleStarted = 0;
  10. char scramble[100] = {0};
  11. int notifications_enabled = 0;
  12. static void success_vibration()
  13. {
  14. furi_hal_vibro_on(false);
  15. furi_hal_vibro_on(true);
  16. furi_delay_ms(50);
  17. furi_hal_vibro_on(false);
  18. return;
  19. }
  20. static void draw_callback(Canvas *canvas, void *ctx)
  21. {
  22. UNUSED(ctx);
  23. canvas_clear(canvas);
  24. canvas_set_font(canvas, FontPrimary);
  25. canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler");
  26. if (scrambleStarted)
  27. {
  28. genScramble();
  29. scrambleReplace();
  30. valid();
  31. strcpy(scramble, printData());
  32. if (notifications_enabled)
  33. {
  34. success_vibration();
  35. }
  36. scrambleStarted = 0;
  37. }
  38. canvas_set_font(canvas, FontSecondary);
  39. canvas_draw_str_aligned(canvas, 64, 33, AlignCenter, AlignCenter, scramble);
  40. elements_button_center(canvas, "New");
  41. elements_button_left(canvas, notifications_enabled ? "On" : "Off");
  42. };
  43. static void input_callback(InputEvent *input_event, void *ctx)
  44. {
  45. furi_assert(ctx);
  46. FuriMessageQueue *event_queue = ctx;
  47. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  48. }
  49. int32_t rubiks_cube_scrambler_main(void *p)
  50. {
  51. UNUSED(p);
  52. InputEvent event;
  53. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  54. ViewPort *view_port = view_port_alloc();
  55. view_port_draw_callback_set(view_port, draw_callback, NULL);
  56. view_port_input_callback_set(view_port, input_callback, event_queue);
  57. Gui *gui = furi_record_open(RECORD_GUI);
  58. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  59. while (true)
  60. {
  61. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  62. if (event.key == InputKeyOk && event.type == InputTypeShort)
  63. {
  64. scrambleStarted = 1;
  65. }
  66. if (event.key == InputKeyLeft && event.type == InputTypeShort)
  67. {
  68. if (notifications_enabled)
  69. {
  70. notifications_enabled = 0;
  71. }
  72. else
  73. {
  74. notifications_enabled = 1;
  75. success_vibration();
  76. }
  77. }
  78. if (event.key == InputKeyBack)
  79. {
  80. break;
  81. }
  82. }
  83. furi_message_queue_free(event_queue);
  84. gui_remove_view_port(gui, view_port);
  85. view_port_free(view_port);
  86. furi_record_close(RECORD_GUI);
  87. return 0;
  88. }