RaZe 3 лет назад
Сommit
0af361e5cf
5 измененных файлов с 236 добавлено и 0 удалено
  1. 20 0
      application.fam
  2. BIN
      cube.png
  3. 116 0
      rubiks_cube_scrambler.c
  4. 94 0
      scrambler.c
  5. 6 0
      scrambler.h

+ 20 - 0
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",
+)


+ 116 - 0
rubiks_cube_scrambler.c

@@ -0,0 +1,116 @@
+#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"
+#include <furi_hal.h>
+
+char* currentKeyPressed;
+int BUFFER = 10;
+char scramble[100] = {0};
+int notifications_enabled = 0;
+
+static void success_vibration() {
+    furi_hal_vibro_on(false);
+    furi_hal_vibro_on(true);
+    furi_delay_ms(50);
+    furi_hal_vibro_on(false);
+    return;
+}
+
+static void draw_callback(Canvas* canvas, void* ctx) {
+    UNUSED(ctx);
+    canvas_clear(canvas);
+    canvas_set_font(canvas, FontPrimary);
+    canvas_draw_str(canvas, 0, 13, "Rubik's Cube Scrambler");
+
+        
+    if(strcmp(currentKeyPressed, "OK") == 0) {
+/*         const char* moves[] = {"U", "D", "L", "R", "F", "B"};
+        const char* directions[] = {"", "'", "2"};
+        int index = 0;
+        int prevMove = -1; // Initialize previous move to an invalid value
+        for(int i = 0; i < 10; i++) {
+            int move;
+            do {
+                move = rand() % 6;
+            } while(move == prevMove);
+            int direction = rand() % 3;
+            prevMove = move;
+            index += snprintf(
+                scramble + index,
+                sizeof(scramble) - index,
+                "%s%s ",
+                moves[move],
+                directions[direction]);
+        } */
+        genScramble ();
+        scrambleReplace();
+        valid();
+        strcpy(scramble, printData());
+        if (notifications_enabled) {
+            success_vibration();
+        }
+        currentKeyPressed = "";
+    }
+    canvas_set_font(canvas, FontSecondary);
+    canvas_draw_str(canvas, 0, 30, scramble);
+    
+    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);
+    currentKeyPressed = (char*)malloc(sizeof(char) * BUFFER);
+    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) {
+            currentKeyPressed = "OK";
+        }
+        if (event.key == InputKeyLeft && event.type == InputTypeShort) {
+            if (notifications_enabled) {
+                notifications_enabled = 0;
+            } else {
+                notifications_enabled = 1;
+                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;
+}

+ 94 - 0
scrambler.c

@@ -0,0 +1,94 @@
+#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[7]= {"RUFBLD"};
+char dir[4]={" 2'"};
+//Scramble length
+const int SLEN=10;
+
+//Structure which holds main scramble
+struct GetScramble{
+	char mainScramble [25][3];
+};struct GetScramble a;//Its object
+
+//Function prototypes to avoid bugs
+void scrambleReplace ();
+void genScramble ();
+void valid ();
+int getRand (int upr,int lwr);
+char *printData ();
+void writeToFile ();
+
+//Main function
+/* int main(){
+	genScramble ();//Calling genScramble
+	scrambleReplace();//Calling scrambleReplace
+	valid();//Calling valid to validate the scramble
+	printData ();//Printing the final scramble
+	//writeToFile();//If you want to write to a file, please uncomment this
+	
+	return 0;
+} */
+
+void genScramble (){
+	//Stage 1
+	for(int i=0; i<SLEN; i++) {
+		strcpy(a.mainScramble[i],"00");
+	}
+	//This makes array like this 00 00 00.......
+}
+
+void scrambleReplace (){
+	//Stage 2
+	//Actual process begins here
+	//Random ints are generated and respective values are fed inside the array
+	for (int i = 0; i<SLEN; i++){
+		a.mainScramble[i][0] = moves[getRand (5,0)];
+		a.mainScramble[i][1] = dir[getRand (2,0)];
+	}
+	//But scramble is still isn't correct due to repeating moves
+}
+
+void valid (){
+	//Stage 3
+	//Variables for loop
+	int loopOne, loopTwo;
+	
+	//This will actually start to make the scramble usable
+	//It will remove stuff like R R F L, etc.
+	for (loopOne=1;loopOne<SLEN;loopOne++){
+		while (a.mainScramble[loopOne][0] == a.mainScramble[loopOne-1][0]){
+			a.mainScramble[loopOne][0]=moves[getRand(5,0)];
+		}
+	}
+	
+	//This will further check it and remove stuff like R L R
+	for (loopTwo=2; loopTwo<SLEN; loopTwo++){
+		while ((a.mainScramble[loopTwo][0] == a.mainScramble[loopTwo-2][0]) || (a.mainScramble[loopTwo][0]) == a.mainScramble[loopTwo-1][0]){
+			a.mainScramble[loopTwo][0]=moves[getRand(5,0)];
+		}
+	}
+	//Scramble generation complete
+}
+
+int getRand(int upr, int lwr){
+	int randNum;
+	randNum=(rand() % (upr - lwr + 1)) + lwr;
+	return randNum;
+}
+
+char *printData () {
+    char *result = malloc(100);
+    int offset = 0;
+    for (int loop = 0; loop < SLEN; loop++) {
+        offset += snprintf(result + offset, 100 - offset, "%s ", a.mainScramble[loop]);
+    }
+    return result;
+}
+

+ 6 - 0
scrambler.h

@@ -0,0 +1,6 @@
+void scrambleReplace ();
+void genScramble ();
+void valid ();
+int getRand (int upr,int lwr);
+char *printData ();
+void writeToFile ();