Skip to content

Conversation

@harry-iii-lord
Copy link

Hello folks.

I wanted to propose a small extension of current Periodic class to make it more c++ friendly (or so called more modern). Idea behind is to use std::function to hold callback.

Having class in such form give you ability of making nice encapsulation of functionalities without need to declaring C-style callbacks, for instance this way:

class Broadcaster2
{
public:
    Broadcaster2(const char *message) 
    {
        m_periodic = new Periodic("Broadcaster2", [this](){
            return this->callback();
        });
        //rest of code
    }

    int32_t callback() 
    {
        // something
        return 1;
    }

private:
    concurrency::Periodic *m_periodic;
    std::string m_message;
};

Proposed code is keeping existing C-style ctor, thus such change should be backward compatible.
Unfortunately such change is not binary equal (there is a member m_callback) and is bit bigger. Not sure this is that big issue. Leaving to your consideration.

Happy reviewing.

@github-actions
Copy link
Contributor

@harry-iii-lord, Welcome to Meshtastic!

Thanks for opening your first pull request. We really appreciate it.

We discuss work as a team in discord, please join us in the #firmware channel.
There's a big backlog of patches at the moment. If you have time,
please help us with some code review and testing of other PRs!

Welcome to the team 😄

@github-actions github-actions bot added first-contribution needs-review Needs human review enhancement New feature or request labels Jan 31, 2026
@jp-bennett
Copy link
Collaborator

Looks like this PR unintentionally picked up changes in the device-install scripts. The flash size change is a mere 80 bytes.

@thebentern thebentern changed the base branch from develop to master February 2, 2026 15:31
@CLAassistant
Copy link

CLAassistant commented Feb 2, 2026

CLA assistant check
All committers have signed the CLA.

@harry-iii-lord
Copy link
Author

Looks like this PR unintentionally picked up changes in the device-install scripts. The flash size change is a mere 80 bytes.

Indeed. It wasn't intention :) and I see you fix that. Thank you.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Extends the concurrency::Periodic thread helper to support “modern” C++ callables (via std::function) in addition to the existing legacy function-pointer callback, enabling lambdas and member-function captures while keeping source compatibility.

Changes:

  • Add std::function<int32_t()>-based constructor overload for Periodic.
  • Replace the raw function pointer member with a std::function callback invocation path.
  • Update class docstring to reflect dual callback support.

Comment on lines 3 to 19
#include <functional>

#include "concurrency/OSThread.h"

namespace concurrency
{

/**
* @brief Periodically invoke a callback. This just provides C-style callback conventions
* rather than a virtual function - FIXME, remove?
* @brief Periodically invoke a callback.
* Supports both legacy function pointers and modern callables.
*/
class Periodic : public OSThread
{
int32_t (*callback)();

public:
// callback returns the period for the next callback invocation (or 0 if we should no longer be called)
Periodic(const char *name, int32_t (*_callback)()) : OSThread(name), callback(_callback) {}
Periodic(const char *name, int32_t (*cb)()) : OSThread(name), m_callback(cb) {}
Periodic(const char *name, std::function<int32_t()> cb) : OSThread(name), m_callback(std::move(cb)) {}
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::move(cb) is used in the std::function constructor, but this header does not include <utility> (the standard header that provides std::move). Relying on it being pulled in transitively via <functional> is not portable; please add #include <utility> (or avoid std::move here).

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That make sense. I will add it.


protected:
int32_t runOnce() override { return callback(); }
int32_t runOnce() override { return m_callback ? m_callback() : 0; }
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_callback is declared under protected:, which changes it from the previous implicit-private member (callback) and exposes it to subclasses. To keep the Periodic scheduling behavior encapsulated and avoid unintended mutation from derived classes, make the callback member private (keeping only runOnce() protected).

Suggested change
int32_t runOnce() override { return m_callback ? m_callback() : 0; }
int32_t runOnce() override { return m_callback ? m_callback() : 0; }
private:

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Wil change that.

Comment on lines 22 to 23
int32_t runOnce() override { return m_callback ? m_callback() : 0; }
std::function<int32_t()> m_callback;
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Member naming here (m_callback) introduces an m_ prefix that doesn’t match the surrounding concurrency code’s member style (typically plain camelCase, e.g. controller in concurrency/OSThread.h:33 and notification in concurrency/NotifiedWorkerThread.h:16). Consider renaming to callback/callbackFn (and keep consistent casing) to match established conventions.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This make sense too.

@harry-iii-lord harry-iii-lord force-pushed the feature/modern-periodic-util branch from 27f8d70 to bf55b5f Compare February 10, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request first-contribution needs-review Needs human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants