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 <gui/modules/submenu.h>
  8. #include "scrambler.h"
  9. #include "furi_hal_random.h"
  10. bool scrambleStarted = false;
  11. char scramble_str[100] = {0};
  12. char scramble_start[100] = {0};
  13. char scramble_end[100] = {0};
  14. bool notifications_enabled = false;
  15. static void success_vibration()
  16. {
  17. furi_hal_vibro_on(false);
  18. furi_hal_vibro_on(true);
  19. furi_delay_ms(50);
  20. furi_hal_vibro_on(false);
  21. return;
  22. }
  23. void split_array(char original[], int size, char first[], char second[]) {
  24. int32_t mid = size / 2;
  25. if (size % 2 != 0) {
  26. mid++;
  27. }
  28. int32_t first_index = 0, second_index = 0;
  29. for (int32_t i = 0; i < size; i++) {
  30. if (i < mid) {
  31. first[first_index++] = original[i];
  32. } else {
  33. if (i == mid && (original[i] == '2' || original[i] == '\'')) {
  34. continue;
  35. }
  36. second[second_index++] = original[i];
  37. }
  38. }
  39. first[first_index] = '\0';
  40. second[second_index] = '\0';
  41. }
  42. static void draw_callback(Canvas *canvas, void *ctx)
  43. {
  44. UNUSED(ctx);
  45. canvas_clear(canvas);
  46. canvas_set_font(canvas, FontPrimary);
  47. canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler");
  48. if (scrambleStarted)
  49. {
  50. genScramble();
  51. scrambleReplace();
  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 = true;
  88. }
  89. if (event.key == InputKeyLeft && event.type == InputTypeShort)
  90. {
  91. if (notifications_enabled)
  92. {
  93. notifications_enabled = false;
  94. }
  95. else
  96. {
  97. notifications_enabled = true;
  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. }