HardwareTimer for STM32 (blue pill) board

I'm trying to implement class function from "hardwareTimer.h" lib. There are two versions of the function:

"void hardwareTimer::attachInterrupt(int channel, void (*handler)(void))"
"void hardwareTimer::attachInterrupt(int channel, std::function<void(void)>)"

I'm not sure if they are same. Question is, how to write handler function outside of the class that would work with thesse attachInterrupt() functions?
I'm not sure if they are same.
They're clearly not.

Question is, how to write handler function outside of the class that would work with thesse attachInterrupt() functions?
I imagine it's something like
1
2
3
4
5
6
7
8
9
void some_simple_handler(){
    //do something
}

ht.attachInterrupt(channel, some_simple_handler);
auto some_object = std::make_shared<Foo>();
ht.attachInterrupt(channel + 1, [some_object](){
    //do something using some_object
});
Watch out, since it's possible to leak some_object using the above, depending on what happens inside attachInterrupt.
Last edited on
Is it? With a shared_ptr? What could it do to leak it?
It's probably not going to be lost forever, unless something silly happens. But the shared_ptr might be cached inside some library data structure for an unknown length of time.
Last edited on
For platform I am using (Arduino IDE) std::make_shared<>() doesn't work. This is what I tried to do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <HardwareTimer.h>

HardwareTimer T1(TIM1);

void handler(void){/*do something*/}

void setup(){
  T1.pause();  // Disable all microcontroler's counters
  // Set timer1 to run handler function every 100ms
  T1.setOverflow(10, HERTZ_FORMAT);
  T1.attachInterrupt(channel, handler); // here I have an error 

  T1.resume(); // Resume all counters
}


and my error message looks like this:
invalid conversion from 'void (*)()' to 'void (*)(HardwareTimer*)' [-fpermissive]

I suspect handler function to be in different form because handler function should be of type "std::function<void(void)>". I do not quite understand this type. Can anybody explain it to me? And how I can implement my handler function correctly?
Topic archived. No new replies allowed.