hiroya taura / Mbed 2 deprecated LAURUS_program

Dependencies:   ConfigFile SDFileSystem mbed

Fork of LAURUS_program by LAURUS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC5883L.cpp Source File

HMC5883L.cpp

00001 #include "mbed.h"
00002 #include "HMC5883L.h"
00003 
00004 HMC5883L::HMC5883L(I2C* p_i2c): i2c(p_i2c) {}
00005 
00006 HMC5883L::~HMC5883L() {
00007     i2c = NULL;
00008 }
00009 
00010 int HMC5883L::init() {
00011     return 1;
00012 }
00013 
00014 int HMC5883L::read() {
00015     char cmd[2] = {0x02, 0x01};
00016     int ret = i2c->write(hmc_addr, cmd, 2);
00017     if(ret != 0) return 0;
00018     
00019     cmd[0] = 0x03;
00020     i2c->write(hmc_addr, cmd, 1);
00021     i2c->read(hmc_addr | 0x01, data.reg, 6, true);
00022     
00023     // データのHとLが逆に読み込まれているのでスワップする
00024     for(int i=0; i<3; i++) {
00025         char temp = 0;
00026         temp = data.reg[i*2];
00027         data.reg[i*2] = data.reg[i*2+1];
00028         data.reg[i*2+1] = temp;
00029     }
00030     
00031     // 軸を加速度センサーとあわせる
00032     int16_t temp = data.value[1];
00033     data.value[1] = data.value[2];
00034     data.value[2] = temp;
00035     
00036     temp = data.value[1];
00037     data.value[1] = -data.value[0];
00038     data.value[0] = temp;
00039     
00040     
00041     return 1;
00042 }