Log data to a micro SD card.

Dependencies:   SDFileSystem mbed

Committer:
onaka
Date:
Mon May 04 07:10:55 2015 +0000
Revision:
1:c5e56e2580bf
revision 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
onaka 1:c5e56e2580bf 1 #include "Log.h"
onaka 1:c5e56e2580bf 2
onaka 1:c5e56e2580bf 3 Log::Log(PinName rx, PinName tx, PinName mosi, PinName miso, PinName sck, PinName cs, const char* name) :
onaka 1:c5e56e2580bf 4 _device(rx, tx), _sd(mosi, miso, sck, cs, name){
onaka 1:c5e56e2580bf 5 buf_send.initialize_buffer();
onaka 1:c5e56e2580bf 6 _device.attach(this, &Log::int_serial_tx, Serial::TxIrq);
onaka 1:c5e56e2580bf 7 }
onaka 1:c5e56e2580bf 8
onaka 1:c5e56e2580bf 9 int Log::initialize_sdlog(const char* str){
onaka 1:c5e56e2580bf 10 char filename[15];
onaka 1:c5e56e2580bf 11 int n = find_last();
onaka 1:c5e56e2580bf 12 if(n < 0) return 0;
onaka 1:c5e56e2580bf 13
onaka 1:c5e56e2580bf 14 //ログ番号を+1してファイルを新規作成
onaka 1:c5e56e2580bf 15 //ファイル名は"logXXX.csv"
onaka 1:c5e56e2580bf 16 sprintf(filename, "/sd/log%03d.csv", n+1);
onaka 1:c5e56e2580bf 17 fp = fopen(filename, "w");
onaka 1:c5e56e2580bf 18
onaka 1:c5e56e2580bf 19 _device.printf(str);
onaka 1:c5e56e2580bf 20 fprintf(fp, str);
onaka 1:c5e56e2580bf 21 return 1;
onaka 1:c5e56e2580bf 22 }
onaka 1:c5e56e2580bf 23
onaka 1:c5e56e2580bf 24 void Log::close(){
onaka 1:c5e56e2580bf 25 wait(0.5);
onaka 1:c5e56e2580bf 26 fclose(fp);
onaka 1:c5e56e2580bf 27 }
onaka 1:c5e56e2580bf 28
onaka 1:c5e56e2580bf 29 //SDカード内からログ番号の最大値を取得する関数
onaka 1:c5e56e2580bf 30 int Log::find_last() {
onaka 1:c5e56e2580bf 31 int i, n = 0;
onaka 1:c5e56e2580bf 32 char c;
onaka 1:c5e56e2580bf 33 DIR *dp;
onaka 1:c5e56e2580bf 34 struct dirent *dirst;
onaka 1:c5e56e2580bf 35 dp = opendir("/sd/");
onaka 1:c5e56e2580bf 36 if (!dp){
onaka 1:c5e56e2580bf 37 printf("Could not open directry\n");
onaka 1:c5e56e2580bf 38 return -1;
onaka 1:c5e56e2580bf 39 }
onaka 1:c5e56e2580bf 40 while((dirst = readdir(dp)) != NULL) {
onaka 1:c5e56e2580bf 41 if(sscanf(dirst->d_name, "log%03d.csv%c", &i, &c) == 1 && i>n) {
onaka 1:c5e56e2580bf 42 n = i;
onaka 1:c5e56e2580bf 43 }
onaka 1:c5e56e2580bf 44 }
onaka 1:c5e56e2580bf 45 closedir(dp);
onaka 1:c5e56e2580bf 46 return n;
onaka 1:c5e56e2580bf 47 }
onaka 1:c5e56e2580bf 48
onaka 1:c5e56e2580bf 49 void Log::puts(const char* str){
onaka 1:c5e56e2580bf 50 int16_t len=strlen(str);
onaka 1:c5e56e2580bf 51 int16_t capa=buf_send.buffer_capacity();
onaka 1:c5e56e2580bf 52 bool empty=buf_send.is_buffer_empty();
onaka 1:c5e56e2580bf 53 char ch;
onaka 1:c5e56e2580bf 54 if(len>capa){
onaka 1:c5e56e2580bf 55 len=capa;
onaka 1:c5e56e2580bf 56 }
onaka 1:c5e56e2580bf 57 buf_send.write_buffer((const uint8_t*)str, 0, len);
onaka 1:c5e56e2580bf 58 if((_device.writeable()) && (empty)){
onaka 1:c5e56e2580bf 59 ch=buf_send.read_buffer_byte();
onaka 1:c5e56e2580bf 60 _device.putc(ch);
onaka 1:c5e56e2580bf 61 fputc(ch, fp);
onaka 1:c5e56e2580bf 62 }
onaka 1:c5e56e2580bf 63 }
onaka 1:c5e56e2580bf 64
onaka 1:c5e56e2580bf 65 void Log::putc(char ch){
onaka 1:c5e56e2580bf 66 int16_t capa=buf_send.buffer_capacity();
onaka 1:c5e56e2580bf 67 if(capa==0) return;
onaka 1:c5e56e2580bf 68 buf_send.write_buffer_byte(ch);
onaka 1:c5e56e2580bf 69 }
onaka 1:c5e56e2580bf 70
onaka 1:c5e56e2580bf 71 void Log::write_data(uint8_t* buf, int16_t size){
onaka 1:c5e56e2580bf 72 int capa=buf_send.buffer_capacity();
onaka 1:c5e56e2580bf 73 if(size>capa){
onaka 1:c5e56e2580bf 74 size=capa;
onaka 1:c5e56e2580bf 75 }
onaka 1:c5e56e2580bf 76 buf_send.write_buffer(buf, 0, size);
onaka 1:c5e56e2580bf 77 }
onaka 1:c5e56e2580bf 78
onaka 1:c5e56e2580bf 79 bool Log::is_empty(){
onaka 1:c5e56e2580bf 80 return buf_send.is_buffer_empty();
onaka 1:c5e56e2580bf 81 }
onaka 1:c5e56e2580bf 82
onaka 1:c5e56e2580bf 83 int16_t Log::recieve_buffer_size(){
onaka 1:c5e56e2580bf 84 return buf_recieve.buffer_size();
onaka 1:c5e56e2580bf 85 }
onaka 1:c5e56e2580bf 86
onaka 1:c5e56e2580bf 87 int16_t Log::getc(){
onaka 1:c5e56e2580bf 88 if(buf_recieve.is_buffer_empty()){
onaka 1:c5e56e2580bf 89 return -1;
onaka 1:c5e56e2580bf 90 }
onaka 1:c5e56e2580bf 91 return buf_recieve.read_buffer_byte();
onaka 1:c5e56e2580bf 92 }
onaka 1:c5e56e2580bf 93
onaka 1:c5e56e2580bf 94 int16_t Log::read_data(uint8_t* buf, int16_t size){
onaka 1:c5e56e2580bf 95 int len=buf_recieve.buffer_size();
onaka 1:c5e56e2580bf 96 if(size>len){
onaka 1:c5e56e2580bf 97 size=len;
onaka 1:c5e56e2580bf 98 }
onaka 1:c5e56e2580bf 99 buf_recieve.read_buffer(buf, 0, size);
onaka 1:c5e56e2580bf 100 return size;
onaka 1:c5e56e2580bf 101 }
onaka 1:c5e56e2580bf 102
onaka 1:c5e56e2580bf 103 char Log::int_tx(){
onaka 1:c5e56e2580bf 104 return buf_send.read_buffer_byte();
onaka 1:c5e56e2580bf 105 }
onaka 1:c5e56e2580bf 106
onaka 1:c5e56e2580bf 107 void Log::int_serial_tx(){
onaka 1:c5e56e2580bf 108 char ch;
onaka 1:c5e56e2580bf 109 while((_device.writeable()) && (buf_send.is_buffer_empty()==false)){
onaka 1:c5e56e2580bf 110 ch=buf_send.read_buffer_byte();
onaka 1:c5e56e2580bf 111 _device.putc(ch);
onaka 1:c5e56e2580bf 112 fputc(ch, fp);
onaka 1:c5e56e2580bf 113 }
onaka 1:c5e56e2580bf 114 }