contrem / arduino-timer

Non-blocking library for delaying function calls
BSD 3-Clause "New" or "Revised" License
334 stars 50 forks source link

Accessing timers from other files #28

Closed rowifi closed 3 years ago

rowifi commented 3 years ago

Hi. This is probably mode of a programming issue than an issue with the library but I'm struggling to find the appropriate syntax.

I create some timers in the main setup function and update them in the loop as expected, but I need to access and control the timer object from a function within another file.

I've tried.. both without success.

extern Timer<> timer0 ; extern auto timer0;

What would be the correct format to use for this as I'm not familiar using externs with templates.

philj404 commented 3 years ago

Your code is close to what works for me. Remember that when you are using header files you need to include the header of what your code depends on. In this case your timer0 declaration needs to understand what a Timer<> is. This is defined in <arduino-timer.h>. For example:

// utilities.h
// basic utility objects and function declarations

// classes and object declarations that this header file depends on

#include <arduino-timer.h>
//                (arduino-timer.h tells the compiler what "Timer<>" means)
...

// declarations
extern Timer<> timer0;  // some source file will define timer0
// utilities.cpp
// common utility definitions -- only defined once
...
Timer<> timer0;  // the actual timer0 instance
...

I hope this helps.