Skip to content

Commit

Permalink
wip: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jul 16, 2024
1 parent 5dfbeca commit 71f5282
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 75 deletions.
44 changes: 13 additions & 31 deletions packages/core/src/models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import type {
StreamDeckEvents,
StreamDeckLcdStripService,
} from '../types'
import type { StreamdeckImageWriter } from '../services/imageWriter/types'
import { ButtonLcdImagePacker, ButtonsLcdService } from '../services/buttonsLcd'
import type { ButtonsLcdService } from '../services/buttonsLcd'
import type { StreamDeckButtonControlDefinition, StreamDeckControlDefinition } from './controlDefinition'

export type EncodeJPEGHelper = (buffer: Buffer, width: number, height: number) => Promise<Buffer>
Expand Down Expand Up @@ -42,7 +41,7 @@ export type StreamDeckProperties = Readonly<{
KEY_SPACING_VERTICAL: number
}>

export abstract class StreamDeckInputBase extends EventEmitter<StreamDeckEvents> implements StreamDeck {
export abstract class StreamDeckBase extends EventEmitter<StreamDeckEvents> implements StreamDeck {
get CONTROLS(): Readonly<StreamDeckControlDefinition[]> {
return this.deviceProperties.CONTROLS
}
Expand Down Expand Up @@ -78,14 +77,21 @@ export abstract class StreamDeckInputBase extends EventEmitter<StreamDeckEvents>

protected readonly device: HIDDevice
protected readonly deviceProperties: Readonly<StreamDeckProperties>
private readonly buttonsLcdService: ButtonsLcdService
// private readonly options: Readonly<OpenStreamDeckOptions>
private readonly keyState: boolean[]

constructor(device: HIDDevice, _options: OpenStreamDeckOptions, properties: StreamDeckProperties) {
constructor(
device: HIDDevice,
_options: OpenStreamDeckOptions,
properties: StreamDeckProperties,
buttonsLcdService: ButtonsLcdService
) {
super()

this.deviceProperties = properties
this.device = device
this.buttonsLcdService = buttonsLcdService

const maxButtonIndex = properties.CONTROLS.filter(
(control): control is StreamDeckButtonControlDefinition => control.type === 'button'
Expand Down Expand Up @@ -136,7 +142,9 @@ export abstract class StreamDeckInputBase extends EventEmitter<StreamDeckEvents>
}
}

public abstract calculateFillPanelDimensions(options?: FillPanelDimensionsOptions): Dimension | null
public calculateFillPanelDimensions(options?: FillPanelDimensionsOptions): Dimension | null {
return this.buttonsLcdService.calculateFillPanelDimensions(options)
}

public async close(): Promise<void> {
return this.device.close()
Expand All @@ -153,32 +161,6 @@ export abstract class StreamDeckInputBase extends EventEmitter<StreamDeckEvents>
public abstract getFirmwareVersion(): Promise<string>
public abstract getSerialNumber(): Promise<string>

public abstract fillKeyColor(keyIndex: KeyIndex, r: number, g: number, b: number): Promise<void>
public abstract fillKeyBuffer(keyIndex: KeyIndex, imageBuffer: Buffer, options?: FillImageOptions): Promise<void>
public abstract fillPanelBuffer(imageBuffer: Buffer, options?: FillPanelOptions): Promise<void>

public abstract clearKey(keyIndex: KeyIndex): Promise<void>
public abstract clearPanel(): Promise<void>
}

export abstract class StreamDeckBase extends StreamDeckInputBase {
protected readonly buttonsLcdService: ButtonsLcdService

constructor(
device: HIDDevice,
options: OpenStreamDeckOptions,
properties: StreamDeckProperties,
imageWriter: StreamdeckImageWriter,
imagePacker: ButtonLcdImagePacker
) {
super(device, options, properties)
this.buttonsLcdService = new ButtonsLcdService(imageWriter, imagePacker, device, properties)
}

public calculateFillPanelDimensions(options?: FillPanelDimensionsOptions): Dimension | null {
return this.buttonsLcdService.calculateFillPanelDimensions(options)
}

public async fillKeyColor(keyIndex: KeyIndex, r: number, g: number, b: number): Promise<void> {
this.checkValidKeyIndex(keyIndex, null)

Expand Down
22 changes: 14 additions & 8 deletions packages/core/src/models/generic-gen1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HIDDevice } from '../hid-device'
import { OpenStreamDeckOptions, StreamDeckBase, StreamDeckProperties } from './base'
import { StreamdeckImageWriter } from '../services/imageWriter/types'
import { BMP_HEADER_LENGTH, FillImageTargetOptions, transformImageBuffer, writeBMPHeader } from '../util'
import { ButtonLcdImagePacker, InternalFillImageOptions } from '../services/buttonsLcd'
import { ButtonLcdImagePacker, DefaultButtonsLcdService, InternalFillImageOptions } from '../services/buttonsLcd'

function extendDevicePropertiesForGen1(rawProps: StreamDeckGen1Properties): StreamDeckProperties {
return {
Expand All @@ -25,16 +25,22 @@ export class StreamDeckGen1 extends StreamDeckBase {
targetOptions: FillImageTargetOptions,
bmpImagePPM: number
) {
const fullProperties = extendDevicePropertiesForGen1(properties)

super(
device,
options,
extendDevicePropertiesForGen1(properties),
imageWriter,
new Gen1ButtonLcdImagePacker(
targetOptions,
bmpImagePPM,
properties.BUTTON_WIDTH_PX,
properties.BUTTON_HEIGHT_PX
fullProperties,
new DefaultButtonsLcdService(
imageWriter,
new Gen1ButtonLcdImagePacker(
targetOptions,
bmpImagePPM,
properties.BUTTON_WIDTH_PX,
properties.BUTTON_HEIGHT_PX
),
device,
fullProperties
)
)
}
Expand Down
22 changes: 14 additions & 8 deletions packages/core/src/models/generic-gen2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { StreamdeckDefaultImageWriter } from '../services/imageWriter/imageWrite
import { StreamdeckGen2ImageHeaderGenerator } from '../services/imageWriter/headerGenerator'
import type { StreamDeckLcdStripService, StreamDeckLcdStripServiceInternal } from '../types'
import { EncoderInputService } from '../services/encoder'
import { ButtonLcdImagePacker, InternalFillImageOptions } from '../services/buttonsLcd'
import { ButtonLcdImagePacker, DefaultButtonsLcdService, InternalFillImageOptions } from '../services/buttonsLcd'

function extendDevicePropertiesForGen2(rawProps: StreamDeckGen2Properties): StreamDeckProperties {
return {
Expand Down Expand Up @@ -35,16 +35,22 @@ export class StreamDeckGen2 extends StreamDeckBase {
lcdStripService: StreamDeckLcdStripServiceInternal | null,
disableXYFlip?: boolean
) {
const fullProperties = extendDevicePropertiesForGen2(properties)

super(
device,
options,
extendDevicePropertiesForGen2(properties),
new StreamdeckDefaultImageWriter(new StreamdeckGen2ImageHeaderGenerator()),
new Gen2ButtonLcdImagePacker(
options.encodeJPEG,
!disableXYFlip,
properties.BUTTON_WIDTH_PX,
properties.BUTTON_HEIGHT_PX
fullProperties,
new DefaultButtonsLcdService(
new StreamdeckDefaultImageWriter(new StreamdeckGen2ImageHeaderGenerator()),
new Gen2ButtonLcdImagePacker(
options.encodeJPEG,
!disableXYFlip,
properties.BUTTON_WIDTH_PX,
properties.BUTTON_HEIGHT_PX
),
device,
fullProperties
)
)

Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/models/pedal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { HIDDevice } from '../hid-device'
import { OpenStreamDeckOptions, StreamDeckInputBase, StreamDeckProperties } from './base'
import { OpenStreamDeckOptions, StreamDeckBase, StreamDeckProperties } from './base'
import { DeviceModelId, Dimension } from '../id'
import { FillImageOptions, FillPanelDimensionsOptions, FillPanelOptions } from '../types'
import { StreamDeckControlDefinition } from './controlDefinition'
import { freezeDefinitions } from './controlsGenerator'
import type { ButtonsLcdService } from '../services/buttonsLcd'

const pedalControls: StreamDeckControlDefinition[] = [
{
Expand Down Expand Up @@ -46,9 +47,9 @@ const pedalProperties: StreamDeckProperties = {
KEY_SPACING_VERTICAL: 0,
}

class StreamDeckPedal extends StreamDeckInputBase {
class StreamDeckPedal extends StreamDeckBase {
constructor(device: HIDDevice, options: Required<OpenStreamDeckOptions>) {
super(device, options, pedalProperties)
super(device, options, pedalProperties, new PedalLcdService())
}

/**
Expand All @@ -74,25 +75,26 @@ class StreamDeckPedal extends StreamDeckInputBase {
const val = await this.device.getFeatureReport(6, 32)
return val.toString('ascii', 2, 14)
}
}

class PedalLcdService implements ButtonsLcdService {
public calculateFillPanelDimensions(_options?: FillPanelDimensionsOptions): Dimension | null {
// Not supported
return null
}

public async fillKeyColor(_keyIndex: number, _r: number, _g: number, _b: number): Promise<void> {
public async clearKey(_keyIndex: number): Promise<void> {
// Not supported
}
public async fillKeyBuffer(_keyIndex: number, _imageBuffer: Buffer, _options?: FillImageOptions): Promise<void> {
public async clearPanel(): Promise<void> {
// Not supported
}
public async fillPanelBuffer(_imageBuffer: Buffer, _options?: FillPanelOptions): Promise<void> {
public async fillKeyColor(_keyIndex: number, _r: number, _g: number, _b: number): Promise<void> {
// Not supported
}
public async clearKey(_keyIndex: number): Promise<void> {
public async fillKeyBuffer(_keyIndex: number, _imageBuffer: Buffer, _options?: FillImageOptions): Promise<void> {
// Not supported
}
public async clearPanel(): Promise<void> {
public async fillPanelBuffer(_imageBuffer: Buffer, _options?: FillPanelOptions): Promise<void> {
// Not supported
}
}
Expand Down
49 changes: 30 additions & 19 deletions packages/core/src/services/buttonsLcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ interface GridSpan {
maxCol: number
}

export class ButtonsLcdService {
export interface ButtonsLcdService {
calculateFillPanelDimensions(options: FillPanelDimensionsOptions | undefined): Dimension | null

clearPanel(): Promise<void>
clearKey(keyIndex: KeyIndex): Promise<void>

fillKeyColor(keyIndex: KeyIndex, r: number, g: number, b: number): Promise<void>
fillKeyBuffer(keyIndex: KeyIndex, imageBuffer: Buffer, options?: FillImageOptions): Promise<void>
fillPanelBuffer(imageBuffer: Buffer, options: FillPanelOptions | undefined): Promise<void>
}

export class DefaultButtonsLcdService implements ButtonsLcdService {
readonly #imageWriter: StreamdeckImageWriter
readonly #imagePacker: ButtonLcdImagePacker
readonly #device: HIDDevice
Expand Down Expand Up @@ -128,6 +139,24 @@ export class ButtonsLcdService {
await Promise.all(ps)
}

public async clearKey(keyIndex: KeyIndex): Promise<void> {
const control = this.#deviceProperties.CONTROLS.find(
(control): control is StreamDeckButtonControlDefinition =>
control.type === 'button' && control.index === keyIndex
)

if (this.#deviceProperties.SUPPORTS_RGB_KEY_FILL || control?.feedbackType === 'rgb') {
await this.sendKeyRgb(keyIndex, 0, 0, 0)
} else {
const pixels = Buffer.alloc(this.imageRgbBytes, 0)
await this.fillImageRange(keyIndex, pixels, {
format: 'rgb',
offset: 0,
stride: this.#imagePacker.imageWidth * 3,
})
}
}

public async fillKeyColor(keyIndex: KeyIndex, r: number, g: number, b: number): Promise<void> {
this.checkRGBValue(r)
this.checkRGBValue(g)
Expand Down Expand Up @@ -214,24 +243,6 @@ export class ButtonsLcdService {
await this.#device.sendFeatureReport(Buffer.from([0x03, 0x06, keyIndex, red, green, blue]))
}

public async clearKey(keyIndex: KeyIndex): Promise<void> {
const control = this.#deviceProperties.CONTROLS.find(
(control): control is StreamDeckButtonControlDefinition =>
control.type === 'button' && control.index === keyIndex
)

if (this.#deviceProperties.SUPPORTS_RGB_KEY_FILL || control?.feedbackType === 'rgb') {
await this.sendKeyRgb(keyIndex, 0, 0, 0)
} else {
const pixels = Buffer.alloc(this.imageRgbBytes, 0)
await this.fillImageRange(keyIndex, pixels, {
format: 'rgb',
offset: 0,
stride: this.#imagePacker.imageWidth * 3,
})
}
}

private async fillImageRange(keyIndex: KeyIndex, imageBuffer: Buffer, sourceOptions: InternalFillImageOptions) {
const buttonControl = this.#deviceProperties.CONTROLS.find(
(control): control is StreamDeckButtonControlDefinition =>
Expand Down

0 comments on commit 71f5282

Please sign in to comment.