diff --git a/include/gyro.h b/include/gyro.h index aa07b0b..eae91f3 100644 --- a/include/gyro.h +++ b/include/gyro.h @@ -45,6 +45,8 @@ class Gyro MPU6050 mpu; + uint32_t time0; + uint32_t delay; bool (*_Enabled)(); public: @@ -54,18 +56,20 @@ class Gyro void init(); 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; } - 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 setInvertZ(bool invert_z = true) { this->invert_z = invert_z? -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 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 getY() { return y; } diff --git a/include/input_mapper.h b/include/input_mapper.h index 49cbb79..fe60fb0 100644 --- a/include/input_mapper.h +++ b/include/input_mapper.h @@ -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); - bool gyroEnabled(); - void gyroUpdate(); - void update(uint32_t time); void mapTriggers(uint32_t value[2]); diff --git a/src/gyro.cpp b/src/gyro.cpp index 1022d39..bd8307d 100644 --- a/src/gyro.cpp +++ b/src/gyro.cpp @@ -37,6 +37,8 @@ Gyro::Gyro() invert_x = 1; invert_y = 1; invert_z = 1; + + time0 = 0; } void Gyro::init() @@ -69,30 +71,42 @@ void Gyro::init() _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; - y = y_filter.filter(y) * invert_y; - z = z_filter.filter(z) * invert_z; + x = x_filter.filter(x) * invert_x; + y = y_filter.filter(y) * invert_y; + z = z_filter.filter(z) * invert_z; + } + } } int32_t Gyro::getDX() { + int32_t dx; + switch (bind_to_x) { case BIND_X: - return x; + dx = x; + break; case BIND_Z: - return z; + dx = z; + break; 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() diff --git a/src/input_mapper.cpp b/src/input_mapper.cpp index add3afd..2f7524e 100644 --- a/src/input_mapper.cpp +++ b/src/input_mapper.cpp @@ -178,9 +178,10 @@ namespace InputMapper gyro.setEnabledCallback([]{ return tjoystick_right.getTouching() > TouchControl::CT_NONE; }); //gyro.setEnabledCallback([]{ return xinput_counter[USB_Device::BUMPER_RIGHT] > 0; }); gyro.setMappedId(1); - gyro.setInvertX(); + //gyro.setInvertX(); gyro.setInvertY(); gyro.setBindToX(Gyro::BIND_XZ); + gyro.setDelay(1000); device.begin(); } @@ -258,16 +259,6 @@ namespace InputMapper } } - bool gyroEnabled() - { - return gyro.Enabled(); - } - - void gyroUpdate() - { - gyro.update(); - } - void update(uint32_t time) { for (uint8_t id = 0; id < 2; ++id) @@ -290,6 +281,8 @@ namespace InputMapper } } + + gyro.update(time); } void mapTriggers(uint32_t value[2]) diff --git a/src/main.cpp b/src/main.cpp index 175e255..a532a9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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::sendReport();