Skip to content

Commit

Permalink
Merge pull request #1 from Gnarus-G/feat/sensitivity
Browse files Browse the repository at this point in the history
feat: allowing a sensitivity parameter like rawaccel
  • Loading branch information
Gnarus-G authored Jan 31, 2024
2 parents f155a4d + 4540581 commit c40d107
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 87 deletions.
53 changes: 19 additions & 34 deletions driver/accel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ typedef struct {
static const fixedpt FIXEDPT_ZERO = fixedpt_rconst(0.0);

/**
* Calculate the normalized factor by which to multiply the input vector
* Calculate the factor by which to multiply the input vector
* in order to get the desired output speed.
*
*/
extern inline fixedpt acceleration_factor(fixedpt input_speed,
fixedpt param_accel,
fixedpt param_offset,
fixedpt param_output_cap) {

// printk("input speed %s, with interval %s", fixedpt_cstr(speed_in, 5),
// fixedpt_cstr(polling_interval, 5));
extern inline fixedpt sensitivity(fixedpt input_speed, fixedpt param_sens_mult,
fixedpt param_accel, fixedpt param_offset,
fixedpt param_output_cap) {

input_speed = fixedpt_sub(input_speed, param_offset);

fixedpt accel_factor = FIXEDPT_ONE;
fixedpt sens = FIXEDPT_ONE;

if (input_speed > FIXEDPT_ZERO) {
accel_factor =
fixedpt_add(FIXEDPT_ONE, fixedpt_mul((param_accel), input_speed));
sens = fixedpt_add(FIXEDPT_ONE, fixedpt_mul((param_accel), input_speed));
}

sens = fixedpt_mul(sens, param_sens_mult);

if (param_output_cap != FIXEDPT_ZERO && accel_factor > param_output_cap) {
accel_factor = param_output_cap;
}
fixedpt output_cap = fixedpt_mul(param_output_cap, param_sens_mult);

if (param_output_cap != FIXEDPT_ZERO && sens > output_cap) {
sens = output_cap;
}

return accel_factor;
return sens;
}

static inline AccelResult f_accelerate(s8 x, s8 y, u32 polling_interval,
fixedpt param_sens_mult,
fixedpt param_accel,
fixedpt param_offset,
fixedpt param_output_cap) {
Expand All @@ -51,26 +51,16 @@ static inline AccelResult f_accelerate(s8 x, s8 y, u32 polling_interval,
fixedpt dx = fixedpt_fromint(x);
fixedpt dy = fixedpt_fromint(y);

// printk(KERN_INFO "[MOUSE_MOVE] (%s, %s)", fixedpt_cstr(dx, 5),
// fixedpt_cstr(dy, 5));

fixedpt distance =
fixedpt_sqrt(fixedpt_add(fixedpt_mul(dx, dx), fixedpt_mul(dy, dy)));

// printk("distance %s", fixedpt_cstr(distance, 5));

fixedpt speed_in = fixedpt_div(distance, fixedpt_fromint(polling_interval));

// printk("input speed %s, with interval %s", fixedpt_cstr(speed_in, 5),
// fixedpt_cstr(fixedpt_fromint(polling_interval), 5));

fixedpt accel_factor = acceleration_factor(speed_in, param_accel,
param_offset, param_output_cap);

// printk("accel_factor %s", fixedpt_cstr(accel_factor, 5));
fixedpt sens = sensitivity(speed_in, param_sens_mult, param_accel,
param_offset, param_output_cap);

fixedpt dx_out = fixedpt_mul(dx, accel_factor);
fixedpt dy_out = fixedpt_mul(dy, accel_factor);
fixedpt dx_out = fixedpt_mul(dx, sens);
fixedpt dy_out = fixedpt_mul(dy, sens);

dx_out = fixedpt_add(dx_out, carry_x);
dy_out = fixedpt_add(dy_out, carry_y);
Expand All @@ -81,10 +71,5 @@ static inline AccelResult f_accelerate(s8 x, s8 y, u32 polling_interval,
carry_x = fixedpt_sub(dx_out, fixedpt_fromint(result.x));
carry_y = fixedpt_sub(dy_out, fixedpt_fromint(result.y));

// printk(KERN_INFO "[MOUSE_MOVE_ACCEL] (%d, %d)", result.x, result.y);

// printk(KERN_INFO "[MOUSE_MOVE] carry (%s, %s)", fixedpt_cstr(carry_x, 5),
// fixedpt_cstr(carry_y, 5));

return result;
}
3 changes: 2 additions & 1 deletion driver/maccel.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ static AccelResult inline accelerate(s8 x, s8 y) {
ms = 100;
}

return f_accelerate(x, y, ms, PARAM_ACCEL, PARAM_OFFSET, PARAM_OUTPUT_CAP);
return f_accelerate(x, y, ms, PARAM_SENS_MULT, PARAM_ACCEL, PARAM_OFFSET,
PARAM_OUTPUT_CAP);
}

void on_complete(struct urb *u) {
Expand Down
6 changes: 4 additions & 2 deletions driver/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
module_param_named(param, PARAM_##param, int, RW_USER_GROUP); \
MODULE_PARM_DESC(param, desc);

PARAM(ACCEL, 0.0, "Control the acceleration factor.");
PARAM(SENS_MULT, 1.0,
"A factor applied the sensitivity calculation after ACCEL is applied.");
PARAM(ACCEL, 0.0, "Control the sensitivity calculation.");
PARAM(OFFSET, 0.0, "Control the input speed past which to allow acceleration.");
PARAM(OUTPUT_CAP, 0.0, "Control the maximum output speed.");
PARAM(OUTPUT_CAP, 0.0, "Control the maximum sensitivity.");
2 changes: 1 addition & 1 deletion maccel-cli/src/libmaccel.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../../driver/accel.h"
#include "../../driver/fixedptc.h"

extern char *fixedpt_to_str(fixedpt num) { return fixedpt_cstr(num, 3); }
extern char *fixedpt_to_str(fixedpt num) { return fixedpt_cstr(num, 5); }

extern fixedpt fixedpt_from_float(float value) { return fixedpt_rconst(value); }

Expand Down
70 changes: 48 additions & 22 deletions maccel-cli/src/libmaccel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ use crate::params::Param;

use self::fixedptc::Fixedpt;

extern "C" {
fn acceleration_factor(
speed_in: i32,
param_accel: i32,
param_offset: i32,
param_output_cap: i32,
) -> i32;
}

pub struct Params {
sens_mult: i32,
accel: i32,
offset: i32,
output_cap: i32,
Expand All @@ -20,6 +12,10 @@ pub struct Params {
impl Params {
pub fn new() -> Self {
Self {
sens_mult: Param::SensMult
.get()
.expect("failed to read Sens_Mult parameter")
.0,
accel: Param::Accel
.get()
.expect("failed to read Accel parameter")
Expand All @@ -39,26 +35,28 @@ impl Params {
/// Ratio of Output speed to Input speed
pub fn sensitivity(s_in: f32, params: Params) -> f64 {
let s_in = fixedptc::fixedpt(s_in);
let a_factor =
unsafe { acceleration_factor(s_in.0, params.accel, params.offset, params.output_cap) };
let a_factor = unsafe {
c_lib::sensitivity(
s_in.0,
params.sens_mult,
params.accel,
params.offset,
params.output_cap,
)
};
let a_factor: f32 = Fixedpt(a_factor).into();

return a_factor as f64;
}

pub mod fixedptc {
use std::ffi::c_char;
use std::ffi::CStr;

extern "C" {
fn fixedpt_to_str(num: i32) -> *const c_char;
fn fixedpt_from_float(value: f32) -> i32;
fn fixedpt_to_float(value: i32) -> f32;
}
use super::c_lib;

pub fn fixedpt_as_str(num: &i32) -> anyhow::Result<&str> {
fn fixedpt_as_str(num: &i32) -> anyhow::Result<&str> {
unsafe {
let s = CStr::from_ptr(fixedpt_to_str(*num));
let s = CStr::from_ptr(c_lib::fixedpt_to_str(*num));
let s = core::str::from_utf8(s.to_bytes())?;
return Ok(s);
}
Expand All @@ -68,23 +66,51 @@ pub mod fixedptc {

impl From<Fixedpt> for f32 {
fn from(value: Fixedpt) -> Self {
unsafe { fixedpt_to_float(value.0) }
unsafe { c_lib::fixedpt_to_float(value.0) }
}
}

impl From<f32> for Fixedpt {
fn from(value: f32) -> Self {
unsafe {
let i = fixedpt_from_float(value);
let i = c_lib::fixedpt_from_float(value);
return Fixedpt(i);
}
}
}

impl<'a> TryFrom<&'a Fixedpt> for &'a str {
type Error = anyhow::Error;

fn try_from(value: &'a Fixedpt) -> Result<Self, Self::Error> {
return fixedpt_as_str(&value.0);
}
}

pub fn fixedpt(num: f32) -> Fixedpt {
unsafe {
let i = fixedpt_from_float(num);
let i = c_lib::fixedpt_from_float(num);
return Fixedpt(i);
}
}
}

mod c_lib {
use std::ffi::c_char;

extern "C" {
pub fn sensitivity(
speed_in: i32,
param_sens_mult: i32,
param_accel: i32,
param_offset: i32,
param_output_cap: i32,
) -> i32;
}

extern "C" {
pub fn fixedpt_to_str(num: i32) -> *const c_char;
pub fn fixedpt_from_float(value: f32) -> i32;
pub fn fixedpt_to_float(value: i32) -> f32;
}
}
4 changes: 3 additions & 1 deletion maccel-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ fn main() -> anyhow::Result<()> {
name.set(value)?;
}
ParamsCommand::Get { name } => {
println!("{}", name.get_as_str()?);
let value = name.get()?;
let string_value: &str = (&value).try_into()?;
println!("{}", string_value);
}
ParamsCommand::Bind { device_id } => {
bind_device(&device_id)?;
Expand Down
13 changes: 4 additions & 9 deletions maccel-cli/src/params.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::libmaccel::fixedptc::{fixedpt, fixedpt_as_str, Fixedpt};
use crate::libmaccel::fixedptc::{fixedpt, Fixedpt};
use std::{
fs::File,
io::Read,
Expand All @@ -10,8 +10,9 @@ use clap::ValueEnum;

const SYS_MODULE_PATH: &str = "/sys/module/maccel";

#[derive(Debug, Clone, Copy, ValueEnum)]
#[derive(Debug, Clone, Copy, ValueEnum, PartialEq)]
pub enum Param {
SensMult,
Accel,
Offset,
OutputCap,
Expand Down Expand Up @@ -68,20 +69,14 @@ impl Param {
return Ok(Fixedpt(value));
}

pub fn get_as_str(&self) -> anyhow::Result<String> {
let value = self.get()?;
let value = fixedpt_as_str(&value.0)?.to_string();

return Ok(value);
}

pub fn display_name(&self) -> String {
return format!("{:?}", self);
}

/// The cannonical name of parameter, exactly what can be read from /sys/module/maccel/parameters
fn name(&self) -> &'static str {
let param_name = match self {
Param::SensMult => "SENS_MULT",
Param::Accel => "ACCEL",
Param::Offset => "OFFSET",
Param::OutputCap => "OUTPUT_CAP",
Expand Down
Loading

0 comments on commit c40d107

Please sign in to comment.