Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "mDot.h" 00003 #include "MTSLog.h" 00004 #include <vector> 00005 00006 Serial pc(USBTX, USBRX); 00007 00008 int main() 00009 { 00010 mDot* dot; 00011 std::vector<mDot::mdot_file> files; 00012 mDot::mdot_file file; 00013 const char filename[] = "test_file.txt"; 00014 uint8_t buf[1024]; 00015 00016 pc.baud(115200); 00017 00018 // get a mDot handle 00019 dot = mDot::getInstance(); 00020 dot->resetConfig(); 00021 dot->saveConfig(); 00022 00023 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL); 00024 00025 // print library version information 00026 logInfo("version: %s", dot->getId().c_str()); 00027 00028 logInfo("deleting user files"); 00029 files = dot->listUserFiles(); 00030 for (std::vector<mDot::mdot_file>::iterator it = files.begin(); it != files.end(); it++) { 00031 printf("\tdeleting %s [%d]\r\n", it->name, it->size); 00032 dot->deleteUserFile(it->name); 00033 } 00034 00035 memset(buf, 0x3B, sizeof(buf)); 00036 00037 logInfo("creating 1kB file of 0x3B"); 00038 file = dot->openUserFile(filename, mDot::FM_RDWR | mDot::FM_CREAT | mDot::FM_APPEND); 00039 if (file.fd < 0) { 00040 logError("failed to open file"); 00041 } else { 00042 if (dot->writeUserFile(file, (void*)buf, sizeof(buf)) != sizeof(buf)) { 00043 logError("didn't write entire buffer"); 00044 } 00045 00046 dot->closeUserFile(file); 00047 } 00048 00049 for (int i = 0; i < 10; i++) { 00050 uint8_t buf[8]; 00051 uint32_t loc; 00052 00053 memset(buf, 0xA2, sizeof(buf)); 00054 00055 file = dot->openUserFile(filename, mDot::FM_RDWR); 00056 if (file.fd < 0) { 00057 logError("failed to open file"); 00058 break; 00059 } else { 00060 loc = (uint32_t)(rand() % (file.size - sizeof(buf))); 00061 logInfo("seeking to 0x%04X", loc); 00062 if (! dot->seekUserFile(file, loc, SEEK_SET)) { 00063 logError("seek failed"); 00064 break; 00065 } 00066 logInfo("writing %d '0xA2' bytes to 0x%04X", sizeof(buf), loc); 00067 if (dot->writeUserFile(file, (void*)buf, sizeof(buf)) != sizeof(buf)) { 00068 logError("didn't write entire buffer"); 00069 break; 00070 } 00071 00072 dot->closeUserFile(file); 00073 } 00074 } 00075 00076 file = dot->openUserFile(filename, mDot::FM_RDONLY); 00077 if (file.fd < 0) { 00078 logError("failed to open file"); 00079 } else { 00080 uint8_t buf[16]; 00081 uint32_t read = 0; 00082 uint32_t ret = 0; 00083 00084 logInfo("file data:"); 00085 while (read < file.size) { 00086 ret = dot->readUserFile(file, (void*)buf, sizeof(buf)); 00087 if (ret > 0) { 00088 printf("0x%04X\t", read); 00089 for (int i = 0; i < ret; i++) { 00090 printf("%02X ", buf[i]); 00091 } 00092 printf("\r\n"); 00093 read += ret; 00094 } else { 00095 logError("error reading file"); 00096 break; 00097 } 00098 } 00099 00100 dot->closeUserFile(file); 00101 } 00102 00103 return 0; 00104 }
Generated on Thu Aug 4 2022 10:39:20 by
1.7.2