Does anyone have swift (iOS app) code example on how to connect to VESC via bluetooth?

So a general idea of how I did it is as follows (not Swift sorry):

You need to look at the implementation of the method. A rough idea of what I did was define a struct that has all the values I wanna store (actually just copied from the vesc tool struct), have an index value to keep track of where I am in the packet, and increase that index by the relevant number of indexes every time I process a set of bytes.

For example, for COMM_FW_VERSION, You can do the following:

First, declare an index tracker, some way of keeping track of your original payload, and the struct you’re writing data to. Sorry it’s not Swift, I’m not too familiar with the ins and outs of Swift yet.

    int32_t ind = 0;
    uint8_t payload2[sizeof(payload)];
    memcpy(payload2, payload + 1, sizeof(payload) - 1);
    struct vescFW fw;

Then,

fw.fw_version_major = buffer_get_int8(payload2, &ind);
fw.fw_version_minor...

where

uint8_t buffer_get_int8(const uint8_t *buffer, int32_t *index) {
    uint8_t res = buffer[*index];
    *index += 1;
    return res;
}

Of course, you need to be mindful of what kind of stuff you’re processing. uint16 needs to increase index by 2, uint32 by 4, etc. Same applies to floats.

For the following hardware string, you just need to copy the remainder of the original payload starting from [ind] with a 30 character limit. You can simply interpret the remaining as a C string and use something like -[NSString stringWithCString:encoding] to decode it.

Basically, look at the implementation of the method itself as defined in the firmware (not tool, though that’s helpful to determine how things are supposed to be read), and do the reverse of whatever the method is doing in order to read it. Hope this helps.

2 Likes