Delphy
December 17, 2013, 4:25pm
1
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:
/**
******************************************************************************
* @file spark_utilities.h
* @author Satish Nair, Zachary Crockett and Mohit Bhoite
* @version V1.0.0
* @date 13-March-2013
* @brief Header for spark_utilities.c module
******************************************************************************
Copyright (c) 2013 Spark Labs, Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
This file has been truncated. show original
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.
Thanks
Delphy
Dave
December 17, 2013, 4:34pm
2
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:
/**
******************************************************************************
* @file spark_protocol.cpp
* @authors Zachary Crockett
* @version V1.0.0
* @date 15-Nov-2013
* @brief SPARK PROTOCOL
******************************************************************************
Copyright (c) 2013 Spark Labs, Inc. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
This file has been truncated. show original
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
Delphy
December 17, 2013, 4:39pm
3
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.
Thanks again!
Dave
December 17, 2013, 4:45pm
4
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)
X.690 is an ITU-T standard specifying several ASN.1 encoding formats: The Basic Encoding Rules were the original rules laid out by the ASN.1 standard for encoding abstract information into a concrete data stream. The rules, collectively referred to as a transfer syntax in ASN.1 parlance, specify the exact octet sequences which are used to encode a given data item. The syntax defines such elements as: the representations for basic data types, the structure of length information, and the means for ...
Delphy
December 17, 2013, 7:45pm
5
Hrm maybe I’m missing something but I can’t see a LONG type there.
Dave
December 17, 2013, 7:56pm
6
Maybe it’s got another name or there is another way for us to encode a LONG? Maybe @zachary can jump in here.
zachary
December 17, 2013, 8:11pm
7
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.
Delphy
December 18, 2013, 8:45am
8
@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.
zachary
December 20, 2013, 3:05am
9
I just tested this and it worked perfectly!
int x(String s) {
return millis();
}
void setup() {
Spark.function("x", x);
}
void loop() {
}
Delphy
December 21, 2013, 11:36am
10
Wow ok, why didn’t I think of using a function? Thanks Zachary that works great!
wtfuzz
February 1, 2014, 9:11pm
11
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();
}
zachary
February 5, 2014, 4:04am
12
Yup, that should have the same result.
FLOAT would be cool to have too!
Dave
March 5, 2014, 6:22pm
14
I think double’s are treated the same way as floats,
double foo;
Spark.variable("foo", &foo, DOUBLE);
Thanks!
David