findFree

Indexed filename string combiner with some extra's: -checks the SDcard how many tables are saved using the same prefix (only as long as indices follow up, counter stops when it finds an unused index number). -seperate filename outputs for selected filename (for loading) and free filename (for saving) -wraps the index input so it can only select from the available indices. -3 extra load inputs: first index (normally index 000) last index (free index minus one, eg. index 011 if index 012 is empty, in other words, last one that you've saved) random index (randomly selects an index number from the available indices)
Author: Remco van der Most
License: BSD
Github: sss/string/findFree.axo

Inlets

bool32.rising trigger

int32 index

bool32 first

bool32 last

bool32 rnd

Outlets

bool32 autoload

charptr32 selected

charptr32 free

Attributes

table prefix

table suffix

Displays

int32.label free

int32.label sel

Declaration
int ntrig, stop;
int cnt = 0;
char c[64], C[64];
int offset;
int pval, first, I;
Init
ntrig = 0;
strcpy(&c[0], "attr_prefix000attr_suffix");
offset = strlen("attr_prefix");
strcpy(&C[0], "attr_prefix000attr_suffix");

pval = 0;
cnt = 0;
stop = 0;
Control Rate
if (inlet_findFree && !ntrig) {
  stop = 0;
  ntrig = 1;
} else if ((!inlet_findFree) && ntrig) {
  ntrig = 0;
}

while (!stop) {
  int i = cnt;
  int i0 = i / 10;
  C[offset + 2] = '0' + i - 10 * i0;
  i = i0;
  i0 = i / 10;
  C[offset + 1] = '0' + i - 10 * i0;
  i = i0;
  i0 = i / 10;
  C[offset + 0] = '0' + i - 10 * i0;
  FIL FileObject;
  FRESULT err;
  err = f_open(&FileObject, &C[0], FA_READ | FA_OPEN_EXISTING);
  if (err == FR_OK) {
    cnt += 1;
  } else if (err != FR_OK) {
    stop = 1;
  }
}
outlet_autoload = 0;

if ((inlet_index) != pval) {
  pval = inlet_index;
  int i = inlet_index % (cnt > 0 ? cnt : 1);
  I = i;
  int i0 = i / 10;
  c[offset + 2] = '0' + i - 10 * i0;
  i = i0;
  i0 = i / 10;
  c[offset + 1] = '0' + i - 10 * i0;
  i = i0;
  i0 = i / 10;
  c[offset + 0] = '0' + i - 10 * i0;
  outlet_autoload = 1;
}
int trg = inlet_first || inlet_last || inlet_rnd;
if (trg && !first) {
  first = 1;
  int i = 0;
  if (inlet_last) {
    i = cnt - 1;
  } else if (inlet_rnd) {
    i = (int32_t)((GenerateRandomNumber() >> 6) % cnt);
  }

  I = i;
  int i0 = i / 10;
  c[offset + 2] = '0' + i - 10 * i0;
  i = i0;
  i0 = i / 10;
  c[offset + 1] = '0' + i - 10 * i0;
  i = i0;
  i0 = i / 10;
  c[offset + 0] = '0' + i - 10 * i0;
  outlet_autoload = 1;
} else if (!trg) {
  first = 0;
}

outlet_selected = &c[0];
outlet_free = &C[0];

disp_free = cnt;
disp_sel = I;

Privacy

© 2024 Zrna Research