Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_TASK_STD_FUNCTION should be for everyone. #184

Open
issuefiler opened this issue Feb 12, 2024 · 0 comments
Open

_TASK_STD_FUNCTION should be for everyone. #184

issuefiler opened this issue Feb 12, 2024 · 0 comments

Comments

@issuefiler
Copy link

issuefiler commented Feb 12, 2024

The development environment

I’m working with Raspberry Pi Pico W’s on PlatformIO and my development environment is capable of building C++23 code. It has ARDUINO_ARCH_RP2040 defined and the __cplusplus expands to 202100L.

The problem

The documentation instructs to define _TASK_STD_FUNCTION in order to enable the support for std::function callbacks. However, defining it produces an error on my environment because of this microcontroller limitation.

#if !defined (ARDUINO_ARCH_ESP8266) && !defined (ARDUINO_ARCH_ESP32) && !defined (ARDUINO_ARCH_STM32)
#ifdef _TASK_STD_FUNCTION
#error Support for std::function only for ESP8266 or ESP32 architecture
#undef _TASK_STD_FUNCTION
#endif // _TASK_STD_FUNCTION
#endif // ARDUINO_ARCH_ESP8266

Why std::function callbacks are needed

This is necessary to call non-static class methods.

Buzzer(const std::uint8_t pin_number) noexcept
	: pin_number{pin_number},
	task{
		TaskScheduler::immediate,
		TaskScheduler::forever,
		[this](void) -> void {this->play_the_next_note();},
		&(TaskScheduler::scheduler)
	} {
}

My suggestion

It’s all about std::function.

#ifdef _TASK_STD_FUNCTION
#include <functional>
typedef std::function<void()> TaskCallback;
typedef std::function<void()> TaskOnDisable;
typedef std::function<bool()> TaskOnEnable;
#else
typedef void (*TaskCallback)();
typedef void (*TaskOnDisable)();
typedef bool (*TaskOnEnable)();
#endif // _TASK_STD_FUNCTION

It might be more prudent to use the __cplusplus and __has_cpp_attribute for the C++ feature detection, instead of endlessly expanding the whitelist for future devices.

My temporary solution

The code works flawlessly with #define ARDUINO_ARCH_STM32.

// TaskScheduler.cpp
// Written on January 28, February 12, 24, March 2, 4, 7, 9–11, and 16, 2024.
////////////////////////////////////// 80 //////////////////////////////////////

module;

#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
	// Enable the support for “std::function” callbacks.
#define ARDUINO_ARCH_STM32 // Disguising as an STM32 microcontroller
	// to suppress the “Support for std::function
	// only for ESP8266 or ESP32 architecture” error.
#include <TaskScheduler.h> // Task, Scheduler, TASK_IMMEDIATE, TASK_FOREVER.
#undef ARDUINO_ARCH_STM32

export module TaskScheduler;

export namespace TaskScheduler {
	inline constexpr unsigned long int immediate{TASK_IMMEDIATE};
	inline constexpr long int forever{TASK_FOREVER};

	using Task = ::Task;

	::Scheduler scheduler;

	inline bool execute(void) {
		// “true” if it has called a task callback.
		return !(TaskScheduler::scheduler.execute());
	}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant