|
| 1 | +#include "MycobotSaver.h" |
| 2 | + |
| 3 | +MycobotSaver::MycobotSaver() |
| 4 | +{ |
| 5 | + |
| 6 | +} |
| 7 | + |
| 8 | + |
| 9 | +void MycobotSaver::listDir(fs::FS &fs, const char * dirname, uint8_t levels){ |
| 10 | + Serial.printf("Listing directory: %s\r\n", dirname); |
| 11 | + |
| 12 | + File root = fs.open(dirname); |
| 13 | + if(!root){ |
| 14 | + Serial.println("- failed to open directory"); |
| 15 | + return; |
| 16 | + } |
| 17 | + if(!root.isDirectory()){ |
| 18 | + Serial.println(" - not a directory"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + File file = root.openNextFile(); //less 8 coloumns |
| 23 | + while(file){ |
| 24 | + if(file.isDirectory()){ |
| 25 | + Serial.print(" DIR : "); |
| 26 | + Serial.println(file.name()); |
| 27 | + if(levels){ |
| 28 | + listDir(fs, file.name(), levels -1); |
| 29 | + } |
| 30 | + } else { |
| 31 | + Serial.print(" FILE: "); |
| 32 | + Serial.print(file.name()); |
| 33 | + Serial.print("\tSIZE: "); |
| 34 | + Serial.println(file.size()); |
| 35 | + } |
| 36 | + file = root.openNextFile(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +void MycobotSaver::writeFile(fs::FS &fs, const char * path, const char * message){ |
| 42 | + Serial.printf("Writing file: %s\r\n", path); |
| 43 | + |
| 44 | + File file = fs.open(path, FILE_WRITE); |
| 45 | + if(!file){ |
| 46 | + Serial.println("- failed to open file for writing"); |
| 47 | + return; |
| 48 | + } |
| 49 | + if(file.print(message)){ |
| 50 | + Serial.println("- file written"); |
| 51 | + } else { |
| 52 | + Serial.println("- frite failed"); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void MycobotSaver::appendFile(fs::FS &fs, const char * path, const char * message){ |
| 57 | + |
| 58 | + File file = fs.open(path, FILE_APPEND); |
| 59 | + if(!file){ |
| 60 | + return; |
| 61 | + } |
| 62 | + file.print(message); |
| 63 | +} |
| 64 | + |
| 65 | +void MycobotSaver::deleteFile(fs::FS &fs, const char * path){ |
| 66 | + Serial.printf("Deleting file: %s\r\n", path); |
| 67 | + if(fs.remove(path)){ |
| 68 | + Serial.println("- file deleted"); |
| 69 | + } else { |
| 70 | + Serial.println("- delete failed"); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void MycobotSaver::readFile(fs::FS &fs, const char * path){ |
| 75 | + |
| 76 | + File file = fs.open(path); |
| 77 | + if(!file || file.isDirectory()){ |
| 78 | + Serial.println("- failed to open file for reading"); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + String this_line = ""; |
| 83 | + |
| 84 | + while(file.available()){ |
| 85 | + char this_char = char(file.read()); |
| 86 | + this_line += this_char; |
| 87 | + if (this_char == '\n') |
| 88 | + { |
| 89 | + saver_angles_enc jae_this; |
| 90 | + jae_this = processStringIntoInts(this_line); |
| 91 | + |
| 92 | + this_line = ""; |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +MycobotSaver::saver_angles_enc MycobotSaver::processStringIntoInts(String string_input) |
| 101 | +{ |
| 102 | + saver_angles_enc sae; |
| 103 | + int data_index = 0; |
| 104 | + String data_angle_string=""; |
| 105 | + for (int i = 0; string_input[i] != '\n'; i++) { |
| 106 | + |
| 107 | + if(string_input[i] == ',') |
| 108 | + { |
| 109 | + if ((data_index < 6)&&(i>1)){ |
| 110 | + sae.joint_angle[data_index] = data_angle_string.toInt(); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + break; |
| 115 | + } |
| 116 | + data_angle_string = ""; |
| 117 | + data_index ++; |
| 118 | + continue; |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + data_angle_string+=string_input[i]; |
| 123 | + } |
| 124 | + } |
| 125 | + return sae; |
| 126 | +} |
0 commit comments