cAT icon indicating copy to clipboard operation
cAT copied to clipboard

Question, is there an easy way to use and update a 'double' or 'float' variable type?

Open loganbenda opened this issue 2 years ago • 0 comments

Hello,

I've been digging into this library and trying to write a new function to handle 'double' or 'float' data types. Right now the best I have is writing and reading the float variable as a char buffer. I have this reading and writing out, however, this doesn't seem to allow me to modify the actual variable when mapped through 'static struct cat_variable'. I set up a new type CAT_VAR_NUM_DOUBLE to handle it.

static struct cat_variable go_vars[] = { { .type = CAT_VAR_NUM_DOUBLE, .data = &global.x, .data_size = sizeof(global.x), .write = x_write, .name = "x", .access = CAT_VAR_ACCESS_READ_WRITE }, }

I have global.x defined as the following in order to not truncate the data I send.

typedef struct { double x[16]; } GlobalControl;

My parsing of the write arguments is:

`static int parse_num_double(struct cat_object *self) { assert(self != NULL); char ch; int state = 0; size_t size = 0;

    while (1) {
            ch = get_atcmd_buf(self)[self->position++];

            switch (state) {
                case 0:
                        if (ch == '.'){
                                return -1;
                        }
                        if ((ch == 0) || (ch == ',')) {
                                if (size > self->var->data_size){
                                        return -1;                                        
                                }
                                if (self->var->access == CAT_VAR_ACCESS_READ_ONLY) {
                                        self->write_size = 0;
                                } else {
                                        ((int8_t *)(self->var->data))[size] = 0;
                                        self->write_size = size;
                                }
                                return (ch == ',') ? 1 : 0;
                        } else {
                                ((int8_t *)(self->var->data))[size++] = ch;
                        }
                        state = 1;
                        break;
                case 1:
                        if ((ch == ',')) {
                                if (size > self->var->data_size){
                                        return -1;                                        
                                }
                                return (ch == ',') ? 1 : 0;
                        } 
                        if (size > self->var->data_size){
                                return -1;
                        }       
                        if (self->var->access == CAT_VAR_ACCESS_READ_ONLY) {
                                size++;
                        } else {
                                ((int8_t *)(self->var->data))[size++] = ch;
                        }
                        break;
                default:
                    break;
            }            
    } 
    return -1; 

}`

My question is when storing the float variable as a char buffer, is there a method I can call to update the float variable directly as a char buffer? Or if my current approach does not make sense, maybe you can point me in a new direction?

Thanks!

loganbenda avatar Mar 21 '23 19:03 loganbenda