Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update JoystickButtons every nth loop #71

Open
Kython89 opened this issue Feb 28, 2018 · 1 comment
Open

Only update JoystickButtons every nth loop #71

Kython89 opened this issue Feb 28, 2018 · 1 comment

Comments

@Kython89
Copy link
Contributor

Kython89 commented Feb 28, 2018

According to Omar at CTRE each api call to a joystick can take ~0.3ms. With the hope of running at 100hz this can be problematic given everything else that is going on.

It would be relatively easy to cache the value of the JoystickButtons and only update the cache every nth loop. The buttons would be staggered to avoid everything nth loop being slower.

@Kython89
Copy link
Contributor Author

Kython89 commented Mar 1, 2018

Using a wrapper like this around all our buttons should work.

class JoystickButton2481 {
private:
    class Button* m_button;
    static int s_buttonCount;
    int m_loopCounter;
    bool m_cachedState;

public:
    JoystickButton2481(Button* button) {
        m_button = button;
        m_loopCounter = s_buttonCount++ % 5; //Offset the loop counter by 1 to 5 so not all the buttons update on the loop.
        m_cachedState = false;
    }

    bool Get() {
        if (m_loopCounter++ % 5 == 0) {
            m_cachedState = m_button->Get();
        }
        return m_cachedState;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant