I modified the Esplora Joystick Mouse demo and came up with this, better controls and good for general purpose day-to-day use.
Same as the Joystick Mouse example the joystick moves the cursor and pressing the joystick button down issues a click.
I added the feature of using the D-pad as mouse buttons and wheel. Pressing down left and right buttons issue a mouse press, left and right button respectively. Pushing the up and down buttons issues the scroll wheel commands. And the slider controls the speed of the mouse.
Also I added a little bit of threshold of mouse movement to prevent the pointer jerking when the controller is released.
Here comes the code:
#include <Esplora.h>
#include <Mouse.h>
void setup(void)
{
Mouse.begin();
}
int oldScrollUp = 0;
int oldScrollDown = 0;
int stateTime = 0;
#define THRESHOLD 20
void loop(void)
{
int xValue = Esplora.readJoystickX();
int yValue = Esplora.readJoystickY();
int leftButton = Esplora.readJoystickSwitch() && Esplora.readButton(SWITCH_LEFT);
int rightButton = Esplora.readButton(SWITCH_RIGHT);
int scrollUp = Esplora.readButton(SWITCH_UP);
int scrollDown = Esplora.readButton(SWITCH_DOWN);
int range = Esplora.readSlider();
int mouseRange = map(range, 0, 1023, 2, 10);
int mouseX = (xValue < THRESHOLD && xValue > -THRESHOLD) ? 0 : map(xValue, -512, 512, mouseRange, -mouseRange);
int mouseY = (yValue < THRESHOLD && yValue > -THRESHOLD) ? 0 : map(yValue, -512, 512, -mouseRange, mouseRange);
int wheel = 0;
if (oldScrollUp != scrollUp || oldScrollDown != scrollDown)
{
wheel = (scrollUp != scrollDown) ? (scrollUp ? -mouseRange : mouseRange) : 0;
oldScrollUp = scrollUp;
oldScrollDown = scrollDown;
stateTime = millis();
}
else if ((millis() - stateTime) % 100 == 0 && (millis() - stateTime) >= 1000)
{
wheel = (scrollUp != scrollDown) ? (scrollUp ? -mouseRange : mouseRange) : 0;
}
Mouse.move(mouseX, mouseY, wheel);
if (!leftButton)
Mouse.press(MOUSE_LEFT);
else
Mouse.release(MOUSE_LEFT);
if (!rightButton)
Mouse.press(MOUSE_RIGHT);
else
Mouse.release(MOUSE_RIGHT);
}