Developing your first pyb module:

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
SwimDude0614
Posts: 32
Joined: Mon Mar 10, 2014 1:32 am

Developing your first pyb module:

Post by SwimDude0614 » Mon Jun 23, 2014 11:16 pm

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!
Last edited by SwimDude0614 on Mon Jun 23, 2014 11:44 pm, edited 1 time in total.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Developing your first pyb module:

Post by dhylands » Mon Jun 23, 2014 11:27 pm

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.
I'd recommend creating a Wiki page. This can then be edited/updated by others.
http://wiki.micropython.org/Home

SwimDude0614
Posts: 32
Joined: Mon Mar 10, 2014 1:32 am

Re: Developing your first pyb module:

Post by SwimDude0614 » Mon Jun 23, 2014 11:34 pm

Oh wow... now I feel truly stupid. To the wiki I go...

SwimDude0614
Posts: 32
Joined: Mon Mar 10, 2014 1:32 am

Re: Developing your first pyb module:

Post by SwimDude0614 » Mon Jun 23, 2014 11:43 pm

Can a moderator/admin delete all the reserved posts? I'd like to keep the thread around for questions/discussion though

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Developing your first pyb module:

Post by dhylands » Mon Jun 23, 2014 11:48 pm

SwimDude0614 wrote:Can a moderator/admin delete all the reserved posts? I'd like to keep the thread around for questions/discussion though
Done

SwimDude0614
Posts: 32
Joined: Mon Mar 10, 2014 1:32 am

Re: Developing your first pyb module:

Post by SwimDude0614 » Tue Jun 24, 2014 1:54 am

Here we go with the questions!

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;
   ....
   ...
};
Poking through the definitions and its usage makes it look like recursive definitions? I'm certainly confused what is happening and why.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Developing your first pyb module:

Post by dhylands » Tue Jun 24, 2014 3:51 am

SwimDude0614 wrote:Here we go with the questions!

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;
   ....
   ...
};
Poking through the definitions and its usage makes it look like recursive definitions? I'm certainly confused what is happening and why.
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'.

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'>

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Developing your first pyb module:

Post by dhylands » Tue Jun 24, 2014 4:52 pm

Looking at the example you posted on your wiki page, you've got:

Code: Select all

const mp_obj_type_t pyb_saab_type = { {&mp_type_type } };
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.

Code: Select all

typedef struct {
    mp_obj_base_t base;
    machine_uint_t year;
} pyb_saab_obj_t;
Now you could go ahead and declare some static instances of this like so:

Code: Select all

const pyb_saab_obj_t first_car  = {
    { &pyb_saab_type },
    1997
};
Now, let's create a print function for your saab object:

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);
}
And a function to dynamically allocate some new ones:

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;
}
and lets create a function to return that statically allocated object:

Code: Select all

STATIC mp_obj_t saab_first_car(void) {
    return (mp_obj_t)&first_car;
}
I'll leave the rest (tying this all together) to you for now.

SwimDude0614
Posts: 32
Joined: Mon Mar 10, 2014 1:32 am

Re: Developing your first pyb module:

Post by SwimDude0614 » Tue Jun 24, 2014 6:02 pm

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

SwimDude0614
Posts: 32
Joined: Mon Mar 10, 2014 1:32 am

Re: Developing your first pyb module:

Post by SwimDude0614 » Wed Jun 25, 2014 3:08 am

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?! 8-) :mrgreen: )

Post Reply