vec o = get_orientation();
の初期化のエラーみたいなのですが自力では解決できませんでした。
どうすれば解決できるでしょうか。
手をお貸しください。
コンパイラ gcc
OS Arch Linux
main.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/i2c-dev.h>
#include "mg.h"
int main(){
//Open I2C bus
if((i2c_bus = open(I2CNODE, O_RDWR)) < 0){return 0;}
int addr = I2CADDR;
if(ioctl(i2c_bus, I2C_SLAVE, addr) < 0) {return 0;}
vec o = get_orientation();
printf("X: %f, Y: %f, Z: %f\n", o.x, o.y, o.z);
return 0;
}
#define I2CNODE "/dev/i2c-1"
#define I2CADDR 0xE1
typedef struct vec {
float x;
float y;
float z;
} vec;
int i2c_bus;
char buf[8];
vec get_orientaton();
int write_byte(unsigned char reg, unsigned char data);
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <linux/i2c-dev.h>
#include mgl.h"
//Write a byte to an address
int write_byte(unsigned char reg, unsigned char data){
buf[0] = reg;
buf[1] = data;
if (write(i2c_bus, buf, 2) != 2) {
//printf("Failed to write to the i2c bus.\n");
//printf("%s\n\n",strerror(errno));
return 0;
}
return 1;
}
int read_multibyte(unsigned char reg, unsigned char * data, unsigned char len){
unsigned char ret = 0;
ret = write(i2c_bus, ®, 1);
ret = read(i2c_bus, data, len);
if (ret != len) {
printf("MultiByte read error\n");
}
return ret;
}
vec get_orientation() {
char xyz[6];
vec orientation;
read_multibyte(0x03, xyz, 6);
orientation.x = (xyz[0] << 8) | xyz[1];
orientation.y = (xyz[2] << 8) | xyz[3];
orientation.z = (xyz[4] << 8) | xyz[5];
/*
int i;
for(i=0;i<6;i++){
printf("%d\n",xyz[i]);
}
*/
return orientation;
}