HMC5883L.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*****************************************************************************/
  2. // Function: Header file for HMC5883L
  3. // Hardware: Grove - 3-Axis Digital Compass
  4. // Arduino IDE: Arduino-1.0
  5. // Author: Frankie.Chu
  6. // Date: Jan 10,2013
  7. // Version: v1.0
  8. // by www.seeedstudio.com
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2.1 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the Free Software
  22. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. //
  24. /*******************************************************************************/
  25. #ifndef __HMC5883L_H__
  26. #define __HMC5883L_H__
  27. #include <Arduino.h>
  28. #include <Wire.h>
  29. #define HMC5883L_ADDRESS 0x1E
  30. #define CONFIGURATION_REGISTERA 0x00
  31. #define CONFIGURATION_REGISTERB 0x01
  32. #define MODE_REGISTER 0x02
  33. #define DATA_REGISTER_BEGIN 0x03
  34. #define MEASUREMENT_CONTINUOUS 0x00
  35. #define MEASUREMENT_SINGLE_SHOT 0x01
  36. #define MEASUREMENT_IDLE 0x03
  37. #define ERRORCODE_1 "Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1"
  38. #define ERRORCODE_1_NUM 1
  39. #define ERRORCODE_2 "Entered amount of samples was not valid, valid amounts are: 1, 2, 4, 8"
  40. #define ERRORCODE_2_NUM 2
  41. struct MagnetometerScaled {
  42. float XAxis;
  43. float YAxis;
  44. float ZAxis;
  45. };
  46. struct MagnetometerRaw {
  47. short XAxis;
  48. short YAxis;
  49. short ZAxis;
  50. };
  51. class HMC5883L {
  52. public: // used by xadow phone
  53. void initCompass();
  54. int getCompass();
  55. public:
  56. HMC5883L(TwoWire& w = Wire);
  57. MagnetometerRaw readRawAxis();
  58. MagnetometerScaled readScaledAxis();
  59. short setMeasurementMode(uint8_t mode);
  60. short setAverageSamples(uint8_t mode);
  61. short setScale(float gauss);
  62. const char* getErrorText(short errorCode);
  63. protected:
  64. void write(short address, short byte);
  65. uint8_t* read(short address, short length);
  66. private:
  67. TwoWire* _wire;
  68. float m_Scale;
  69. uint8_t _buffer[16];
  70. };
  71. #endif