Validation error in Build - invalid conversion from 'void (*)(int)' to 'int (*)(String)'[SOLVED]

Hello All,

got my Spark recently and have done some playing with Tinker, built the initial Blink firmware with Build and was able to flash that to my core successfully. Now I am trying to flash my own firmware onto the core, but am getting the following error when I validate -

…/ec89def10abc5d1530f956bf80bc46ed2b9f22e098871e2c0cd24c3d7144/the_user_app.cpp: In function ‘void setup()’:
…/ec89def10abc5d1530f956bf80bc46ed2b9f22e098871e2c0cd24c3d7144/the_user_app.cpp:21:42: error: invalid conversion from ‘void ()(int)’ to 'int ()(String)’ [-fpermissive]
In file included from …/inc/spark_wiring.h:31:0,
from …/inc/application.h:29,
from …/ec89def10abc5d1530f956bf80bc46ed2b9f22e098871e2c0cd24c3d7144/the_user_app.cpp:2:
…/inc/spark_utilities.h:73:14: error: initializing argument 2 of ‘static void SparkClass::function(const char*, int (*)(String))’ [-fpermissive]
make: *** […/ec89def10abc5d1530f956bf80bc46ed2b9f22e098871e2c0cd24c3d7144/the_user_app.o] Error 1

the code of my firmware is inlined below -

int LED_ONE = D0;
int LED_TWO = D1;
int LED_THREE = D2;

bool LED_ONE_LIT = false;
bool LED_TWO_LIT = false;
bool LED_THREE_LIT = false;

int lightlevel = 0;

void setup() {
    pinMode(LED_ONE, OUTPUT);
    pinMode(LED_TWO, OUTPUT);
    pinMode(LED_THREE, OUTPUT);
    Spark.function("toggleLed", toggleLed);
    Spark.variable("lightlevel", &lightlevel, INT);
    pinMode(A0, INPUT);
}

void loop() {
    lightlevel = analogRead(A0);
}

void toggleLed(int led){
    switch(led){
        case 1:
            LED_ONE_LIT == true ? LED_ONE_LIT = true : LED_ONE_LIT = false;
            LED_ONE_LIT == true ? digitalWrite(LED_ONE, HIGH) : digitalWrite(LED_ONE, LOW);
            break;
        case 2:
            LED_TWO_LIT == true ? LED_TWO_LIT = true : LED_TWO_LIT = false;
            LED_TWO_LIT == true ? digitalWrite(LED_TWO, HIGH) : digitalWrite(LED_TWO, LOW);
            break;
        case 3:
            LED_THREE_LIT == true ? LED_THREE_LIT = true : LED_THREE_LIT = false;
            LED_THREE_LIT == true ? digitalWrite(LED_THREE, HIGH) : digitalWrite(LED_THREE, LOW);
            break;
    }
}

I get the same results on both Safari and Firefox (Mac OS X 10.8.5).

Am I missing something here?

TIA for any help.

Hello @Patdroid
A few things to note about the Spark.function

  1. The return type should always be declared an int
  2. The function should always return 1 (success) or -1 (failure)
  3. You can only pass String datatype to the function

If you keep track of these things, the code compiles.

Some notes on the Spark function: http://spark.github.io/docs/#/firmware/cloud-spark-function

Hope this helps!

1 Like

Should it always return 1 or -1, or should it always return an int? I’m successfully using it to return sensor values as ints instead of just a 1 or -1. I also use -1000 as a failure since, as of late, it’s entirely possible for one of my temperature sensors to return something less than 0F!

1 Like

@wgbartley
I maybe a little ill-informed here and you could be right. As long as it is returning an int, things should work.

The API I was working with relied on 1/-1 for success/ failure.

Maybe @zachary can shed some more light here.

Haha, that reading can be treated as a fail for some of us.

OK, this was a case of RTFM with a side of PEBKAC. From the docs here:

http://docs.spark.io/#/api/basic-functions-controlling-a-core

…“All Spark functions take a String as the only argument and must return a 32-bit integer.”…

Onward!

2 Likes

Just chiming in that @mohit is right on here. The firmware only requires that Spark functions return an int. We were trying to be consistent within our own documented examples to usually return -1 to indicate failure and 1 to indicate success if there was no other meaningful data to return. This was only a convention for our examples, not a requirement of the firmware.