some simple mouse acceleration
This commit is contained in:
parent
ec0f7eda73
commit
2ab8d0a68a
1 changed files with 20 additions and 6 deletions
|
@ -145,7 +145,13 @@ byte mouse_left = 0;
|
|||
byte mouse_right = 0;
|
||||
bool mouse_button = false;
|
||||
|
||||
const int mouse_scroll_sensitivity = 10;
|
||||
const int num_mouse_samples = 10;
|
||||
int current_mouse_sample = 0;
|
||||
int mouse_movement_sample = 0;
|
||||
float mouse_acceleration = 0.0f;
|
||||
const float mouse_accel_factor = 0.7f;
|
||||
|
||||
const int mouse_scroll_sensitivity = 6;
|
||||
int mouse_scroll_dist = 0;
|
||||
bool mouse_middle_pressed = false;
|
||||
bool mouse_scrolled = false;
|
||||
|
@ -167,10 +173,9 @@ void setup() {
|
|||
pinMode(PIN_LED2, OUTPUT);
|
||||
pinMode(PIN_LED3, OUTPUT);
|
||||
|
||||
// poll trackball at 100Hz
|
||||
trackball_timer.begin(trackball_isr, 1000);
|
||||
// poll trackball at 10kHz
|
||||
trackball_timer.begin(trackball_isr, 100);
|
||||
|
||||
Serial.begin(115200);
|
||||
Wire.begin();
|
||||
}
|
||||
|
||||
|
@ -238,6 +243,7 @@ void loop() {
|
|||
if (!mouse_scrolled)
|
||||
Mouse.click(MOUSE_MIDDLE);
|
||||
mouse_middle_pressed = mouse_scrolled = false;
|
||||
mouse_scroll_dist = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -264,6 +270,15 @@ void loop() {
|
|||
|
||||
int mouse_x = mouse_right - mouse_left;
|
||||
int mouse_y = mouse_down - mouse_up;
|
||||
|
||||
mouse_movement_sample += abs(mouse_x) + abs(mouse_y);
|
||||
current_mouse_sample++;
|
||||
if (current_mouse_sample > num_mouse_samples) {
|
||||
current_mouse_sample = 0;
|
||||
mouse_acceleration = 1.0f + mouse_movement_sample * mouse_accel_factor;
|
||||
mouse_movement_sample = 0;
|
||||
}
|
||||
|
||||
if (mouse_middle_pressed) {
|
||||
mouse_scroll_dist += mouse_y;
|
||||
if (abs(mouse_scroll_dist) > mouse_scroll_sensitivity) {
|
||||
|
@ -272,8 +287,7 @@ void loop() {
|
|||
mouse_scrolled = true;
|
||||
}
|
||||
} else {
|
||||
Mouse.move(sign(mouse_x) * mouse_x * mouse_x,
|
||||
sign(mouse_y) * mouse_y * mouse_y);
|
||||
Mouse.move(mouse_x * mouse_acceleration, mouse_y * mouse_acceleration);
|
||||
}
|
||||
|
||||
trackball_update = false;
|
||||
|
|
Loading…
Reference in a new issue