SDapi

Author:
License: GPL
Github: rbrt/testing/SDapi.axo

Inlets

None

Outlets

None

Declaration
FILINFO fno;
DIR dir;
FIL FileObject;
FRESULT err;
UINT bytes_written;
UINT bytes_read;

char fname_flt[16];
char fname_raw[16];

// load file from SD:: fload (filename,destination table adress,start
// index,length)
void fload(const char *fn, char *table, int start, int bytes_rem)
    __attribute__((noinline)) {
  err = f_open(&FileObject, fn, FA_READ | FA_OPEN_EXISTING);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  int offset = 0;
  while (bytes_rem > 0) {
    if (bytes_rem > sizeof(fbuff)) {
      err = f_read(&FileObject, fbuff, sizeof(fbuff), &bytes_read);
      if (bytes_read == 0)
        break;
      memcpy(table + offset + start, (char *)fbuff, bytes_read);
      bytes_rem -= bytes_read;
      offset += bytes_read;
    } else {
      err = f_read(&FileObject, fbuff, bytes_rem, &bytes_read);
      memcpy(table + offset + start, (char *)fbuff, bytes_read);
      bytes_rem = 0;
    }
  }
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  err = f_close(&FileObject);
}

// save array to SD::fsave (filename,source table adress,start index,length)
void fsave(const char *fn, char *table, int start, int bytes_rem)
    __attribute__((noinline)) {
  err = f_open(&FileObject, fn, FA_WRITE | FA_CREATE_ALWAYS);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  int offset = 0;
  while (bytes_rem > 0) {
    if (bytes_rem > sizeof(fbuff)) {
      memcpy((char *)fbuff, table + offset + start, sizeof(fbuff));
      err = f_write(&FileObject, fbuff, sizeof(fbuff), &bytes_written);
      bytes_rem -= sizeof(fbuff);
      offset += sizeof(fbuff);
    } else {
      memcpy((char *)fbuff, table + offset + start, bytes_rem);
      err = f_write(&FileObject, fbuff, bytes_rem, &bytes_written);
      bytes_rem = 0;
    }
  }
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  }
  err = f_close(&FileObject);
}

// get file length in 8-bit bytes::fsize (filename)
int fsize(const char *fn) __attribute__((noinline)) {
  err = f_open(&FileObject, fn, FA_READ);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return 0;
  } else {
    return f_size(&FileObject);
    err = f_close(&FileObject);
  }
}

void getdir(const char *path, const char *filter, int start_index)
    __attribute__((noinline)) {
  memset(&fname_flt[0], 0, 16);
  memset(&fname_raw[0], 0, 16);
  int count = -1;
  int fltlen = strlen(filter);
  f_opendir(&dir, path);
  for (;;) {
    err = f_readdir(&dir, &fno);
    if (err != FR_OK || fno.fname[0] == 0)
      break;
    int filelen = strlen(fno.fname);
    if (!memcmp(&filter[0], &fno.fname[filelen - fltlen], fltlen) ||
        !strlen(filter)) {
      count++;
      if (count == start_index) {
        memcpy(&fname_flt[0], fno.fname, filelen - fltlen);
        memcpy(&fname_raw[0], fno.fname, filelen - fltlen);
      }
    }
  }
}

// rename a file
void frename(const char *old_n, const char *new_n) __attribute__((noinline)) {
  f_rename(old_n, new_n);
}

// delete a file
void fdelete(const char *del_f) __attribute__((noinline)) { f_unlink(del_f); }

Privacy

© 2024 Zrna Research