Skip to content

Commit

Permalink
add Rgb modes dropdown (#4)
Browse files Browse the repository at this point in the history
* add rgbMode support

* add RGB Mode dropdowns

* set modes when state changes

* hide RGB sliders when in DYNAMIC mode
  • Loading branch information
aarron-lee authored Nov 27, 2023
1 parent 5cae4df commit 23783b4
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 117 deletions.
10 changes: 7 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ async def get_settings(self):
async def save_rgb_per_game_profiles_enabled(self, enabled: bool):
return settings.set_setting('rgbPerGameProfilesEnabled', enabled)

async def save_rgb_settings(self, rgbProfiles):
return settings.set_all_rgb_profiles(rgbProfiles)
async def save_rgb_settings(self, rgbProfiles, currentGameId):
result = settings.set_all_rgb_profiles(rgbProfiles)
if currentGameId:
rgb.sync_rgb_settings(currentGameId)
return result

# sync state in settings.json to actual controller RGB hardware
async def sync_rgb_settings(self, currentGameId):
decky_plugin.logger.info(f"sync rgb settings {currentGameId}")
# decky_plugin.logger.info(f"sync rgb settings {currentGameId}")
return rgb.sync_rgb_settings(currentGameId)

async def remap_button(self, button: str, action: str):
Expand Down
7 changes: 6 additions & 1 deletion py_modules/controller_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ class RemapActions(Enum):
R_TRIGGER = 0x19

VIEW = 0x23
MENU = 0x24
MENU = 0x24

class RgbModes(Enum):
SOLID = 0x01
DYNAMIC = 0x03
BLINKING = 0x02
8 changes: 6 additions & 2 deletions py_modules/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ def sync_rgb_settings(current_game_id):
rgb_profile = s.get('rgb').get(current_game_id)
for controller in ['LEFT', 'RIGHT']:
rgb_light = rgb_profile.get(controller)
rgb_mode = rgb_light.get('mode')

if rgb_light.get('enabled'):
rgb_on(current_game_id, controller)
time.sleep(0.1)
rgb_color(
current_game_id,
controller,
rgb_mode,
rgb_light.get('red'),
rgb_light.get('blue'),
rgb_light.get('green'),
Expand All @@ -38,10 +40,12 @@ def rgb_off(current_game_id, controller: str):
legion_configurator.send_command(rgb_off_command)


def rgb_color(current_game_id, controller: str, red, blue, green, brightness):
def rgb_color(current_game_id, controller: str, mode: str, red, blue, green, brightness):
hex_brightness = int(brightness)
color = bytes([red, green, blue])
controller_code = controller_enums.Controller[controller].value
rgb = legion_configurator.create_rgb_control_command(controller_code,0x01,color, hex_brightness, 0x01)
rgb_mode_code = controller_enums.RgbModes[mode].value or controller_enums.RgbModes['SOLID'].value

rgb = legion_configurator.create_rgb_control_command(controller_code, rgb_mode_code, color, hex_brightness, 0x01)
# decky_plugin.logger.info(list(rgb))
legion_configurator.send_command(rgb)
6 changes: 6 additions & 0 deletions src/backend/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ export enum RemapActions {
VIEW = 'VIEW',
MENU = 'MENU'
}

export enum RgbModes {
SOLID = 'SOLID',
DYNAMIC = 'DYNAMIC',
BLINKING = 'BLINKING'
}
190 changes: 101 additions & 89 deletions src/components/ControllerLightingPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import { useState } from 'react';
import {
useRgb,
usePerGameRgbProfilesEnabled,
useRgbProfileDisplayName
useRgbProfileDisplayName,
useRgbMode
} from '../hooks/rgb';
import RgbModeDropdown from './RgbModeDropdown';
import { RgbModes } from '../backend/constants';
const DEFAULT_STATE = {
isTouchpad: true
};

const ControllerLightingPanel: VFC<{ serverAPI: ServerAPI }> = ({
serverAPI
}) => {
const [leftMode] = useRgbMode('LEFT');
const [rightMode] = useRgbMode('RIGHT');

const [perGameProfilesEnabled, setPerGameProfilesEnabled] =
usePerGameRgbProfilesEnabled();
const displayName = useRgbProfileDisplayName();
Expand Down Expand Up @@ -89,6 +95,7 @@ const ControllerLightingPanel: VFC<{ serverAPI: ServerAPI }> = ({
)}
{showRightOptions && isRightRgbOn && (
<>
<RgbModeDropdown controller="RIGHT" />
<SliderField
label="Right Stick Brightness"
value={brightnessR}
Expand All @@ -98,50 +105,52 @@ const ControllerLightingPanel: VFC<{ serverAPI: ServerAPI }> = ({
validValues="range"
onChange={(value) => setRightLedBrightness(value)}
></SliderField>
<>
<SliderField
label="Red"
value={redR}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setRightColor('red', value);
}}
/>
<SliderField
label="Green"
value={greenR}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setRightColor('green', value);
}}
/>
<SliderField
label="Blue"
value={blueR}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setRightColor('blue', value);
}}
/>
<div
style={{
width: '100%',
height: '30px',
backgroundColor: `rgb(${redR}, ${greenR}, ${blueR})`,
border: '1px solid black',
borderRadius: '10px'
}}
/>
</>
{rightMode !== RgbModes.DYNAMIC && (
<>
<SliderField
label="Red"
value={redR}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setRightColor('red', value);
}}
/>
<SliderField
label="Green"
value={greenR}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setRightColor('green', value);
}}
/>
<SliderField
label="Blue"
value={blueR}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setRightColor('blue', value);
}}
/>
<div
style={{
width: '100%',
height: '30px',
backgroundColor: `rgb(${redR}, ${greenR}, ${blueR})`,
border: '1px solid black',
borderRadius: '10px'
}}
/>
</>
)}
</>
)}

