Skip to content

Commit

Permalink
chore: rename btnIndex-keyIndex for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Nov 17, 2021
1 parent 095a417 commit 715324c
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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 }[]
Expand All @@ -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,
Expand Down Expand Up @@ -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: [
Expand Down
36 changes: 18 additions & 18 deletions packages/core/src/xkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
}

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions packages/node-record-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -312,43 +312,43 @@ function askQuestion(query: string): Promise<string | number> {
})
)
}
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)
Expand Down
4 changes: 2 additions & 2 deletions packages/node/examples/manually-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

// ...
Expand Down
12 changes: 6 additions & 6 deletions packages/webhid-demo/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ async function openDevice(device: HIDDevice): Promise<void> {

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}`)
Expand Down

0 comments on commit 715324c

Please sign in to comment.