Pololu3piPlus32U4 library
Pololu3piPlus32U4IMU.h
Go to the documentation of this file.
1 // Copyright (C) Pololu Corporation. See www.pololu.com for details.
2 
12 
13 #pragma once
14 #include <Wire.h>
16 
17 namespace Pololu3piPlus32U4
18 {
19 
20 void IMU::writeReg(uint8_t addr, uint8_t reg, uint8_t value)
21 {
22  Wire.beginTransmission(addr);
23  Wire.write(reg);
24  Wire.write(value);
25  lastError = Wire.endTransmission();
26 }
27 
28 uint8_t IMU::readReg(uint8_t addr, uint8_t reg)
29 {
30  Wire.beginTransmission(addr);
31  Wire.write(reg);
32  lastError = Wire.endTransmission();
33  if (lastError) { return 0; }
34 
35  uint8_t byteCount = Wire.requestFrom(addr, (uint8_t)1);
36  if (byteCount != 1)
37  {
38  lastError = 50;
39  return 0;
40  }
41  return Wire.read();
42 }
43 
44 int16_t IMU::testReg(uint8_t addr, uint8_t reg)
45 {
46  Wire.beginTransmission(addr);
47  Wire.write(reg);
48  if (Wire.endTransmission() != 0)
49  {
50  return -1;
51  }
52 
53  uint8_t byteCount = Wire.requestFrom(addr, (uint8_t)1);
54  if (byteCount != 1)
55  {
56  return -1;
57  }
58  return Wire.read();
59 }
60 
61 void IMU::readAxes16Bit(uint8_t addr, uint8_t firstReg, vector<int16_t> & v)
62 {
63  Wire.beginTransmission(addr);
64  Wire.write(firstReg);
65  lastError = Wire.endTransmission();
66  if (lastError) { return; }
67 
68  uint8_t byteCount = (Wire.requestFrom(addr, (uint8_t)6));
69  if (byteCount != 6)
70  {
71  lastError = 50;
72  return;
73  }
74  uint8_t xl = Wire.read();
75  uint8_t xh = Wire.read();
76  uint8_t yl = Wire.read();
77  uint8_t yh = Wire.read();
78  uint8_t zl = Wire.read();
79  uint8_t zh = Wire.read();
80 
81  // combine high and low bytes
82  v.x = (int16_t)(xh << 8 | xl);
83  v.y = (int16_t)(yh << 8 | yl);
84  v.z = (int16_t)(zh << 8 | zl);
85 }
86 
87 }
void writeReg(uint8_t addr, uint8_t reg, uint8_t value)
Writes an 8-bit sensor register.
uint8_t readReg(uint8_t addr, uint8_t reg)
Reads an 8-bit sensor register.
Top-level namespace for the Pololu3piPlus32U4 library.