Get a random seed to randomize auto file order [C++, MarlinFW] March 04, 2023 06:01AM |
Registered: 2 years ago Posts: 31 |
#if DISABLED(NO_SD_AUTOSTART) /** Run all the auto#.g files. Called: On boot after successful card init. From the LCD command to Run Auto Files / void CardReader::autofile_begin() { autofile_index = 1; (void)autofile_check(); } /* Run the next auto#.g file. Called: On boot after successful card init After finishing the previous auto#.g file From the LCD command to begin the auto#.g files Return 'true' if an auto file was started */ bool CardReader::autofile_check() { if (!autofile_index) return false; if (!isMounted()) mount(); else if (ENABLED(SDCARD_EEPROM_EMULATION)) settings.first_load(); // Don't run auto#.g when a PLR file exists if (isMounted() && TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) { char autoname[10]; sprintf_P(autoname, PSTR("/auto%c.g"), '0' + autofile_index - 1); if (fileExists(autoname)) { cdroot(); openAndPrintFile(autoname); // Generate a random number between 2 and 10 int randomNumber = random(2,10); autofile_index = randomNumber; // return true; } } autofile_cancel(); return false; } #endif`
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 04, 2023 04:27PM |
Registered: 7 years ago Posts: 341 |
millis_t seed_32 = millis(); uint16_t seed_16 = seed_32 & 0xFFFF;
#include /* srand, rand */ /* initialize random seed: */ millis_t seed_32 = millis(); uint16_t seed_16 = seed_32 & 0xFFFF; srand (seed_16); #define M 2 // lowest number of desired range #define N 10 // highest number of desired range int randomNumber = M + rand() / (RAND_MAX / (N - M + 1) + 1);
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 05, 2023 11:06AM |
Registered: 2 years ago Posts: 31 |
< >
#if DISABLED(NO_SD_AUTOSTART) /** * Run all the auto#.g files. Called: * - On boot after successful card init. * - From the LCD command to Run Auto Files */ void CardReader::autofile_begin() { autofile_index = 1; (void)autofile_check(); } /** * Run the next auto#.g file. Called: * - On boot after successful card init * - After finishing the previous auto#.g file * - From the LCD command to begin the auto#.g files * * Return 'true' if an auto file was started */ bool CardReader::autofile_check() { if (!autofile_index) return false; if (!isMounted()) mount(); else if (ENABLED(SDCARD_EEPROM_EMULATION)) settings.first_load(); // Don't run auto#.g when a PLR file exists if (isMounted() && TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) { char autoname[10]; sprintf_P(autoname, PSTR("/auto%c.g"), '0' + autofile_index - 1); if (fileExists(autoname)) { cdroot(); openAndPrintFile(autoname); // Generate a random number between 2 and 10 /* initialize random seed: */ millis_t seed_32 = millis(); uint16_t seed_16 = seed_32 & 0xFFFF; srand (seed_16); #define M 2 // lowest number of desired range #define N 10 // highest number of desired range int randomNumber = M + rand() / (RAND_MAX / (N - M + 1) + 1); autofile_index = randomNumber; return true; } } autofile_cancel(); return false; } #endif
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 05, 2023 12:58PM |
Registered: 7 years ago Posts: 341 |
#define seed_addr ????? uint16_t seed_16 = EEPROM_READ(seed_addr); srand (seed_16); seed_next = rand(); EEPROM_WRITE(seed_addr, seed_next); . . int randomNumber = M + seed_next / (RAND_MAX / (N - M + 1) + 1); // notice that rand() has been replaced by seed_next
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 06, 2023 02:14AM |
Registered: 2 years ago Posts: 31 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 07, 2023 03:34AM |
Registered: 7 years ago Posts: 341 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 07, 2023 08:15AM |
Registered: 2 years ago Posts: 31 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 07, 2023 02:47PM |
Registered: 7 years ago Posts: 341 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 09, 2023 02:21AM |
Registered: 2 years ago Posts: 31 |
int randomNumber = 2;
autofile_index = randomNumber;
In file included from Marlin\src\sd\random_auto_g_file_loader.cpp:51: Marlin\src\sd\../feature/powerloss.h: In static member function 'static bool PrintJobRecovery::exists()': Marlin\src\sd\../feature/powerloss.h:178:40: error: 'class CardReader' has no member named 'jobRecoverFileExists' 178 | static bool exists() { return card.jobRecoverFileExists(); } | ^~~~~~~~~~~~~~~~~~~~ Marlin\src\sd\../feature/powerloss.h: In static member function 'static void PrintJobRecoverypen(bool)': Marlin\src\sd\../feature/powerloss.h:179:46: error: 'class CardReader' has no member named 'openJobRecoveryFile' 179 | static void open(const bool read) { card.openJobRecoveryFile(read); } | ^~~~~~~~~~~~~~~~~~~ In file included from C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/WString.h:29, from C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/Print.h:27, from C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/Stream.h:26, from C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/HardwareSerial.h:29, from C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/WSerial.h:5, from C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/wiring.h:47, from C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/Arduino.h:36, from Marlin\src\sd\../module/../inc/../HAL/./STM32/../shared/Marduino.h:36, from Marlin\src\sd\../module/../inc/../HAL/./STM32/HAL.h:28, from Marlin\src\sd\../module/../inc/../HAL/HAL.h:30, from Marlin\src\sd\../module/../inc/MarlinConfig.h:31, from Marlin\src\sd\../module/motion.h:31, from Marlin\src\sd\random_auto_g_file_loader.cpp:48: Marlin\src\sd\random_auto_g_file_loader.cpp: In function 'bool random_auto_g_file_loader()': Marlin\src\sd\random_auto_g_file_loader.cpp:127:51: error: 'utostr3' was not declared in this scope; did you mean 'ftostr3'? 127 | sprintf_P(autoname, PSTR("AUTO%s.g"), utostr3(randomNumber)); | ^~~~~~~ C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/avr/pgmspace.h:71:40: note: in definition of macro 'sprintf_P' 71 | #define sprintf_P(s, ...) sprintf((s), __VA_ARGS__) | ^~~~~~~~~~~ Marlin\src\sd\random_auto_g_file_loader.cpp: In function 'uint8_t get_max_autofile()': Marlin\src\sd\random_auto_g_file_loader.cpp:160:45: error: 'utostr3' was not declared in this scope; did you mean 'ftostr3'? 160 | sprintf_P(autoname, PSTR("AUTO%s.g"), utostr3(counter)); | ^~~~~~~ C:\Users\danin\.platformio\packages\framework-arduinoststm32\cores\arduino/avr/pgmspace.h:71:40: note: in definition of macro 'sprintf_P' | ^~~~~~~~~~~ *** [.pio\build\STM32F103RC_btt\src\src\sd\random_auto_g_file_loader.cpp.o] Error 1 Marlin\src\sd\cardreader.cpp: In static member function 'static bool CardReader::autofile_check()': Marlin\src\sd\cardreader.cpp:853:26: error: 'randomNumber' was not declared in this scope 853 | autofile_index = randomNumber; | ^~~~~~~~~~~~ *** [.pio\build\STM32F103RC_btt\src\src\sd\cardreader.cpp.o] Error 1
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 09, 2023 12:50PM |
Registered: 7 years ago Posts: 341 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 10, 2023 02:34AM |
Registered: 2 years ago Posts: 31 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 10, 2023 12:16PM |
Registered: 7 years ago Posts: 341 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 16, 2023 02:47PM |
Registered: 2 years ago Posts: 31 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 16, 2023 04:08PM |
Registered: 7 years ago Posts: 341 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 17, 2023 03:11AM |
Registered: 2 years ago Posts: 31 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 17, 2023 01:53PM |
Registered: 7 years ago Posts: 341 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 19, 2023 10:19AM |
Registered: 2 years ago Posts: 31 |
echo:Now fresh file: seed.txt Writing to file: seed.txt echo:Now fresh file: AUTO3.g File opened: AUTO3.g Size: 1343625 File selected Exception in thread read thread: Traceback (most recent call last): File "printrun\printcore.py", line 333, in _readline File "printrun\printcore.py", line 329, in _readline_nb File "serial\serialwin32.py", line 275, in read serial.serialutil.SerialException: ClearCommError failed (PermissionError(13, 'The device does not recognizes the command.', None, 22)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "threading.py", line 932, in _bootstrap_inner File "threading.py", line 870, in run File "printrun\printcore.py", line 418, in _listen File "printrun\printcore.py", line 387, in _listen_until_online File "printrun\printcore.py", line 354, in _readline IndexError: tuple index out of range
Re: Get a random seed to randomize auto file order [C++, MarlinFW] March 19, 2023 03:32PM |
Registered: 7 years ago Posts: 341 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] April 21, 2023 09:20AM |
Registered: 2 years ago Posts: 31 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] April 21, 2023 10:11AM |
Admin Registered: 13 years ago Posts: 7,000 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] April 23, 2023 12:56PM |
Registered: 2 years ago Posts: 31 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] April 24, 2023 01:40AM |
Admin Registered: 13 years ago Posts: 7,000 |
Re: Get a random seed to randomize auto file order [C++, MarlinFW] April 24, 2023 10:55AM |
Registered: 2 years ago Posts: 31 |