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: SDFileSystem mbed
Log.cpp
00001 #include "Log.h" 00002 00003 Log::Log(PinName rx, PinName tx, PinName mosi, PinName miso, PinName sck, PinName cs, const char* name) : 00004 _device(rx, tx), _sd(mosi, miso, sck, cs, name){ 00005 buf_send.initialize_buffer(); 00006 _device.attach(this, &Log::int_serial_tx, Serial::TxIrq); 00007 } 00008 00009 int Log::initialize_sdlog(const char* str){ 00010 char filename[15]; 00011 int n = find_last(); 00012 if(n < 0) return 0; 00013 00014 //ログ番号を+1してファイルを新規作成 00015 //ファイル名は"logXXX.csv" 00016 sprintf(filename, "/sd/log%03d.csv", n+1); 00017 fp = fopen(filename, "w"); 00018 00019 _device.printf(str); 00020 fprintf(fp, str); 00021 return 1; 00022 } 00023 00024 void Log::close(){ 00025 wait(0.5); 00026 fclose(fp); 00027 } 00028 00029 //SDカード内からログ番号の最大値を取得する関数 00030 int Log::find_last() { 00031 int i, n = 0; 00032 char c; 00033 DIR *dp; 00034 struct dirent *dirst; 00035 dp = opendir("/sd/"); 00036 if (!dp){ 00037 printf("Could not open directry\n"); 00038 return -1; 00039 } 00040 while((dirst = readdir(dp)) != NULL) { 00041 if(sscanf(dirst->d_name, "log%03d.csv%c", &i, &c) == 1 && i>n) { 00042 n = i; 00043 } 00044 } 00045 closedir(dp); 00046 return n; 00047 } 00048 00049 void Log::puts(const char* str){ 00050 int16_t len=strlen(str); 00051 int16_t capa=buf_send.buffer_capacity(); 00052 bool empty=buf_send.is_buffer_empty(); 00053 char ch; 00054 if(len>capa){ 00055 len=capa; 00056 } 00057 buf_send.write_buffer((const uint8_t*)str, 0, len); 00058 if((_device.writeable()) && (empty)){ 00059 ch=buf_send.read_buffer_byte(); 00060 _device.putc(ch); 00061 fputc(ch, fp); 00062 } 00063 } 00064 00065 void Log::putc(char ch){ 00066 int16_t capa=buf_send.buffer_capacity(); 00067 if(capa==0) return; 00068 buf_send.write_buffer_byte(ch); 00069 } 00070 00071 void Log::write_data(uint8_t* buf, int16_t size){ 00072 int capa=buf_send.buffer_capacity(); 00073 if(size>capa){ 00074 size=capa; 00075 } 00076 buf_send.write_buffer(buf, 0, size); 00077 } 00078 00079 bool Log::is_empty(){ 00080 return buf_send.is_buffer_empty(); 00081 } 00082 00083 int16_t Log::recieve_buffer_size(){ 00084 return buf_recieve.buffer_size(); 00085 } 00086 00087 int16_t Log::getc(){ 00088 if(buf_recieve.is_buffer_empty()){ 00089 return -1; 00090 } 00091 return buf_recieve.read_buffer_byte(); 00092 } 00093 00094 int16_t Log::read_data(uint8_t* buf, int16_t size){ 00095 int len=buf_recieve.buffer_size(); 00096 if(size>len){ 00097 size=len; 00098 } 00099 buf_recieve.read_buffer(buf, 0, size); 00100 return size; 00101 } 00102 00103 char Log::int_tx(){ 00104 return buf_send.read_buffer_byte(); 00105 } 00106 00107 void Log::int_serial_tx(){ 00108 char ch; 00109 while((_device.writeable()) && (buf_send.is_buffer_empty()==false)){ 00110 ch=buf_send.read_buffer_byte(); 00111 _device.putc(ch); 00112 fputc(ch, fp); 00113 } 00114 }
Generated on Tue Jul 12 2022 18:37:36 by
1.7.2