Advertisement
j0h

std accel Mouse

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