[SOLVED]how to get pointer to items in hierarchy

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

[SOLVED]how to get pointer to items in hierarchy

Post by jickster » Tue Mar 27, 2018 5:59 pm

I'm making my own CAN library and it's going well so far.

If I have a hierarchy device.CAN1.receive(), I know that in the definition of receive(), I will get a pointer - self - to CAN1 which I've defined as

Code: Select all

typedef struct _atiupy_canch_obj_t {
	mp_obj_base_t base;
	void* ptr_can_channel;
	qstr name;
} atiupy_canch_obj_t;
My question: how can I access the pointer to device in the definition of receive()?

I know that in the definition of atiupy_canch_obj_t I could put in a pointer mp_obj_t the_parent which points to device i.e. put a pointer to the mp_obj_t that is one level up.

PITFALL:

Someone may be saying "just set base.type->parent equal to device" but that parent entry is meant for inheritance which is not what I'm doing here.

Aside from adding mp_obj_t the_parent to atiupy_canch_obj_t, how can I find the mp_obj_t one level up the hierarchy?
Last edited by jickster on Wed Mar 28, 2018 2:19 pm, edited 1 time in total.

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

Re: how to get pointer to items in hierarchy

Post by dhylands » Tue Mar 27, 2018 7:07 pm

From the receive method there isn't any builtin way to recover the device. You used this example:

Code: Select all

device.CAN1.receive()
but consider this case which does the same thing:

Code: Select all

can1 = device.CAN1
can1.receive()
or this slightly more obtuse example:

Code: Select all

class Foo:
  pass
  
foo = Foo()
foo.CAN1 = device.CAN1

foo.CAN1.receive()
Getting the foo object wouldn't help you here. It just happens to be the container than the CAN1 object was stored in. You want to get back the actual device associated with the CAN1 object. So you need to store a pointer to the device inside your CAN1 object (i.e. your atiupy_canch_obj_t structure). This would normally be done at the time you create the CAN1 object.

A concrete example would be between TimerChannel objects and the Timer object that they belong to. If you look at the structures: https://github.com/micropython/micropyt ... #L115-L132 you can see that the pyb_timer_channel_obj_t struct includes a timer member which allows it to determine the timer that a timer channel belongs to.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: how to get pointer to items in hierarchy

Post by jickster » Wed Mar 28, 2018 2:19 pm

dhylands wrote:
Tue Mar 27, 2018 7:07 pm
From the receive method there isn't any builtin way to recover the device. You used this example:

Code: Select all

device.CAN1.receive()
but consider this case which does the same thing:

Code: Select all

can1 = device.CAN1
can1.receive()
or this slightly more obtuse example:

Code: Select all

class Foo:
  pass
  
foo = Foo()
foo.CAN1 = device.CAN1

foo.CAN1.receive()
Getting the foo object wouldn't help you here. It just happens to be the container than the CAN1 object was stored in. You want to get back the actual device associated with the CAN1 object. So you need to store a pointer to the device inside your CAN1 object (i.e. your atiupy_canch_obj_t structure). This would normally be done at the time you create the CAN1 object.

A concrete example would be between TimerChannel objects and the Timer object that they belong to. If you look at the structures: https://github.com/micropython/micropyt ... #L115-L132 you can see that the pyb_timer_channel_obj_t struct includes a timer member which allows it to determine the timer that a timer channel belongs to.
Thank you.

That's what I feared: I have to store my own reference to the "level up" member.

Yeah I know I could do the hierarchy differently; still figuring it out.

Post Reply