From 5adcfbe4a49dad39f0f81934ab6861e3058ccb91 Mon Sep 17 00:00:00 2001 From: RaZe Date: Fri, 27 Jan 2023 23:25:32 +0200 Subject: [PATCH] refactor: move the scramble generation --- .../rubiks_cube_scrambler.c | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/rubiks_cube_scrambler/rubiks_cube_scrambler.c b/src/rubiks_cube_scrambler/rubiks_cube_scrambler.c index 0d214c7..c9298d7 100644 --- a/src/rubiks_cube_scrambler/rubiks_cube_scrambler.c +++ b/src/rubiks_cube_scrambler/rubiks_cube_scrambler.c @@ -4,7 +4,6 @@ #include #include #include -#include #include "scrambler.h" #include "furi_hal_random.h" @@ -23,17 +22,24 @@ static void success_vibration() furi_hal_vibro_on(false); return; } -void split_array(char original[], int size, char first[], char second[]) { +void split_array(char original[], int size, char first[], char second[]) +{ int32_t mid = size / 2; - if (size % 2 != 0) { + if (size % 2 != 0) + { mid++; } int32_t first_index = 0, second_index = 0; - for (int32_t i = 0; i < size; i++) { - if (i < mid) { + for (int32_t i = 0; i < size; i++) + { + if (i < mid) + { first[first_index++] = original[i]; - } else { - if (i == mid && (original[i] == '2' || original[i] == '\'')) { + } + else + { + if (i == mid && (original[i] == '2' || original[i] == '\'')) + { continue; } second[second_index++] = original[i]; @@ -42,7 +48,12 @@ void split_array(char original[], int size, char first[], char second[]) { first[first_index] = '\0'; second[second_index] = '\0'; } - +void genScramble() +{ + scrambleReplace(); + strcpy(scramble_str, printData()); + split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end); +} static void draw_callback(Canvas *canvas, void *ctx) { @@ -50,18 +61,6 @@ static void draw_callback(Canvas *canvas, void *ctx) canvas_clear(canvas); canvas_set_font(canvas, FontPrimary); canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler"); - - if (scrambleStarted) - { - scrambleReplace(); - strcpy(scramble_str, printData()); - if (notifications_enabled) - { - success_vibration(); - } - split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end); - scrambleStarted = 0; - } canvas_set_font(canvas, FontSecondary); canvas_draw_str_aligned(canvas, 64, 28, AlignCenter, AlignCenter, scramble_start); canvas_draw_str_aligned(canvas, 64, 38, AlignCenter, AlignCenter, scramble_end); @@ -72,6 +71,7 @@ static void draw_callback(Canvas *canvas, void *ctx) static void input_callback(InputEvent *input_event, void *ctx) { + furi_assert(ctx); FuriMessageQueue *event_queue = ctx; furi_message_queue_put(event_queue, input_event, FuriWaitForever); @@ -99,7 +99,11 @@ int32_t rubiks_cube_scrambler_main(void *p) if (event.key == InputKeyOk && event.type == InputTypeShort) { - scrambleStarted = true; + genScramble(); + if (notifications_enabled) + { + success_vibration(); + } } if (event.key == InputKeyLeft && event.type == InputTypeShort) { @@ -122,7 +126,7 @@ int32_t rubiks_cube_scrambler_main(void *p) furi_message_queue_free(event_queue); gui_remove_view_port(gui, view_port); - + view_port_free(view_port); furi_record_close(RECORD_GUI); return 0;