rubiks_cube_scrambler.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. char *data = printData();
  48. strcpy(scramble, data);
  49. free(data);
  50. if (notifications_enabled) {
  51. success_vibration();
  52. }
  53. currentKeyPressed = "";
  54. }
  55. canvas_set_font(canvas, FontSecondary);
  56. canvas_draw_str(canvas, 0, 30, scramble);
  57. elements_button_center(canvas, "New");
  58. elements_button_left(canvas, notifications_enabled ? "On" : "Off");
  59. };
  60. static void input_callback(InputEvent* input_event, void* ctx) {
  61. furi_assert(ctx);
  62. FuriMessageQueue* event_queue = ctx;
  63. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  64. }
  65. int32_t rubiks_cube_scrambler_main(void* p) {
  66. UNUSED(p);
  67. currentKeyPressed = (char*)malloc(sizeof(char) * BUFFER);
  68. InputEvent event;
  69. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  70. ViewPort* view_port = view_port_alloc();
  71. view_port_draw_callback_set(view_port, draw_callback, NULL);
  72. view_port_input_callback_set(view_port, input_callback, event_queue);
  73. Gui* gui = furi_record_open(RECORD_GUI);
  74. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  75. while(true) {
  76. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  77. if(event.key == InputKeyOk && event.type == InputTypeShort) {
  78. currentKeyPressed = "OK";
  79. }
  80. if (event.key == InputKeyLeft && event.type == InputTypeShort) {
  81. if (notifications_enabled) {
  82. notifications_enabled = 0;
  83. } else {
  84. notifications_enabled = 1;
  85. success_vibration();
  86. }
  87. }
  88. if(event.key == InputKeyBack) {
  89. break;
  90. }
  91. }
  92. furi_message_queue_free(event_queue);
  93. gui_remove_view_port(gui, view_port);
  94. view_port_free(view_port);
  95. furi_record_close(RECORD_GUI);
  96. return 0;
  97. }