SAMFD with WebUI?

I’ve been playing around with iPlug2 and the WebUI and I love it so far!

I’ve been able to send messages from the UI to the module using SAMFUI, etc. However, I’m trying to wrap my head around receiving messages and can’t find any documentation or examples.

Here’s what I’ve tried so far:

// ts

export const Elem = () => {
  useEffect(() => {
    window.SAMFD = (msgTag: number, dataSize: number, msg: Array<number>) => {
      //  var decodedData = window.atob(msg);
      console.log("SAMFD msgTag:" + msgTag + " msg:" + msg);
    }
  }, [])

  return (
    <Button
      onClick={() => {
        if (NODE_ENV === 'production') {
          SAMFUI(2, -1, '');
        }
      }}
    >
      Ping
    </Button>
  )
}

// MyModule.cpp

const uint8_t pData = 7;
const uint8_t* pDataPtr = &pData;

bool MyModule::OnMessage(int msgTag, int ctrlTag, int dataSize, const void* pData)
{
  DBGMSG("have mesage; tag %d\n", msgTag);
  switch (msgTag) {
  case 2:
  {
    MyModule::SendArbitraryMsgFromDelegate(0, 8, pDataPtr);
  }
  default:
  {
    DBGMSG("unknown mesage tag %d\n", msgTag);
  }
  }

  return false;
}

However, I get a heap corruption error. What have I done wrong? Thanks!

Are you trying to send a message back to the UI when you receive msgTag with ID2?

The data pointed to by pDataPtr is 1 char / 1byte not 8 bytes, so the 2nd argument to SendArbitraryMsgFromDelegate() is wrong. That might be what’s crashing it

1 Like

Yup!

Hmmm… thanks for the suggestion. I changed the second argument to 1 and the crash persists. I also tried sizeof.

Baby steps…

If I delete this line, I no longer get the heap error. However, the only thing I can seem to send is NULL.

I thought str.c_string() and str.length() would have worked for pData and dataSize, respectively, but nothing got logged in the ui.

Found the bug!

Thanks, i’ll try it soon

1 Like