Arguments to a spark function :- which data type and how many can I send?

If my understanding is correct then:

  1. A Spark function can have only 1 argument and that to only a String??
  2. The length of the argument can be up to a maximum of 64 bits??
  3. I can only expose 4 spark functions through Spark cloud??
  4. If only 4 functions can be exposed then what is the point of having 16 I/O pins; if I can’t specify separate function for each pin??
  5. The return type of a spark function should always be integer??

Can anybody please let me know if I am right.

Thanks

  1. yup
  2. 64 bytes (not bits) and really it is 63 as the 64th must be a null termination
  3. yup
  4. first not everyone wants to discretely control pins from the internet ( I know I don’t) I use the strings to pass setting variables (offsets, thresholds, and the like). If you do what to discretely control pins from the internet, there are plenty of ways, just look around the libraries and tutorials and TRUST ME, you can control 16 i/o pins with 4, 64 bytes strings… actually I am pretty sure you control 16 i/o pins with one 2 byte string, but why would you when you have four 64 bytes strings! Great job spark core team!
  5. I think so. I remember this being important, but I don’t remember why. I haven’t had my coffee yet.
1 Like

1: Yes
2: 64 bytes not 64 bits
3: yes
4: You could compress the string using bits:
bit 0-1: message type
00: read pin
{
bit 2-5:pin
}

01: write pin
{
bit 2-5:pin
}

02: pwm pin
{
bit 2-5: pin
bit 6-7: number of bytes + 1
bytes 1 to (1+(bit 6-7)): data
}

03: other
{
bit 2-5: message type (up to you)

}
5: yes

1 Like

M sorry.I thought I am writing 64 byte but ended up with 64 bit.
Anyways Thanks a lot for your valuable suggestions @GregF & @jerome .

@GregF
Thanks again but I actually couldn’t understand how to compress strings using bits. Could you please point me towards any reference or tutorials?

You could take a look at the Tinker firmware to get an idea of how you could control all pins using 4 functions. This is one of many ways this can be done, but it should get you started.

3 Likes

Assuming (hopefully) that you know about main(int argc, char * argv[]) where argc is the number of parameters and argv is a list of the parameters.

Where you can pass an arbitrary number of parameters in the code above:
./display this is a test
argc == 5
argv[0] == ./display
argv[1] == this
argv[2] == is
argv[3] == a
argv[4] == test

With the spark code you can only pass one parameter, the 64 character string.
By knowing in advance the types of data to expect, you can do multiple things by stepping through the string:
Byte 0 has 8 bits. these 8 bits can be broken down further:
bit 7 6 5 4 3 2 1 0
bit 0-3 (0-15) pin number
bit 4: (0) read pin (1) write pin

  • if read pin, read the byte and set the return value to the read value and skip to byte 1 if it exists, if not return
  • if write pin, write byte 1 to pin and skip to byte 2 if it exists, if not return
    Note that with the above method only the last read pin value will be returned.

Hope that helps.

1 Like

Here’s a code that i use for controlling a thermostat. It detects the first two letters and determines what functions to send the rest of the string to, so I have one function that can do a lot of different things.

int genericCommand(String command) {
    char com = command.charAt(0);
    switch (com) {
        case 'S':
            //schedule
            if (command.charAt(1) == 'W') {
                //write schedule
                return writeSchedule(command.substring(2));
            } else {
                return readSchedule(command.substring(2));
            }
            break;
        case 'P':
            //PID
            if (command.charAt(1) == 'W') {
                return writePID(command.substring(2));
            } else {
                return readPID(command.substring(2));
            }
            break;
        case 'T':
            // Temp sensor serials
            if (command.charAt(1) == 'W') {
                //write serial
                return writeTempSerial(command.substring(2));
            } else {
                return readTempSerial(command.substring(2));
            }
            break;
        case 'C':
            // find current temp setting
            if (command.charAt(1) < 0 || command.charAt(1) >= NUM_OF_ZONES) return -1;
            sprintf(outputString, "%0.4f", schedules[command.charAt(1)].findCurTempSetting());
            return command.charAt(1);
            break;
        case 'R':
            //Reset.  Need "RR" so that one doesn't get accidentally called
            if (command.charAt(1) == 'R') System.reset();
            break;
    }
    return -20;
}

So, if you send PR1, it will read the PID values for zone 1, and print them to a String that is an exposed variable, which you can use to output data other than ints. Alternatively SW0.1:24|22;35|15 writes (W) the temperature schedule (S) for zone 0, on day 1 (Monday), setting it to 22C at time 2415 minutes (6:00am), and 15C at time 3515 (8:45 am) minutes. So, get creative!

5 Likes

Thanks @GregF, @Moors7 & @mumblepins for your suggestions.
It really helped.

1 Like