rubiks_cube_scrambler.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. bool scrambleStarted = false;
  10. char scramble_str[100] = {0};
  11. char scramble_start[100] = {0};
  12. char scramble_end[100] = {0};
  13. bool notifications_enabled = false;
  14. static void success_vibration()
  15. {
  16. furi_hal_vibro_on(false);
  17. furi_hal_vibro_on(true);
  18. furi_delay_ms(50);
  19. furi_hal_vibro_on(false);
  20. return;
  21. }
  22. void split_array(char original[], int size, char first[], char second[])
  23. {
  24. int32_t mid = size / 2;
  25. if (size % 2 != 0)
  26. {
  27. mid++;
  28. }
  29. int32_t first_index = 0, second_index = 0;
  30. for (int32_t i = 0; i < size; i++)
  31. {
  32. if (i < mid)
  33. {
  34. first[first_index++] = original[i];
  35. }
  36. else
  37. {
  38. if (i == mid && (original[i] == '2' || original[i] == '\''))
  39. {
  40. continue;
  41. }
  42. second[second_index++] = original[i];
  43. }
  44. }
  45. first[first_index] = '\0';
  46. second[second_index] = '\0';
  47. }
  48. void genScramble()
  49. {
  50. scrambleReplace();
  51. strcpy(scramble_str, printData());
  52. split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end);
  53. }
  54. static void draw_callback(Canvas *canvas, void *ctx)
  55. {
  56. UNUSED(ctx);
  57. canvas_clear(canvas);
  58. canvas_set_font(canvas, FontPrimary);
  59. canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler");
  60. canvas_set_font(canvas, FontSecondary);
  61. canvas_draw_str_aligned(canvas, 64, 28, AlignCenter, AlignCenter, scramble_start);
  62. canvas_draw_str_aligned(canvas, 64, 38, AlignCenter, AlignCenter, scramble_end);
  63. elements_button_center(canvas, "New");
  64. elements_button_left(canvas, notifications_enabled ? "On" : "Off");
  65. };
  66. static void input_callback(InputEvent *input_event, void *ctx)
  67. {
  68. furi_assert(ctx);
  69. FuriMessageQueue *event_queue = ctx;
  70. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  71. }
  72. int32_t rubiks_cube_scrambler_main(void *p)
  73. {
  74. UNUSED(p);
  75. InputEvent event;
  76. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  77. ViewPort *view_port = view_port_alloc();
  78. view_port_draw_callback_set(view_port, draw_callback, NULL);
  79. view_port_input_callback_set(view_port, input_callback, event_queue);
  80. Gui *gui = furi_record_open(RECORD_GUI);
  81. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  82. while (true)
  83. {
  84. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  85. if (event.key == InputKeyOk && event.type == InputTypeShort)
  86. {
  87. genScramble();
  88. if (notifications_enabled)
  89. {
  90. success_vibration();
  91. }
  92. }
  93. if (event.key == InputKeyLeft && event.type == InputTypeShort)
  94. {
  95. if (notifications_enabled)
  96. {
  97. notifications_enabled = false;
  98. }
  99. else
  100. {
  101. notifications_enabled = true;
  102. success_vibration();
  103. }
  104. }
  105. if (event.key == InputKeyBack)
  106. {
  107. break;
  108. }
  109. view_port_update(view_port);
  110. }
  111. furi_message_queue_free(event_queue);
  112. gui_remove_view_port(gui, view_port);
  113. view_port_free(view_port);
  114. furi_record_close(RECORD_GUI);
  115. return 0;
  116. }