From 715324c2480b5886378d35278329956567244760 Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Wed, 17 Nov 2021 14:39:52 +0100 Subject: [PATCH] chore: rename btnIndex-keyIndex for consistency --- packages/core/src/api.ts | 4 +-- packages/core/src/lib.ts | 8 ++--- packages/core/src/products.ts | 10 +++--- packages/core/src/xkeys.ts | 36 +++++++++++----------- packages/node-record-test/src/index.ts | 28 ++++++++--------- packages/node/examples/manually-connect.js | 4 +-- packages/webhid-demo/src/app.ts | 12 ++++---- 7 files changed, 51 insertions(+), 51 deletions(-) diff --git a/packages/core/src/api.ts b/packages/core/src/api.ts index 949bbe1..28782d8 100644 --- a/packages/core/src/api.ts +++ b/packages/core/src/api.ts @@ -53,8 +53,8 @@ export interface ButtonEventMetadata extends EventMetadata { export interface XKeysEvents { // Note: This interface defines strong typings for any events that are emitted by the XKeys class. - down: (btnIndex: number, metadata: ButtonEventMetadata) => void - up: (btnIndex: number, metadata: ButtonEventMetadata) => void + down: (keyIndex: number, metadata: ButtonEventMetadata) => void + up: (keyIndex: number, metadata: ButtonEventMetadata) => void jog: (index: number, value: number, eventMetadata: EventMetadata) => void shuttle: (index: number, value: number, eventMetadata: EventMetadata) => void diff --git a/packages/core/src/lib.ts b/packages/core/src/lib.ts index d385536..8aa4cc4 100644 --- a/packages/core/src/lib.ts +++ b/packages/core/src/lib.ts @@ -17,13 +17,13 @@ export function describeEvent(event: string, args: any[]): string { } if (event === 'down') { - const btnIndex = args[0] + const keyIndex = args[0] const metadata = args[1] - return `Button ${btnIndex} pressed. Metadata: ${metadataStr(metadata)}` + return `Button ${keyIndex} pressed. Metadata: ${metadataStr(metadata)}` } else if (event === 'up') { - const btnIndex = args[0] + const keyIndex = args[0] const metadata = args[1] - return `Button ${btnIndex} released. Metadata: ${metadataStr(metadata)}` + return `Button ${keyIndex} released. Metadata: ${metadataStr(metadata)}` } else if (event === 'jog') { const index = args[0] const value = args[1] diff --git a/packages/core/src/products.ts b/packages/core/src/products.ts index 851ee16..83720fa 100644 --- a/packages/core/src/products.ts +++ b/packages/core/src/products.ts @@ -33,7 +33,7 @@ export interface Product { */ layouts?: [string, number, number, number, number, number][] - /** Maps the (internal) btnIndex to a [Row, Column] */ + /** Maps the (internal) keyIndex to a [Row, Column] */ btnLocation?: [number, number][] /** The index of the start to the 4 byte time stamp. */ @@ -45,9 +45,9 @@ export interface Product { joyZbyte: number }[] - /** used to determine what btnIndex to back light mapping should be used. */ + /** used to determine what keyIndex to back light mapping should be used. */ backLightType: BackLightType - /** blocks certain btnIndex from calling button events. */ + /** blocks certain keyIndex from calling button events. */ disableButtons?: number[] hasJog?: { jogByte: number }[] @@ -68,7 +68,7 @@ export enum BackLightType { LEGACY = 2, /** Only blue light. Is the stick buttons, that requires special mapping. */ STICK_BUTTONS = 3, - /** Backlight LED type 4, is the 40 buttons, map btnIndex-1 to ledIndex */ + /** Backlight LED type 4, is the 40 buttons, map keyIndex-1 to ledIndex */ LINEAR = 4, /** Backlight LED type 5 is the RGB 24 buttons */ REMAP_24 = 5, @@ -283,7 +283,7 @@ export const PRODUCTS: { [name: string]: Product } = { colCount: 20, // number of physical columns, rowCount: 2, // number of physical rows hasPS: true, // behind small hole on right side. - backLightType: BackLightType.LINEAR, // map btnIndex-1 to ledIndex + backLightType: BackLightType.LINEAR, // map keyIndex-1 to ledIndex backLight2offset: 40, // off set to control second led bank. timestampByte: 31, // ms time since device boot 4 byte BE btnLocation: [ diff --git a/packages/core/src/xkeys.ts b/packages/core/src/xkeys.ts index c35e375..4f4f391 100644 --- a/packages/core/src/xkeys.ts +++ b/packages/core/src/xkeys.ts @@ -133,7 +133,7 @@ export class XKeys extends EventEmitter { // program switch/button is on byte index 1 , bit 1 const d = data.readUInt8(1) const bit = d & (1 << 0) ? true : false // get first bit only - newButtonStates.set(0, bit) // always btnIndex of PS to 0 + newButtonStates.set(0, bit) // always keyIndex of PS to 0 } this.product.hasJog?.forEach((jog, index) => { const d = data[jog.jogByte] // Jog @@ -163,8 +163,8 @@ export class XKeys extends EventEmitter { // Disabled/nonexisting buttons: important as some "buttons" in the jog & shuttle devices are used for shuttle events in hardware. if (this.product.disableButtons) { - this.product.disableButtons.forEach((btnIndex) => { - newButtonStates.set(btnIndex, false) + this.product.disableButtons.forEach((keyIndex) => { + newButtonStates.set(keyIndex, false) }) } @@ -344,24 +344,24 @@ export class XKeys extends EventEmitter { } /** * Sets the backlight of a button - * @param btnIndex The button of which to set the backlight color + * @param keyIndex The button of which to set the backlight color * @param color r,g,b or string (RGB, RRGGBB, #RRGGBB) * @param flashing boolean: flashing or not (if on) * @returns undefined */ public setBacklight( - btnIndex: number, + keyIndex: number, /** RGB, RRGGBB, #RRGGBB */ color: Color | string | boolean | null, flashing?: boolean ): void { this.ensureInitialized() - if (btnIndex === 0) return // PS-button has no backlight + if (keyIndex === 0) return // PS-button has no backlight - this._verifyButtonIndex(btnIndex) + this._verifyButtonIndex(keyIndex) color = this._interpretColor(color, this.product.backLightType) - const location = this._findBtnLocation(btnIndex) + const location = this._findBtnLocation(keyIndex) if (this.product.backLightType === BackLightType.REMAP_24) { const ledIndex = (location.col - 1) * 8 + location.row - 1 @@ -382,7 +382,7 @@ export class XKeys extends EventEmitter { } else if (this.product.backLightType === BackLightType.LINEAR) { // The 40 buttons, that requires special mapping. - const ledIndex = btnIndex - 1 // 0 based linear numbering sort of... + const ledIndex = keyIndex - 1 // 0 based linear numbering sort of... const on: boolean = color.r > 0 || color.g > 0 || color.b > 0 @@ -587,24 +587,24 @@ export class XKeys extends EventEmitter { return message } - private _verifyButtonIndex(btnIndex: number): void { - if (!(btnIndex >= 0 && btnIndex < 8 * this.product.bBytes + 1)) { - throw new Error(`Invalid btnIndex: ${btnIndex}`) + private _verifyButtonIndex(keyIndex: number): void { + if (!(keyIndex >= 0 && keyIndex < 8 * this.product.bBytes + 1)) { + throw new Error(`Invalid keyIndex: ${keyIndex}`) } } - private _findBtnLocation(btnIndex: number): { row: number; col: number } { + private _findBtnLocation(keyIndex: number): { row: number; col: number } { let location: { row: number; col: number } = { row: 0, col: 0 } // derive the Row and Column from the button index for many products - if (btnIndex !== 0) { + if (keyIndex !== 0) { // program switch is always on index 0 and always R:0, C:0 unless remapped by btnLocaion array - location.row = btnIndex - this.product.bBits * (Math.ceil(btnIndex / this.product.bBits) - 1) - location.col = Math.ceil(btnIndex / this.product.bBits) + location.row = keyIndex - this.product.bBits * (Math.ceil(keyIndex / this.product.bBits) - 1) + location.col = Math.ceil(keyIndex / this.product.bBits) } // if the product has a btnLocaion array, then look up the Row and Column if (this.product.btnLocation !== undefined) { location = { - row: this.product.btnLocation[btnIndex][0], - col: this.product.btnLocation[btnIndex][1], + row: this.product.btnLocation[keyIndex][0], + col: this.product.btnLocation[keyIndex][1], } } return location diff --git a/packages/node-record-test/src/index.ts b/packages/node-record-test/src/index.ts index 9305a37..1e05f40 100644 --- a/packages/node-record-test/src/index.ts +++ b/packages/node-record-test/src/index.ts @@ -278,8 +278,8 @@ async function startRecording(panel: HID_Device) { console.log(description) if (event === 'down') { - const btnIndex = args[0] - colorLoop = doColorLoop(xkeys, btnIndex) + const keyIndex = args[0] + colorLoop = doColorLoop(xkeys, keyIndex) } else if (event === 'up') { if (colorLoop) { colorLoop.stop() @@ -312,43 +312,43 @@ function askQuestion(query: string): Promise { }) ) } -function doColorLoop(xkeys: XKeys, btnIndex: number) { +function doColorLoop(xkeys: XKeys, keyIndex: number) { let active = true const doLoop = async () => { while (active) { - xkeys.setBacklight(btnIndex, '0000ff') + xkeys.setBacklight(keyIndex, '0000ff') await waitTime(300) - xkeys.setBacklight(btnIndex, '000000') + xkeys.setBacklight(keyIndex, '000000') await waitTime(200) if (!active) break - xkeys.setBacklight(btnIndex, 'ff0000') + xkeys.setBacklight(keyIndex, 'ff0000') await waitTime(300) - xkeys.setBacklight(btnIndex, '000000') + xkeys.setBacklight(keyIndex, '000000') await waitTime(200) if (!active) break - xkeys.setBacklight(btnIndex, '00ff00') + xkeys.setBacklight(keyIndex, '00ff00') await waitTime(300) - xkeys.setBacklight(btnIndex, '000000') + xkeys.setBacklight(keyIndex, '000000') await waitTime(200) if (!active) break - xkeys.setBacklight(btnIndex, 'ffffff') + xkeys.setBacklight(keyIndex, 'ffffff') await waitTime(300) - xkeys.setBacklight(btnIndex, '000000') + xkeys.setBacklight(keyIndex, '000000') await waitTime(200) if (!active) break - xkeys.setBacklight(btnIndex, '000000') + xkeys.setBacklight(keyIndex, '000000') await waitTime(300) - xkeys.setBacklight(btnIndex, '000000') + xkeys.setBacklight(keyIndex, '000000') await waitTime(200) await waitTime(1000) } - xkeys.setBacklight(btnIndex, '000000') + xkeys.setBacklight(keyIndex, '000000') } doLoop().catch(console.log) diff --git a/packages/node/examples/manually-connect.js b/packages/node/examples/manually-connect.js index 09bee17..a3b0b2c 100644 --- a/packages/node/examples/manually-connect.js +++ b/packages/node/examples/manually-connect.js @@ -17,8 +17,8 @@ setupXkeysPanel() console.log('X-keys error:', ...errs) }) - xkeysPanel.on('down', (btnIndex, metadata) => { - console.log('Button pressed', btnIndex, metadata) + xkeysPanel.on('down', (keyIndex, metadata) => { + console.log('Button pressed', keyIndex, metadata) }) // ... diff --git a/packages/webhid-demo/src/app.ts b/packages/webhid-demo/src/app.ts index 829db16..bbd3136 100644 --- a/packages/webhid-demo/src/app.ts +++ b/packages/webhid-demo/src/app.ts @@ -16,13 +16,13 @@ async function openDevice(device: HIDDevice): Promise { appendLog(`Connected to "${xkeys.info.name}"`) - xkeys.on('down', (btnIndex: number) => { - appendLog(`Button ${btnIndex} down`) - xkeys.setBacklight(btnIndex, 'blue') + xkeys.on('down', (keyIndex: number) => { + appendLog(`Button ${keyIndex} down`) + xkeys.setBacklight(keyIndex, 'blue') }) - xkeys.on('up', (btnIndex: number) => { - appendLog(`Button ${btnIndex} up`) - xkeys.setBacklight(btnIndex, null) + xkeys.on('up', (keyIndex: number) => { + appendLog(`Button ${keyIndex} up`) + xkeys.setBacklight(keyIndex, null) }) xkeys.on('jog', (index, value) => { appendLog(`Jog #${index}: ${value}`)