eclipse / mita

mita
Eclipse Public License 2.0
56 stars 20 forks source link

XDK documentation for foreign functions not complete #379

Open john-robert opened 3 years ago

john-robert commented 3 years ago

Problem: Documentation under https://www.eclipse.org/mita/language/foreignfunctioninterface/#custom-c-file-include is not complete and partly broken. Especially in sections "Custom C file include" and "Pitfalls" crucial information are missing as to what these sections try to teach.

Specfically: It seems diffuclt to understand how one is supposed to write a C-wrapper to include a Mita produced structure into one's own C-code. I was hoping the documentation could be updated or someone may share a code that allows me to study what I want to achieve.

What I want to achieve: Collect 100 data points of one seismometer axis and calculate the mean value. The mean calculation shall be done in my own C-code that either takes a normal array or the structure array_float as produced by Mita.

Thank you for helping a self-doubting Python programmer.

plneappl commented 3 years ago

When you declare a array of floats in Mita, the compiler will generate this structure definition in MitaGeneratedTypes.h:

typedef struct {
    float* data;
    uint32_t length;
} array_float;

So if you want to pass that array to a C function, the function needs to accept that structure, for example like this:

#include "MitaGeneratedTypes.h"
float avg(array_float a) {
  float sum = 0;
  for(int i = 0; i < a.length; i++) {
    sum += a.data[i];
  }
  return sum/a.length;
}

Then you can call that function from Mita:

native unchecked fn avg(a: array<float>): float header "avg.h";

fn calculateAvg() {
  var samples = new array<float>(100);
  var x = avg(samples);
}

Hope this helps :)