COSA
An Object-Oriented Platform for Arduino Programming
HMC5883L.cpp
Go to the documentation of this file.
1 
21 #include "HMC5883L.hh"
22 
23 const uint16_t HMC5883L::s_gain[] __PROGMEM = {
24  1370,
25  1090,
26  820,
27  660,
28  440,
29  390,
30  330,
31  230
32 };
33 
34 bool
36 {
37  // Read the device identity register
38  uint8_t id[3];
39  twi.acquire(this);
40  twi.write((uint8_t) IDENTITY);
41  twi.read(id, sizeof(id));
42  twi.release();
43 
44  // Sanity check the identity
45  static const uint8_t ID[3] __PROGMEM = { 'H', '4', '3' };
46  if (memcmp_P(id, ID, sizeof(ID))) return (false);
47 
48  // Write configuration
49  return (write_config());
50 }
51 
52 bool
54 {
55  twi.acquire(this);
56  int count = twi.write((uint8_t) CONFIG, &m_config, sizeof(m_config));
57  twi.release();
58  return (count == (sizeof(m_config) + 1));
59 }
60 
61 bool
63 {
64  twi.acquire(this);
65  int count = twi.write((uint8_t) MODE, &mode, sizeof(mode));
66  twi.release();
67  return (count == (sizeof(mode) + 1));
68 }
69 
70 bool
72 {
73  twi.acquire(this);
74  twi.write((uint8_t) STATUS);
75  int count = twi.read(&status, sizeof(status));
76  twi.release();
77  return (count == sizeof(status));
78 }
79 
80 bool
82 {
83  // Read output data from the device
84  twi.acquire(this);
85  twi.write((uint8_t) OUTPUT);
86  int count = twi.read(&m_output, sizeof(m_output));
87  twi.release();
88  if (count != sizeof(m_output)) return (false);
89 
90  // Adjust to little endian
91  swap<data_t>(&m_output);
92 
93  // Check if an overflow occured
94  m_overflow =
95  (m_output.x == -4096) ||
96  (m_output.y == -4096) ||
97  (m_output.z == -4096);
98  return (true);
99 }
100 
101 void
103 {
104  // Do not scale if overflow
105  if (is_overflow()) return;
106 
107  // Scale with the current gain setting
108  uint16_t gain = pgm_read_word(&s_gain[m_config.GN]);
109  m_output.x = (1000L * m_output.x) / gain;
110  m_output.y = (1000L * m_output.y) / gain;
111  m_output.z = (1000L * m_output.z) / gain;
112 }
113 
114 IOStream&
115 operator<<(IOStream& outs, HMC5883L& compass)
116 {
117  if (compass.is_overflow()) {
118  outs << PSTR("HMC5883L(overflow)");
119  }
120  else {
121  HMC5883L::data_t value;
122  compass.heading(value);
123  outs << PSTR("HMC5883L(x = ") << value.x
124  << PSTR(", y = ") << value.y
125  << PSTR(", z = ") << value.z
126  << PSTR(")");
127  }
128  return (outs);
129 }
Identity register(0-2).
Definition: HMC5883L.hh:328
TWI twi
Definition: TWI.cpp:27
uint8_t GN
Gain configuration bits.
Definition: HMC5883L.hh:340
Mode register.
Definition: HMC5883L.hh:325
Output data register X, Y, Z.
Definition: HMC5883L.hh:326
Status register.
Definition: HMC5883L.hh:327
bool mode(Mode mode)
Definition: HMC5883L.cpp:62
static const uint16_t s_gain[]
Definition: HMC5883L.hh:332
#define PSTR(s)
Definition: Types.h:202
const uint16_t HMC5883L::s_gain[] __PROGMEM
Definition: HMC5883L.cpp:23
bool begin()
Definition: HMC5883L.cpp:35
void heading(data_t &data) const
Definition: HMC5883L.hh:226
bool m_overflow
Definition: HMC5883L.hh:357
void release()
Definition: TWI.cpp:58
data_t m_output
Definition: HMC5883L.hh:360
config_t m_config
Definition: HMC5883L.hh:351
int read(void *buf, size_t size)
Definition: TWI.hh:326
bool is_overflow() const
Definition: HMC5883L.hh:308
int write(void *buf, size_t size)
Definition: TWI.hh:282
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
Configuration register A, B.
Definition: HMC5883L.hh:324
bool read_heading()
Definition: HMC5883L.cpp:81
IOStream & operator<<(IOStream &outs, HMC5883L &compass)
Definition: HMC5883L.cpp:115
bool read_status(status_t &status)
Definition: HMC5883L.cpp:71
bool write_config()
Definition: HMC5883L.cpp:53
void to_milli_gauss()
Definition: HMC5883L.cpp:102