Skip to content

Commit

Permalink
Adaptive Brightness - Clean up + refactor (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
kprinssu authored May 1, 2024
1 parent d036cc1 commit e5d3d3c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 28 deletions.
33 changes: 7 additions & 26 deletions src/backend/alsListener.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createServerApiHelpers, getServerApi, logInfo } from './utils';

const log = false;
const log = true;

// Brightness steps
// [ALS delta, brightness add in %]
Expand All @@ -13,13 +13,13 @@ const BRIGHTNESS_STEPS = [

let enableAdaptiveBrightness = false;

const smoothTime = 250;
const smoothTime = 300;
const stepCount = 10;
const ignoreThreshold = 30;
const ignoreThreshold = BRIGHTNESS_STEPS[0][0];
const alsPollingRate = 125;

let steamRegistration: any;
let previousAlsValues = [-1, -1, -1, -1];
let previousAlsValues = Array(10).fill(-1);
let currentBrightness = 40;

const handleAls = async () => {
Expand All @@ -28,38 +28,26 @@ const handleAls = async () => {
const serverAPI = getServerApi();

if (!serverAPI) {
logInfo('Server API not available');
return;
}

const { readAls } = createServerApiHelpers(serverAPI);

while (true) {
while (enableAdaptiveBrightness) {
sleep(alsPollingRate);

if (!enableAdaptiveBrightness) {
log && logInfo('Adaptive brightness disabled');
break;
}

const alsValue = await readAls();
log && logInfo(`ALS value: ${alsValue}`);
if (typeof alsValue !== 'number') {
continue;
}

// Keep track of the last 4 values
// Keep track of the last N values
previousAlsValues.push(alsValue);
previousAlsValues.shift();

// Set the initial values
if (
previousAlsValues[0] === -1 ||
previousAlsValues[1] === -1 ||
previousAlsValues[2] === -1 ||
previousAlsValues[3] === -1
) {
log && logInfo('Initial values are being set');
if (previousAlsValues.includes(-1)) {
continue;
}

Expand All @@ -75,7 +63,6 @@ const handleAls = async () => {

// Ignore small changes
if (absDelta < ignoreThreshold) {
log && logInfo(`Ignoring small change: ${absDelta}`);
continue;
}

Expand Down Expand Up @@ -121,10 +108,6 @@ const handleAls = async () => {
);

await sleep(smoothTime / stepCount);

// Ensure that we don't have stale data
previousAlsValues.push(alsValue);
previousAlsValues.shift();
}
}
};
Expand All @@ -141,7 +124,6 @@ export const enableAlsListener = () => {
currentBrightness = data.flBrightness * 100;
}
);
logInfo(`als listener enabled`);
};

export const clearAlsListener = () => {
Expand All @@ -155,5 +137,4 @@ export const clearAlsListener = () => {
steamRegistration.unregister();
}
steamRegistration = undefined;
logInfo(`als listener disabled`);
};
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const Content: VFC<{ serverAPI?: ServerAPI }> = memo(() => {
/>
</PanelSectionRow>
</PanelSection>
{/* <ErrorBoundary title="Ambient Light Sensor">
<ErrorBoundary title="Adaptive ">
<AlsPanel />
</ErrorBoundary> */}
</ErrorBoundary>
<ErrorBoundary title={'Controller Lighting Panel'}>
<ControllerLightingPanel />
</ErrorBoundary>
Expand Down

0 comments on commit e5d3d3c

Please sign in to comment.