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

Can't call function defined as (void) #4

Closed
kratochviljan opened this issue Jul 25, 2017 · 5 comments
Closed

Can't call function defined as (void) #4

kratochviljan opened this issue Jul 25, 2017 · 5 comments

Comments

@kratochviljan
Copy link

Any function defined as (void) can not be called by tasker.
Error while compiling:

error: invalid conversion from 'void ()()' to 'TaskCallback {aka void ()(int)}' [-fpermissive]
tasker.setInterval(nrfReceive, 5000)

Tasker.h: 53:6: error: initializing argument 1 of 'bool Tasker::setInterval(TaskCallback, long unsigned int, int, byte)' [-fpermissive]
bool Tasker*: setInterval(TaskCallback func, unsigned long interval, int param, byte prio)
Error compiling project sources

Seems that it is not BUG in Tasker itself, but some restrictions in C/C++

@joysfera
Copy link
Owner

could you please show me the relevant bits (i.e. the definition of nrfReceive) of your source code?

@kratochviljan
Copy link
Author

Yes, surely I can. But it does not matter on mentioned code. Look at two example below, first one not working - error while compiling, second one working fine. The only difference is in the declaration of the called function (void) x (int).

void nrfReceive(void) {
	if (radio.available()) {
		while (radio.available()) {
			radio.read(&meteo, sizeof(meteo));
		}
	}

#ifdef DEBUG
	Serial.println("");
	Serial.print("Velikost packetu: ");
	Serial.print(sizeof(meteo));
	Serial.println("");
	Serial.print("NRF-RX>>>  ");
	Serial.print(meteo.crc);
#endif
}
// NOT WORKING
#include <Tasker.h>
Tasker tasker;

void setup() {
	Serial.begin(115200);
	tasker.setInterval(printsomething, 1000);
}

void loop() {
	tasker.loop();
}

void printsomething(void) {
	Serial.println("Hello Word!");
}

// WORKING FINE

#include <Tasker.h>
Tasker tasker;

void setup() {
	Serial.begin(115200);
	tasker.setInterval(printsomething, 1000);
}

void loop() {
	tasker.loop();
}

void printsomething(int) {
	Serial.println("Hello Word!");
}

@joysfera
Copy link
Owner

Tasker expects the callback to be defined as void fn(int). The documentation does not explain it explicitly but all examples show that so please define your callbacks with the (int parameter) even if you didn't want to use it. Just like in the MultiBlink example:

void blink1(int /* unused */)
{
...

@kratochviljan
Copy link
Author

kratochviljan commented Jul 26, 2017

Yes, I already alter my code that way.

You're definitely a better coder in C/C++ than me, so please correct me if I'm wrong.
Valid declaraction of function i C which has no input arguments and return nothing should be: void function(void) {...}, which is exactly my mentioned case. If I change the declaration to void function (int) {...} copmpiler expect, that function has (int) argument or not ?
(maybe we can continue by mail directly and in Czech I guess...)

@joysfera
Copy link
Owner

joysfera commented Jul 26, 2017

That function will receive an int argument - Tasker is sending that just in case you needed a flag, a counter or something. It does come handy, as the examples show. You are not obliged to use that parameter's value in your function, of course. You can even declare it without a name so it will get ignored. The examples show this as well.

Feel free to mail me directly, I'm going to close this issue.

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

2 participants