rubiks_cube_scrambler.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include "furi_hal_random.h"
  5. #include <input/input.h>
  6. #include <gui/elements.h>
  7. #include "scrambler.h"
  8. #include <furi_hal.h>
  9. char* currentKeyPressed;
  10. int BUFFER = 10;
  11. char scramble[100] = {0};
  12. int notifications_enabled = 0;
  13. static void success_vibration() {
  14. furi_hal_vibro_on(false);
  15. furi_hal_vibro_on(true);
  16. furi_delay_ms(50);
  17. furi_hal_vibro_on(false);
  18. return;
  19. }
  20. static void draw_callback(Canvas* canvas, void* ctx) {
  21. UNUSED(ctx);
  22. canvas_clear(canvas);
  23. canvas_set_font(canvas, FontPrimary);
  24. canvas_draw_str(canvas, 0, 13, "Rubik's Cube Scrambler");
  25. if(strcmp(currentKeyPressed, "OK") == 0) {
  26. /* const char* moves[] = {"U", "D", "L", "R", "F", "B"};
  27. const char* directions[] = {"", "'", "2"};
  28. int index = 0;
  29. int prevMove = -1; // Initialize previous move to an invalid value
  30. for(int i = 0; i < 10; i++) {
  31. int move;
  32. do {
  33. move = rand() % 6;
  34. } while(move == prevMove);
  35. int direction = rand() % 3;
  36. prevMove = move;
  37. index += snprintf(
  38. scramble + index,
  39. sizeof(scramble) - index,
  40. "%s%s ",
  41. moves[move],
  42. directions[direction]);
  43. } */
  44. genScramble ();
  45. scrambleReplace();
  46. valid();
  47. strcpy(scramble, printData());
  48. if (notifications_enabled) {
  49. success_vibration();
  50. }
  51. currentKeyPressed = "";
  52. }
  53. canvas_set_font(canvas, FontSecondary);
  54. canvas_draw_str(canvas, 0, 30, scramble);
  55. elements_button_center(canvas, "New");
  56. elements_button_left(canvas, notifications_enabled ? "On" : "Off");
  57. };
  58. static void input_callback(InputEvent* input_event, void* ctx) {
  59. furi_assert(ctx);
  60. FuriMessageQueue* event_queue = ctx;
  61. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  62. }
  63. int32_t rubiks_cube_scrambler_main(void* p) {
  64. UNUSED(p);
  65. currentKeyPressed = (char*)malloc(sizeof(char) * BUFFER);
  66. InputEvent event;
  67. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  68. ViewPort* view_port = view_port_alloc();
  69. view_port_draw_callback_set(view_port, draw_callback, NULL);
  70. view_port_input_callback_set(view_port, input_callback, event_queue);
  71. Gui* gui = furi_record_open(RECORD_GUI);
  72. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  73. while(true) {
  74. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  75. if(event.key == InputKeyOk && event.type == InputTypeShort) {
  76. currentKeyPressed = "OK";
  77. }
  78. if (event.key == InputKeyLeft && event.type == InputTypeShort) {
  79. if (notifications_enabled) {
  80. notifications_enabled = 0;
  81. } else {
  82. notifications_enabled = 1;
  83. success_vibration();
  84. }
  85. }
  86. if(event.key == InputKeyBack) {
  87. break;
  88. }
  89. }
  90. furi_message_queue_free(event_queue);
  91. gui_remove_view_port(gui, view_port);
  92. view_port_free(view_port);
  93. furi_record_close(RECORD_GUI);
  94. return 0;
  95. }