Skip to content

Commit

Permalink
add sensitivity slider
Browse files Browse the repository at this point in the history
  • Loading branch information
aarron-lee committed May 9, 2024
1 parent 2c032de commit 0a6a0bb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/backend/alsListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const BRIGHTNESS_THRESHOLDS = [
let steamRegistration: any;
let enableAdaptiveBrightness = false;

export const sensitivityInfo = {
range: [25, 100],
step: 25
};

export const pollInfo = {
range: [100, 500],
step: 50
Expand All @@ -42,12 +47,13 @@ export const smoothTimeInfo = {

export const DEFAULT_POLLING_RATE = 100;
export const DEFAULT_SMOOTH_TIME = 500;
export const DEFAULT_SENSITIVITY = 50;

let pollingRate = DEFAULT_POLLING_RATE; // Time in milliseconds
let smoothTime = DEFAULT_SMOOTH_TIME; // Time in milliseconds
const stepCount = 10; // Less steps = smoother transition

let previousAlsValues = Array(75).fill(-1); // Increase length to increase read times (less sensitive to changes)
let previousAlsValues = Array(DEFAULT_SENSITIVITY).fill(-1); // Increase length to increase read times (less sensitive to changes)
let currentBrightness = 50;

const handleAls = async () => {
Expand Down Expand Up @@ -158,11 +164,8 @@ export const clearAlsListener = () => {
enableAdaptiveBrightness = false;

previousAlsValues.fill(-1);
// if (
// steamRegistration &&
// typeof steamRegistration?.unregister === 'function'
// ) {
// steamRegistration.unregister();
// }
// steamRegistration = undefined;
};

export const setSensitivity = (arrSize: number) => {
previousAlsValues = Array(arrSize).fill(-1);
};
32 changes: 31 additions & 1 deletion src/components/als/ALSPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import { useDispatch, useSelector } from 'react-redux';
import {
selectAlsEnabled,
selectAlsPollingRate,
selectSensitivity,
selectSmoothTime,
uiSlice
} from '../../redux-modules/uiSlice';
import { pollInfo, smoothTimeInfo } from '../../backend/alsListener';
import {
pollInfo,
sensitivityInfo,
smoothTimeInfo
} from '../../backend/alsListener';

// let currentBrightness = 40;

Expand All @@ -37,6 +42,17 @@ const useAlsPollingRate = () => {
return { pollingRate, setPollingRate };
};

const useSensitivity = () => {
const sensitivity = useSelector(selectSensitivity);
const dispatch = useDispatch();

const setSensitivity = (val: number) => {
return dispatch(uiSlice.actions.setSensitivity(val));
};

return { sensitivity, setSensitivity };
};

const useSmoothTime = () => {
const smoothTime = useSelector(selectSmoothTime);
const dispatch = useDispatch();
Expand All @@ -52,9 +68,11 @@ export default function () {
const { enabledAls, setAlsEnabled } = useAlsEnabled();
const { pollingRate, setPollingRate } = useAlsPollingRate();
const { smoothTime, setSmoothTime } = useSmoothTime();
const { sensitivity, setSensitivity } = useSensitivity();

const [minPollRate, maxPollRate] = pollInfo.range;
const [minSmoothTime, maxSmoothTime] = smoothTimeInfo.range;
const [minSensitivity, maxSensitivity] = sensitivityInfo.range;

return (
<>
Expand All @@ -68,6 +86,18 @@ export default function () {
</PanelSectionRow>
{enabledAls && (
<>
<PanelSectionRow>
<SliderField
label="Sensitivity"
value={sensitivity}
min={minSensitivity}
max={maxSensitivity}
onChange={setSensitivity}
step={sensitivityInfo.step}
notchTicksVisible
showValue
/>
</PanelSectionRow>
<PanelSectionRow>
<SliderField
label="Poll Rate (ms)"
Expand Down
19 changes: 17 additions & 2 deletions src/redux-modules/uiSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {
} from '../backend/utils';
import {
DEFAULT_POLLING_RATE,
DEFAULT_SENSITIVITY,
DEFAULT_SMOOTH_TIME,
clearAlsListener,
enableAlsListener,
setPollRate,
setSensitivity,
setSmoothTime
} from '../backend/alsListener';
// import type { RootState } from './store';
Expand All @@ -27,6 +29,7 @@ type UiStateType = {
alsInfo: {
pollingRate: number;
smoothTime: number;
sensitivity: number;
};
};

Expand All @@ -38,7 +41,8 @@ const initialState: UiStateType = {
pluginVersionNum: '',
alsInfo: {
pollingRate: DEFAULT_POLLING_RATE,
smoothTime: DEFAULT_SMOOTH_TIME
smoothTime: DEFAULT_SMOOTH_TIME,
sensitivity: DEFAULT_SENSITIVITY
}
};

Expand All @@ -63,6 +67,10 @@ export const uiSlice = createSlice({
setSmoothTime(state, action: PayloadAction<number>) {
setSmoothTime(action.payload);
state.alsInfo.smoothTime = action.payload;
},
setSensitivity(state, action: PayloadAction<number>) {
setSensitivity(action.payload);
state.alsInfo.sensitivity = action.payload;
}
},
extraReducers: (builder) => {
Expand All @@ -78,10 +86,11 @@ export const uiSlice = createSlice({
state.alsEnabled = Boolean(action.payload?.alsEnabled);
}
if (action.payload?.alsInfo) {
const { pollingRate, smoothTime } = action.payload.alsInfo;
const { pollingRate, smoothTime, sensitivity } = action.payload.alsInfo;
state.alsInfo = action.payload.alsInfo;
setPollRate(pollingRate || DEFAULT_POLLING_RATE);
setSmoothTime(smoothTime || DEFAULT_SMOOTH_TIME);
setSensitivity(sensitivity || DEFAULT_SENSITIVITY);
}
});
builder.addCase(setCurrentGameId, (state, action) => {
Expand Down Expand Up @@ -114,6 +123,8 @@ export const selectAlsPollingRate = (state: RootState) =>
state.ui.alsInfo.pollingRate;
export const selectSmoothTime = (state: RootState) =>
state.ui.alsInfo.smoothTime;
export const selectSensitivity = (state: RootState) =>
state.ui.alsInfo.sensitivity;

export const uiSliceMiddleware =
(_store: any) => (next: any) => (action: any) => {
Expand All @@ -140,6 +151,10 @@ export const uiSliceMiddleware =
const alsInfo = { smoothTime: action.payload };
saveSettings({ alsInfo });
}
if (type === uiSlice.actions.setSensitivity.type) {
const alsInfo = { sensitivity: action.payload };
saveSettings({ alsInfo });
}

if (type === uiSlice.actions.setChargeLimit.type) {
setChargeLimit(action.payload);
Expand Down

0 comments on commit 0a6a0bb

Please sign in to comment.