Call Ticker library from another class

Hi, there.

I’m creating a library, and would like to make use of the Ticker library from it. But apparently the function attach (from the Ticker library) expects a callback into another function. It works fine from a sketch. But how do I pass this on from another library? What is the syntax for doing this from a class?

This is a part of my header file:

#ifndef konnec_h
#define konnec_h

#include "Arduino.h"

#define ledB 12

class Ticker;

class Konnec {
public:
  void openAP();
  void begin();

private:
   Ticker *_ticker = NULL;
    void tick();
}

And this is part of my .cpp file:

#include <konnec.h>
#include "Arduino.h"
#include <Ticker.h>

void Konnec::begin() {
        _ticker = new Ticker();
}

void Konnec::tick() {    //This is the funcion I'd like to Ticker library to execute
	//toggle state
	byte state = digitalRead(ledB);
	digitalWrite(ledB, !state);
}

void Konnec::openAP() {    //This is the function where I'd to attach the ticker
       _ticker->attach(0.6, tick);
}

This is the error I get:

Compiling 'HomeCenter_v20' for 'NodeMCU 1.0 (ESP-12E Module)'
konnec.cpp:In member function 'void Konnec::openAP()
konnec.cpp:141:27: error: no matching function for call to 'Ticker::attach(double, <unresolved overloaded function type>)
:_ticker->attach(0.6, tick)
konnec.cpp:candidates are
konnec.cpp:In file included from
Ticker.h:callback_t)
:void attach(float seconds, callback_t callback)
Ticker.h:callback_t {aka void (*)()}
Ticker.h:attach(float, void (*)(TArg), TArg)
:void attach(float seconds, void (*callback)(TArg), TArg arg)
Ticker.h:template argument deduction\substitution failed
konnec.cpp:*)()
:_ticker->attach(0.6, tick)
konnec.cpp:tick
Error compiling libraries

But if I change the implementation to:

void Konnec::openAP() {    //This is the function where I wanna attach the ticker
       _ticker->attach(0.6, tick());
}

I get this error:

Compiling 'HomeCenter_v20' for 'NodeMCU 1.0 (ESP-12E Module)'
konnec.cpp:In member function 'void Konnec::openAP()
konnec.cpp:141:29: error: invalid use of void expression
:_ticker->attach(0.6, tick())
Error compiling libraries

Can anybody please help? :slight_smile:

cimd:
This is a part of my header file:

This is a part of my answer:

All you need to do is gr

The method in the class that you want to use a callback function must be declared as a static method.

Otherwise, when the callback needs to be called, the Ticker class has no idea which instance of your class should have it's tick() method called.

You're brilliant, PaulS. Worked like a charm!

Is there really no way to call any register the callback from any of the Ticker libraries to call a method on a class?

I haven't used C++ since its had lambdas. In Swift, this would be trivial, but I haven't figured out the syntax here. Can the callback be a lambda that's closing over 'this' to have it reference a method on an object.

Thanks

dm33:
Is there really no way to call any register the callback from any of the Ticker libraries to call a method on a class?

I haven't used C++ since its had lambdas. In Swift, this would be trivial, but I haven't figured out the syntax here. Can the callback be a lambda that's closing over 'this' to have it reference a method on an object.

Thanks

Your question seems to be missing at least one word. I can't figure out which word is missing, so I can't make sense of the question.

Suppose that you have 8 instances of the Ticker class. A timer goes off. Which instance cares?

When you can answer that question, then the solution becomes trivial to implement.