Sille Van Landschoot / HMC5583L

Dependents:   m3Dpi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hmc5583l.cpp Source File

hmc5583l.cpp

00001 #include "hmc5583l.h"
00002 #include "stdint.h"
00003 
00004 HMC5583L::HMC5583L(I2C &_i2c, int _address): i2c(_i2c), address(_address)
00005 {
00006     initialize();
00007 }
00008 
00009 HMC5583L::HMC5583L(PinName sda, PinName scl, int _address): i2c(sda, scl), address(_address)
00010 {
00011     initialize();
00012 }
00013 
00014 void HMC5583L::initialize()
00015 {
00016     char data[2];
00017     data[0] = 0x02; // select mode register
00018     data[1] = 0x00; // continous measurement mode
00019 
00020     i2c.write(address, data, 2);   
00021 }
00022 
00023 coord HMC5583L::getCompass()
00024 {
00025     coord c;
00026     // tell where to begin reading data
00027     char data[] = {0x03}; // select register 3, MSB register
00028     i2c.write(address, data, 1);
00029     
00030     char values[6];
00031     i2c.read(address, values, 6);
00032     
00033     // x = (x >> 11) == 0 ? x : -1 ^ 0xFFF | x;
00034     c.x = regToInt(values[0], values[1]);
00035     c.y = regToInt(values[2], values[3]);
00036     c.z = regToInt(values[4], values[5]);
00037         
00038     return c;
00039 }
00040 
00041 int HMC5583L::regToInt(char msb, char lsb){
00042     int16_t value = msb << 8 | lsb;
00043     return (int) value;
00044 }