-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for brightness message (#45)
* Add support for brightness message Fixes #44 * add slider
- Loading branch information
1 parent
e6e44d7
commit fc9c520
Showing
10 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { cn } from '@/lib/utils'; | ||
import * as SliderPrimitive from '@radix-ui/react-slider'; | ||
import * as React from 'react'; | ||
|
||
const Slider = React.forwardRef< | ||
React.ElementRef<typeof SliderPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root> | ||
>(({ className, ...props }, ref) => ( | ||
<SliderPrimitive.Root | ||
ref={ref} | ||
className={cn( | ||
'relative flex w-full touch-none select-none items-center', | ||
className, | ||
)} | ||
{...props} | ||
> | ||
<SliderPrimitive.Track | ||
className={`relative h-2 w-full grow overflow-hidden | ||
rounded-full bg-neutral-100 dark:bg-neutral-800`} | ||
> | ||
<SliderPrimitive.Range className="absolute h-full bg-neutral-900 dark:bg-neutral-50" /> | ||
</SliderPrimitive.Track> | ||
<SliderPrimitive.Thumb | ||
className={`block h-5 w-5 rounded-full border-2 border-neutral-900 | ||
bg-white ring-offset-white transition-colors focus-visible:outline-none | ||
focus-visible:ring-2 focus-visible:ring-neutral-950 focus-visible:ring-offset-2 | ||
disabled:pointer-events-none disabled:opacity-50 dark:border-neutral-50 dark:bg-neutral-950 | ||
dark:ring-offset-neutral-950 dark:focus-visible:ring-neutral-300`} | ||
/> | ||
</SliderPrimitive.Root> | ||
)); | ||
Slider.displayName = SliderPrimitive.Root.displayName; | ||
|
||
export { Slider }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { PaxDecryptedPacket } from '@/pax/containers/lib'; | ||
import { Messages } from '@/pax/shared/enums'; | ||
|
||
import { MessageAbs } from './MessageAbs'; | ||
import { ReadAndWriteMessageAbs } from './ReadAndWriteMessageAbs'; | ||
|
||
export class BrightnessMessage | ||
extends ReadAndWriteMessageAbs | ||
implements MessageAbs | ||
{ | ||
readonly brightness: number; | ||
readonly messageType: Messages; | ||
readonly packet: PaxDecryptedPacket; | ||
|
||
readonly PaxBrightnessMin = 0; | ||
readonly PaxBrightnessMax = 128; | ||
|
||
constructor( | ||
builder: | ||
| BrightnessMessageBuilderFromPacket<BrightnessMessage> | ||
| BrightnessMessageBuilderFromValue<BrightnessMessage>, | ||
) { | ||
super(); | ||
this.messageType = Messages.ATTRIBUTE_BRIGHTNESS; | ||
if (builder instanceof BrightnessMessageBuilderFromPacket) { | ||
this.packet = builder.getPacket(); | ||
const absoluteBrightness = this.packet.getUint8(1); | ||
const brightnessPercentage = | ||
this.brightnessToPercentage(absoluteBrightness); | ||
|
||
this.brightness = brightnessPercentage; | ||
} else if (builder instanceof BrightnessMessageBuilderFromValue) { | ||
this.brightness = builder.getBrightness(); | ||
const buffer = new ArrayBuffer(16); | ||
const view = new PaxDecryptedPacket(buffer); | ||
view.setUint8(0, this.messageType); | ||
view.setUint8(1, this.brightnessToAbsolute(this.brightness)); | ||
this.packet = view; | ||
} else { | ||
throw new Error('Invalid builder'); | ||
} | ||
} | ||
|
||
private brightnessToPercentage(brightness: number): number { | ||
return ( | ||
(brightness - this.PaxBrightnessMin) / | ||
(this.PaxBrightnessMax - this.PaxBrightnessMin) | ||
); | ||
} | ||
|
||
private brightnessToAbsolute(brightness: number): number { | ||
return brightness * (this.PaxBrightnessMax - this.PaxBrightnessMin); | ||
} | ||
|
||
static createWithPacket(packet: PaxDecryptedPacket): BrightnessMessage { | ||
const builder = new BrightnessMessageBuilderFromPacket<BrightnessMessage>(); | ||
builder.setPacket(packet); | ||
return new BrightnessMessage(builder); | ||
} | ||
|
||
static createWithBrightness(brightness: number): BrightnessMessage { | ||
const builder = new BrightnessMessageBuilderFromValue<BrightnessMessage>(); | ||
builder.setBrightness(brightness); | ||
return new BrightnessMessage(builder); | ||
} | ||
} | ||
|
||
export class BrightnessMessageBuilderFromPacket<T extends BrightnessMessage> { | ||
private packet?: PaxDecryptedPacket; | ||
|
||
setPacket(packet: PaxDecryptedPacket): BrightnessMessageBuilderFromPacket<T> { | ||
this.packet = packet; | ||
return this; | ||
} | ||
|
||
getPacket(): PaxDecryptedPacket { | ||
if (!this.packet) { | ||
throw new Error('Packet is not set'); | ||
} | ||
return this.packet; | ||
} | ||
|
||
build(ctor: new (builder: BrightnessMessageBuilderFromPacket<T>) => T): T { | ||
if (!this.packet) { | ||
throw new Error('Packet is not set'); | ||
} | ||
return new ctor(this); | ||
} | ||
} | ||
|
||
export class BrightnessMessageBuilderFromValue<T extends BrightnessMessage> { | ||
private brightness?: number; | ||
|
||
setBrightness(brightness: number): BrightnessMessageBuilderFromValue<T> { | ||
this.brightness = brightness; | ||
return this; | ||
} | ||
|
||
getBrightness(): number { | ||
if (!this.brightness) { | ||
throw new Error('Theme is not set'); | ||
} | ||
return this.brightness; | ||
} | ||
|
||
build(ctor: new (builder: BrightnessMessageBuilderFromValue<T>) => T): T { | ||
if (!this.brightness) { | ||
throw new Error('Brightness is not set'); | ||
} | ||
return new ctor(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters