9 template <
typename T>
struct vector
14 enum deviceType { device_DLH, device_DLM, device_DLHC, device_D, device_auto };
15 enum sa0State { sa0_low, sa0_high, sa0_auto };
53 HP_FILTER_RESET_A = 0x25,
67 FIFO_CTRL_REG_A = 0x2E,
69 FIFO_SRC_REG_A = 0x2F,
78 INT1_DURATION_A = 0x33,
86 INT2_DURATION_A = 0x37,
97 TIME_LATENCY_A = 0x3C,
134 DLH_OUT_X_H_M = 0x03,
135 DLH_OUT_X_L_M = 0x04,
136 DLH_OUT_Y_H_M = 0x05,
137 DLH_OUT_Y_L_M = 0x06,
138 DLH_OUT_Z_H_M = 0x07,
139 DLH_OUT_Z_L_M = 0x08,
141 DLM_OUT_X_H_M = 0x03,
142 DLM_OUT_X_L_M = 0x04,
143 DLM_OUT_Z_H_M = 0x05,
144 DLM_OUT_Z_L_M = 0x06,
145 DLM_OUT_Y_H_M = 0x07,
146 DLM_OUT_Y_L_M = 0x08,
148 DLHC_OUT_X_H_M = 0x03,
149 DLHC_OUT_X_L_M = 0x04,
150 DLHC_OUT_Z_H_M = 0x05,
151 DLHC_OUT_Z_L_M = 0x06,
152 DLHC_OUT_Y_H_M = 0x07,
153 DLHC_OUT_Y_L_M = 0x08,
165 vector<int16_t> m_max;
166 vector<int16_t> m_min;
172 bool init(deviceType device = device_auto, sa0State sa0 = sa0_auto);
173 deviceType getDeviceType(
void) {
return _device; }
175 void enableDefault(
void);
177 void writeAccReg(
byte reg,
byte value);
178 byte readAccReg(
byte reg);
179 void writeMagReg(
byte reg,
byte value);
180 byte readMagReg(
int reg);
182 void writeReg(
byte reg,
byte value);
183 byte readReg(
int reg);
189 void setTimeout(
unsigned int timeout);
190 unsigned int getTimeout(
void);
191 bool timeoutOccurred(
void);
194 template <
typename T>
float heading(vector<T> from);
197 template <
typename Ta,
typename Tb,
typename To>
static void vector_cross(
const vector<Ta> *a,
const vector<Tb> *b, vector<To> *out);
198 template <
typename Ta,
typename Tb>
static float vector_dot(
const vector<Ta> *a,
const vector<Tb> *b);
199 static void vector_normalize(vector<float> *a);
206 static const int dummy_reg_count = 6;
207 regAddr translated_regs[dummy_reg_count + 1];
209 unsigned int io_timeout;
212 int testReg(
byte address, regAddr reg);
228 template <
typename T>
float LSM303::heading(vector<T> from)
230 vector<int32_t> temp_m = {m.x, m.y, m.z};
233 temp_m.x -= ((int32_t)m_min.x + m_max.x) / 2;
234 temp_m.y -= ((int32_t)m_min.y + m_max.y) / 2;
235 temp_m.z -= ((int32_t)m_min.z + m_max.z) / 2;
240 vector_cross(&temp_m, &a, &E);
241 vector_normalize(&E);
242 vector_cross(&a, &E, &N);
243 vector_normalize(&N);
246 float heading = atan2(vector_dot(&E, &from), vector_dot(&N, &from)) * 180 / PI;
247 if (heading < 0) heading += 360;
251 template <
typename Ta,
typename Tb,
typename To>
void LSM303::vector_cross(
const vector<Ta> *a,
const vector<Tb> *b, vector<To> *out)
253 out->x = (a->y * b->z) - (a->z * b->y);
254 out->y = (a->z * b->x) - (a->x * b->z);
255 out->z = (a->x * b->y) - (a->y * b->x);
258 template <
typename Ta,
typename Tb>
float LSM303::vector_dot(
const vector<Ta> *a,
const vector<Tb> *b)
260 return (a->x * b->x) + (a->y * b->y) + (a->z * b->z);