saveloadPreset

save and load table to sdcard
Author: Johannes Taelman edit by Remco van der Most
License: BSD
Github: sss/table/saveloadPreset.axo

Inlets

bool32.rising trigger

bool32.rising trigger

charptr32 filename

Outlets

None

Attributes

objref table

Declaration
int ntrig;
int ltrig;
char c[64];
int offset;
int pval;
Init
ntrig = 0;
ltrig = 0;
strcpy(&c[0], "attr_prefix000attr_suffix");
offset = strlen("attr_prefix");
pval = 0;
Control Rate
if ((inlet_save > 0) && !ntrig) {
  ntrig = 1;
  FIL FileObject;
  FRESULT err;
  UINT bytes_written;
  err = f_open(&FileObject, inlet_filename, FA_WRITE | FA_CREATE_ALWAYS);
  if (err != FR_OK) {
    report_fatfs_error(err, "inlet_filename");
    return;
  }
  int rem_sz = sizeof(*attr_table.array) * attr_table.LENGTH;
  int offset = 0;
  while (rem_sz > 0) {
    if (rem_sz > sizeof(fbuff)) {
      memcpy((char *)fbuff, (char *)(&attr_table.array[0]) + offset,
             sizeof(fbuff));
      err = f_write(&FileObject, fbuff, sizeof(fbuff), &bytes_written);
      rem_sz -= sizeof(fbuff);
      offset += sizeof(fbuff);
    } else {
      memcpy((char *)fbuff, (char *)(&attr_table.array[0]) + offset, rem_sz);
      err = f_write(&FileObject, fbuff, rem_sz, &bytes_written);
      rem_sz = 0;
    }
  }
  if (err != FR_OK)
    report_fatfs_error(err, "inlet_filename");
  err = f_close(&FileObject);
  if (err != FR_OK)
    report_fatfs_error(err, "inlet_filename");
} else if (!(inlet_save > 0))
  ntrig = 0;

if ((inlet_load > 0) && !ltrig) {
  ltrig = 1;
  FIL FileObject;
  FRESULT err;
  UINT bytes_read;
  err = f_open(&FileObject, inlet_filename, FA_READ | FA_OPEN_EXISTING);
  if (err != FR_OK) {
    report_fatfs_error(err, inlet_filename);
    return;
  }
  int rem_sz = sizeof(*attr_table.array) * attr_table.LENGTH;
  int offset = 0;
  while (rem_sz > 0) {
    if (rem_sz > sizeof(fbuff)) {
      err = f_read(&FileObject, fbuff, sizeof(fbuff), &bytes_read);
      if (bytes_read == 0)
        break;
      memcpy((char *)(&attr_table.array[0]) + offset, (char *)fbuff,
             bytes_read);
      rem_sz -= bytes_read;
      offset += bytes_read;
    } else {
      err = f_read(&FileObject, fbuff, rem_sz, &bytes_read);
      memcpy((char *)(&attr_table.array[0]) + offset, (char *)fbuff,
             bytes_read);
      rem_sz = 0;
    }
  }
  if (err != FR_OK) {
    report_fatfs_error(err, inlet_filename);
    return;
  };
  err = f_close(&FileObject);
  if (err != FR_OK) {
    report_fatfs_error(err, inlet_filename);
    return;
  };
} else if (!(inlet_load > 0))
  ltrig = 0;

Privacy

© 2024 Zrna Research