Get an Instance of a Timer

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
Scalli
Posts: 9
Joined: Tue Jun 12, 2018 5:37 am

Get an Instance of a Timer

Post by Scalli » Wed Jul 18, 2018 12:18 pm

Hello,
I am currently trying to implement a MicroPython module that init a timer and starts it. But with the following code:
[code]
mp_obj_t TimerInit (uint8_t uint8TimerNum) {
mp_obj_t args[1] = {
MP_OBJ_NEW_SMALL_INT(uint8TimerNum)
};
return pyb_timer_type.make_new((mp_obj_t)&pyb_timer_type, 1, 0, args);
}
[/code]

I only get the error message:
[code]
...:82:2: error: unknown type name 'pyb_timer_obj_t'; did you mean 'pin_obj_t'?
[/code]
Can someone tell me how to initalize a new timer in C and then access his arguments?

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

Re: Get an Instance of a Timer

Post by jickster » Wed Jul 18, 2018 5:34 pm

Scalli wrote:
Wed Jul 18, 2018 12:18 pm
Hello,
I am currently trying to implement a MicroPython module that init a timer and starts it. But with the following code:

Code: Select all

mp_obj_t TimerInit (uint8_t uint8TimerNum) {
	mp_obj_t args[1] = {
		MP_OBJ_NEW_SMALL_INT(uint8TimerNum)
	};
	return pyb_timer_type.make_new((mp_obj_t)&pyb_timer_type, 1, 0, args);
}
I only get the error message:

Code: Select all

...:82:2: error: unknown type name 'pyb_timer_obj_t'; did you mean 'pin_obj_t'?
Can someone tell me how to initalize a new timer in C and then access his arguments?
There's plenty of examples of initializing timers.

https://github.com/micropython/micropyt ... r_make_new

What specifically do you need to do?

Scalli
Posts: 9
Joined: Tue Jun 12, 2018 5:37 am

Re: Get an Instance of a Timer

Post by Scalli » Thu Jul 19, 2018 5:42 am

[quote=jickster post_id=28696 time=1531935285 user_id=3167]
There's plenty of examples of initializing timers.

https://github.com/micropython/micropyt ... r_make_new

What specifically do you need to do?
[/quote]
Hi jickster,
thanks for your repl.
I know there is the make_new method in each objekt. But I can't access the pyb_timer_obj_t type of the timer.
I want to do the following in c:
[code]
timer = pyb.Timer(x, freq=y)
[/code]
after init I want to access the number of the timer. I thought the tim_id variable stores it but I can't get the init done.
If i modyfy the timer.h and the timer.c I can declare the timer but can't access the variables in it after initialisation. So I thought there must be another way to declare and init a Timer.

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

Re: Get an Instance of a Timer

Post by jickster » Thu Jul 19, 2018 7:43 pm

Scalli wrote:
Thu Jul 19, 2018 5:42 am
jickster wrote:
Wed Jul 18, 2018 5:34 pm
There's plenty of examples of initializing timers.

https://github.com/micropython/micropyt ... r_make_new

What specifically do you need to do?
Hi jickster,
thanks for your repl.
I know there is the make_new method in each objekt. But I can't access the pyb_timer_obj_t type of the timer.
I want to do the following in c:

Code: Select all

timer = pyb.Timer(x, freq=y)
after init I want to access the number of the timer. I thought the tim_id variable stores it but I can't get the init done.
If i modyfy the timer.h and the timer.c I can declare the timer but can't access the variables in it after initialisation. So I thought there must be another way to declare and init a Timer.

Code: Select all

mp_obj_t TimerInit(uint8_t uint8TimerNum)
{
	mp_obj_t args[1];
	args[0] = MP_OBJ_NEW_SMALL_INT(uint8TimerNum);
	return pyb_timer_type.make_new(NULL, 1, 0, args);
}
 
pyb_timer_obj_t *tim = MP_OBJ_TO_PTR(TimerInit(0));

Scalli
Posts: 9
Joined: Tue Jun 12, 2018 5:37 am

Re: Get an Instance of a Timer

Post by Scalli » Fri Jul 20, 2018 8:48 am

Hey jickster,
thanks a lot for your time.
I tryed to copy your code into my moule,
but I get a "unknown type name 'pyb_timer_obj_t'" Compiler Error.
I've included the following files:

Code: Select all

#include "py/mpconfig.h"
#include "py/nlr.h"
#include "py/misc.h"
#include "py/qstr.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "py/objmodule.h"
#include <stdio.h>
(#include "uart.h")
#include "timer.h"
could you help me?

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

Re: Get an Instance of a Timer

Post by jickster » Fri Jul 20, 2018 6:45 pm

Scalli wrote:
Fri Jul 20, 2018 8:48 am
Hey jickster,
thanks a lot for your time.
I tryed to copy your code into my moule,
but I get a "unknown type name 'pyb_timer_obj_t'" Compiler Error.
I've included the following files:

Code: Select all

#include "py/mpconfig.h"
#include "py/nlr.h"
#include "py/misc.h"
#include "py/qstr.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "py/objmodule.h"
#include <stdio.h>
(#include "uart.h")
#include "timer.h"
could you help me?
In the example code, "pyb_timer_obj_t" is only defined in .c files
https://github.com/micropython/micropyt ... imer_obj_t
so including the .h files does nothing.

Scalli
Posts: 9
Joined: Tue Jun 12, 2018 5:37 am

Re: Get an Instance of a Timer

Post by Scalli » Mon Jul 23, 2018 5:50 am

Oh, I forgot that.
How can I access the pyb_timer_obj then?

edit:
I tryed to modify the timer code so the obj is now public:
timer.h:

Code: Select all

typedef struct _pyb_timer_obj_t pyb_timer_obj_t
and the needed change in the timer.c.
I have access to the timer obj. now, but now i get the following error:

Code: Select all

"dereferencing pointer to incomplete type 'pyb_timer_obj_t {aka struct _pyb_timer_obj_t}"
With the following code:

Code: Select all

pyb_timer_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("timer ID: %d", self->tim_id);

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

Re: Get an Instance of a Timer

Post by jickster » Mon Jul 23, 2018 2:40 pm

Scalli wrote:
Mon Jul 23, 2018 5:50 am
Oh, I forgot that.
How can I access the pyb_timer_obj then?

edit:
I tryed to modify the timer code so the obj is now public:
timer.h:

Code: Select all

typedef struct _pyb_timer_obj_t pyb_timer_obj_t
and the needed change in the timer.c.
I have access to the timer obj. now, but now i get the following error:

Code: Select all

"dereferencing pointer to incomplete type 'pyb_timer_obj_t {aka struct _pyb_timer_obj_t}"
With the following code:

Code: Select all

pyb_timer_obj_t *self = MP_OBJ_TO_PTR(self_in);
printf("timer ID: %d", self->tim_id);
What is the task you're trying to do?

Scalli
Posts: 9
Joined: Tue Jun 12, 2018 5:37 am

Re: Get an Instance of a Timer

Post by Scalli » Tue Jul 24, 2018 7:47 am

At the moment Im playing around and try to understand how the MircoPython Objekts work.

Scalli
Posts: 9
Joined: Tue Jun 12, 2018 5:37 am

Re: Get an Instance of a Timer

Post by Scalli » Thu Jul 26, 2018 1:33 pm

OK I fond it out by myself,
I cut and past the definition of _pyb_timer_obj_t into the Timer.
If Somebody have a better Solution please tell me.
I don't want to change the HAL of the firmware because of my module.

Post Reply