Skip to content

Commit

Permalink
improved mouse acceleration in pad mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisVeigl committed Oct 25, 2023
1 parent e41d105 commit 2240f87
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
3 changes: 2 additions & 1 deletion FLipWare/FLipWare.ino
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const struct SlotSettings defaultSlotSettings = { // default slotSettings v
static variables and data structures for settings and sensor data management
*/
struct SensorData sensorData {
.x=0, .y=0, .xRaw=0, .yRaw=0, .pressure=0,
.x=0, .y=0, .xRaw=0, .yRaw=0, .padState=0, .pressure=0,
.deadZone=0, .force=0, .forceRaw=0, .angle=0,
.dir=0,
.autoMoveX=0, .autoMoveY=0,
Expand Down Expand Up @@ -185,6 +185,7 @@ void loop() {
// get coordinates and handle tap gestures
sensorData.xRaw=sensorData.x=padX;
sensorData.yRaw=sensorData.y=padY;
sensorData.padState=padState;
handleTapClicks(padState, slotSettings.gv*10); // perform clicks and drag actions when in pad mode

// calculate angular direction and force
Expand Down
5 changes: 3 additions & 2 deletions FLipWare/FlipWare.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
#include "bluetooth.h"
#include "hid_hal.h"

#define VERSION_STRING "v1.3"
#define VERSION_STRING "v1.4"

// V1.4: improved mouse acceleration in pad mode
// V1.3: fixed memory allocation problem (removed malloc, reduced static memory allocation)

// V1.2: added check for MPRLS pressure sensor
Expand Down Expand Up @@ -114,7 +115,7 @@ struct SlotSettings {
contains working data of sensors (raw and processed values)
*/
struct SensorData {
int x, y, xRaw,yRaw;
int x, y, xRaw,yRaw, padState;
int pressure;
float deadZone, force, forceRaw, angle;
uint8_t dir;
Expand Down
58 changes: 46 additions & 12 deletions FLipWare/modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "gpio.h"
#include "tone.h"
#include "utils.h"
#include "cirque.h"


/**
static variables for mode handling
Expand Down Expand Up @@ -210,12 +212,12 @@ void handleUserInteraction()
}

/**
@name getAccelFactor
@brief calculates acceleration for mouse pointer movements
@name getStickAccelFactor
@brief calculates acceleration for mouse pointer movements in stick mode
according to sensordata and acceleration settings
@return float value of current acceleration factor
*/
float getAccelFactor() {
float getStickAccelFactor() {
static float accelFactor=0;
static int xo = 0, yo = 0;
static float accelMaxForce = 0, lastAngle = 0;
Expand Down Expand Up @@ -244,27 +246,59 @@ float getAccelFactor() {
return(accelFactor);
}


/**
@name getPadAccelFactor
@brief calculates acceleration for mouse pointer movements in pad mode
according to sensordata and acceleration settings
@return float value of current acceleration factor
*/
float getPadAccelFactor() {
static float accelFactor=0;
static float distance=0;

if ((sensorData.padState==CIRQUE_STATE_LIFTOFF) || (sensorData.padState==CIRQUE_STATE_HOVERING)) {
accelFactor = 0;
distance=0;
}
else {
distance += (fabsf(sensorData.x) + fabsf(sensorData.y)) / 700000;
accelFactor = 0.0005+distance;
if (accelFactor>0.01) accelFactor=0.01;
}
return(accelFactor);
}

/**
@name acceleratedMouseMove
@brief performs accelerated mouse pointer movement
@param accelFactor current acceleration factor
@return none
*/
void acceleratedMouseMove(float accelFactor) {
void acceleratedMouseMove(float accelFactor, uint8_t use_maxSpeed) {
static float accumXpos = 0;
static float accumYpos = 0;

float moveValX = sensorData.x * (float)slotSettings.ax * accelFactor;
float moveValY = sensorData.y * (float)slotSettings.ay * accelFactor;

float actSpeed = __ieee754_sqrtf (moveValX * moveValX + moveValY * moveValY);
float max_speed = (float)slotSettings.ms / 10.0f;

if (actSpeed > max_speed) {
moveValX *= (max_speed / actSpeed);
moveValY *= (max_speed / actSpeed);
if (use_maxSpeed) {
float actSpeed = __ieee754_sqrtf (moveValX * moveValX + moveValY * moveValY);
float max_speed = (float)slotSettings.ms / 10.0f;
if (actSpeed > max_speed) {
moveValX *= (max_speed / actSpeed);
moveValY *= (max_speed / actSpeed);
}
}

/*
if ((sensorData.x!=0) && (sensorData.y!=0)) {
Serial.print ("x=");Serial.print (sensorData.x); Serial.print (" y=");Serial.print (sensorData.y);
Serial.print (" / moveValX=");Serial.print (moveValX); Serial.print (" moveValY=");Serial.print (moveValY);
Serial.println(" ");
}
*/

accumXpos += moveValX;
accumYpos += moveValY;

Expand Down Expand Up @@ -310,11 +344,11 @@ void handleMovement()
switch (slotSettings.padMode) {

case PADMODE_MOUSE: // handle mouse stick mode
acceleratedMouseMove(getAccelFactor());
acceleratedMouseMove(getStickAccelFactor(), 1);
break;

case PADMODE_PAD: // handle mouse pad mode
acceleratedMouseMove(0.01);
acceleratedMouseMove(getPadAccelFactor(), 0);
break;

case PADMODE_ALTERNATIVE: // handle alternative actions (non-sticky)
Expand Down

0 comments on commit 2240f87

Please sign in to comment.