rubiks_cube_scrambler.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. strcpy(scramble_str, printData());
  52. if (notifications_enabled)
  53. {
  54. success_vibration();
  55. }
  56. split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end);
  57. scrambleStarted = 0;
  58. }
  59. canvas_set_font(canvas, FontSecondary);
  60. canvas_draw_str_aligned(canvas, 64, 28, AlignCenter, AlignCenter, scramble_start);
  61. canvas_draw_str_aligned(canvas, 64, 38, AlignCenter, AlignCenter, scramble_end);
  62. elements_button_center(canvas, "New");
  63. elements_button_left(canvas, notifications_enabled ? "On" : "Off");
  64. };
  65. static void input_callback(InputEvent *input_event, void *ctx)
  66. {
  67. furi_assert(ctx);
  68. FuriMessageQueue *event_queue = ctx;
  69. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  70. }
  71. int32_t rubiks_cube_scrambler_main(void *p)
  72. {
  73. UNUSED(p);
  74. InputEvent event;
  75. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  76. ViewPort *view_port = view_port_alloc();
  77. view_port_draw_callback_set(view_port, draw_callback, NULL);
  78. view_port_input_callback_set(view_port, input_callback, event_queue);
  79. Gui *gui = furi_record_open(RECORD_GUI);
  80. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  81. while (true)
  82. {
  83. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  84. if (event.key == InputKeyOk && event.type == InputTypeShort)
  85. {
  86. scrambleStarted = 1;
  87. }
  88. if (event.key == InputKeyLeft && event.type == InputTypeShort)
  89. {
  90. if (notifications_enabled)
  91. {
  92. notifications_enabled = 0;
  93. }
  94. else
  95. {
  96. notifications_enabled = 1;
  97. success_vibration();
  98. }
  99. }
  100. if (event.key == InputKeyBack)
  101. {
  102. break;
  103. }
  104. }
  105. furi_message_queue_free(event_queue);
  106. gui_remove_view_port(gui, view_port);
  107. view_port_free(view_port);
  108. furi_record_close(RECORD_GUI);
  109. return 0;
  110. }