gyro update moved inside the class

gyro
NepEgor 2 years ago
parent 3a23dd5fc8
commit 816887a1b8

@ -45,6 +45,8 @@ class Gyro
MPU6050 mpu; MPU6050 mpu;
uint32_t time0;
uint32_t delay;
bool (*_Enabled)(); bool (*_Enabled)();
public: public:
@ -54,18 +56,20 @@ class Gyro
void init(); void init();
void setEnabledCallback(bool (*_Enabled)()) { this->_Enabled = _Enabled; } void setEnabledCallback(bool (*_Enabled)()) { this->_Enabled = _Enabled; }
bool Enabled() { return _Enabled(); }; bool Enabled() { return _Enabled(); }
void setMappedId(uint8_t mapped_id) { this->mapped_id = mapped_id; }; void setMappedId(uint8_t mapped_id) { this->mapped_id = mapped_id; }
uint8_t getMappedId() { return mapped_id; } uint8_t getMappedId() { return mapped_id; }
void setInvertX(bool invert_x = true) { this->invert_x = invert_x? -1 : 1; }; void setInvertX(bool invert_x = true) { this->invert_x = invert_x? -1 : 1; }
void setInvertY(bool invert_y = true) { this->invert_y = invert_y? -1 : 1; }; void setInvertY(bool invert_y = true) { this->invert_y = invert_y? -1 : 1; }
void setInvertZ(bool invert_z = true) { this->invert_z = invert_z? -1 : 1; }; void setInvertZ(bool invert_z = true) { this->invert_z = invert_z? -1 : 1; }
void setBindToX(BindToX bind_to_x) { this->bind_to_x = bind_to_x; }; void setBindToX(BindToX bind_to_x) { this->bind_to_x = bind_to_x; }
void update(); void setDelay(uint32_t delay) { this->delay = delay; }
void update(uint32_t time);
int16_t getX() { return x; } int16_t getX() { return x; }
int16_t getY() { return y; } int16_t getY() { return y; }

@ -9,9 +9,6 @@ namespace InputMapper
void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y, int32_t dx, int32_t dy, uint32_t time); void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y, int32_t dx, int32_t dy, uint32_t time);
bool gyroEnabled();
void gyroUpdate();
void update(uint32_t time); void update(uint32_t time);
void mapTriggers(uint32_t value[2]); void mapTriggers(uint32_t value[2]);

@ -37,6 +37,8 @@ Gyro::Gyro()
invert_x = 1; invert_x = 1;
invert_y = 1; invert_y = 1;
invert_z = 1; invert_z = 1;
time0 = 0;
} }
void Gyro::init() void Gyro::init()
@ -69,30 +71,42 @@ void Gyro::init()
_Enabled = [] { return false; }; _Enabled = [] { return false; };
} }
void Gyro::update() void Gyro::update(uint32_t time)
{ {
mpu.getRotation(&x, &y, &z); if (Enabled())
{
if (time - time0 > delay)
{
time0 = time;
mpu.getRotation(&x, &y, &z);
x = x_filter.filter(x) * invert_x; x = x_filter.filter(x) * invert_x;
y = y_filter.filter(y) * invert_y; y = y_filter.filter(y) * invert_y;
z = z_filter.filter(z) * invert_z; z = z_filter.filter(z) * invert_z;
}
}
} }
int32_t Gyro::getDX() int32_t Gyro::getDX()
{ {
int32_t dx;
switch (bind_to_x) switch (bind_to_x)
{ {
case BIND_X: case BIND_X:
return x; dx = x;
break;
case BIND_Z: case BIND_Z:
return z; dx = z;
break;
case BIND_XZ: case BIND_XZ:
return (int32_t)x + (int32_t)z; dx = (int32_t)x + (int32_t)z;
} }
return 0; return dx;
} }
int16_t Gyro::getDY() int16_t Gyro::getDY()

@ -178,9 +178,10 @@ namespace InputMapper
gyro.setEnabledCallback([]{ return tjoystick_right.getTouching() > TouchControl::CT_NONE; }); gyro.setEnabledCallback([]{ return tjoystick_right.getTouching() > TouchControl::CT_NONE; });
//gyro.setEnabledCallback([]{ return xinput_counter[USB_Device::BUMPER_RIGHT] > 0; }); //gyro.setEnabledCallback([]{ return xinput_counter[USB_Device::BUMPER_RIGHT] > 0; });
gyro.setMappedId(1); gyro.setMappedId(1);
gyro.setInvertX(); //gyro.setInvertX();
gyro.setInvertY(); gyro.setInvertY();
gyro.setBindToX(Gyro::BIND_XZ); gyro.setBindToX(Gyro::BIND_XZ);
gyro.setDelay(1000);
device.begin(); device.begin();
} }
@ -258,16 +259,6 @@ namespace InputMapper
} }
} }
bool gyroEnabled()
{
return gyro.Enabled();
}
void gyroUpdate()
{
gyro.update();
}
void update(uint32_t time) void update(uint32_t time)
{ {
for (uint8_t id = 0; id < 2; ++id) for (uint8_t id = 0; id < 2; ++id)
@ -290,6 +281,8 @@ namespace InputMapper
} }
} }
gyro.update(time);
} }
void mapTriggers(uint32_t value[2]) void mapTriggers(uint32_t value[2])

@ -132,17 +132,6 @@ void loop()
} }
} }
static uint32_t gyro_start = micros();
if (InputMapper::gyroEnabled())
{
uint32_t gyro_now = micros();
if (gyro_now - gyro_start > 1000)
{
gyro_start = gyro_now;
InputMapper::gyroUpdate();
}
}
InputMapper::update(micros()); InputMapper::update(micros());
InputMapper::sendReport(); InputMapper::sendReport();

Loading…
Cancel
Save