rubiks_cube_scrambler.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include "furi_hal_random.h"
  5. #include <input/input.h>
  6. #include <gui/elements.h>
  7. #include "scrambler.h"
  8. #include <furi_hal.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 == 1)
  27. {
  28. /* const char* moves[] = {"U", "D", "L", "R", "F", "B"};
  29. const char* directions[] = {"", "'", "2"};
  30. int index = 0;
  31. int prevMove = -1; // Initialize previous move to an invalid value
  32. for(int i = 0; i < 10; i++) {
  33. int move;
  34. do {
  35. move = rand() % 6;
  36. } while(move == prevMove);
  37. int direction = rand() % 3;
  38. prevMove = move;
  39. index += snprintf(
  40. scramble + index,
  41. sizeof(scramble) - index,
  42. "%s%s ",
  43. moves[move],
  44. directions[direction]);
  45. } */
  46. genScramble();
  47. scrambleReplace();
  48. valid();
  49. char *data = printData();
  50. strcpy(scramble, data);
  51. free(data);
  52. if (notifications_enabled)
  53. {
  54. success_vibration();
  55. }
  56. scrambleStarted = 0;
  57. }
  58. canvas_set_font(canvas, FontSecondary);
  59. canvas_draw_str(canvas, 0, 30, scramble);
  60. elements_button_center(canvas, "New");
  61. elements_button_left(canvas, notifications_enabled ? "On" : "Off");
  62. };
  63. static void input_callback(InputEvent *input_event, void *ctx)
  64. {
  65. furi_assert(ctx);
  66. FuriMessageQueue *event_queue = ctx;
  67. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  68. }
  69. int32_t rubiks_cube_scrambler_main(void *p)
  70. {
  71. UNUSED(p);
  72. InputEvent event;
  73. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  74. ViewPort *view_port = view_port_alloc();
  75. view_port_draw_callback_set(view_port, draw_callback, NULL);
  76. view_port_input_callback_set(view_port, input_callback, event_queue);
  77. Gui *gui = furi_record_open(RECORD_GUI);
  78. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  79. while (true)
  80. {
  81. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  82. if (event.key == InputKeyOk && event.type == InputTypeShort)
  83. {
  84. scrambleStarted = 1;
  85. }
  86. if (event.key == InputKeyLeft && event.type == InputTypeShort)
  87. {
  88. if (notifications_enabled)
  89. {
  90. notifications_enabled = 0;
  91. }
  92. else
  93. {
  94. notifications_enabled = 1;
  95. success_vibration();
  96. }
  97. }
  98. if (event.key == InputKeyBack)
  99. {
  100. break;
  101. }
  102. }
  103. furi_message_queue_free(event_queue);
  104. gui_remove_view_port(gui, view_port);
  105. view_port_free(view_port);
  106. furi_record_close(RECORD_GUI);
  107. return 0;
  108. }