rubiks_cube_scrambler.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_str[100] = {0};
  11. char scramble_start[100] = {0};
  12. char scramble_end[100] = {0};
  13. int notifications_enabled = 0;
  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. int mid = size / 2;
  24. if (size % 2 != 0) {
  25. mid++;
  26. }
  27. int first_index = 0, second_index = 0;
  28. for (int i = 0; i < size; i++) {
  29. if (i < mid) {
  30. first[first_index++] = original[i];
  31. } else {
  32. if (i == mid && (original[i] == '2' || original[i] == '\'')) {
  33. continue;
  34. }
  35. second[second_index++] = original[i];
  36. }
  37. }
  38. first[first_index] = '\0';
  39. second[second_index] = '\0';
  40. }
  41. static void draw_callback(Canvas *canvas, void *ctx)
  42. {
  43. UNUSED(ctx);
  44. canvas_clear(canvas);
  45. canvas_set_font(canvas, FontPrimary);
  46. canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler");
  47. if (scrambleStarted)
  48. {
  49. genScramble();
  50. scrambleReplace();
  51. valid();
  52. strcpy(scramble_str, printData());
  53. if (notifications_enabled)
  54. {
  55. success_vibration();
  56. }
  57. split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end);
  58. scrambleStarted = 0;
  59. }
  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. scrambleStarted = 1;
  88. }
  89. if (event.key == InputKeyLeft && event.type == InputTypeShort)
  90. {
  91. if (notifications_enabled)
  92. {
  93. notifications_enabled = 0;
  94. }
  95. else
  96. {
  97. notifications_enabled = 1;
  98. success_vibration();
  99. }
  100. }
  101. if (event.key == InputKeyBack)
  102. {
  103. break;
  104. }
  105. }
  106. furi_message_queue_free(event_queue);
  107. gui_remove_view_port(gui, view_port);
  108. view_port_free(view_port);
  109. furi_record_close(RECORD_GUI);
  110. return 0;
  111. }