9 #define D20_SA0_HIGH_ADDRESS 0b1101011 // also applies to D20H
10 #define D20_SA0_LOW_ADDRESS 0b1101010 // also applies to D20H
11 #define L3G4200D_SA0_HIGH_ADDRESS 0b1101001
12 #define L3G4200D_SA0_LOW_ADDRESS 0b1101000
14 #define TEST_REG_ERROR -1
16 #define D20H_WHO_ID 0xD7
17 #define D20_WHO_ID 0xD4
18 #define L3G4200D_WHO_ID 0xD3
24 _device = device_auto;
33 bool L3G::timeoutOccurred()
35 bool tmp = did_timeout;
40 void L3G::setTimeout(
unsigned int timeout)
45 unsigned int L3G::getTimeout()
50 bool L3G::init(deviceType device, sa0State sa0)
55 if (device == device_auto || sa0 == sa0_auto)
58 if (device == device_auto || device == device_D20H || device == device_D20)
61 if (sa0 != sa0_low && (
id = testReg(D20_SA0_HIGH_ADDRESS, WHO_AM_I)) != TEST_REG_ERROR)
65 if (device == device_auto)
68 device = (
id == D20H_WHO_ID) ? device_D20H : device_D20;
72 else if (sa0 != sa0_high && (
id = testReg(D20_SA0_LOW_ADDRESS, WHO_AM_I)) != TEST_REG_ERROR)
76 if (device == device_auto)
79 device = (
id == D20H_WHO_ID) ? device_D20H : device_D20;
85 if (device == device_auto || device == device_4200D)
87 if (sa0 != sa0_low && testReg(L3G4200D_SA0_HIGH_ADDRESS, WHO_AM_I) == L3G4200D_WHO_ID)
90 device = device_4200D;
93 else if (sa0 != sa0_high && testReg(L3G4200D_SA0_LOW_ADDRESS, WHO_AM_I) == L3G4200D_WHO_ID)
96 device = device_4200D;
102 if (device == device_auto || sa0 == sa0_auto)
115 address = (sa0 == sa0_high) ? D20_SA0_HIGH_ADDRESS : D20_SA0_LOW_ADDRESS;
119 address = (sa0 == sa0_high) ? L3G4200D_SA0_HIGH_ADDRESS : L3G4200D_SA0_LOW_ADDRESS;
135 void L3G::enableDefault(
void)
137 if (_device == device_D20H)
141 writeReg(LOW_ODR, 0x00);
146 writeReg(CTRL_REG4, 0x00);
150 writeReg(CTRL_REG1, 0x6F);
154 void L3G::writeReg(
byte reg,
byte value)
156 Wire.beginTransmission(address);
159 last_status = Wire.endTransmission();
163 byte L3G::readReg(
byte reg)
167 Wire.beginTransmission(address);
169 last_status = Wire.endTransmission();
170 Wire.requestFrom(address, (
byte)1);
172 Wire.endTransmission();
180 Wire.beginTransmission(address);
183 Wire.write(OUT_X_L | (1 << 7));
184 Wire.endTransmission();
185 Wire.requestFrom(address, (
byte)6);
187 unsigned int millis_start = millis();
188 while (Wire.available() < 6)
190 if (io_timeout > 0 && ((
unsigned int)millis() - millis_start) > io_timeout)
197 uint8_t xlg = Wire.read();
198 uint8_t xhg = Wire.read();
199 uint8_t ylg = Wire.read();
200 uint8_t yhg = Wire.read();
201 uint8_t zlg = Wire.read();
202 uint8_t zhg = Wire.read();
205 g.x = (int16_t)(xhg << 8 | xlg);
206 g.y = (int16_t)(yhg << 8 | ylg);
207 g.z = (int16_t)(zhg << 8 | zlg);
210 void L3G::vector_normalize(vector<float> *a)
212 float mag = sqrt(vector_dot(a,a));
220 int L3G::testReg(
byte address, regAddr reg)
222 Wire.beginTransmission(address);
223 Wire.write((
byte)reg);
224 if (Wire.endTransmission() != 0)
226 return TEST_REG_ERROR;
229 Wire.requestFrom(address, (
byte)1);
230 if (Wire.available())
236 return TEST_REG_ERROR;