button_pot

enable serial2( PA2/PA3 = SD2, baudrate: 115200) as extra midi i/o to use with euxoloti. Pot, Button values are transmitted by midi and finally routed to object outlets.
Author: Paul
License: BSD
Github: euxo/1 2/midi/buttonpot.axo

Inlets

None

Outlets

bool32 button1

bool32 button2

bool32 button3

bool32 button4

frac32.positive pot1

frac32.positive pot2

frac32.positive pot3

frac32.positive pot4

frac32.positive pot5

frac32.positive pot6

frac32.positive pot7

frac32.positive pot8

Declaration
int32_t pot1, pot2, pot3, pot4, pot5, pot6, pot7, pot8;
int32_t button1;
int32_t button2;
int32_t button3;
int32_t button4;

unsigned char MidiByte0;
unsigned char MidiByte1;
unsigned char MidiByte2;
unsigned char MidiCurData;
unsigned char MidiNumData;

uint8_t StatusToMsgLength(uint8_t t) {
  switch (t) {
  case 0x0:
  case 0x1:
  case 0x2:
  case 0x3:
  case 0x4:
  case 0x5:
  case 0x6:
  case 0x7:
    return 0;
  case 0x8:
  case 0x9:
  case 0xa:
  case 0xb:
    return 3;
  case 0xc:
  case 0xd:
    return 2;
  case 0xe:
    return 3;
  default:
    return -1;
  }
}

void MidiInByteHandler(uint8_t data) {
  int8_t len;
  if (data & 0x80) {
    len = StatusToMsgLength(data >> 4);

    MidiByte0 = data;
    MidiNumData = len - 1;
    MidiCurData = 0;

  } else // not a status byte
  {
    if (MidiCurData == 0) {
      MidiByte1 = data;
      MidiCurData++;
    } else if (MidiCurData == 1) {
      MidiByte2 = data;
      if (MidiNumData == 2) {

        if (MidiByte0 == 0xb0) {

          switch (MidiByte1) {

          case 1:
            pot1 = (MidiByte2 << 20);
            break;
          case 2:
            pot2 = (MidiByte2 << 20);
            break;
          case 3:
            pot3 = (MidiByte2 << 20);
            break;
          case 4:
            pot4 = (MidiByte2 << 20);
            break;
          case 5:
            pot5 = (MidiByte2 << 20);
            break;
          case 6:
            pot6 = (MidiByte2 << 20);
            break;
          case 7:
            pot7 = (MidiByte2 << 20);
            break;
          case 8:
            pot8 = (MidiByte2 << 20);
            break;
          }
        }

        else if (MidiByte0 == 0x90 || MidiByte0 == 0x80) {
          switch (MidiByte1) {

          case 0:
            button1 = (MidiByte2 << 20);
            break;
          case 1:
            button2 = (MidiByte2 << 20);
            break;
          case 2:
            button3 = (MidiByte2 << 20);
            break;
          case 3:
            button4 = (MidiByte2 << 20);
            break;
          }
        }

        MidiCurData = 0;
      }
    }
  }
}

msg_t ThreadX2() {
#if CH_USE_REGISTRY
  chRegSetThreadName("euxo button pot"); // define thread name
#endif

  MidiNumData = 0;
  MidiCurData = 0;

  sdPut(&SD2, 0xFF);

  while (!chThdShouldTerminate()) {
    while (!sdGetWouldBlock(&SD2)) {

      char ch = sdGet(&SD2);
      MidiInByteHandler(ch);
    }
    chThdSleepMilliseconds(1);
  }
  chThdExit((msg_t)0);
}

static msg_t EuxoButPot(void *arg) { ((attr_parent *)arg)->ThreadX2(); }
WORKING_AREA(waEuxoButPot, 256);
Thread *Thd;
Init
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7) | PAL_MODE_INPUT); // RX
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));                  // TX

static const SerialConfig sd2Cfg = {
    115200, 0, 0,
    0}; // set to midi baud rate but works also with higher baud rates.
sdStart(&SD2, &sd2Cfg);

Thd = chThdCreateStatic(waEuxoButPot, sizeof(waEuxoButPot), NORMALPRIO,
                        EuxoButPot, (void *)this);
Dispose
chThdTerminate(Thd);
chThdWait(Thd);
sdStop(&SD2);

Privacy

© 2024 Zrna Research