libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
l3gd20.h
Go to the documentation of this file.
1 #ifndef LIBPROPELLER_L3GD20_H_
2 #define LIBPROPELLER_L3GD20_H_
3 
4 #ifndef UNIT_TEST
5 #include "libpropeller/i2c/i2c.h"
6 #else
8 #endif
9 
19 class L3GD20 {
20 public:
21 
23  enum AddressLSB {
25  };
26 
29  L3GD20(){
30  bus_ = NULL;
31  status_ = false;
32  }
33 
53  bool Init(I2C * i2c_bus, const AddressLSB address = LSB_0) {
54  SetAddress(address);
55  bus_ = i2c_bus;
56 
57  //Check to make sure the gyro is actually there.
58  status_ = bus_->Ping(device_address);
59  if (status_ == false) {
60  return false;
61  }
62 
63  bus_->Put(device_address, kCTRL_REG1, 0b11111111);
64  bus_->Put(device_address, kCTRL_REG4, 0b00110000);
65 
66  return status_;
67  }
68 
79  bool ReadGyro(int& x, int& y, int& z) {
80  char data[6];
81 
82  if (status_ == false) {
83  x = y = z = 0;
84  return false;
85  }
86 
87  if (bus_->Get(device_address, kOUT_X_L, data, 6) == false) {
88  x = y = z = 0;
89  return false;
90  }
91  x = ((data[0] | (data[1] << 8)) << 16) >> 16;
92  y = ((data[2] | (data[3] << 8)) << 16) >> 16;
93  z = ((data[4] | (data[5] << 8)) << 16) >> 16;
94 
95  return true;
96  }
97 
98 private:
99  I2C * bus_;
100 
101  int status_;
102 
103 
104  const static unsigned char kCTRL_REG1 = 0x20;
105  const static unsigned char kCTRL_REG4 = 0x23;
106  const static unsigned char kOUT_X_L = 0x28 | 0x80; //(turn on auto increment)
107 
108  unsigned char device_address;
109 
110  void SetAddress(const AddressLSB address) {
111  if (address == LSB_0) {
112  device_address = 0b11010100;
113  } else if (address == LSB_1) {
114  device_address = 0b11010110;
115  }
116  }
117 
118 };
119 
120 #endif // LIBPROPELLER_L3GD20_H_