for my current project i had the idea to separate my code into different modules.
(for better readability and simpler later additions)
my idea was to do one class per module and to combine the modules in the main ino file.
(some great information about what is possible is written in this answer from Nick Gammon on StackExchange Arduino)
at my first test i got stuck with a library i want to use that uses a callback function (function pointers)
after some reasearch i now know that the pointer to a function of an class is not compatible with a pointer to a ‘normal’ function.
i also found that a static function in a class works for function pointers.
but i think i want to use non-static members of the class and than i will again run into trouble…
(as fare as i know i not allowed to mix static and non static things… - and that makes logically sense)
i have setup a minimal example:
on github and attached[/edit]
- the main sketch that clues all together
- a basic module
- a example lib with a function pointer
the example is ‘self-contained’ (no external dependencies)
the libClass implements a basic timer
→ that fires after x ms and calls the function pointer
and is used two times:
- in the main ino file
- and in the moduleClass.
the callback functions are just printing a counter value to the serial port.
currently i have the static variant uncommented.
So for me there are two questions arising:
- is there a better/other way to do such modularization?
- what is the best way to do callback functions?
(i can rewrite the used libraries - most of theme are my own…)
hopefully someone of you can bring some light into this
test__callbackfunctions.ino (2.03 KB)
moduleClass.h (998 Bytes)
moduleClass.cpp (1.2 KB)
libClass.h (1.11 KB)
libClass.cpp (1.32 KB)