MPU6050 library

gyro
NepEgor 2 years ago
parent 98c7f2637b
commit 8c27bd0cd7

@ -32,7 +32,8 @@ namespace InputMapper
bool mapButton(HardwareButtons button, bool value); bool mapButton(HardwareButtons button, bool value);
void mapGyro(int32_t x, int32_t y, int32_t z); bool gyroEnabled();
void mapGyro(int16_t x, int16_t y, int16_t z);
void sendReport(); void sendReport();
} }

@ -329,9 +329,15 @@ namespace InputMapper
return res; return res;
} }
void mapGyro(int32_t x, int32_t y, int32_t z) bool gyroEnabled()
{ {
device.joystick(1, x, y); // how do I map this?
return tjoystick_right.getTouching() > TouchControl::CT_NONE;
}
void mapGyro(int16_t x, int16_t y, int16_t z)
{
//device.joystick(1, x, y);
} }
void sendReport() void sendReport()

@ -1,7 +1,8 @@
#include <Arduino.h> #include <Arduino.h>
#include "Wire.h" #include <MPU6050.h>
const uint8_t MPU_addr = 0x68;
MPU6050 mpu;
#include "trackpad.h" #include "trackpad.h"
#include "input_mapper.h" #include "input_mapper.h"
@ -59,11 +60,21 @@ void setup()
trackpad_maxX = trackpad[0].getMaxX(); trackpad_maxX = trackpad[0].getMaxX();
trackpad_maxY = trackpad[0].getMaxY(); trackpad_maxY = trackpad[0].getMaxY();
Wire.begin(); #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.beginTransmission(MPU_addr); Wire.begin();
Wire.write(0x6B); // PWR_MGMT_1 register #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Wire.write(0); // set to zero (wakes up the MPU-6050) Fastwire::setup(400, true);
Wire.endTransmission(true); #endif
mpu.initialize();
Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
/*
mpu.setXGyroOffset(0);
mpu.setYGyroOffset(0);
mpu.setZGyroOffset(0);
*/
mpu.CalibrateGyro();
InputMapper::begin(); InputMapper::begin();
@ -139,18 +150,13 @@ void loop()
} }
} }
int16_t data[7]; if (InputMapper::gyroEnabled())
Wire.beginTransmission(MPU_addr); {
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) int16_t x, y, z;
Wire.endTransmission(false); mpu.getRotation(&x, &y, &z);
Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers InputMapper::mapGyro(x, y, z);
for (byte i = 0; i < 7; i++) {
data[i] = Wire.read() << 8 | Wire.read();
} }
//InputMapper::mapAccel(data[0], data[1], data[2]);
InputMapper::mapGyro(data[4], data[5], data[6]);
InputMapper::update(micros()); InputMapper::update(micros());
InputMapper::sendReport(); InputMapper::sendReport();

Loading…
Cancel
Save