COSA
An Object-Oriented Platform for Arduino Programming
L3G4200D.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_L3G4200D_HH
22 #define COSA_L3G4200D_HH
23 
24 #include "Cosa/TWI.hh"
25 #include "Cosa/IOStream.hh"
26 
55 class L3G4200D : private TWI::Driver {
56 public:
62  L3G4200D(uint8_t subaddr = 0) : TWI::Driver(0x68 | (subaddr != 0)) {}
63 
68  bool begin();
69 
75  bool end();
76 
80  struct sample_t {
81  int x;
82  int y;
83  int z;
84  };
85 
90  void sample(sample_t& s)
91  __attribute__((always_inline))
92  {
93  read(OUT, &s, sizeof(s));
94  }
95 
96 protected:
100  enum Register {
101  WHO_A_I = 0x0F,
102  CTRL_REG1 = 0x20,
103  CTRL_REG2 = 0x21,
104  CTRL_REG3 = 0x22,
105  CTRL_REG4 = 0x23,
106  CTRL_REG5 = 0x24,
107  REFERENCE = 0x25,
108  OUT_TEMP = 0x26,
109  STATUS_REG = 0x27,
110  OUT = 0x28,
111  OUT_X = 0x28,
112  OUT_X_L = 0x28,
113  OUT_X_H = 0x29,
114  OUT_Y = 0x2A,
115  OUT_Y_L = 0x2A,
116  OUT_Y_H = 0x2B,
117  OUT_Z = 0x2C,
118  OUT_Z_L = 0x2C,
119  OUT_Z_H = 0x2D,
120  FIFO_CTRL_REG = 0x2E,
121  FIFO_SRC_REG = 0x2F,
122  INT1_CFG = 0x30,
123  INT1_SRC = 0x31,
124  INT1_TSH = 0x32,
125  INT1_TSH_XH = 0x32,
126  INT1_TSH_XL = 0x33,
127  INT1_TSH_YH = 0x34,
128  INT1_TSH_YL = 0x35,
129  INT1_TSH_ZH = 0x36,
130  INT1_TSH_ZL = 0x37,
132  } __attribute__((packed));
133 
135  static const uint8_t AUTO_INC = 0x80;
136 
140  union ctrl_reg1_t {
141  uint8_t as_uint8;
142  struct {
143  uint8_t Xen:1;
144  uint8_t Yen:1;
145  uint8_t Zen:1;
146  uint8_t PD:1;
147  uint8_t BW:2;
148  uint8_t DR:2;
149  };
150  ctrl_reg1_t(uint8_t value = 0)
151  {
152  as_uint8 = value;
153  }
154  operator uint8_t()
155  {
156  return (as_uint8);
157  }
158  };
159  enum {
164  } __attribute__((packed));
165  enum {
170  } __attribute__((packed));
171 
175  union ctrl_reg2_t {
176  uint8_t as_uint8;
177  struct {
178  uint8_t HPCF:4;
179  uint8_t HPM:2;
180  uint8_t reserved:2;
181  };
182  ctrl_reg2_t(uint8_t value = 0)
183  {
184  as_uint8 = value;
185  }
186  operator uint8_t()
187  {
188  return (as_uint8);
189  }
190  };
191  enum {
193  HPM_MODE = 1,
195  } __attribute__((packed));
196 
200  union ctrl_reg3_t {
201  uint8_t as_uint8;
202  struct {
203  uint8_t I2_Empty:1;
204  uint8_t I2_ORun:1;
205  uint8_t I2_WTM:1;
206  uint8_t I2_DRDY:1;
207  uint8_t PP_OD:1;
208  uint8_t H_Lactive:1;
209  uint8_t I1_Boot:1;
210  uint8_t I1_Int1:1;
211  };
212  ctrl_reg3_t(uint8_t value = 0)
213  {
214  as_uint8 = value;
215  }
216  operator uint8_t()
217  {
218  return (as_uint8);
219  }
220  };
221 
225  union ctrl_reg4_t {
226  uint8_t as_uint8;
227  struct {
228  uint8_t SIM:1;
229  uint8_t ST:2;
230  uint8_t reserved:1;
231  uint8_t FS:2;
232  uint8_t BLE:1;
233  uint8_t BDU:1;
234  };
235  ctrl_reg4_t(uint8_t value = 0)
236  {
237  as_uint8 = value;
238  }
239  operator uint8_t()
240  {
241  return (as_uint8);
242  }
243  };
244 
248  union ctrl_reg5_t {
249  uint8_t as_uint8;
250  struct {
251  uint8_t Out_Sel:2;
252  uint8_t INT1_Sel:2;
253  uint8_t HPen:1;
254  uint8_t FIFO_EN:1;
255  uint8_t BOOT:1;
256  };
257  ctrl_reg5_t(uint8_t value = 0)
258  {
259  as_uint8 = value;
260  }
261  operator uint8_t()
262  {
263  return (as_uint8);
264  }
265  };
266 
270  union status_reg_t {
271  uint8_t as_uint8;
272  struct {
273  uint8_t XDA:1;
274  uint8_t YDA:1;
275  uint8_t ZDA:1;
276  uint8_t XYZDA:1;
277  uint8_t XOR:1;
278  uint8_t YOR:1;
279  uint8_t ZOR:1;
280  uint8_t XYZOR:1;
281  };
282  status_reg_t(uint8_t value = 0)
283  {
284  as_uint8 = value;
285  }
286  operator uint8_t()
287  {
288  return (as_uint8);
289  }
290  };
291 
296  uint8_t as_uint8;
297  struct {
298  uint8_t WTM:5;
299  uint8_t FM:3;
300  };
301  fifo_ctrl_reg_t(uint8_t value = 0)
302  {
303  as_uint8 = value;
304  }
305  operator uint8_t()
306  {
307  return (as_uint8);
308  }
309  };
310  enum {
316  } __attribute__((packed));
317 
322  uint8_t as_uint8;
323  struct {
324  uint8_t FSS:5;
325  uint8_t EMPTY:1;
326  uint8_t OVRN:1;
327  uint8_t WTM:1;
328  };
329  fifo_src_reg_t(uint8_t value = 0)
330  {
331  as_uint8 = value;
332  }
333  operator uint8_t()
334  {
335  return (as_uint8);
336  }
337  };
338 
342  union int1_cfg_t {
343  uint8_t as_uint8;
344  struct {
345  uint8_t XLIE:1;
346  uint8_t XHIE:1;
347  uint8_t YLIE:1;
348  uint8_t YHIE:1;
349  uint8_t ZLIE:1;
350  uint8_t ZHIE:1;
351  uint8_t LIR:1;
352  uint8_t AND_OR:1;
353  };
354  int1_cfg_t(uint8_t value = 0)
355  {
356  as_uint8 = value;
357  }
358  operator uint8_t()
359  {
360  return (as_uint8);
361  }
362  };
363 
367  struct int1_src_t {
368  uint8_t as_uint8;
369  struct {
370  uint8_t XL:1;
371  uint8_t XH:1;
372  uint8_t YL:1;
373  uint8_t YH:1;
374  uint8_t ZL:1;
375  uint8_t ZH:1;
376  uint8_t IA:1;
377  uint8_t reserved:1;
378  };
379  int1_src_t(uint8_t value = 0)
380  {
381  as_uint8 = value;
382  }
383  operator uint8_t()
384  {
385  return (as_uint8);
386  }
387  };
388 
393  uint8_t as_uint8;
394  struct {
395  uint8_t value:7;
396  uint8_t WAIT:1;
397  };
398  int1_duration_t(uint8_t value = 0)
399  {
400  as_uint8 = value;
401  }
402  operator uint8_t()
403  {
404  return (as_uint8);
405  }
406  };
407 
413  void write(Register reg, uint8_t value);
414 
421  void write(Register reg, void* buffer, uint8_t count);
422 
428  uint8_t read(Register reg);
429 
436  void read(Register reg, void* buffer, uint8_t count);
437 };
438 
439 extern IOStream& operator<<(IOStream& outs, L3G4200D& gyroscope);
440 
441 #endif
Reference value for interrupt generation.
Definition: L3G4200D.hh:107
Definition: TWI.hh:51
IOStream & operator<<(IOStream &outs, L3G4200D &gyroscope)
Definition: L3G4200D.cpp:79
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:176
X low threshold.
Definition: L3G4200D.hh:126
Wait duration.
Definition: L3G4200D.hh:131
Z low byte.
Definition: L3G4200D.hh:118
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:249
ctrl_reg1_t(uint8_t value=0)
Definition: L3G4200D.hh:150
fifo_src_reg_t(uint8_t value=0)
Definition: L3G4200D.hh:329
uint8_t read(Register reg)
Definition: L3G4200D.cpp:59
Y low threshold.
Definition: L3G4200D.hh:128
Control register#3.
Definition: L3G4200D.hh:104
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:393
ctrl_reg4_t(uint8_t value=0)
Definition: L3G4200D.hh:235
Y high threshold.
Definition: L3G4200D.hh:127
X high threshold.
Definition: L3G4200D.hh:125
FIFO control register.
Definition: L3G4200D.hh:120
Interrupt source register.
Definition: L3G4200D.hh:123
Z high threshold.
Definition: L3G4200D.hh:129
L3G4200D(uint8_t subaddr=0)
Definition: L3G4200D.hh:62
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:201
Interrupt threshold.
Definition: L3G4200D.hh:124
Bandwidth selection (table 22).
Definition: L3G4200D.hh:166
static const uint8_t AUTO_INC
Definition: L3G4200D.hh:135
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:322
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:296
bool end()
Definition: L3G4200D.cpp:36
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:271
Status register.
Definition: L3G4200D.hh:109
X,Y,Z-axis angular rate data.
Definition: L3G4200D.hh:110
void sample(sample_t &s)
Definition: L3G4200D.hh:90
FIFO status register.
Definition: L3G4200D.hh:121
FIFO mode.
Definition: L3G4200D.hh:312
X axis angular rate data.
Definition: L3G4200D.hh:111
Control register#4.
Definition: L3G4200D.hh:105
int1_duration_t(uint8_t value=0)
Definition: L3G4200D.hh:398
Control register#2.
Definition: L3G4200D.hh:103
Y high byte.
Definition: L3G4200D.hh:116
Driver(uint8_t addr)
Definition: TWI.hh:70
ctrl_reg2_t(uint8_t value=0)
Definition: L3G4200D.hh:182
Control register#5.
Definition: L3G4200D.hh:106
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:368
Autoreset on interrupt event.
Definition: L3G4200D.hh:194
Device identification register.
Definition: L3G4200D.hh:101
bool begin()
Definition: L3G4200D.cpp:24
Reference signal for filtering.
Definition: L3G4200D.hh:193
Interrupt configuration.
Definition: L3G4200D.hh:122
Stream-to-FIFO.
Definition: L3G4200D.hh:314
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:226
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:141
High pass filter mode (table 26).
Definition: L3G4200D.hh:192
ctrl_reg5_t(uint8_t value=0)
Definition: L3G4200D.hh:257
int1_src_t(uint8_t value=0)
Definition: L3G4200D.hh:379
Z high byte.
Definition: L3G4200D.hh:119
int1_cfg_t(uint8_t value=0)
Definition: L3G4200D.hh:354
FIFO mode configuration (table 45).
Definition: L3G4200D.hh:311
X high byte.
Definition: L3G4200D.hh:113
X low byte.
Definition: L3G4200D.hh:112
uint8_t as_uint8
As a byte.
Definition: L3G4200D.hh:343
Bypass-to-Stream.
Definition: L3G4200D.hh:315
Y axis angular rate data.
Definition: L3G4200D.hh:117
Data rate (table 22).
Definition: L3G4200D.hh:160
Temperature data.
Definition: L3G4200D.hh:108
Control register#1.
Definition: L3G4200D.hh:102
ctrl_reg3_t(uint8_t value=0)
Definition: L3G4200D.hh:212
void write(Register reg, uint8_t value)
Definition: L3G4200D.cpp:43
Y axis angular rate data.
Definition: L3G4200D.hh:114
fifo_ctrl_reg_t(uint8_t value=0)
Definition: L3G4200D.hh:301
Z low threshold.
Definition: L3G4200D.hh:130
status_reg_t(uint8_t value=0)
Definition: L3G4200D.hh:282
Y low byte.
Definition: L3G4200D.hh:115