According to http://docs.spark.io/#/firmware/data-and-control-spark-variable it is possible to return ints, doubles and strings. My particular use case requires an array of bytes so string seems to be the closest possible supported datatype (my client app will have to string split and convert bytes accordingly).
However, I am unable to retrieve a string from a Spark.variable. More accurately, I cannot change the value of an initialized string and return it.
I have globally declared char *colour[128]; // Once working will be [3] to represent RGB bytes
and in Setup() Spark.variable("colour", &colour, STRING);
I have a Spark.function fadeTo(String command) that attempts to set the global colour variable to the command value. Since the Spark function POST param is String and the global var is char[] I have tried the following to convert/cast: char charBuf[128]; command.toCharArray(charBuf, 128); colour = command;
But that returns
error: incompatible types in assignment of ‘String’ to 'char [128]’*
Two questions:
Why does the documented example for returning strings actually use a character array and not a string?
What is the correct way to implement a RESTful interface that supports strings for both POSTs (Spark.functions) and GETs (Spark.variables)?
@Hypnopompia is right–string variable are supported! I think your problem is that you declared colour as char* and then used &colour in the Spark.variable call. If you look at TJ’s example, it uses char colour (not char*) and then uses colour in the Spark.variable, which is the same as &colour for an array.
Let us know if that doesn’t work for you and we will keep digging!
@Hypnopompia’s example is working for me. I think the difference was strcpy() which I had in one of my attempts but possibly with some other syntax issues. Colour was declared as char* based on the sample in the documentation. I’ve been working on this all night and gone through numerous iterations but thanks to @Hypnopompia it is working now.
I haven’t worked with C or C++ for a couple of decades but have strength in C#. I think its time to brush up on the arcane C++ syntax, pointers and ampersands.
Forgive me for being so opinionated, but I couldn’t help it – we’re talking about C++, designed by groups of academicians who really should be cross-examined for misguiding generations of software engineers. Thus my question is, which takes less time and is more valuable: to build a cross-compiler for C# that targets P0/P1, or learn C++ constructs?