pianoMemory

save/load up to 8 tables to/from sdcard in a row Meant to be used with the pianoBar and pianoDecoder modules, but could also be used for other applications for saving multiple arrays at once Tables that aren't used still need an entry, so just copy one of the module-names that you've used for the active slots. Set the amount of modules/tables with the "chained" attribute.
Author: Remco van der Most
License: BSD
Github: sss/seq/pianoMemory.axo

Inlets

int32 index

bool32.rising trigger

bool32.rising load

Outlets

None

Attributes

objref table1

objref table2

objref table3

objref table4

objref table5

objref table6

objref table7

objref table8

table prefix

table suffix

spinner chained

Declaration
int save, load;
int i, pval;
char c[64];
int ofs;
Init
load = save = 0;
for (i = 0; i < attr_chained; i++) {
  strcpy(&c[0], "attr_prefix0000attr_suffix");
}
ofs = strlen("attr_prefix");
pval = 0;
Control Rate
if ((inlet_load > 0) && !load) {
  load = 1;
  for (i = 0; i < attr_chained; i++) {
    int j = (inlet_index * attr_chained) + i;
    int j0 = j / 10;
    c[ofs + 3] = '0' + j - 10 * j0;
    j = j0;
    j0 = j / 10;
    c[ofs + 2] = '0' + j - 10 * j0;
    j = j0;
    j0 = j / 10;
    c[ofs + 1] = '0' + j - 10 * j0;
    j = j0;
    j0 = j / 10;
    c[ofs + 0] = '0' + j - 10 * j0;
    FIL FileObject;
    FRESULT err;
    UINT bytes_read;
    codec_clearbuffer();
    err = f_open(&FileObject, &c[0], FA_READ | FA_OPEN_EXISTING);
    if (err != FR_OK) {
      report_fatfs_error(err, &c[0]);
      return;
    }
    int rem_sz;
    switch (i) {
    case 0:
      rem_sz = sizeof(*attr_table1.array) * attr_table1.LENGTH;
      break;
    case 1:
      rem_sz = sizeof(*attr_table2.array) * attr_table2.LENGTH;
      break;
    case 2:
      rem_sz = sizeof(*attr_table3.array) * attr_table3.LENGTH;
      break;
    case 3:
      rem_sz = sizeof(*attr_table4.array) * attr_table4.LENGTH;
      break;
    case 4:
      rem_sz = sizeof(*attr_table5.array) * attr_table5.LENGTH;
      break;
    case 5:
      rem_sz = sizeof(*attr_table6.array) * attr_table6.LENGTH;
      break;
    case 6:
      rem_sz = sizeof(*attr_table7.array) * attr_table7.LENGTH;
      break;
    case 7:
      rem_sz = sizeof(*attr_table8.array) * attr_table8.LENGTH;
      break;
    }
    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;
        switch (i) {
        case 0:
          memcpy((char *)(&attr_table1.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 1:
          memcpy((char *)(&attr_table2.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 2:
          memcpy((char *)(&attr_table3.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 3:
          memcpy((char *)(&attr_table4.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 4:
          memcpy((char *)(&attr_table5.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 5:
          memcpy((char *)(&attr_table6.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 6:
          memcpy((char *)(&attr_table7.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 7:
          memcpy((char *)(&attr_table8.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        }
        rem_sz -= bytes_read;
        offset += bytes_read;
      } else {
        err = f_read(&FileObject, fbuff, rem_sz, &bytes_read);
        switch (i) {
        case 0:
          memcpy((char *)(&attr_table1.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 1:
          memcpy((char *)(&attr_table2.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 2:
          memcpy((char *)(&attr_table3.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 3:
          memcpy((char *)(&attr_table4.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 4:
          memcpy((char *)(&attr_table5.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 5:
          memcpy((char *)(&attr_table6.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 6:
          memcpy((char *)(&attr_table7.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        case 7:
          memcpy((char *)(&attr_table8.array[0]) + offset, (char *)fbuff,
                 bytes_read);
          break;
        }
        rem_sz = 0;
      }
    }
    if (err != FR_OK) {
      report_fatfs_error(err, &c[0]);
      return;
    };
    err = f_close(&FileObject);
    if (err != FR_OK) {
      report_fatfs_error(err, &c[0]);
      return;
    };
  }
} else if (!(inlet_load > 0))
  load = 0;

if ((inlet_save > 0) && !save) {
  save = 1;
  for (i = 0; i < attr_chained; i++) {
    int j = (inlet_index * attr_chained) + i;
    int j0 = j / 10;
    c[ofs + 3] = '0' + j - 10 * j0;
    j = j0;
    j0 = j / 10;
    c[ofs + 2] = '0' + j - 10 * j0;
    j = j0;
    j0 = j / 10;
    c[ofs + 1] = '0' + j - 10 * j0;
    j = j0;
    j0 = j / 10;
    c[ofs + 0] = '0' + j - 10 * j0;
    FIL FileObject;
    FRESULT err;
    UINT bytes_written;
    codec_clearbuffer();
    err = f_open(&FileObject, &c[0], FA_WRITE | FA_CREATE_ALWAYS);
    if (err != FR_OK) {
      report_fatfs_error(err, &c[0]);
      return;
    }
    int rem_sz;
    switch (i) {
    case 0:
      rem_sz = sizeof(*attr_table1.array) * attr_table1.LENGTH;
      break;
    case 1:
      rem_sz = sizeof(*attr_table2.array) * attr_table2.LENGTH;
      break;
    case 2:
      rem_sz = sizeof(*attr_table3.array) * attr_table3.LENGTH;
      break;
    case 3:
      rem_sz = sizeof(*attr_table4.array) * attr_table4.LENGTH;
      break;
    case 4:
      rem_sz = sizeof(*attr_table5.array) * attr_table5.LENGTH;
      break;
    case 5:
      rem_sz = sizeof(*attr_table6.array) * attr_table6.LENGTH;
      break;
    case 6:
      rem_sz = sizeof(*attr_table7.array) * attr_table7.LENGTH;
      break;
    case 7:
      rem_sz = sizeof(*attr_table8.array) * attr_table8.LENGTH;
      break;
    }
    int offset = 0;
    while (rem_sz > 0) {
      if (rem_sz > sizeof(fbuff)) {
        switch (i) {
        case 0:
          memcpy((char *)fbuff, (char *)(&attr_table1.array[0]) + offset,
                 sizeof(fbuff));
          break;
        case 1:
          memcpy((char *)fbuff, (char *)(&attr_table2.array[0]) + offset,
                 sizeof(fbuff));
          break;
        case 2:
          memcpy((char *)fbuff, (char *)(&attr_table3.array[0]) + offset,
                 sizeof(fbuff));
          break;
        case 3:
          memcpy((char *)fbuff, (char *)(&attr_table4.array[0]) + offset,
                 sizeof(fbuff));
          break;
        case 4:
          memcpy((char *)fbuff, (char *)(&attr_table5.array[0]) + offset,
                 sizeof(fbuff));
          break;
        case 5:
          memcpy((char *)fbuff, (char *)(&attr_table6.array[0]) + offset,
                 sizeof(fbuff));
          break;
        case 6:
          memcpy((char *)fbuff, (char *)(&attr_table7.array[0]) + offset,
                 sizeof(fbuff));
          break;
        case 7:
          memcpy((char *)fbuff, (char *)(&attr_table8.array[0]) + offset,
                 sizeof(fbuff));
          break;
        }
        err = f_write(&FileObject, fbuff, sizeof(fbuff), &bytes_written);
        rem_sz -= sizeof(fbuff);
        offset += sizeof(fbuff);
      } else {
        switch (i) {
        case 0:
          memcpy((char *)fbuff, (char *)(&attr_table1.array[0]) + offset,
                 rem_sz);
          break;
        case 1:
          memcpy((char *)fbuff, (char *)(&attr_table2.array[0]) + offset,
                 rem_sz);
          break;
        case 2:
          memcpy((char *)fbuff, (char *)(&attr_table3.array[0]) + offset,
                 rem_sz);
          break;
        case 3:
          memcpy((char *)fbuff, (char *)(&attr_table4.array[0]) + offset,
                 rem_sz);
          break;
        case 4:
          memcpy((char *)fbuff, (char *)(&attr_table5.array[0]) + offset,
                 rem_sz);
          break;
        case 5:
          memcpy((char *)fbuff, (char *)(&attr_table6.array[0]) + offset,
                 rem_sz);
          break;
        case 6:
          memcpy((char *)fbuff, (char *)(&attr_table7.array[0]) + offset,
                 rem_sz);
          break;
        case 7:
          memcpy((char *)fbuff, (char *)(&attr_table8.array[0]) + offset,
                 rem_sz);
          break;
        }
        err = f_write(&FileObject, fbuff, rem_sz, &bytes_written);
        rem_sz = 0;
      }
    }
    if (err != FR_OK)
      report_fatfs_error(err, &c[0]);
    err = f_close(&FileObject);
    if (err != FR_OK)
      report_fatfs_error(err, &c[0]);
  }
} else if (!(inlet_save > 0))
  save = 0;

Privacy

© 2024 Zrna Research