Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jul 11, 2024
1 parent 16efb4e commit 8bd045a
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 70 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs'
import * as path from 'path'

export function readFixtureJSON(fileName: string): Buffer {
export function readFixtureJSON(fileName: string): Uint8Array {
const filePath = path.resolve(__dirname, '../../../../fixtures', fileName)
const fileData = fs.readFileSync(filePath)
return Buffer.from(JSON.parse(fileData.toString()) as Array<number>)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/__tests__/hid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export class DummyHID extends EventEmitter implements HIDDevice {
expect(typeof path).toEqual('string')
}

public async sendFeatureReport(_data: Buffer): Promise<void> {
public async sendFeatureReport(_data: Uint8Array): Promise<void> {
throw new Error('Method not implemented.')
}
public async getFeatureReport(_reportId: number, _reportLength: number): Promise<Buffer> {
public async getFeatureReport(_reportId: number, _reportLength: number): Promise<Uint8Array> {
throw new Error('Method not implemented.')
}
public async sendReports(_data: Buffer[]): Promise<void> {
public async sendReports(_data: Uint8Array[]): Promise<void> {
throw new Error('Method not implemented.')
}
public async close(): Promise<void> {
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/__tests__/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DummyHID } from './hid'
import { EncodeJPEGHelper } from '../models/base'

function openStreamDeck(path: string, deviceModel: DeviceModelId, userOptions?: OpenStreamDeckOptions): StreamDeck {
const encodeJpegMock: jest.MockedFunction<EncodeJPEGHelper> = jest.fn((_b: Buffer, _w: number, _h: number) => {
const encodeJpegMock: jest.MockedFunction<EncodeJPEGHelper> = jest.fn((_b: Uint8Array, _w: number, _h: number) => {
throw new Error('Not implemented')
})
const options: Required<OpenStreamDeckOptions> = {
Expand Down Expand Up @@ -115,7 +115,7 @@ function runForDevice(path: string, model: DeviceModelId): void {

test('firmwareVersion', async () => {
const device = getDevice()
device.getFeatureReport = async (): Promise<Buffer> => {
device.getFeatureReport = async (): Promise<Uint8Array> => {
return Buffer.from([4, 85, 170, 212, 4, 49, 46, 48, 46, 49, 55, 48, 49, 51, 51, 0, 0])
}

Expand All @@ -125,7 +125,7 @@ function runForDevice(path: string, model: DeviceModelId): void {

test('serialNumber', async () => {
const device = getDevice()
device.getFeatureReport = async (): Promise<Buffer> => {
device.getFeatureReport = async (): Promise<Uint8Array> => {
return Buffer.from([3, 85, 170, 211, 3, 65, 76, 51, 55, 71, 49, 65, 48, 50, 56, 52, 48])
}

Expand Down Expand Up @@ -167,7 +167,7 @@ function runForDevice(path: string, model: DeviceModelId): void {

test('firmwareVersion-jpeg', async () => {
const device = getDevice()
device.getFeatureReport = async (): Promise<Buffer> => {
device.getFeatureReport = async (): Promise<Uint8Array> => {
// prettier-ignore
return Buffer.from([ 5, 12, 254, 90, 239, 250, 49, 46, 48, 48, 46, 48, 48, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
}
Expand All @@ -178,7 +178,7 @@ function runForDevice(path: string, model: DeviceModelId): void {

test('serialNumber-jpeg', async () => {
const device = getDevice()
device.getFeatureReport = async (): Promise<Buffer> => {
device.getFeatureReport = async (): Promise<Uint8Array> => {
// prettier-ignore
return Buffer.from([ 6, 12, 67, 76, 49, 56, 73, 49, 65, 48, 48, 57, 49, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
}
Expand Down Expand Up @@ -589,7 +589,7 @@ describe('StreamDeck XL', () => {

test('firmwareVersion', async () => {
const device = getDevice()
device.getFeatureReport = async (): Promise<Buffer> => {
device.getFeatureReport = async (): Promise<Uint8Array> => {
// prettier-ignore
return Buffer.from([ 5, 12, 254, 90, 239, 250, 49, 46, 48, 48, 46, 48, 48, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
}
Expand All @@ -600,7 +600,7 @@ describe('StreamDeck XL', () => {

test('serialNumber', async () => {
const device = getDevice()
device.getFeatureReport = async (): Promise<Buffer> => {
device.getFeatureReport = async (): Promise<Uint8Array> => {
// prettier-ignore
return Buffer.from([ 6, 12, 67, 76, 49, 56, 73, 49, 65, 48, 48, 57, 49, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
}
Expand All @@ -611,7 +611,7 @@ describe('StreamDeck XL', () => {

test('fillImage', async () => {
const device = getDevice()
device.encodeJPEG.mockImplementationOnce(async (buffer: Buffer) => {
device.encodeJPEG.mockImplementationOnce(async (buffer: Uint8Array) => {
const start = buffer.length / 8
return buffer.subarray(start, start * 2)
})
Expand Down Expand Up @@ -664,7 +664,7 @@ describe('StreamDeck Original V2', () => {

test('fillImage', async () => {
const device = getDevice()
device.encodeJPEG.mockImplementationOnce(async (buffer: Buffer) => {
device.encodeJPEG.mockImplementationOnce(async (buffer: Uint8Array) => {
const start = buffer.length / 8
return buffer.subarray(start, start * 2)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { transformImageBuffer } from '../util'

function getSimpleBuffer(dim: number, components: 3 | 4): Buffer {
function getSimpleBuffer(dim: number, components: 3 | 4): Uint8Array {
const buf = Buffer.alloc(dim * dim * components)
for (let i = 0; i < buf.length; i++) {
buf[i] = i
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export interface HIDDevice {

close(): Promise<void>

sendFeatureReport(data: Buffer): Promise<void>
getFeatureReport(reportId: number, reportLength: number): Promise<Buffer>
sendFeatureReport(data: Uint8Array): Promise<void>
getFeatureReport(reportId: number, reportLength: number): Promise<Uint8Array>

sendReports(buffers: Buffer[]): Promise<void>
sendReports(buffers: Uint8Array[]): Promise<void>

getDeviceInfo(): Promise<HIDDeviceInfo>
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/imageWriter/imageWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StreamdeckImageHeaderGenerator, StreamdeckImageWriter, StreamdeckImageW
export class StreamdeckOriginalImageWriter implements StreamdeckImageWriter {
private readonly headerGenerator = new StreamdeckGen1ImageHeaderGenerator()

public generateFillImageWrites(props: StreamdeckImageWriterProps, byteBuffer: Buffer): Buffer[] {
public generateFillImageWrites(props: StreamdeckImageWriterProps, byteBuffer: Uint8Array): Uint8Array[] {
const MAX_PACKET_SIZE = 8191
const PACKET_HEADER_LENGTH = this.headerGenerator.getFillImageCommandHeaderLength()

Expand Down Expand Up @@ -33,12 +33,12 @@ export class StreamdeckDefaultImageWriter<TProps = StreamdeckImageWriterProps>
this.headerGenerator = headerGenerator
}

public generateFillImageWrites(props: TProps, byteBuffer: Buffer): Buffer[] {
public generateFillImageWrites(props: TProps, byteBuffer: Uint8Array): Uint8Array[] {
const MAX_PACKET_SIZE = 1024
const PACKET_HEADER_LENGTH = this.headerGenerator.getFillImageCommandHeaderLength()
const MAX_PAYLOAD_SIZE = MAX_PACKET_SIZE - PACKET_HEADER_LENGTH

const result: Buffer[] = []
const result: Uint8Array[] = []

let remainingBytes = byteBuffer.length
for (let part = 0; remainingBytes > 0; part++) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/imageWriter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ export interface StreamdeckImageWriterProps {
}

export interface StreamdeckImageWriter<TProps = StreamdeckImageWriterProps> {
generateFillImageWrites(props: TProps, byteBuffer: Buffer): Buffer[]
generateFillImageWrites(props: TProps, byteBuffer: Uint8Array): Uint8Array[]
}

export interface StreamdeckImageHeaderGenerator<TProps = StreamdeckImageWriterProps> {
getFillImageCommandHeaderLength(): number
writeFillImageCommandHeader(
buffer: Buffer,
buffer: Uint8Array,
props: TProps,
partIndex: number,
isLast: boolean,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/models/base-gen2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export abstract class StreamDeckGen2Base extends StreamDeckBase {
return val.toString('ascii', 2, end)
}

protected async convertFillImage(sourceBuffer: Buffer, sourceOptions: InternalFillImageOptions): Promise<Buffer> {
protected async convertFillImage(
sourceBuffer: Uint8Array,
sourceOptions: InternalFillImageOptions
): Promise<Uint8Array> {
const byteBuffer = transformImageBuffer(
sourceBuffer,
sourceOptions,
Expand Down
25 changes: 16 additions & 9 deletions packages/core/src/models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../types'
import type { StreamdeckImageWriter } from '../imageWriter/types'

export type EncodeJPEGHelper = (buffer: Buffer, width: number, height: number) => Promise<Buffer>
export type EncodeJPEGHelper = (buffer: Uint8Array, width: number, height: number) => Promise<Uint8Array>

export interface OpenStreamDeckOptions {
useOriginalKeyOrder?: boolean
Expand Down Expand Up @@ -153,23 +153,27 @@ export abstract class StreamDeckInputBase extends EventEmitter<StreamDeckEvents>
}

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 fillKeyBuffer(
keyIndex: KeyIndex,
imageBuffer: Uint8Array,
options?: FillImageOptions
): Promise<void>
public abstract fillPanelBuffer(imageBuffer: Uint8Array, options?: FillPanelOptions): Promise<void>

public async fillLcd(_imageBuffer: Buffer, _sourceOptions: FillImageOptions): Promise<void> {
throw new Error('Not supported for this model')
}
public async fillEncoderLcd(
_index: EncoderIndex,
_buffer: Buffer,
_buffer: Uint8Array,
_sourceOptions: FillImageOptions
): Promise<void> {
throw new Error('Not supported for this model')
}
public async fillLcdRegion(
_x: number,
_y: number,
_imageBuffer: Buffer,
_imageBuffer: Uint8Array,
_sourceOptions: FillLcdImageOptions
): Promise<void> {
throw new Error('Not supported for this model')
Expand Down Expand Up @@ -212,7 +216,7 @@ export abstract class StreamDeckBase extends StreamDeckInputBase {
}
}

public async fillKeyBuffer(keyIndex: KeyIndex, imageBuffer: Buffer, options?: FillImageOptions): Promise<void> {
public async fillKeyBuffer(keyIndex: KeyIndex, imageBuffer: Uint8Array, options?: FillImageOptions): Promise<void> {
this.checkValidKeyIndex(keyIndex)

const sourceFormat = options?.format ?? 'rgb'
Expand All @@ -231,7 +235,7 @@ export abstract class StreamDeckBase extends StreamDeckInputBase {
})
}

public async fillPanelBuffer(imageBuffer: Buffer, options?: FillPanelOptions): Promise<void> {
public async fillPanelBuffer(imageBuffer: Uint8Array, options?: FillPanelOptions): Promise<void> {
const sourceFormat = options?.format ?? 'rgb'
this.checkSourceFormat(sourceFormat)

Expand Down Expand Up @@ -314,9 +318,12 @@ export abstract class StreamDeckBase extends StreamDeckInputBase {
await Promise.all(ps)
}

protected abstract convertFillImage(imageBuffer: Buffer, sourceOptions: InternalFillImageOptions): Promise<Buffer>
protected abstract convertFillImage(
imageBuffer: Uint8Array,
sourceOptions: InternalFillImageOptions
): Promise<Uint8Array>

private async fillImageRange(keyIndex: KeyIndex, imageBuffer: Buffer, sourceOptions: InternalFillImageOptions) {
private async fillImageRange(keyIndex: KeyIndex, imageBuffer: Uint8Array, sourceOptions: InternalFillImageOptions) {
this.checkValidKeyIndex(keyIndex)

const byteBuffer = await this.convertFillImage(imageBuffer, sourceOptions)
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/models/mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export class StreamDeckMini extends StreamDeckGen1Base {
super(device, options, miniProperties)
}

protected async convertFillImage(sourceBuffer: Buffer, sourceOptions: InternalFillImageOptions): Promise<Buffer> {
protected async convertFillImage(
sourceBuffer: Uint8Array,
sourceOptions: InternalFillImageOptions
): Promise<Uint8Array> {
const byteBuffer = transformImageBuffer(
sourceBuffer,
sourceOptions,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/models/miniv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export class StreamDeckMiniV2 extends StreamDeckGen1Base {
super(device, options, miniV2Properties)
}

protected async convertFillImage(sourceBuffer: Buffer, sourceOptions: InternalFillImageOptions): Promise<Buffer> {
protected async convertFillImage(
sourceBuffer: Uint8Array,
sourceOptions: InternalFillImageOptions
): Promise<Uint8Array> {
const byteBuffer = transformImageBuffer(
sourceBuffer,
sourceOptions,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/models/original.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export class StreamDeckOriginal extends StreamDeckGen1Base {
}
}

protected async convertFillImage(sourceBuffer: Buffer, sourceOptions: InternalFillImageOptions): Promise<Buffer> {
protected async convertFillImage(
sourceBuffer: Uint8Array,
sourceOptions: InternalFillImageOptions
): Promise<Uint8Array> {
const byteBuffer = transformImageBuffer(
sourceBuffer,
sourceOptions,
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/models/pedal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ export class StreamDeckPedal extends StreamDeckInputBase {
public async fillKeyColor(_keyIndex: number, _r: number, _g: number, _b: number): Promise<void> {
// Not supported
}
public async fillKeyBuffer(_keyIndex: number, _imageBuffer: Buffer, _options?: FillImageOptions): Promise<void> {
public async fillKeyBuffer(
_keyIndex: number,
_imageBuffer: Uint8Array,
_options?: FillImageOptions
): Promise<void> {
// Not supported
}
public async fillPanelBuffer(_imageBuffer: Buffer, _options?: FillPanelOptions): Promise<void> {
public async fillPanelBuffer(_imageBuffer: Uint8Array, _options?: FillPanelOptions): Promise<void> {
// Not supported
}
public async clearKey(_keyIndex: number): Promise<void> {
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/models/plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class StreamDeckPlus extends StreamDeckGen2Base {

public override async fillEncoderLcd(
index: EncoderIndex,
buffer: Buffer,
buffer: Uint8Array,
sourceOptions: FillImageOptions
): Promise<void> {
if (this.NUM_ENCODERS === 0) throw new Error(`There are no encoders`)
Expand All @@ -153,7 +153,7 @@ export class StreamDeckPlus extends StreamDeckGen2Base {
public override async fillLcdRegion(
x: number,
y: number,
imageBuffer: Buffer,
imageBuffer: Uint8Array,
sourceOptions: FillLcdImageOptions
): Promise<void> {
// Basic bounds checking
Expand All @@ -177,7 +177,10 @@ export class StreamDeckPlus extends StreamDeckGen2Base {
await this.device.sendReports(packets)
}

private async convertFillLcdBuffer(sourceBuffer: Buffer, sourceOptions: FillLcdImageOptions): Promise<Buffer> {
private async convertFillLcdBuffer(
sourceBuffer: Uint8Array,
sourceOptions: FillLcdImageOptions
): Promise<Uint8Array> {
const sourceOptions2: InternalFillImageOptions = {
format: sourceOptions.format,
offset: 0,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ export class StreamDeckProxy implements StreamDeck {
public async fillKeyColor(keyIndex: KeyIndex, r: number, g: number, b: number): Promise<void> {
return this.device.fillKeyColor(keyIndex, r, g, b)
}
public async fillKeyBuffer(keyIndex: KeyIndex, imageBuffer: Buffer, options?: FillImageOptions): Promise<void> {
public async fillKeyBuffer(keyIndex: KeyIndex, imageBuffer: Uint8Array, options?: FillImageOptions): Promise<void> {
return this.device.fillKeyBuffer(keyIndex, imageBuffer, options)
}
public async fillPanelBuffer(imageBuffer: Buffer, options?: FillPanelOptions): Promise<void> {
public async fillPanelBuffer(imageBuffer: Uint8Array, options?: FillPanelOptions): Promise<void> {
return this.device.fillPanelBuffer(imageBuffer, options)
}
public async clearKey(keyIndex: KeyIndex): Promise<void> {
Expand Down Expand Up @@ -109,7 +109,7 @@ export class StreamDeckProxy implements StreamDeck {

public async fillEncoderLcd(
index: EncoderIndex,
imageBuffer: Buffer,
imageBuffer: Uint8Array,
sourceOptions: FillImageOptions
): Promise<void> {
return this.device.fillEncoderLcd(index, imageBuffer, sourceOptions)
Expand All @@ -118,7 +118,7 @@ export class StreamDeckProxy implements StreamDeck {
public async fillLcdRegion(
x: number,
y: number,
imageBuffer: Buffer,
imageBuffer: Uint8Array,
sourceOptions: FillLcdImageOptions
): Promise<void> {
return this.device.fillLcdRegion(x, y, imageBuffer, sourceOptions)
Expand Down
Loading

0 comments on commit 8bd045a

Please sign in to comment.