๐Ÿ“ฃ๐Ÿ“ช ๐Ÿšš Adafruit will not be shipping USPS orders Monday January 20, 2020 ๐Ÿ—“. Expedited USPS orders placed after 11am ET ๐Ÿ•š Friday January 17 will go out Tuesday January 21 ๐Ÿ“ฃ๐Ÿ“ช๐Ÿšš
0

Logger Read Question
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Logger Read Question

by dcrackel on Tue Sep 08, 2009 11:30 pm

I recently picked up a SD module for the Arduino with a ATmega328.

I found the lib for logging very helpfull. Found at http://www.ladyada.net/make/gpsshield/download.html

However I would like to be able to read from the log file also.

In AF_SDlog.h I added this sig: uint8_t read_file(File f, uint8_t* b, uint8_t num);

Code: Select all | TOGGLE FULL SIZE

#ifndef _AF_SDLOG_h_
#define _AF_SDLOG_h_

#include "sd_raw.h"
#include "sd_raw_config.h"
#include "sd-reader_config.h"
#include "partition.h"
#include "partition_config.h"
#include "fat16.h"

typedef struct fat16_file_struct * File;

class AF_SDLog {
  struct partition_struct *partition;
  struct fat16_fs_struct* fs;
  struct fat16_dir_struct* dd;
  struct fat16_dir_entry_struct file_entry;

 public:
  AF_SDLog(void);
  uint8_t init_card(void);
  uint8_t open_partition(void);
  uint8_t open_filesys(void);
  uint8_t open_dir(char *path);
  uint8_t close_dir(void);
  char *get_next_name_in_dir(void);
  void reset_dir(void);
  File open_file(char *name);
  void close_file(File f);
  uint8_t create_file(char *name);
  uint8_t write_file(File f, uint8_t *b, uint8_t num);
  int16_t read_file(File f, uint8_t* buffer, uint16_t buffer_len);
  uint8_t seek_file(File fd, int32_t *offset, uint8_t whence);
};

#endif


and in AF_SDLog.cpp I added the function

int16_t AF_SDLog::read_file(File f, uint8_t* buffer, uint16_t buffer_len){
return fat16_read_file(f, buffer, buffer_len);
}



Code: Select all | TOGGLE FULL SIZE

#include <avr/io.h>
#include "AF_SDLog.h"
#include "fat16.h"
#include "sd_raw.h"
#include "partition.h"
#include <string.h>


#if !USE_DYNAMIC_MEMORY


  struct partition_struct partition_handles[PARTITION_COUNT];
#endif




AF_SDLog::AF_SDLog(void) {

}

uint8_t AF_SDLog::init_card(void) {
  return sd_raw_init();
}

uint8_t AF_SDLog::open_partition(void) {
  /* open first partition */
  partition =
    partition_open(sd_raw_read,
         sd_raw_read_interval,
         sd_raw_write,
         sd_raw_write_interval,
         0);

  if(!partition) {
    /* If the partition did not open, assume the storage device
     * is a "superfloppy", i.e. has no MBR.
     */
    partition =
      partition_open(sd_raw_read,
           sd_raw_read_interval,
           sd_raw_write,
           sd_raw_write_interval,
           -1);
  }
  if(!partition) {
    return 0;
  }
  return 1;
}

uint8_t AF_SDLog::open_filesys(void) {
  /* open file system */
  fs = fat16_open(partition);
  if(!fs) {
    return 0;
  }

  return 1;
}

uint8_t AF_SDLog::open_dir(char *path) {
  // Open root directory
  struct fat16_dir_entry_struct rootdirectory;

  fat16_get_dir_entry_of_root(fs, &rootdirectory);
  dd = fat16_open_dir(fs, &rootdirectory);
  if(!dd) {
    return 0;
  }
  return 1;
}


uint8_t find_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name, struct fat16_dir_entry_struct* dir_entry)
{
    while(fat16_read_dir(dd, dir_entry))
    {
      if(strcmp(dir_entry->long_name, name) == 0)
        {
     fat16_reset_dir(dd);
     return 1;
        }
    }

    return 0;
}

struct fat16_file_struct* open_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name)
{
    struct fat16_dir_entry_struct file_entry;
    if(!find_file_in_dir(fs, dd, name, &file_entry)) {

      //Serial.println("File not found");

        return 0;
    }
    return fat16_open_file(fs, &file_entry);
}


