tserial 6

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

Inlets

bool32.rising trigger

bool32.rising trigger

charptr32 filename

Outlets

int32 test

Attributes

objref table

Declaration
int strig;
int ltrig;
int test;
bool BUSY;
bool save;
bool load;
char filename[16];
Control Rate
if (save) {
  save = 0;
  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");
  BUSY = 0;
}

if (load) {
  load = 0;
  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;
  };
  BUSY = 0;
}

if (inlet_save && !strig) {
  save = 1;
  BUSY = 1;
}
if (!inlet_save)
  strig = 0;
if (inlet_load && !ltrig) {
  load = 1;
  BUSY = 1;
}
if (!inlet_load)
  ltrig = 0;

outlet_test = BUSY;

Privacy

© 2024 Zrna Research