Skip to content

Commit

Permalink
fix: some typings errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian authored Oct 15, 2023
1 parent 5759f0b commit b5d5991
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 39 deletions.
1 change: 1 addition & 0 deletions lib/Surface/IP/ElgatoPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SurfaceIPElgatoPlugin extends EventEmitter {
constructor(registry, devicePath, socket, clientInfo) {
super()
this.controls = registry.controls
this.page = registry.page

this.socket = socket

Expand Down
143 changes: 104 additions & 39 deletions lib/Surface/USB/Infinitton.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,50 @@
import imageRs from '@julusian/image-rs'
import Infinitton from 'infinitton-idisplay'
import { translateRotation } from '../../Resources/Util.js'

class SurfaceUSBInfinitton {
constructor(ipcWrapper, devicePath) {
this.ipcWrapper = ipcWrapper
import { EventEmitter } from 'events'
import LogController from '../../Log/Controller.js'
import { convertPanelIndexToXY, convertXYToIndexForPanel } from '../Util.js'

class SurfaceUSBInfinitton extends EventEmitter {
/**
* @type {import('winston').Logger}
* @access private
* @readonly
*/
#logger

/**
* @type {Record<string, any>}
* @access private
*/
config

/**
* @type {Infinitton}
* @access private
*/
#infinitton

/**
* @param {string} devicePath
*/
constructor(devicePath) {
super()

this.#logger = LogController.createLogger(`Surface/USB/ElgatoStreamdeck/${devicePath}`)

try {
this.config = {
brightness: 100,
rotation: 0,
}

this.ipcWrapper.log('debug', 'Adding infinitton iDisplay USB device', devicePath)
this.#logger.debug(`Adding infinitton iDisplay USB device: ${devicePath}`)

this.Infinitton = new Infinitton(devicePath)
this.#infinitton = new Infinitton(devicePath)

const serialNumber = this.Infinitton.device.getDeviceInfo().serialNumber
// @ts-ignore
const serialNumber = this.#infinitton.device.getDeviceInfo().serialNumber

this.info = {
type: 'Infinitton iDisplay device',
Expand All @@ -47,62 +75,81 @@ class SurfaceUSBInfinitton {
rows: 3,
}

this.Infinitton.on('down', (keyIndex) => {
const key = this.reverseButton(keyIndex)
if (key === undefined) {
return
}
this.#infinitton.on('down', (keyIndex) => {
const key = this.#reverseButton(keyIndex)
if (key === undefined) return

this.ipcWrapper.click(key, true)
this.#emitClick(key, true)
})

this.Infinitton.on('up', (keyIndex) => {
const key = this.reverseButton(keyIndex)
if (key === undefined) {
return
}
this.#infinitton.on('up', (keyIndex) => {
const key = this.#reverseButton(keyIndex)
if (key === undefined) return

this.ipcWrapper.click(key, false)
this.#emitClick(key, false)
})

this.Infinitton.on('error', (error) => {
this.#infinitton.on('error', (error) => {
console.error(error)
this.ipcWrapper.remove()
this.emit('remove')
})
} catch (e) {
if (this.Infinitton) {
this.Infinitton.close()
if (this.#infinitton) {
this.#infinitton.close()
}

throw e
}
}

/**
* Produce a click event
* @param {number} key
* @param {boolean} state
*/
#emitClick(key, state) {
const xy = convertPanelIndexToXY(key, this.gridSize)
if (xy) {
this.emit('click', ...xy, state)
}
}

async #init() {
this.ipcWrapper.log('debug', `Infinitton iDisplay detected`)
this.#logger.debug(`Infinitton iDisplay detected`)

// Make sure the first clear happens properly
this.clearDeck()
}

static async create(ipcWrapper, devicePath) {
const self = new SurfaceUSBInfinitton(ipcWrapper, devicePath)
/**
* Open an infinitton
* @param {string} devicePath
* @returns {Promise<SurfaceUSBInfinitton>}
*/
static async create(devicePath) {
const self = new SurfaceUSBInfinitton(devicePath)

await self.#init()

return self
}

/**
* Process the information from the GUI and what is saved in database
* @param {Record<string, any>} config
* @param {boolean=} force
* @returns false when nothing happens
*/
setConfig(config, force) {
if ((force || this.config.brightness != config.brightness) && config.brightness !== undefined) {
this.Infinitton.setBrightness(config.brightness)
this.#infinitton.setBrightness(config.brightness)
}

this.config = config
}

quit() {
const dev = this.Infinitton
const dev = this.#infinitton

if (dev !== undefined) {
try {
Expand All @@ -113,11 +160,22 @@ class SurfaceUSBInfinitton {
}
}

draw(key, render) {
key = this.mapButton(key)
/**
* Draw a button
* @param {number} x
* @param {number} y
* @param {import('../../Graphics/ImageWrapper.js').ImageResult} render
* @returns {void}
*/
draw(x, y, render) {
let key = convertXYToIndexForPanel(x, y, this.gridSize)
if (key === null) return

key = this.#mapButton(key)

if (key >= 0 && !isNaN(key)) {
const imagesize = Math.sqrt(render.buffer.length / 4) // TODO: assuming here that the image is square
const targetSize = 72
const rotation = translateRotation(this.config.rotation)

try {
Expand All @@ -131,17 +189,20 @@ class SurfaceUSBInfinitton {
if (rotation !== null) image = image.rotate(rotation)

const newbuffer = image.toBufferSync(imageRs.PixelFormat.Rgb)
this.Infinitton.fillImage(key, newbuffer)
} catch (e) {
this.logger.debug(`scale image failed: ${e}\n${e.stack}`)
this.#infinitton.fillImage(key, newbuffer)
} catch (/** @type {any} */ e) {
this.#logger.debug(`scale image failed: ${e}\n${e.stack}`)
this.emit('remove')
return
}
}
return true
}

mapButton(input) {
/**
* @param {number} input
* @returns {number}
*/
#mapButton(input) {
const map = '4 3 2 1 0 9 8 7 6 5 14 13 12 11 10'.split(/ /)
if (input < 0) {
return -1
Expand All @@ -150,21 +211,25 @@ class SurfaceUSBInfinitton {
return parseInt(map[input])
}

reverseButton(input) {
/**
* @param {number} input
* @returns {number | undefined}
*/
#reverseButton(input) {
const map = '4 3 2 1 0 9 8 7 6 5 14 13 12 11 10'.split(/ /)
for (let pos = 0; pos < map.length; pos++) {
if (map[input] == pos) return pos
if (Number(map[input]) == pos) return pos
}

return
}

clearDeck() {
this.ipcWrapper.log('debug', 'infinitton.prototype.clearDeck()')
this.#logger.debug('infinitton.prototype.clearDeck()')

const keysTotal = this.gridSize.columns * this.gridSize.rows
for (let x = 0; x < keysTotal; x++) {
this.Infinitton.clearKey(x)
this.#infinitton.clearKey(x)
}
}
}
Expand Down

0 comments on commit b5d5991

Please sign in to comment.