Developing your first pyb module:
-
- Posts: 32
- Joined: Mon Mar 10, 2014 1:32 am
Developing your first pyb module:
Hello all. I am going to document, as best I can, creating my first hardware pyb module. The end-result will be a CAN bus since there is currently no implementation in pyb and I would like to use it.
I make *NO* promises as to the usefulness or accuracy of any information given below. As I type this now, I have only just started creating my first module, so what you will read are not the words of an experienced micropython developer but simply someone thinking out loud as he stumbles his way through it.
I am relying on the more experienced members here to correct me where I go wrong and hopefully add some helpful tips along the way.
The work can be found on the wiki here: http://wiki.micropython.org/Creating-Pyb-Modules
and I hope we can get some good discussion and questions going on here. I know I'll have lots of ???s along the way!
I make *NO* promises as to the usefulness or accuracy of any information given below. As I type this now, I have only just started creating my first module, so what you will read are not the words of an experienced micropython developer but simply someone thinking out loud as he stumbles his way through it.
I am relying on the more experienced members here to correct me where I go wrong and hopefully add some helpful tips along the way.
The work can be found on the wiki here: http://wiki.micropython.org/Creating-Pyb-Modules
and I hope we can get some good discussion and questions going on here. I know I'll have lots of ???s along the way!
Last edited by SwimDude0614 on Mon Jun 23, 2014 11:44 pm, edited 1 time in total.
Re: Developing your first pyb module:
I'd recommend creating a Wiki page. This can then be edited/updated by others.SwimDude0614 wrote:Hello all. I am going to document, as best I can, creating my first hardware pyb module. The end-result will be a CAN bus since there is currently no implementation in pyb and I would like to use it.
When I'm all done, I will try and compile this into a useful and indexed PDF (or another format? who knows). We'll see how much motivation I have left by the end of it.
http://wiki.micropython.org/Home
-
- Posts: 32
- Joined: Mon Mar 10, 2014 1:32 am
Re: Developing your first pyb module:
Oh wow... now I feel truly stupid. To the wiki I go...
-
- Posts: 32
- Joined: Mon Mar 10, 2014 1:32 am
Re: Developing your first pyb module:
Can a moderator/admin delete all the reserved posts? I'd like to keep the thread around for questions/discussion though
Re: Developing your first pyb module:
DoneSwimDude0614 wrote:Can a moderator/admin delete all the reserved posts? I'd like to keep the thread around for questions/discussion though
-
- Posts: 32
- Joined: Mon Mar 10, 2014 1:32 am
Re: Developing your first pyb module:
Here we go with the questions!
What is the first member of _mp_obj_type_t? It reads:
Poking through the definitions and its usage makes it look like recursive definitions? I'm certainly confused what is happening and why.
What is the first member of _mp_obj_type_t? It reads:
Code: Select all
struct _mp_obj_type_t {
mp_obj_base_t base;
....
...
};
Re: Developing your first pyb module:
Every object in python has a type. base describes that type. A type itself will often have base set to mp_type_type which corresponds to the python type 'type'.SwimDude0614 wrote:Here we go with the questions!
What is the first member of _mp_obj_type_t? It reads:Poking through the definitions and its usage makes it look like recursive definitions? I'm certainly confused what is happening and why.Code: Select all
struct _mp_obj_type_t { mp_obj_base_t base; .... ... };
For example: https://github.com/micropython/micropyt ... /led.c#L53 this declares an instance of the pyb_led_type. The pyb_led_type itself: https://github.com/micropython/micropyt ... led.c#L292 is a type, and its an instance of mp_type_type.
In python:
Code: Select all
>>> led = pyb.LED(1)
>>> type(led)
<class 'LED'>
>>> type(type(led))
<class 'type'>
>>> type(pyb.LED)
<class 'type'>
Re: Developing your first pyb module:
Looking at the example you posted on your wiki page, you've got:
Now lets suppose that you want to have a statically declared object of the type pyb_saab_type. Since you didn't post your type, I'll make one up, that just stores the year of the saab.
Now you could go ahead and declare some static instances of this like so:
Now, let's create a print function for your saab object:
And a function to dynamically allocate some new ones:
and lets create a function to return that statically allocated object:
I'll leave the rest (tying this all together) to you for now.
Code: Select all
const mp_obj_type_t pyb_saab_type = { {&mp_type_type } };
Code: Select all
typedef struct {
mp_obj_base_t base;
machine_uint_t year;
} pyb_saab_obj_t;
Code: Select all
const pyb_saab_obj_t first_car = {
{ &pyb_saab_type },
1997
};
Code: Select all
void saab_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_saab_obj_t *self = self_in;
print(env, "<SAAB year %lu>", self->year);
}
Code: Select all
STATIC mp_obj_t saab_obj_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, false);
// get year
machine_uint_t year = mp_obj_get_int(args[0]);
pyb_saab_obj_t *self = m_new_obj(pyb_saab_obj_t);
self->base.type = type_in;
self->year = year;
return o;
}
Code: Select all
STATIC mp_obj_t saab_first_car(void) {
return (mp_obj_t)&first_car;
}
-
- Posts: 32
- Joined: Mon Mar 10, 2014 1:32 am
Re: Developing your first pyb module:
That stuff is gold, thank you! Most of that I've done and was going to type up tonight, but the dynamically allocated object was something I hadn't yet figured out (and really wanted).
-
- Posts: 32
- Joined: Mon Mar 10, 2014 1:32 am
Re: Developing your first pyb module:
Didn't get much further tonight, but I did improve on the format a lot and added a bit of content. Should be much easier to read going forward. Work will be very minimal for the next several days. I have plans this weekend and preparations to make (Anyone here familiar with TSD rally?! )