Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
loganbenda opened this issue Mar 21, 2023 · 0 comments

Comments

@loganbenda
Copy link

loganbenda commented Mar 21, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant