Spark.variable missing LONG option as 3rd parameter

Hi All,

I was attempting to expose an unsigned long as a Spark.variable() so I could read it, but turns out the 3rd parameter (data type) only support BOOLEAN, INT, STRING and DOUBLE.

The specific code and data type is here:

typedef enum
{
        BOOLEAN = 1, INT = 2, STRING = 4, DOUBLE = 9
} Spark_Data_TypeDef;

class SparkClass {
public:
        static void variable(const char *varKey, void *userVar, Spark_Data_TypeDef userVarType);

Would it be possible to add LONG as a data type? If I knew the meaning of the various ENUMS I could submit a pull request but I’m new to the codebase. :smile:

Thanks
Delphy

Good idea @Delphy, I’ll add that to our firmware backlog.

If someone wanted to implement this in the meantime, or submit a pull request, those enums are used here:

The different types are sent as part of the payload to the device service, and they’re byte encoded (I am having trouble remembering the standard off the top of my head). I’m guessing we’ll be able to get to this when we address the different variable return type / weird encoding bug mentioned in other threads.

Thanks!
David

Thanks for the response. I was curious about why DOUBLE was listed as 9 in the enum - if it was binary, shouldn’t it be 8 instead? I’ll have a browse through the core-communication-lib and see if I can’t contribute something rather than asking lots of questions. :slight_smile:

Thanks again!

Good question, I believe the enum #s have special meaning with regards to ASN.1 encoding (which was the standard I was trying to think of earlier)

Hrm maybe I’m missing something but I can’t see a LONG type there.

Maybe it’s got another name or there is another way for us to encode a LONG? Maybe @zachary can jump in here.

Hey @Delphy, is there a reason you can’t use INT for this? On Cortex M3, I believe int and long are both represented with 32 bits.

@zachary I’m basically using it to return the number of millis() since the last detected pulse (from my electricity meter) so it all depends on what millis() is using. :slight_smile:

I just tested this and it worked perfectly! :smile:

int x(String s) {
  return millis();
}

void setup() {
  Spark.function("x", x);
}

void loop() {
}

Wow ok, why didn’t I think of using a function? :slight_smile: Thanks Zachary that works great!

This is still returning a signed int though. Wouldn’t this be the same as casting millis()?

int x;

void setup() {
   Spark.variable("x", &x, INT);
}
void loop() {
   x = (int)millis();
}

Yup, that should have the same result.

FLOAT would be cool to have too!

I think double’s are treated the same way as floats,

double foo; 
Spark.variable("foo", &foo, DOUBLE);

Thanks!
David