Forked from Jose R. Padron and Aaron Berk's library, customized for my specific application using 9DoF-Stick by Sparkfun.

Fork of HMC5843 by Jose R Padron

HMC5843 is triple axis, digital interface compass (geomagnetic sensor).

This library is forked from Jose R. Padron and Aaron Berk's work.

This library is for specific application using 9DoF-Stick.

Datasheet:

http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC5843.pdf

HMC5843 は3軸のデジタルインターフェースを備えたコンパス(地磁気センサ)です。

このライブラリは 9DoF-Stick を使用した特定の企画のために保守しています。

mbed IDEが日本語をサポートするまでは英語でコメントを書いていきますが、サポートした後もきっと英語で書いていくでしょう。

Files at this revision

API Documentation at this revision

Comitter:
gltest26
Date:
Wed Sep 12 15:54:13 2012 +0000
Parent:
3:69f24a492b69
Child:
5:a83508250db7
Commit message:
Changed internal i2c_ object from a pointer to embedded object.; In this way you don't need to manage heap memory manually and can avoid overheads of memory management.

Changed in this revision

HMC5843.cpp Show annotated file Show diff for this revision Revisions of this file
HMC5843.h Show annotated file Show diff for this revision Revisions of this file
--- a/HMC5843.cpp	Sun Sep 09 17:26:31 2012 +0000
+++ b/HMC5843.cpp	Wed Sep 12 15:54:13 2012 +0000
@@ -1,4 +1,5 @@
 /**
+ * @file HMC5843.cpp
  * @author Jose R. Padron
  * @author Used HMC6352 library  developed by Aaron Berk as template
  * @section LICENSE
@@ -32,16 +33,13 @@
  * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf
  */
 
-/**
- * Includes
- */
+
 #include "HMC5843.h"
 
-HMC5843::HMC5843(PinName sda, PinName scl) {
+HMC5843::HMC5843(PinName sda, PinName scl) : i2c_(sda, scl){
 
-    i2c_ = new I2C(sda, scl);
     //100KHz, as specified by the datasheet.
-    i2c_->frequency(100000);
+    i2c_.frequency(100000);
 
 
 }
@@ -54,7 +52,7 @@
     tx[0]=address;
     tx[1]=data;
 
-    i2c_->write(HMC5843_I2C_WRITE,tx,2);
+    i2c_.write(HMC5843_I2C_WRITE,tx,2);
    
     wait_ms(100);
 
@@ -82,11 +80,11 @@
    tx[0]=HMC5843_IDENT_A;
     
        
-    i2c_->write(HMC5843_I2C_WRITE, tx,1);
+    i2c_.write(HMC5843_I2C_WRITE, tx,1);
    
     wait_ms(1);
     
-    i2c_->read(HMC5843_I2C_READ,rx,3);
+    i2c_.read(HMC5843_I2C_READ,rx,3);
     
     buffer[0]=rx[0];
     buffer[1]=rx[1];
@@ -109,40 +107,19 @@
 
 
 void HMC5843::readData(int* readings) {
-
-  
-    char tx[1];
-/*    char rx[2];
-    
-    
-    tx[0]=HMC5843_X_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    readings[0]= (int)int16_t(uint16_t(rx[0])<<8|uint16_t(rx[1]));
+    char tx[1] = {HMC5843_X_MSB};
+    i2c_.write(HMC5843_I2C_READ, tx, 1);
 
-     
-    tx[0]=HMC5843_Y_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    readings[1]= (int)int16_t(uint16_t(rx[0])<<8|uint16_t(rx[1]));
-     
-    tx[0]=HMC5843_Z_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    readings[2]= (int)int16_t(uint16_t(rx[0])<<8|uint16_t(rx[1]));*/
-
+    char rx[6];
     // Burst read all registers to enhance bus speed.
-    tx[0]=HMC5843_X_MSB;
-    char rx[6];
-    i2c_->write(HMC5843_I2C_READ, tx, 1);
-    i2c_->read(HMC5843_I2C_READ, rx, 6);
+    i2c_.read(HMC5843_I2C_READ, rx, 6);
 
     // Readings are expressed in 16bit 2's complement, so we must first
     // concatenate two bytes to make a word and sign extend it to obtain
     // correct negative values.
-    readings[0] = int(int16_t(uint16_t(rx[0]) << 8 | uint16_t(rx[1])));
-    readings[1] = int(int16_t(uint16_t(rx[2]) << 8 | uint16_t(rx[3])));
-    readings[2] = int(int16_t(uint16_t(rx[4]) << 8 | uint16_t(rx[5])));
+    readings[0] = int(int16_t(unsigned(rx[0]) << 8 | unsigned(rx[1])));
+    readings[1] = int(int16_t(unsigned(rx[2]) << 8 | unsigned(rx[3])));
+    readings[2] = int(int16_t(unsigned(rx[4]) << 8 | unsigned(rx[5])));
 
 }
 
@@ -153,8 +130,8 @@
     
     
     tx[0]=HMC5843_X_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
+    i2c_.write(HMC5843_I2C_READ,tx,1);
+    i2c_.read(HMC5843_I2C_READ,rx,2);
     return ((int)rx[0]<<8|(int)rx[1]);
 
 }
@@ -166,8 +143,8 @@
     
     
     tx[0]=HMC5843_Y_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
+    i2c_.write(HMC5843_I2C_READ,tx,1);
+    i2c_.read(HMC5843_I2C_READ,rx,2);
     return ((int)rx[0]<<8|(int)rx[1]);
  
 }
@@ -180,8 +157,8 @@
     
     
     tx[0]=HMC5843_Z_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
+    i2c_.write(HMC5843_I2C_READ,tx,1);
+    i2c_.read(HMC5843_I2C_READ,rx,2);
     return ((int)rx[0]<<8|(int)rx[1]);
  
 }
--- a/HMC5843.h	Sun Sep 09 17:26:31 2012 +0000
+++ b/HMC5843.h	Wed Sep 12 15:54:13 2012 +0000
@@ -1,4 +1,5 @@
 /**
+ * @file HMC5843.h
  * @author Jose R. Padron
  * @author Used HMC5843 library  developed by Aaron Berk as template
  * @section LICENSE
@@ -35,14 +36,9 @@
 #ifndef HMC5843_H
 #define HMC5843_H
 
-/**
- * Includes
- */
 #include "mbed.h"
 
-/**
- * Defines
- */
+
 #define HMC5843_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read.
 #define HMC5843_I2C_WRITE   0x3C 
 #define HMC5843_I2C_READ    0x3D 
@@ -208,8 +204,18 @@
     int getStatus(void);
 
   
+private:
 
-    I2C* i2c_;
+    /**
+     * Internal I2C object for communicating with the device.
+     *
+     * Changed from a pointer (new allocated object) from embedded object.
+     * The I2C class object is not meant to be allocated in the heap, but
+     * in the global storage.
+     * There's no point dynamically allocating an I2C object in this class,
+     * at least you should write destructor to release it!
+     */
+    I2C i2c_;