Browse Source

Fist attempt at "saved position SD menu"

Alex Voinea 4 năm trước cách đây
mục cha
commit
f0f7db57ea
4 tập tin đã thay đổi với 34 bổ sung178 xóa
  1. 21 123
      Firmware/cardreader.cpp
  2. 2 33
      Firmware/cardreader.h
  3. 9 22
      Firmware/ultralcd.cpp
  4. 2 0
      Firmware/ultralcd.h

+ 21 - 123
Firmware/cardreader.cpp

@@ -416,6 +416,7 @@ void CardReader::openFile(const char* name,bool read, bool replace_current/*=tru
       getfilename(0, fname);
       lcd_setstatus(longFilename[0] ? longFilename : fname);
       lcd_setstatus("SD-PRINTING         ");
+      scrollstuff = 0;
     }
     else
     {
@@ -689,12 +690,20 @@ void CardReader::updir()
 * Get the name of a file in the current directory by sort-index
 */
 void CardReader::getfilename_sorted(const uint16_t nr) {
-	getfilename(
-	#if SDSORT_GCODE
-		sort_alpha &&
-	#endif
-		(nr < sort_count) ? sort_order[nr] : nr
-	);
+	if (nr < sort_count)
+        getfilename_simple(
+        #if SDSORT_GCODE
+            sort_alpha &&
+        #endif
+            sort_positions[sort_order[nr]]
+        );
+    else
+        getfilename(
+        #if SDSORT_GCODE
+            sort_alpha &&
+        #endif
+            nr
+        );
 }
 
 /**
@@ -730,44 +739,10 @@ void CardReader::presort() {
 			fileCnt = SDSORT_LIMIT;
 		}
 		lcd_clear();
-		#if !SDSORT_USES_RAM
-			lcd_set_progress();
-		#endif
+        lcd_set_progress();
 		lcd_puts_at_P(0, 1, _i("Sorting files"));////MSG_SORTING c=20 r=1
 
-		// Sort order is always needed. May be static or dynamic.
-		#if SDSORT_DYNAMIC_RAM
-		sort_order = new uint8_t[fileCnt];
-		#endif
-
-		// Use RAM to store the entire directory during pre-sort.
-		// SDSORT_LIMIT should be set to prevent over-allocation.
-		#if SDSORT_USES_RAM
-
-		// If using dynamic ram for names, allocate on the heap.
-		#if SDSORT_CACHE_NAMES
-		#if SDSORT_DYNAMIC_RAM
-		sortshort = new char*[fileCnt];
-		sortnames = new char*[fileCnt];
-		#endif
-		#elif SDSORT_USES_STACK
-		char sortnames[fileCnt][LONG_FILENAME_LENGTH];
-		uint16_t modification_time[fileCnt];
-		uint16_t modification_date[fileCnt];
-		#endif
-
-		// Folder sorting needs 1 bit per entry for flags.
-		#if HAS_FOLDER_SORTING
-		#if SDSORT_DYNAMIC_RAM
-		isDir = new uint8_t[(fileCnt + 7) >> 3];
-		#elif SDSORT_USES_STACK
-		uint8_t isDir[(fileCnt + 7) >> 3];
-		#endif
-		#endif
-
-		#else // !SDSORT_USES_RAM
-
-		uint32_t positions[fileCnt];
+		// uint32_t positions[fileCnt];
 
 		// By default re-read the names from SD for every compare
 		// retaining only two filenames at a time. This is very
@@ -776,7 +751,6 @@ void CardReader::presort() {
 		uint16_t modification_time_bckp;
 		uint16_t modification_date_bckp;
 
-		#endif
 		position = 0;
 		if (fileCnt > 1) {
 			// Init sort order.
@@ -784,37 +758,8 @@ void CardReader::presort() {
 				if (!IS_SD_INSERTED) return;
 				manage_heater();
 				sort_order[i] = i;
-				positions[i] = position;
+				sort_positions[i] = position;
 				getfilename(i);
-				// If using RAM then read all filenames now.
-				#if SDSORT_USES_RAM
-				getfilename(i);
-				#if SDSORT_DYNAMIC_RAM
-				// Use dynamic method to copy long filename
-				sortnames[i] = strdup(LONGEST_FILENAME);
-				#if SDSORT_CACHE_NAMES
-				// When caching also store the short name, since
-				// we're replacing the getfilename() behavior.
-				sortshort[i] = strdup(filename);
-				#endif
-				#else
-				// Copy filenames into the static array
-				strcpy(sortnames[i], LONGEST_FILENAME);
-				modification_time[i] = modificationTime;
-				modification_date[i] = modificationDate;
-				#if SDSORT_CACHE_NAMES
-				strcpy(sortshort[i], filename);
-				#endif
-				#endif
-				// char out[30];
-				// sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
-				// SERIAL_ECHOLN(out);
-				#if HAS_FOLDER_SORTING
-				const uint16_t bit = i & 0x07, ind = i >> 3;
-				if (bit == 0) isDir[ind] = 0x00;
-				if (filenameIsDir) isDir[ind] |= _BV(bit);
-				#endif
-				#endif
 			}
 
 #ifdef QUICKSORT
@@ -824,41 +769,18 @@ void CardReader::presort() {
 			uint16_t total = 0.5*(fileCnt - 1)*(fileCnt);
 
 			// Compare names from the array or just the two buffered names
-			#if SDSORT_USES_RAM
-			#define _SORT_CMP_NODIR() (strcasecmp(sortnames[o1], sortnames[o2]) > 0)
-			#define _SORT_CMP_TIME_NODIR() (((modification_date[o1] == modification_date[o2]) && (modification_time[o1] < modification_time[o2])) || \
-																	(modification_date[o1] < modification_date [o2]))
-			#else
 			#define _SORT_CMP_NODIR() (strcasecmp(name1, name2) > 0) //true if lowercase(name1) > lowercase(name2)
-			#define _SORT_CMP_TIME_NODIR() (((modification_date_bckp == modificationDate) && (modification_time_bckp > modificationTime)) || \
-																	(modification_date_bckp > modificationDate))
-
-			#endif
+			#define _SORT_CMP_TIME_NODIR() (((modification_date_bckp == modificationDate) && (modification_time_bckp > modificationTime)) || (modification_date_bckp > modificationDate))
 
 			#if HAS_FOLDER_SORTING
-			#if SDSORT_USES_RAM
-			// Folder sorting needs an index and bit to test for folder-ness.
-			const uint8_t ind1 = o1 >> 3, bit1 = o1 & 0x07,
-				ind2 = o2 >> 3, bit2 = o2 & 0x07;
-			#define _SORT_CMP_DIR(fs) \
-										  (((isDir[ind1] & _BV(bit1)) != 0) == ((isDir[ind2] & _BV(bit2)) != 0) \
-											? _SORT_CMP_NODIR() \
-											: (isDir[fs > 0 ? ind1 : ind2] & (fs > 0 ? _BV(bit1) : _BV(bit2))) != 0)
-			#define _SORT_CMP_TIME_DIR(fs) \
-										  (((isDir[ind1] & _BV(bit1)) != 0) == ((isDir[ind2] & _BV(bit2)) != 0) \
-											? _SORT_CMP_TIME_NODIR() \
-											: (isDir[fs > 0 ? ind1 : ind2] & (fs > 0 ? _BV(bit1) : _BV(bit2))) != 0)
-			#else
 			#define _SORT_CMP_DIR(fs) ((dir1 == filenameIsDir) ? _SORT_CMP_NODIR() : (fs > 0 ? dir1 : !dir1))
 			#define _SORT_CMP_TIME_DIR(fs) ((dir1 == filenameIsDir) ? _SORT_CMP_TIME_NODIR() : (fs < 0 ? dir1 : !dir1))
 			#endif
-			#endif
 
 			for (uint16_t i = fileCnt; --i;) {
 				if (!IS_SD_INSERTED) return;
 				bool didSwap = false;
 
-				#if !SDSORT_USES_RAM //show progresss bar only if slow sorting method is used
 				int8_t percent = (counter * 100) / total;//((counter * 100) / pow((fileCnt-1),2));
 				for (int column = 0; column < 20; column++) {
 					if (column < (percent / 5))
@@ -868,7 +790,6 @@ void CardReader::presort() {
 					}
 				}
 				counter++;
-				#endif
 
 				//MYSERIAL.println(int(i));
 				for (uint16_t j = 0; j < i; ++j) {
@@ -880,14 +801,14 @@ void CardReader::presort() {
 					// throughout the loop. Slow if there are many.
 					#if !SDSORT_USES_RAM
 					counter++;
-					getfilename_simple(positions[o1]);
+					getfilename_simple(sort_positions[o1]);
 					strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it)
 					modification_date_bckp = modificationDate;
 					modification_time_bckp = modificationTime;
 					#if HAS_FOLDER_SORTING
 					bool dir1 = filenameIsDir;
 					#endif
-					getfilename_simple(positions[o2]);
+					getfilename_simple(sort_positions[o2]);
 					char *name2 = LONGEST_FILENAME; // use the string in-place
 
 					#endif // !SDSORT_USES_RAM
@@ -909,32 +830,9 @@ void CardReader::presort() {
 				if (!didSwap) break;
 			} //end of bubble sort loop
 #endif
-			  // Using RAM but not keeping names around
-			#if (SDSORT_USES_RAM && !SDSORT_CACHE_NAMES)
-			#if SDSORT_DYNAMIC_RAM
-			for (uint16_t i = 0; i < fileCnt; ++i) free(sortnames[i]);
-			#if HAS_FOLDER_SORTING
-			free(isDir);
-			#endif
-			#endif
-			#endif
 		}
 		else {
 			sort_order[0] = 0;
-		#if (SDSORT_USES_RAM && SDSORT_CACHE_NAMES)
-			getfilename(0);
-			#if SDSORT_DYNAMIC_RAM
-			sortnames = new char*[1];
-			sortnames[0] = strdup(LONGEST_FILENAME); // malloc
-			sortshort = new char*[1];
-			sortshort[0] = strdup(filename);         // malloc
-			isDir = new uint8_t[1];
-			#else
-			strcpy(sortnames[0], LONGEST_FILENAME);
-			strcpy(sortshort[0], filename);
-			#endif
-			isDir[0] = filenameIsDir ? 0x01 : 0x00;
-		#endif
 		}
 
 		sort_count = fileCnt;

+ 2 - 33
Firmware/cardreader.h

@@ -93,39 +93,8 @@ private:
 							//bool sort_reverse;      // Flag to enable / disable reverse sorting
   #endif
 
-							// By default the sort index is static
-  #if SDSORT_DYNAMIC_RAM
-  uint8_t *sort_order;
-  #else
-  uint8_t sort_order[SDSORT_LIMIT];
-  #endif
-  // Cache filenames to speed up SD menus.
-  #if SDSORT_USES_RAM
-
-  // If using dynamic ram for names, allocate on the heap.
-  #if SDSORT_CACHE_NAMES
-    #if SDSORT_DYNAMIC_RAM
-      char **sortshort, **sortnames;
-    #else
-      char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
-      char sortnames[SDSORT_LIMIT][FILENAME_LENGTH];
-    #endif
-  #elif !SDSORT_USES_STACK
-    char sortnames[SDSORT_LIMIT][FILENAME_LENGTH];
-    uint16_t modification_time[SDSORT_LIMIT];
-    uint16_t modification_date[SDSORT_LIMIT];
-  #endif
-
-  // Folder sorting uses an isDir array when caching items.
-  #if HAS_FOLDER_SORTING
-    #if SDSORT_DYNAMIC_RAM
-      uint8_t *isDir;
-    #elif (SDSORT_CACHE_NAMES) || !(SDSORT_USES_STACK)
-      uint8_t isDir[(SDSORT_LIMIT + 7) >> 3];
-    #endif
-  #endif
-
-  #endif // SDSORT_USES_RAM
+  uint8_t sort_order[SDSORT_LIMIT]; // By default the sort index is static.
+  uint32_t sort_positions[SDSORT_LIMIT];
 
 #endif // SDCARD_SORT_ALPHA
 

+ 9 - 22
Firmware/ultralcd.cpp

@@ -57,7 +57,7 @@ static void lcd_mesh_bed_leveling_settings();
 static void lcd_backlight_menu();
 
 int8_t ReInitLCD = 0;
-
+uint8_t scrollstuff = 0;
 
 int8_t SilentModeMenu = SILENT_MODE_OFF;
 uint8_t SilentModeMenu_MMU = 1; //activate mmu unit stealth mode
@@ -325,6 +325,7 @@ typedef struct
 	bool isDir = 0;
 	uint8_t row = 0;
 	const char* scrollPointer;
+    uint16_t fileCnt;
 } _menu_data_scroll_t;
 static_assert(sizeof(menu_data)>= sizeof(_menu_data_scroll_t),"_menu_data_scroll_t doesn't fit into menu_data");
 
@@ -634,16 +635,6 @@ void lcdui_print_time(void)
 //Print status line on status screen
 void lcdui_print_status_line(void)
 {
-/* 	if (IS_SD_PRINTING)
-	{
-		if (strcmp(longFilenameOLD, (card.longFilename[0] ? card.longFilename : card.filename)) != 0)
-		{
-			memset(longFilenameOLD, '\0', strlen(longFilenameOLD));
-			sprintf_P(longFilenameOLD, PSTR("%s"), (card.longFilename[0] ? card.longFilename : card.filename));
-			scrollstuff = 0;
-		}
-	} */
-
 	if (heating_status)
 	{ // If heating flag, show progress of heating
 		heating_status_counter++;
@@ -687,7 +678,8 @@ void lcdui_print_status_line(void)
 	}
 	else if ((IS_SD_PRINTING) && (custom_message_type == CustomMsg::Status))
 	{ // If printing from SD, show what we are printing
-		/* if(strlen(longFilenameOLD) > LCD_WIDTH)
+		const char* longFilenameOLD = (card.longFilename[0] ? card.longFilename : card.filename);
+		if(strlen(longFilenameOLD) > LCD_WIDTH)
 		{
 			int inters = 0;
 			int gh = scrollstuff;
@@ -713,7 +705,7 @@ void lcdui_print_status_line(void)
 		else
 		{
 			lcd_printf_P(PSTR("%-20s"), longFilenameOLD);
-		} */
+		}
 	}
 	else
 	{ // Otherwise check for other special events
@@ -7292,11 +7284,12 @@ void lcd_sdcard_menu()
   bool scrollEnter = lcd_scrollTimer.expired(500);
   if (lcd_draw_update == 0 && LCD_CLICKED == 0 && !scrollEnter && !menu_entering)
     return; // nothing to do (so don't thrash the SD card)
-  uint16_t fileCnt = card.getnrfilenames();
+  _menu_data_scroll_t* _md = (_menu_data_scroll_t*)&(menu_data[0]);
   if (menu_entering)
   {
     menu_entering = 0; //clear entering flag
 	lcd_draw_update = 1; //draw lines again
+    _md->fileCnt = card.getnrfilenames();
   }
   if (!scrollEnter) lcd_scrollTimer.start();
   MENU_BEGIN();
@@ -7311,17 +7304,11 @@ void lcd_sdcard_menu()
     MENU_ITEM_FUNCTION_P(PSTR(LCD_STR_FOLDER ".."), lcd_sd_updir);
   }
 
-  for (uint16_t i = 0; i < fileCnt; i++)
+  for (uint16_t i = 0; i < _md->fileCnt; i++)
   {
     if (menu_item == menu_line)
     {
-		const uint16_t nr = ((sdSort == SD_SORT_NONE) || farm_mode || (sdSort == SD_SORT_TIME)) ? (fileCnt - 1 - i) : i;
-		/*#ifdef SDCARD_RATHERRECENTFIRST
-			#ifndef SDCARD_SORT_ALPHA
-				fileCnt - 1 -
-			#endif
-		#endif
-		i;*/
+		const uint16_t nr = ((sdSort == SD_SORT_NONE) || farm_mode || (sdSort == SD_SORT_TIME)) ? (_md->fileCnt - 1 - i) : i;
 		#ifdef SDCARD_SORT_ALPHA
 			if (sdSort == SD_SORT_NONE) card.getfilename(nr);
 			else card.getfilename_sorted(nr);

+ 2 - 0
Firmware/ultralcd.h

@@ -144,6 +144,8 @@ extern uint8_t SilentModeMenu_MMU;
 extern bool cancel_heatup;
 extern bool isPrintPaused;
 
+extern uint8_t scrollstuff;
+
 
 void lcd_ignore_click(bool b=true);
 void lcd_commands();