Просмотр исходного кода

fix: use better types, code cleanup

RaZe 3 лет назад
Родитель
Сommit
a151357f87
2 измененных файлов с 21 добавлено и 23 удалено
  1. 10 9
      rubiks_cube_scrambler.c
  2. 11 14
      scrambler.c

+ 10 - 9
rubiks_cube_scrambler.c

@@ -4,15 +4,16 @@
 #include <input/input.h>
 #include <gui/elements.h>
 #include <furi_hal.h>
+#include <gui/modules/submenu.h>
 
 #include "scrambler.h"
 #include "furi_hal_random.h"
 
-int scrambleStarted = 0;
+bool scrambleStarted = false;
 char scramble_str[100] = {0};
 char scramble_start[100] = {0};
 char scramble_end[100] = {0};
-int notifications_enabled = 0;
+bool notifications_enabled = false;
 
 static void success_vibration()
 {
@@ -23,12 +24,12 @@ static void success_vibration()
     return;
 }
 void split_array(char original[], int size, char first[], char second[]) {
-    int mid = size / 2;
+    int32_t mid = size / 2;
     if (size % 2 != 0) {
         mid++;
     }
-    int first_index = 0, second_index = 0;
-    for (int i = 0; i < size; i++) {
+    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 {
@@ -99,17 +100,17 @@ int32_t rubiks_cube_scrambler_main(void *p)
 
         if (event.key == InputKeyOk && event.type == InputTypeShort)
         {
-            scrambleStarted = 1;
+            scrambleStarted = true;
         }
         if (event.key == InputKeyLeft && event.type == InputTypeShort)
         {
             if (notifications_enabled)
             {
-                notifications_enabled = 0;
+                notifications_enabled = false;
             }
             else
             {
-                notifications_enabled = 1;
+                notifications_enabled = true;
                 success_vibration();
             }
         }
@@ -122,7 +123,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;

+ 11 - 14
scrambler.c

@@ -13,7 +13,7 @@ Authors: Tanish Bhongade and RaZe
 // 6 moves along with direction
 char moves[6] = {'R', 'U', 'F', 'B', 'L', 'D'};
 char dir[4] = {' ', '\'', '2'};
-const int SLEN = 20;
+const int32_t SLEN = 20;
 #define RESULT_SIZE 100
 // Structure which holds main scramble
 struct GetScramble
@@ -25,10 +25,7 @@ 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(){
@@ -44,7 +41,7 @@ void writeToFile();
 void genScramble()
 {
 	// Stage 1
-	for (int i = 0; i < SLEN; i++)
+	for (int32_t i = 0; i < SLEN; i++)
 	{
 		strcpy(a.mainScramble[i], "00");
 	}
@@ -57,16 +54,16 @@ void scrambleReplace()
     // Actual process begins here
 
     // Initialize the mainScramble array with all the possible moves
-    for (int i = 0; i < SLEN; i++)
+    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 (int i = 6 - 1; i > 0; i--)
+    for (int32_t i = 6 - 1; i > 0; i--)
     {
-        int j = rand() % (i + 1);
+        int32_t j = rand() % (i + 1);
         char temp[3];
         strcpy(temp, a.mainScramble[i]);
         strcpy(a.mainScramble[i], a.mainScramble[j]);
@@ -74,11 +71,11 @@ void scrambleReplace()
     }
 
     // Select the first 10 elements as the scramble, using only the first three elements of the dir array
-    for (int i = 0; i < SLEN; i++)
+    for (int32_t i = 0; i < SLEN; i++)
     {
         a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
     }
-     for (int i = 1; i < SLEN; i++) {
+     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];
         }
@@ -93,8 +90,8 @@ void scrambleReplace()
 
 // Let this function be here for now till I find out what is causing the extra space bug in the scrambles
 void remove_double_spaces(char *str) {
-  int i, j;
-  int len = strlen(str);
+  int32_t i, j;
+  int32_t len = strlen(str);
   for (i = 0, j = 0; i < len; i++, j++) {
     if (str[i] == ' ' && str[i + 1] == ' ') {
       i++;
@@ -106,8 +103,8 @@ void remove_double_spaces(char *str) {
 char *printData()
 {
     static char result[RESULT_SIZE];
-    int offset = 0;
-    for (int loop = 0; loop < SLEN; loop++)
+    int32_t offset = 0;
+    for (int32_t loop = 0; loop < SLEN; loop++)
     {
         offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
     }