File AF_SDLog::open_file(char *name) {
  return open_file_in_dir(fs, dd, name);
}


uint8_t AF_SDLog::create_file(char *name) {
  struct fat16_dir_entry_struct file_entry;
  return fat16_create_file(dd, name, &file_entry);
}


uint8_t AF_SDLog::write_file(File f, uint8_t *buff, uint8_t siz) {
  return fat16_write_file(f, buff, siz);
}

int16_t AF_SDLog::read_file(File f, uint8_t* buffer, uint16_t buffer_len){
   return fat16_read_file(f, buffer, buffer_len);
}


uint8_t AF_SDLog::seek_file(File fd, int32_t *offset, uint8_t whence) {
  return  fat16_seek_file(fd, offset, whence);
}

void AF_SDLog::close_file(File f) {
   fat16_close_file(f);
}





My program
Code: Select all | TOGGLE FULL SIZE
#include "AF_SDLog.h"
#include "util.h"

AF_SDLog card;
File f;

#define BUFFSIZE 75
char buffer[BUFFSIZE];
char buffer2[BUFFSIZE];
uint8_t i;
uint8_t j;

#define led1Pin 4
#define led2Pin 3
#define powerPin 2


void setup()
{

  Serial.begin(9600);

  if (!card.init_card()) {
    putstring_nl("Card init. failed!");
    error(1);
  }
  if (!card.open_partition()) {
    putstring_nl("No partition!");
    error(2);
  }
  if (!card.open_filesys()) {
    putstring_nl("Can't open filesys");
    error(3);
  }
  if (!card.open_dir("/")) {
    putstring_nl("Can't open /");
    error(4);
  }

  strcpy(buffer, "TEST.TXT");
  for (buffer[6] = '0'; buffer[6] <= '9'; buffer[6]++) {
    for (buffer[7] = '0'; buffer[7] <= '9'; buffer[7]++) {
      //putstring("\ntrying to open ");
      //Serial.println(buffer);
      f = card.open_file(buffer);
      if (!f)
        break;
      card.close_file(f);
    }
    if (!f)
      break;
  }
 
  if(!card.create_file(buffer)) {
    putstring("couldnt create ");
    Serial.println(buffer);
    error(5);
  }
  f = card.open_file(buffer);
  if (!f) {
    putstring("error opening ");
    Serial.println(buffer);
    card.close_file(f);
    error(6);
  }
  putstring("writing to ");
  Serial.println(buffer);
  putstring_nl("ready!");

  strcpy(buffer, "THIS IS A TEST");
  if(card.write_file(f, (uint8_t *) buffer2, BUFFSIZE) != BUFFSIZE) {
         putstring_nl("can't write!");
  }


  //read Test
  j = 75;
  card.read_file(f,(unsigned char*) buffer2, j);
 // Serial.println("------------------");
 
 
  card.close_file(f); 
}


// blink out an error code
void error(uint8_t errno) {
  while(1) {
    for (i=0; i<errno; i++) {
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, HIGH);
      delay(100);
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, LOW);
      delay(100);
    }
    for (; i<10; i++) {
      delay(200);
    }
  }
}

void loop()
{

 
 
}



When I compile I get:
o: In function `setup':
C:\Users\admin\AppData\Local\Temp\build29013.tmp/Temporary_9219_1729.cpp:82: undefined reference to `AF_SDLog::read_file(fat16_file_struct*, unsigned char*, unsigned int)'


Couldn't determine program size: C:\Temp\arduino-0015-win\arduino-0015\hardware/tools/avr/bin/avr-size: 'C:\Users\admin\AppData\Local\Temp\build29013.tmp\BasicLogger2.hex': No such file



Any idea's what I can do to resolve this? I don't understand why it things the signatures do not match.


Thanks
dcrackel
 
Posts: 3
Joined: Sun May 10, 2009 6:10 pm

Re: Logger Read Question

by ericzundel on Sat Sep 12, 2009 8:47 pm

did you try casting your third argument (j) to uint16_t? right now it is a uint8_t.

card.read_file(f,(unsigned char*) buffer2, (uint16_t) j);

If that's not it, you might need to make sure you are actually importing the right library. I know when I modified a library once, I had a second copy in the libraries directory with a different directory name and was importing the unmodified version accidentally.
ericzundel
 
Posts: 38
Joined: Thu Aug 06, 2009 10:42 pm

Please be positive and constructive with your questions and comments.