Expand All @@ -162,6 +171,7 @@ const ControllerLightingPanel: VFC<{ serverAPI: ServerAPI }> = ({
)}
{showLeftOptions && isLeftRgbOn && (
<>
<RgbModeDropdown controller="LEFT" />
<SliderField
label="Left Stick Brightness"
value={brightnessL}
Expand All @@ -171,50 +181,52 @@ const ControllerLightingPanel: VFC<{ serverAPI: ServerAPI }> = ({
validValues="range"
onChange={(value) => setLeftLedBrightness(value)}
></SliderField>
<>
<SliderField
label="Red"
value={redL}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setLeftColor('red', value);
}}
/>
<SliderField
label="Green"
value={greenL}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setLeftColor('green', value);
}}
/>
<SliderField
label="Blue"
value={blueL}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setLeftColor('blue', value);
}}
/>
<div
style={{
width: '100%',
height: '30px',
backgroundColor: `rgb(${redL}, ${greenL}, ${blueL})`,
border: '1px solid black',
borderRadius: '10px'
}}
/>
</>
{leftMode !== RgbModes.DYNAMIC && (
<>
<SliderField
label="Red"
value={redL}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setLeftColor('red', value);
}}
/>
<SliderField
label="Green"
value={greenL}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setLeftColor('green', value);
}}
/>
<SliderField
label="Blue"
value={blueL}
showValue={true}
min={0}
max={255}
validValues="range"
onChange={(value) => {
setLeftColor('blue', value);
}}
/>
<div
style={{
width: '100%',
height: '30px',
backgroundColor: `rgb(${redL}, ${greenL}, ${blueL})`,
border: '1px solid black',
borderRadius: '10px'
}}
/>
</>
)}
</>
)}

Expand Down
46 changes: 46 additions & 0 deletions src/components/RgbModeDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FC } from 'react';
import { DropdownItem } from 'decky-frontend-lib';
import { ControllerType, RgbModes } from '../backend/constants';
import { useRgbMode } from '../hooks/rgb';

type PropType = {
controller: ControllerType;
};

const RgbModeDropdown: FC<PropType> = ({ controller }) => {
const [mode, setMode] = useRgbMode(controller);

const dropdownOptions = Object.values(RgbModes).map((action) => {
return {
data: action,
label: `${action}`,
value: action
};
});

return (
<>
<DropdownItem
menuLabel={'Mode'}
description={'RGB Mode'}
rgOptions={dropdownOptions.map((o) => {
return {
data: o.data,
label: o.label,
value: o.value
};
})}
selectedOption={
dropdownOptions.find((o) => {
return o.data === mode;
})?.data
}
onChange={(value: any) => {
setMode(value.data);
}}
/>
</>
);
};

export default RgbModeDropdown;
19 changes: 17 additions & 2 deletions src/hooks/rgb.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useDispatch, useSelector } from 'react-redux';
import { useCallback } from 'react';
import { ControllerType } from '../backend/constants';
import { ControllerType, RgbModes } from '../backend/constants';
import {
rgbSlice,
selectRgbInfo,
selectRgbProfileDisplayName,
selectPerGameProfilesEnabled
selectPerGameProfilesEnabled,
selectRgbMode
} from '../redux-modules/rgbSlice';

export enum Colors {
Expand All @@ -14,6 +15,20 @@ export enum Colors {
BLUE = 'blue'
}

export const useRgbMode = (controller: ControllerType) => {
const mode = useSelector(selectRgbMode(controller));
const dispatch = useDispatch();

const setMode = useCallback(
(mode: RgbModes) => {
return dispatch(rgbSlice.actions.setRgbMode({ controller, mode }));
},
[controller]
);

return [mode, setMode] as any;
};

export const usePerGameRgbProfilesEnabled = () => {
const isEnabled = useSelector(selectPerGameProfilesEnabled);
const dispatch = useDispatch();
Expand Down
Loading

0 comments on commit 23783b4

Please sign in to comment.