Explorar el Código

Add rubiks_cube_scrambler from https://github.com/RaZeSloth/flipperzero-rubiks-cube-scrambler

git-subtree-dir: rubiks_cube_scrambler
git-subtree-mainline: 86527f9cb59e35e401ca742a27eb1c4b112d80d2
git-subtree-split: 703719f16a48bf2d3392af8d74a2ce7702f6ec1a
Willy-JL hace 2 años
padre
commit
6dcea38ebd

+ 1 - 0
rubiks_cube_scrambler/.gitsubtree

@@ -0,0 +1 @@
+https://github.com/RaZeSloth/flipperzero-rubiks-cube-scrambler master src/rubiks_cube_scrambler

+ 20 - 0
rubiks_cube_scrambler/application.fam

@@ -0,0 +1,20 @@
+# COMPILE ISTRUCTIONS:
+
+# Clean the code and remove old binaries/compilation artefact 
+# ./fbt -c fap_rubiks_cube_scrambler
+
+# Compile FAP
+# ./fbt fap_rubiks_cube_scrambler
+
+# Run application directly inside the Flip.x0
+# ./fbt launch_app APPSRC=rubiks_cube_scrambler
+
+App(
+    appid="rubiks_cube_scrambler",
+    name="Rubik's Cube Scrambler",
+    apptype=FlipperAppType.EXTERNAL,
+    entry_point="rubiks_cube_scrambler_main",
+    stack_size=1 * 1024,
+    fap_category="Misc",
+    fap_icon="cube.png",
+)

BIN
rubiks_cube_scrambler/cube.png


+ 133 - 0
rubiks_cube_scrambler/rubiks_cube_scrambler.c

@@ -0,0 +1,133 @@
+#include <stdio.h>
+#include <furi.h>
+#include <gui/gui.h>
+#include <input/input.h>
+#include <gui/elements.h>
+#include <furi_hal.h>
+
+#include "scrambler.h"
+#include "furi_hal_random.h"
+
+bool scrambleStarted = false;
+char scramble_str[100] = {0};
+char scramble_start[100] = {0};
+char scramble_end[100] = {0};
+bool notifications_enabled = false;
+
+static void success_vibration()
+{
+    furi_hal_vibro_on(false);
+    furi_hal_vibro_on(true);
+    furi_delay_ms(50);
+    furi_hal_vibro_on(false);
+    return;
+}
+void split_array(char original[], int size, char first[], char second[])
+{
+    int32_t mid = size / 2;
+    if (size % 2 != 0)
+    {
+        mid++;
+    }
+    int32_t first_index = 0, second_index = 0;
+    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] == '\''))
+            {
+                continue;
+            }
+            second[second_index++] = original[i];
+        }
+    }
+    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)
+{
+    UNUSED(ctx);
+    canvas_clear(canvas);
+    canvas_set_font(canvas, FontPrimary);
+    canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler");
+    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);
+    elements_button_center(canvas, "New");
+
+    elements_button_left(canvas, notifications_enabled ? "On" : "Off");
+};
+
+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);
+}
+
+int32_t rubiks_cube_scrambler_main(void *p)
+{
+    UNUSED(p);
+    InputEvent event;
+
+    FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
+
+    ViewPort *view_port = view_port_alloc();
+
+    view_port_draw_callback_set(view_port, draw_callback, NULL);
+
+    view_port_input_callback_set(view_port, input_callback, event_queue);
+
+    Gui *gui = furi_record_open(RECORD_GUI);
+    gui_add_view_port(gui, view_port, GuiLayerFullscreen);
+
+    while (true)
+    {
+        furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
+
+        if (event.key == InputKeyOk && event.type == InputTypeShort)
+        {
+            genScramble();
+            if (notifications_enabled)
+            {
+                success_vibration();
+            }
+        }
+        if (event.key == InputKeyLeft && event.type == InputTypeShort)
+        {
+            if (notifications_enabled)
+            {
+                notifications_enabled = false;
+            }
+            else
+            {
+                notifications_enabled = true;
+                success_vibration();
+            }
+        }
+        if (event.key == InputKeyBack)
+        {
+            break;
+        }
+    }
+
+    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;
+}

+ 66 - 0
rubiks_cube_scrambler/scrambler.c

@@ -0,0 +1,66 @@
+/*
+Authors: Tanish Bhongade and RaZe
+*/
+
+#include <stdio.h>
+#include <furi.h>
+#include <gui/gui.h>
+#include "furi_hal_random.h"
+#include <input/input.h>
+#include <gui/elements.h>
+#include "scrambler.h"
+
+// 6 moves along with direction
+char moves[6] = {'R', 'U', 'F', 'B', 'L', 'D'};
+char dir[4] = {'\'', '2'};
+const int32_t SLEN = 20;
+#define RESULT_SIZE 100
+
+struct GetScramble
+{
+	char mainScramble[25][3];
+};
+struct GetScramble a;
+
+
+void scrambleReplace()
+{
+    // Initialize the mainScramble array with all the possible moves
+    for (int32_t i = 0; i < SLEN; i++)
+    {
+        a.mainScramble[i][0] = moves[furi_hal_random_get() % 6];
+        a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
+    }
+
+    /* // Perform the Fisher-Yates shuffle
+    for (int32_t i = 6 - 1; i > 0; i--)
+    {
+        int32_t j = rand() % (i + 1);
+        char temp[3];
+        strcpy(temp, a.mainScramble[i]);
+        strcpy(a.mainScramble[i], a.mainScramble[j]);
+        strcpy(a.mainScramble[j], temp);
+    } */
+
+    // Select the first 10 elements as the scramble, using only the first two elements of the dir array
+    for (int32_t i = 0; i < SLEN; i++)
+    {
+        a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
+    }
+     for (int32_t i = 1; i < SLEN; i++) {
+        while ( a.mainScramble[i][0] == a.mainScramble[i - 2][0] || a.mainScramble[i][0] == a.mainScramble[i - 1][0]) {
+            a.mainScramble[i][0] = moves[furi_hal_random_get()%5];
+        }
+    }
+}
+
+char *printData()
+{
+    static char result[RESULT_SIZE];
+    int32_t offset = 0;
+    for (int32_t loop = 0; loop < SLEN; loop++)
+    {
+        offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
+    }
+    return result;
+}

+ 2 - 0
rubiks_cube_scrambler/scrambler.h

@@ -0,0 +1,2 @@
+void scrambleReplace ();
+char *printData ();