COSA
An Object-Oriented Platform for Arduino Programming
HMC5883L.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_HMC5883L_HH
22 #define COSA_HMC5883L_HH
23 
24 #include "Cosa/TWI.hh"
25 #include "Cosa/Power.hh"
26 #include "Cosa/IOStream.hh"
27 
57 class HMC5883L : public TWI::Driver {
58 public:
62  enum Bias {
63  NORMAL_BIAS = 0x00,
64  POSITIVE_BIAS = 0x01,
66  } __attribute__((packed));
67 
68  enum Rate {
76  } __attribute__((packed));
77 
78  enum Avg {
83  } __attribute__((packed));
84 
88  enum Range {
97  } __attribute__((packed));
98 
102  enum Mode {
106  } __attribute__((packed));
107 
111  struct status_t {
112  uint8_t ready:1;
113  uint8_t lock:1;
114  uint8_t reserved:6;
116  {
117  ready = 0;
118  lock = 0;
119  reserved = 0;
120  }
121  };
122 
126  struct data_t {
127  int16_t x;
128  int16_t y;
129  int16_t z;
131  {
132  x = y = z = 0;
133  }
134  };
135 
136 public:
140  HMC5883L(uint8_t mode = SLEEP_MODE_IDLE) :
141  TWI::Driver(0x1e),
142  m_config(),
143  m_mode(mode),
144  m_overflow(false),
145  m_output()
146  {}
147 
153  bool begin();
154 
160  bool end()
161  {
162  return (mode(IDLE_MEASUREMENT_MODE));
163  }
164 
169  void await_mode(uint8_t mode)
170  {
171  m_mode = mode;
172  }
173 
179  void bias(Bias bias)
180  {
181  m_config.MS = bias;
182  }
183 
189  void output_rate(Rate rate)
190  {
191  m_config.DO = rate;
192  }
193 
199  void samples_avg(Avg avg)
200  {
201  m_config.MA = avg;
202  }
203 
210  {
211  m_config.GN = range;
212  }
213 
220  bool write_config();
221 
226  void heading(data_t& data) const
227  {
228  data = m_output;
229  }
230 
240  bool mode(Mode mode);
241 
248  bool read_status(status_t& status);
249 
254  bool available()
255  __attribute__((always_inline))
256  {
257  status_t current;
258  if (!read_status(current)) return (false);
259  return (current.ready);
260  }
261 
268  {
269  return (mode(SINGLE_MEASUREMENT_MODE));
270  }
271 
276  void await()
277  __attribute__((always_inline))
278  {
279  while (!available()) Power::sleep(m_mode);
280  }
281 
287  bool read_heading();
288 
295  bool read_heading(data_t& data)
296  __attribute__((always_inline))
297  {
298  if (!read_heading()) return (false);
299  heading(data);
300  return (true);
301  }
302 
308  bool is_overflow() const
309  {
310  return (m_overflow);
311  }
312 
317  void to_milli_gauss();
318 
319 protected:
323  enum Register {
324  CONFIG = 0x00,
325  MODE = 0x02,
326  OUTPUT = 0x03,
327  STATUS = 0x09,
328  IDENTITY = 0x0a
329  } __attribute__((packed));
330 
332  static const uint16_t s_gain[] PROGMEM;
333 
335  struct config_t {
336  uint8_t MS:2;
337  uint8_t DO:3;
338  uint8_t MA:2;
339  uint8_t reserved:6;
340  uint8_t GN:3;
342  {
343  MS = NORMAL_BIAS;
344  DO = OUTPUT_RATE_15_HZ;
345  MA = SAMPLES_AVG_1;
346  reserved = 0;
347  GN = RANGE_1_3_GA;
348  }
349  };
352 
354  uint8_t m_mode;
355 
358 
361 };
362 
369 extern IOStream& operator<<(IOStream& outs, HMC5883L& compass);
370 
371 #endif
Identity register(0-2).
Definition: HMC5883L.hh:328
Definition: TWI.hh:51
uint8_t GN
Gain configuration bits.
Definition: HMC5883L.hh:340
IOStream & operator<<(IOStream &outs, HMC5883L &compass)
Definition: HMC5883L.cpp:115
Mode register.
Definition: HMC5883L.hh:325
Output data register X, Y, Z.
Definition: HMC5883L.hh:326
Status register.
Definition: HMC5883L.hh:327
uint8_t m_mode
Definition: HMC5883L.hh:354
bool mode(Mode mode)
Definition: HMC5883L.cpp:62
Table 5: Data Output Rates.
Definition: HMC5883L.hh:69
static const uint16_t s_gain[]
Definition: HMC5883L.hh:332
uint8_t MS
Measurement configuration bits.
Definition: HMC5883L.hh:336
bool begin()
Definition: HMC5883L.cpp:35
Number of averaged samples per output.
Definition: HMC5883L.hh:79
uint8_t MA
Number of samples to average.
Definition: HMC5883L.hh:338
uint8_t DO
Data output rate.
Definition: HMC5883L.hh:337
bool sample_heading_request()
Definition: HMC5883L.hh:267
void heading(data_t &data) const
Definition: HMC5883L.hh:226
bool end()
Definition: HMC5883L.hh:160
bool read_heading(data_t &data)
Definition: HMC5883L.hh:295
bool m_overflow
Definition: HMC5883L.hh:357
bool available()
Definition: HMC5883L.hh:254
static void sleep(uint8_t mode=POWER_SLEEP_MODE)
Definition: Power.cpp:30
void range(Range range)
Definition: HMC5883L.hh:209
data_t m_output
Definition: HMC5883L.hh:360
config_t m_config
Definition: HMC5883L.hh:351
Table 9: Gain Setting.
Definition: HMC5883L.hh:89
bool is_overflow() const
Definition: HMC5883L.hh:308
HMC5883L(uint8_t mode=SLEEP_MODE_IDLE)
Definition: HMC5883L.hh:140
Table 6: Measurement Modes.
Definition: HMC5883L.hh:63
Driver(uint8_t addr)
Definition: TWI.hh:70
Configuration register A, B.
Definition: HMC5883L.hh:324
void bias(Bias bias)
Definition: HMC5883L.hh:179
bool read_heading()
Definition: HMC5883L.cpp:81
bool read_status(status_t &status)
Definition: HMC5883L.cpp:71
void await_mode(uint8_t mode)
Definition: HMC5883L.hh:169
bool write_config()
Definition: HMC5883L.cpp:53
void output_rate(Rate rate)
Definition: HMC5883L.hh:189
void await()
Definition: HMC5883L.hh:276
void samples_avg(Avg avg)
Definition: HMC5883L.hh:199
void to_milli_gauss()
Definition: HMC5883L.cpp:102