sstation saveload

save and load table to sdcard
Author: Johannes Taelman edit by Remco van der Most
License: BSD
Github: rbrt/testing/sstation saveload.axo

Inlets

bool32.rising trigger

bool32.rising trigger

charptr32 fn1

charptr32 fn2

charptr32 fn3

charptr32 fn4

charptr32 fn5

charptr32 fn6

Outlets

bool32 done

int32 test

Attributes

objref smplr

Declaration
char c[64];
int offset;
int pval;
uint8_t index;
bool save;
bool load;
bool busy;
bool trig;
int rem_sz;
char *t_adr;

char fn[32];
Init
strcpy(&c[0], "attr_prefix000attr_suffix");
offset = strlen("attr_prefix");
pval = 0;
Control Rate
outlet_done = 0;

if (inlet_save && !trig) {
  save = 1;
  index = 0;
  busy = 1;
  trig = 1;
}
if (!inlet_save && !busy)
  trig = 0;

if (inlet_load && !trig) {
  load = 1;
  index = 0;
  busy = 1;
  trig = 1;
}
if (!inlet_load && !busy)
  trig = 0;

if (busy) {
  switch (index ? index : 0) {
  case 0: {
    rem_sz = sizeof(*attr_smplr.GLOBAL) * 8;
    t_adr = (char *)(&attr_smplr.GLOBAL[0]);
    // set RECPOS
    attr_smplr.GLOBAL[0] = attr_smplr.RECPOS;
    attr_smplr.GLOBAL[1] = attr_smplr.delcount;
    memcpy(&fn[0], inlet_fn1, strlen(inlet_fn1));
    break;
  }
  case 1: {
    // get RECPOS
    attr_smplr.RECPOS = attr_smplr.GLOBAL[0];
    attr_smplr.delcount = attr_smplr.GLOBAL[1];
    rem_sz = sizeof(*attr_smplr.array) * attr_smplr.LENGTH;
    t_adr = (char *)(&attr_smplr.array[0]);
    memcpy(&fn[0], inlet_fn2, strlen(inlet_fn2));
    break;
  }
  case 2: {
    rem_sz = sizeof(*attr_smplr.WAVE) * attr_smplr.RECPOS;
    t_adr = (char *)(&attr_smplr.WAVE[0]);
    memcpy(&fn[0], inlet_fn3, strlen(inlet_fn3));
    break;
  }
  }
}

if (save) {
  FIL FileObject;
  FRESULT err;
  UINT bytes_written;
  err = f_open(&FileObject, fn, FA_WRITE | FA_CREATE_ALWAYS);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    save = 0;
    busy = 0;
    goto ERROR;
  }
  int offset = 0;
  while (rem_sz > 0) {
    if (rem_sz > sizeof(fbuff)) {
      memcpy((char *)fbuff, t_adr + offset, sizeof(fbuff));
      err = f_write(&FileObject, fbuff, sizeof(fbuff), &bytes_written);
      rem_sz -= sizeof(fbuff);
      offset += sizeof(fbuff);
    } else {
      memcpy((char *)fbuff, t_adr + offset, rem_sz);
      err = f_write(&FileObject, fbuff, rem_sz, &bytes_written);
      rem_sz = 0;
    }
  }
  if (err != FR_OK)
    report_fatfs_error(err, fn);
  err = f_close(&FileObject);
  if (err != FR_OK)
    report_fatfs_error(err, fn);
  index++;
  if (index == 3) {
    save = 0;
    busy = 0;
    outlet_done = 1;
  }
}

if (load) {
  FIL FileObject;
  FRESULT err;
  UINT bytes_read;
  err = f_open(&FileObject, fn, FA_READ | FA_OPEN_EXISTING);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    load = 0;
    busy = 0;
    goto ERROR;
  }
  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(t_adr + offset, (char *)fbuff, bytes_read);
      rem_sz -= bytes_read;
      offset += bytes_read;
    } else {
      err = f_read(&FileObject, fbuff, rem_sz, &bytes_read);
      memcpy(t_adr + offset, (char *)fbuff, bytes_read);
      rem_sz = 0;
    }
  }
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  };
  err = f_close(&FileObject);
  if (err != FR_OK) {
    report_fatfs_error(err, fn);
    return;
  };
  index++;
  if (index == 3) {
    load = 0;
    busy = 0;
    outlet_done = 1;
  }
}

ERROR : outlet_test = sizeof(*attr_smplr.array);

Privacy

© 2024 Zrna Research