Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XDK documentation for foreign functions not complete #379

Open
john-robert opened this issue May 18, 2021 · 1 comment
Open

XDK documentation for foreign functions not complete #379

john-robert opened this issue May 18, 2021 · 1 comment

Comments

@john-robert
Copy link

john-robert commented May 18, 2021

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
Copy link

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants