Skip to content

Instantly share code, notes, and snippets.

@treeherder
Last active August 29, 2015 13:56
Show Gist options
  • Save treeherder/9246336 to your computer and use it in GitHub Desktop.
Save treeherder/9246336 to your computer and use it in GitHub Desktop.
accelerometer structure for handling data in a way that might be sane
#include <stdio.h>
typedef struct ret ret;
struct ret {
char *id;
int accel_x;
int accel_y;
int accel_z;
int gyro_x;
int gyro_y;
int gyro_z;
};
// read from device
int
get_ret(ret *r) {
int devy;
// read device here
devy = 1;
// oops, device failed, return -1
// return -1;
// fill caller's struct
r->accel_y = devy;
return 0;
}
ret global;
int
main(void) {
ret stack, *heap;
if(get_ret(&global) < 0) {
// error
}
if(get_ret(&stack) < 0) {
// error
}
heap = malloc(sizeof(ret));
if(get_ret(heap) < 0) {
// error
free(heap);
}
}
#include <stdio.h>
struct ret
{
char *id;
int accel_x;
int accel_y;
int accel_z;
int gyro_x;
int gyro_y;
int gyro_z;
};
struct ret ret_init (char *id, int accel_x, int accel_y, int accel_z, int gyro_x, int gyro_y, int gyro_z) {
struct ret r;
r.id = id;
r.accel_x = accel_x;
r.accel_y = accel_y;
r.accel_z = accel_z;
r.gyro_x = gyro_x;
r.gyro_y = gyro_y;
r.gyro_z = gyro_z;
return r;
}
int main()
{
struct ret ret;
ret = ret_init("foo", 1, 2, 3, 1, 3, 2);
printf("%s %d %d %d %d %d %d\n", ret.id, ret.accel_x, ret.accel_y, ret.accel_z, ret.gyro_x, ret.gyro_y, ret.gyro_z);
}
void ret_init(struct ret *ret, char *id, int accel_x, int accel_y,
int accel_z, int gyro_x, int gyro_y,int gyro_z)
{
ret->id = id;
ret->x = x;
// rest here
}
int main()
{
struct ret ret;
ret_init(&ret);
struct ret
{
char *id;
int accel_x;
int accel_y;
int accel_z;
int gyro_x;
int gyro_y;
int gyro_z;
};
struct ret *ret_val(char *id, int accel_x, int accel_y, int accel_z, int gyro_x, int gyro_y,int gyro_z)
{
struct ret *ret_pntr = malloc(sizeof *ret_pntr);
//poll the accelerometer and put the values in the structure
return ret_pntr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment