Page 1 of 1

[SOLVED]how to get pointer to items in hierarchy

Posted: Tue Mar 27, 2018 5:59 pm
by jickster
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?

Re: how to get pointer to items in hierarchy

Posted: Tue Mar 27, 2018 7:07 pm
by dhylands
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.

Re: how to get pointer to items in hierarchy

Posted: Wed Mar 28, 2018 2:19 pm
by jickster
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.