Using the C API?
Using the C API?
Could anyone provide me with a small working example of what's discussed here? https://www.kickstarter.com/projects/21 ... sts/679050
Just a simple "Hello World!" function in C that I can call from Python, and the opposite as well.
Thanks!
Just a simple "Hello World!" function in C that I can call from Python, and the opposite as well.
Thanks!
Re: Using the C API?
Here's a simple sample I put on a github branch:
https://github.com/dhylands/micropython ... 0d64ef7172
Here's the output from running it:
And all of the peripherals are essentially similar. Look at things like stmhal/modpyb.c, or stmhal/timer.c (which has an example of calling python from C code).
https://github.com/dhylands/micropython ... 0d64ef7172
Here's the output from running it:
Code: Select all
>>> import c_sample
>>> def foo():
... print('foo called')
...
>>> c_sample.set_callback(foo)
>>> c_sample.call_callback()
foo called
Re: Using the C API?
I tried to base on your code to make a small function but I just can't seem to make it work.
I redefined your C function as:
and the Python session looks like:
What am I doing wrong?
I redefined your C function as:
Code: Select all
STATIC mp_obj_t c_sample_call_callback(void) {
vstr_t vstr;
vstr_init_fixed_buf(&vstr, 32, "some_string");
mp_obj_t obj = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
return mp_call_function_1(MP_STATE_PORT(c_sample_callback_obj),obj);
}
Code: Select all
MicroPython 84405ef-dirty on 2016-02-29; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> def a(b):
... print(b)
...
...
...
>>> import c_sample
>>> c_sample.set_callback(a)
>>> c_sample.call_callback
<function>
Re: Using the C API?
You're missing the parenthesis when you're trying to call the call_callback function. Instead of you should be using
Code: Select all
>>> c_sample.call_callback
Code: Select all
>>> c_sample.call_callback()
Re: Using the C API?
Ah yes. Also known as the "don't try to program when it's 1AM and your eyes can't even focus on the screen"dhylands wrote:You're missing the parenthesis when you're trying to call the call_callback function
But now I get this:
Code: Select all
>>> c_sample.call_callback()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError: memory allocation failed, allocating 1 bytes
Re: Using the C API?
You're misuing vstr. In particular, the vstr passed into mp_obj_new_str_from_vstr needs to be heap allocated.
You could use: You'll need to also add a #include <string.h>. This produces:
If you wanted ot use a vstr, you'd need to do something like:which produces:
mp_obj_t obj = mp_obj_new_str("some_string", strlen("some_string"), false);
You could use:
Code: Select all
STATIC mp_obj_t c_sample_call_callback(void) {
mp_obj_t obj = mp_obj_new_str("some_string", strlen("some_string"), false);
return mp_call_function_1(MP_STATE_PORT(c_sample_callback_obj), obj);
}
Code: Select all
>>> def foo(b):
... print(b)
...
>>> import c_sample
>>> c_sample.set_callback(foo)
>>> c_sample.call_callback()
some_string
>>>
Code: Select all
STATIC mp_obj_t c_sample_call_callback(void) {
vstr_t vstr;
vstr_init_len(&vstr, strlen("some_string"));
strcpy(vstr.buf, "some_string");
mp_obj_t obj = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
return mp_call_function_1(MP_STATE_PORT(c_sample_callback_obj), obj);
}
Code: Select all
>>> def foo(b):
... print(b)
...
>>> import c_sample
>>> c_sample.set_callback(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> c_sample.set_callback(foo)
>>> c_sample.call_callback()
b'some_string'
Re: Using the C API?
Could I add more C callbacks without modifying mpconfigport.h?
Re: Using the C API?
You only need to modify mpconfigport.h to add the module. Once the module has been added you can add as many methods to the module that you want by just modifying c_sample.c (or whatever you call your module).cduran wrote:Could I add more C callbacks without modifying mpconfigport.h?
By convention, I probably should have called the source modsample.c.
Re: Using the C API?
Wouldn't I also have to add the new callbacks to qstrdefsport.h?dhylands wrote:You only need to modify mpconfigport.h to add the module. Once the module has been added you can add as many methods to the module that you want by just modifying c_sample.c (or whatever you call your module).cduran wrote:Could I add more C callbacks without modifying mpconfigport.h?
By convention, I probably should have called the source modsample.c.