|
| 1 | + |
| 2 | + |
| 3 | +#include <Arduino_UnifiedStorage.h> |
| 4 | + |
| 5 | + |
| 6 | +#if defined(ARDUINO_PORTENTA_C33) |
| 7 | +InternalStorage ota = InternalStorage(1, "ota", FS_FAT); |
| 8 | +InternalStorage data = InternalStorage(2, "data", FS_FAT); |
| 9 | + |
| 10 | +#elif defined(ARDUINO_PORTENTA_H7_M7) |
| 11 | +InternalStorage wifi = InternalStorage(1, "wifi", FS_FAT); |
| 12 | +InternalStorage ota = InternalStorage(2, "ota", FS_FAT); |
| 13 | +InternalStorage data = InternalStorage(3, "data", FS_FAT); |
| 14 | +#endif |
| 15 | + |
| 16 | +USBStorage thumbDrive = USBStorage(); |
| 17 | + |
| 18 | + |
| 19 | +void addSomeFakeFiles(Folder * folder){ |
| 20 | + Serial.println("* adding some fake files to: " + String(folder -> getPathString())); |
| 21 | + |
| 22 | + for (int i = 0; i < random(0, 9); i++){ |
| 23 | + UFile thisFile = folder -> createFile("File_"+ String(random(999)), FileMode::WRITE); |
| 24 | + Serial.println("\t * " + thisFile.getPathString()); |
| 25 | + thisFile.write("writing stuff to the file"); |
| 26 | + thisFile.close(); |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + Folder subfolder = folder -> createSubfolder("ChildFolder_"+ String(random(999))); |
| 31 | + for (int i = 0; i < random(0, 9); i++){ |
| 32 | + UFile thisFile = subfolder.createFile("File_"+ String(random(999)), FileMode::WRITE); |
| 33 | + Serial.println("\t * " + thisFile.getPathString()); |
| 34 | + thisFile.write("writing stuff to the file"); |
| 35 | + thisFile.close(); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void move(Folder * source, Folder * dest){ |
| 40 | + for(Folder f: source -> getFolders()){ |
| 41 | + Serial.println("* copying folder :" + String(f.getPathString())); |
| 42 | + f.moveTo(*dest); |
| 43 | + } |
| 44 | + |
| 45 | + for(UFile f: source -> getFiles()){ |
| 46 | + Serial.println("* copying file :" + String(f.getPathString())); |
| 47 | + f.moveTo(*dest); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +#if defined(ARDUINO_PORTENTA_C33) |
| 53 | +void backupPartitionsC33(Folder * backupFolder){ |
| 54 | + |
| 55 | + Serial.println("* backup location: " + String(backupFolder -> getPathString())); |
| 56 | + |
| 57 | + int otaMounted = ota.begin(FS_FAT); |
| 58 | + Serial.println("* ota partition mount: " + String(otaMounted)); |
| 59 | + |
| 60 | + int dataMounted = data.begin(FS_FAT); |
| 61 | + Serial.println("* data partition mount: " + String(dataMounted)); |
| 62 | + |
| 63 | + |
| 64 | + if(otaMounted == 1){ |
| 65 | + Folder otaRoot = ota.getRootFolder(); |
| 66 | + addSomeFakeFiles(&otaRoot); |
| 67 | + Folder otaFolder = backupFolder -> createSubfolder("ota"); |
| 68 | + move(&otaRoot, &otaFolder); |
| 69 | + ota.unmount(); |
| 70 | + } else { |
| 71 | + Serial.println("OTA partition not mounted, cannot proceed"); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + if(dataMounted == 1){ |
| 76 | + Folder dataRoot = data.getRootFolder(); |
| 77 | + addSomeFakeFiles(&dataRoot); |
| 78 | + Folder dataFolder = backupFolder -> createSubfolder("data"); |
| 79 | + move(&dataRoot, &dataFolder); |
| 80 | + data.unmount(); |
| 81 | + } else { |
| 82 | + Serial.println("Data partition not mounted, cannot proceed"); |
| 83 | + } |
| 84 | +} |
| 85 | +#endif |
| 86 | + |
| 87 | + |
| 88 | +#if defined(ARDUINO_PORTENTA_H7_M7) |
| 89 | +void backupPartitionsH7(Folder * backupFolder){ |
| 90 | + Serial.println("* backup location: " + String(backupFolder -> getPathString())); |
| 91 | + |
| 92 | + int wifiMounted = wifi.begin(FS_FAT); |
| 93 | + Serial.println("* wifi partition mount: " + String(wifiMounted)); |
| 94 | + |
| 95 | + int otaMounted = ota.begin(FS_FAT); |
| 96 | + Serial.println("* ota partition mount: " + String(otaMounted)); |
| 97 | + |
| 98 | + int dataMounted = data.begin(FS_FAT); |
| 99 | + Serial.println("* data partition mounted: " + String(dataMounted)); |
| 100 | + |
| 101 | + if(wifiMounted == 1){ |
| 102 | + Folder wifiRoot = wifi.getRootFolder(); |
| 103 | + addSomeFakeFiles(&wifiRoot); |
| 104 | + Folder wifiFolder = backupFolder -> createSubfolder("wifi"); |
| 105 | + move(&wifiRoot, &wifiFolder); |
| 106 | + wifi.unmount(); |
| 107 | + } else { |
| 108 | + Serial.println("WiFi partition not mounted, cannot proceed"); |
| 109 | + } |
| 110 | + |
| 111 | + if(otaMounted == 1){ |
| 112 | + Folder otaRoot = ota.getRootFolder(); |
| 113 | + addSomeFakeFiles(&otaRoot); |
| 114 | + Folder otaFolder = backupFolder -> createSubfolder("ota"); |
| 115 | + move(&otaRoot, &otaFolder); |
| 116 | + ota.unmount(); |
| 117 | + } else { |
| 118 | + Serial.println("OTA partition not mounted, cannot proceed"); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + if(dataMounted == 1){ |
| 123 | + Folder dataRoot = data.getRootFolder(); |
| 124 | + addSomeFakeFiles(&dataRoot); |
| 125 | + Folder dataFolder = backupFolder -> createSubfolder("data"); |
| 126 | + move(&dataRoot, &dataFolder); |
| 127 | + data.unmount(); |
| 128 | + } else { |
| 129 | + Serial.println("Data partition not mounted, cannot proceed"); |
| 130 | + } |
| 131 | +} |
| 132 | +#endif |
| 133 | + |
| 134 | +void setup(){ |
| 135 | + randomSeed(analogRead(A0)); |
| 136 | + |
| 137 | + |
| 138 | + Serial.begin(115200); |
| 139 | + while(!Serial); |
| 140 | + |
| 141 | + int thumbMounted = thumbDrive.begin(FS_FAT); |
| 142 | + Serial.println("* usb drive mounted:" + String(thumbMounted)); |
| 143 | + |
| 144 | + |
| 145 | + Folder thumbRoot = thumbDrive.getRootFolder(); |
| 146 | + String folderName = "InternalBackup_" + String(millis()); |
| 147 | + Folder backupFolder = thumbRoot.createSubfolder(folderName); |
| 148 | + |
| 149 | + |
| 150 | + #if defined(ARDUINO_PORTENTA_H7_M7) |
| 151 | + backupPartitionsH7(&backupFolder); |
| 152 | + #elif defined(ARDUINO_PORTENTA_C33) |
| 153 | + backupPartitionsC33(&backupFolder); |
| 154 | + #endif |
| 155 | + |
| 156 | + thumbDrive.unmount(); |
| 157 | + |
| 158 | + Serial.println("DONE, you can restart the board now"); |
| 159 | + |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +void loop(){ |
| 164 | + |
| 165 | +} |
0 commit comments