| 1 | // Circuit Playground Accelerometer Mouse Example |
|---|
| 2 | // Tilt Circuit Playground left/right and up/down to move your mouse, and |
|---|
| 3 | // press the left and right push buttons to click the mouse buttons! Make sure |
|---|
| 4 | // the slide switch is in the on (+) position to enable the mouse, or slide into |
|---|
| 5 | // the off (-) position to disable it. By default the sketch assumes you hold |
|---|
| 6 | // Circuit Playground with the USB cable coming out the top. |
|---|
| 7 | // Author: Tony DiCola |
|---|
| 8 | // License: MIT License (https://opensource.org/licenses/MIT) |
|---|
| 9 | #include <Adafruit_CircuitPlayground.h> |
|---|
| 10 | #include <Mouse.h> |
|---|
| 11 | #include <Wire.h> |
|---|
| 12 | #include <SPI.h> |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | // Configuration values to adjust the sensitivity and speed of the mouse. |
|---|
| 16 | // X axis (left/right) configuration: |
|---|
| 17 | #define XACCEL_MIN 0.1 // Minimum range of X axis acceleration, values below |
|---|
| 18 | // this won't move the mouse at all. |
|---|
| 19 | #define XACCEL_MAX 8.0 // Maximum range of X axis acceleration, values above |
|---|
| 20 | // this will move the mouse as fast as possible. |
|---|
| 21 | #define XMOUSE_RANGE 25.0 // Range of velocity for mouse movements. The higher |
|---|
| 22 | // this value the faster the mouse will move. |
|---|
| 23 | #define XMOUSE_SCALE 1 // Scaling value to apply to mouse movement, this is |
|---|
| 24 | // useful to set to -1 to flip the X axis movement. |
|---|
| 25 | |
|---|
| 26 | // Y axis (up/down) configuration: |
|---|
| 27 | // Note that the meaning of these values is exactly the same as the X axis above, |
|---|
| 28 | // just applied to the Y axis and up/down mouse movement. You probably want to |
|---|
| 29 | // keep these values the same as for the X axis (which is the default, they just |
|---|
| 30 | // read the X axis values but you can override with custom values). |
|---|
| 31 | #define YACCEL_MIN XACCEL_MIN |
|---|
| 32 | #define YACCEL_MAX XACCEL_MAX |
|---|
| 33 | #define YMOUSE_RANGE XMOUSE_RANGE |
|---|
| 34 | #define YMOUSE_SCALE 1 |
|---|
| 35 | |
|---|
| 36 | // Set this true to flip the mouse X/Y axis with the board X/Y axis (what you want |
|---|
| 37 | // if holding with USB cable facing up). |
|---|
| 38 | #define FLIP_AXES true |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | // Floating point linear interpolation function that takes a value inside one |
|---|
| 42 | // range and maps it to a new value inside another range. This is used to transform |
|---|
| 43 | // each axis of acceleration to mouse velocity/speed. See this page for details |
|---|
| 44 | // on the equation: https://en.wikipedia.org/wiki/Linear_interpolation |
|---|
| 45 | float lerp(float x, float x0, float x1, float y0, float y1) { |
|---|
| 46 | // Check if the input value (x) is outside its desired range and clamp to |
|---|
| 47 | // those min/max y values. |
|---|
| 48 | if (x <= x0) { |
|---|
| 49 | return y0; |
|---|
| 50 | } |
|---|
| 51 | else if (x >= x1) { |
|---|
| 52 | return y1; |
|---|
| 53 | } |
|---|
| 54 | // Otherwise compute the value y based on x's position within its range and |
|---|
| 55 | // the desired y min & max. |
|---|
| 56 | return y0 + (y1-y0)*((x-x0)/(x1-x0)); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | void setup() { |
|---|
| 61 | // Initialize Circuit Playground library. |
|---|
| 62 | CircuitPlayground.begin(); |
|---|
| 63 | // Initialize Arduino mouse library. |
|---|
| 64 | Mouse.begin(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void loop() { |
|---|
| 68 | // Check if the slide switch is enabled (on +) and if not then just exit out |
|---|
| 69 | // and run the loop again. This lets you turn on/off the mouse movement with |
|---|
| 70 | // the slide switch. |
|---|
| 71 | if (!CircuitPlayground.slideSwitch()) { |
|---|
| 72 | return; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | // Grab initial left & right button states to later check if they are pressed |
|---|
| 76 | // or released. Do this early in the loop so other processing can take some |
|---|
| 77 | // time and the button state change can be detected. |
|---|
| 78 | boolean left_first = CircuitPlayground.leftButton(); |
|---|
| 79 | boolean right_first = CircuitPlayground.rightButton(); |
|---|
| 80 | |
|---|
| 81 | // Grab x, y acceleration values (in m/s^2). |
|---|
| 82 | float x = CircuitPlayground.motionX(); |
|---|
| 83 | float y = CircuitPlayground.motionY(); |
|---|
| 84 | // Use the magnitude of acceleration to interpolate the mouse velocity. |
|---|
| 85 | float x_mag = abs(x); |
|---|
| 86 | float x_mouse = lerp(x_mag, XACCEL_MIN, XACCEL_MAX, 0.0, XMOUSE_RANGE); |
|---|
| 87 | float y_mag = abs(y); |
|---|
| 88 | float y_mouse = lerp(y_mag, YACCEL_MIN, YACCEL_MAX, 0.0, YMOUSE_RANGE); |
|---|
| 89 | // Change the mouse direction based on the direction of the acceleration. |
|---|
| 90 | if (x < 0) { |
|---|
| 91 | x_mouse *= -1.0; |
|---|
| 92 | } |
|---|
| 93 | if (y < 0) { |
|---|
| 94 | y_mouse *= -1.0; |
|---|
| 95 | } |
|---|
| 96 | // Apply any global scaling to the axis (to flip it for example) and truncate |
|---|
| 97 | // to an integer value. |
|---|
| 98 | x_mouse = floor(x_mouse*XMOUSE_SCALE); |
|---|
| 99 | y_mouse = floor(y_mouse*YMOUSE_SCALE); |
|---|
| 100 | |
|---|
| 101 | // Move mouse. |
|---|
| 102 | if (!FLIP_AXES) { |
|---|
| 103 | // Non-flipped axes, just map board X/Y to mouse X/Y. |
|---|
| 104 | Mouse.move((int)x_mouse, (int)y_mouse, 0); |
|---|
| 105 | } |
|---|
| 106 | else { |
|---|
| 107 | // Flipped axes, swap them around. |
|---|
| 108 | Mouse.move((int)y_mouse, (int)x_mouse, 0); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | // Small delay to wait for button state changes and slow down processing a bit. |
|---|
| 112 | delay(10); |
|---|
| 113 | |
|---|
| 114 | // Grab a second button state reading to check if the buttons were pressed or |
|---|
| 115 | // released. |
|---|
| 116 | boolean left_second = CircuitPlayground.leftButton(); |
|---|
| 117 | boolean right_second = CircuitPlayground.rightButton(); |
|---|
| 118 | |
|---|
| 119 | // Check for left button pressed / released. |
|---|
| 120 | if (!left_first && left_second) { |
|---|
| 121 | // Low then high, button was pressed! |
|---|
| 122 | Mouse.press(MOUSE_LEFT); |
|---|
| 123 | } |
|---|
| 124 | else if (left_first && !left_second) { |
|---|
| 125 | // High then low, button was released! |
|---|
| 126 | Mouse.release(MOUSE_LEFT); |
|---|
| 127 | } |
|---|
| 128 | // Check for right button pressed / released. |
|---|
| 129 | if (!right_first && right_second) { |
|---|
| 130 | // Low then high, button was pressed! |
|---|
| 131 | Mouse.press(MOUSE_RIGHT); |
|---|
| 132 | } |
|---|
| 133 | else if (right_first && !right_second) { |
|---|
| 134 | // High then low, button was released! |
|---|
| 135 | Mouse.release(MOUSE_RIGHT); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|