Ctrl4_4051

control object for 4 4051 include 'smart' filter select GPIOs S0 low S1 S2 high Input GPIOs Z0 from first 4051 Z1 Z2 Z3 from last 4051
Author: Smashed Transistors
License: LGPL
Github: tiar/HW/Ctrl4_4051.axo

Inlets

None

Outlets

frac32.positive o00

frac32.positive o01

frac32.positive o02

frac32.positive o03

frac32.positive o04

frac32.positive o05

frac32.positive o06

frac32.positive o07

frac32.positive o08

frac32.positive o09

frac32.positive o10

frac32.positive o11

frac32.positive o12

frac32.positive o13

frac32.positive o14

frac32.positive o15

frac32.positive o16

frac32.positive o17

frac32.positive o18

frac32.positive o19

frac32.positive o20

frac32.positive o21

frac32.positive o22

frac32.positive o23

frac32.positive o24

frac32.positive o25

frac32.positive o26

frac32.positive o27

frac32.positive o28

frac32.positive o29

frac32.positive o30

frac32.positive o31

Attributes

combo Z0

combo Z1

combo Z2

combo Z3

combo S0

combo S1

combo S2

Declaration
int32_t a_inF0[32], a_inF1[32], a_inF2[32], a_inF3[32];

uint8_t multiplex_cpt;
uint8_t kcycle2_cpt, kcycle32_cpt;
Init
multiplex_cpt = kcycle2_cpt = kcycle32_cpt = 0;

for (int i = 0; i < 32; i++) {
  a_inF3[i] = a_inF2[i] = a_inF1[i] = a_inF0[i] = 0;
}

// init gpios
// outputs for the 4051 select lines S0 S1 S2
palSetPadMode(attr_S0, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(attr_S1, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(attr_S2, PAL_MODE_OUTPUT_PUSHPULL);
Control Rate
// the inputs are sampled 1/2 kcycle so that the multiplexed signals (4051) are
// stable
kcycle2_cpt++;
if (kcycle2_cpt == 2) {
  kcycle2_cpt = 0;

  int32_t *a_in = a_inF0 + multiplex_cpt;
  // LP with 0.25 coefficient
  *a_in = ___SMMLA(0x40000000, (adcvalues[attr_Z0] << 15) - *a_in, *a_in);
  a_in += 8;
  *a_in = ___SMMLA(0x40000000, (adcvalues[attr_Z1] << 15) - *a_in, *a_in);
  a_in += 8;
  *a_in = ___SMMLA(0x40000000, (adcvalues[attr_Z2] << 15) - *a_in, *a_in);
  a_in += 8;
  *a_in = ___SMMLA(0x40000000, (adcvalues[attr_Z3] << 15) - *a_in, *a_in);

  // prepare next
  multiplex_cpt++;
  multiplex_cpt &= 7;
}
// set gpio outputs
palWritePad(attr_S0, (multiplex_cpt & 1) != 0);
palWritePad(attr_S1, (multiplex_cpt & 2) != 0);
palWritePad(attr_S2, (multiplex_cpt & 4) != 0);

// Repartition des filtrages 'intelligent' des 32 entrees analogiques
{
  kcycle32_cpt++;
  if (kcycle32_cpt == 32) {
    kcycle32_cpt = 0;
  }
  int i = kcycle32_cpt;

  // extends to  [0 64[ even if there is some noise
  a_inF0[i] = __USAT(___SMMLA(a_inF0[i], 0x40800000, -1 << 17) << 2, 27);

  if (abs(a_inF0[i] - a_inF1[i]) > (1 << 21)) { // jump
    a_inF1[i] = a_inF0[i];
  } else { // fast denoise
    a_inF1[i] = ___SMMLA(0x40000000, a_inF0[i] - a_inF1[i], a_inF1[i]);
  }

  int32_t dist = abs(a_inF1[i] - a_inF2[i]);
  if (dist > (1 << 20)) { // jump
    a_inF2[i] = a_inF1[i];
  } else {
    // the closer, the smoother
    int32_t coef;
    if (dist > (1 << 19))
      coef = 0x2A000000;
    else
      coef = (dist << 10) + 0x0A000000;

    a_inF2[i] = ___SMMLA(coef, a_inF1[i] - a_inF2[i], a_inF2[i]);
  }

  dist = abs(a_inF2[i] - a_inF3[i]);
  if (dist > (1 << 18)) { // jump
    a_inF3[i] = a_inF2[i];
  } else {
    // the closer, the smoother
    int32_t coef;
    if (dist > (1 << 19))
      coef = 0x20800000;
    else
      coef = (dist << 10) + 0x00800000;
    a_inF3[i] = ___SMMLA(coef, a_inF2[i] - a_inF3[i], a_inF3[i]);
  }
}

// set outlets
outlet_o00 = a_inF3[0];
outlet_o01 = a_inF3[1];
outlet_o02 = a_inF3[2];
outlet_o03 = a_inF3[3];
outlet_o04 = a_inF3[4];
outlet_o05 = a_inF3[5];
outlet_o06 = a_inF3[6];
outlet_o07 = a_inF3[7];
outlet_o08 = a_inF3[8];
outlet_o09 = a_inF3[9];
outlet_o10 = a_inF3[10];
outlet_o11 = a_inF3[11];
outlet_o12 = a_inF3[12];
outlet_o13 = a_inF3[13];
outlet_o14 = a_inF3[14];
outlet_o15 = a_inF3[15];
outlet_o16 = a_inF3[16];
outlet_o17 = a_inF3[17];
outlet_o18 = a_inF3[18];
outlet_o19 = a_inF3[19];
outlet_o20 = a_inF3[20];
outlet_o21 = a_inF3[21];
outlet_o22 = a_inF3[22];
outlet_o23 = a_inF3[23];
outlet_o24 = a_inF3[24];
outlet_o25 = a_inF3[25];
outlet_o26 = a_inF3[26];
outlet_o27 = a_inF3[27];
outlet_o28 = a_inF3[28];
outlet_o29 = a_inF3[29];
outlet_o30 = a_inF3[30];
outlet_o31 = a_inF3[31];

Privacy

© 2024 Zrna Research