1 /* 2 * Author: Brendan Le Foll <brendan.le.foll@intel.com> 3 * Contributions: Mihai Tudor Panu <mihai.tudor.panu@intel.com> 4 * Copyright (c) 2014 Intel Corporation. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 #pragma once 26 27 #include <mraa/i2c.hpp> 28 29 #define MAX_BUFFER_LENGTH 6 30 31 namespace upm { 32 33 /** 34 * @brief HMC5883L Magnometer library 35 * @defgroup hmc5883l libupm-hmc5883l 36 * @ingroup seeed i2c compass robok 37 */ 38 39 /** 40 * @library hmc5883l 41 * @sensor hmc5883l 42 * @comname HMC5883L 3-Axis Digital Compass 43 * @altname Grove 3-Axis Digital Compass 44 * @type compass 45 * @man seeed 46 * @con i2c 47 * @kit robok 48 * 49 * @brief API for the HMC5883L 3-Axis Digital Compass 50 * 51 * Honeywell [HMC5883L] 52 * (http://www.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf) 53 * is a 3-axis digital compass. Communication with HMC5883L is simple and 54 * all done through an I2C interface. Different breakout boards are available. 55 * Typically, a 3V supply is all that is needed to power the sensor. 56 * 57 * @image html hmc5883l.jpeg 58 * @snippet hmc5883l.cxx Interesting 59 */ 60 class Hmc5883l { 61 public: 62 /** 63 * Creates an Hmc5883l object 64 * 65 * @param bus Number of the used I2C bus 66 */ 67 Hmc5883l(int bus); 68 69 /* 70 * Returns the direction 71 */ 72 float direction(); 73 74 /* 75 * Returns the heading 76 */ 77 float heading(); 78 79 /** 80 * Returns a pointer to an int[3] that contains the coordinates as ints 81 * 82 * @return *int to an int[3] 83 */ 84 int16_t* coordinates(); 85 86 /** 87 * Updates the values by reading from I2C 88 * 89 * @return 0 if successful 90 */ 91 mraa::Result update(); 92 93 /** 94 * Sets the magnetic declination for better calibration 95 */ 96 void set_declination(float dec); 97 98 /** 99 * Gets the current magnetic declination value 100 * 101 * @return Magnetic declination as a floating-point value 102 */ 103 float get_declination(); 104 private: 105 int16_t m_coor[3]; 106 float m_declination; 107 uint8_t m_rx_tx_buf[MAX_BUFFER_LENGTH]; 108 mraa::I2c m_i2c; 109 }; 110 111 } 112