rubiks_cube_scrambler.c 3.4 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 <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. 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 = true;
  87. }
  88. if (event.key == InputKeyLeft && event.type == InputTypeShort)
  89. {
  90. if (notifications_enabled)
  91. {
  92. notifications_enabled = false;
  93. }
  94. else
  95. {
  96. notifications_enabled = true;
  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. }