1.1.1 • Published 1 year ago

stackmat v1.1.1

Weekly downloads
2
License
MIT
Repository
-
Last release
1 year ago

Stackmat

Subscribe to events received from a Stackmat timer connected to the browser via the HTML5 Audio API.

Both a Typescript-based library and a UMD build are available.

Typescript Usage

Install the library:

npm install stackmat

Subscribe to events and start listening for packets:

import { Stackmat, Packet } from 'stackmat'

const stackmat = new Stackmat()

stackmat.on('started', (packet: Packet) => {
    console.log('Timer started')
})

stackmat.on('stopped', (packet: Packet) => {
    console.log('Timer stopped at: ' + packet.timeAsString)
})

stackmat.start()

Browser Usage

Import the UMD build:

<script type="text/javascript" src="https://unpkg.com/stackmat"></script>

Subscribe to events and start listening for packets:

<script type="text/javascript">
const stackmat = new Stackmat();

stackmat.on('started', packet => {
    console.log('Timer started')
})

stackmat.on('stopped', packet => {
    console.log('Timer stopped at: ' + packet.timeAsString)
})

stackmat.start()
</script>

Disconnecting

stackmat.stop() may be called to stop listening for packets, without unsubscribing from any events.

stackmat.off(TimerEvent?) may be called to unsubscribe from a specified event, or all events if no specific event is provided.

Events

TimerEventWhen Fired
packetRecievedEvery time a valid packet is recieved
timerConnectedValid packets start being received
timerDisconnectedValid packets stop being received
startedA solve is started, and the timer starts counting up from zero
stoppedA solve is completed, and the timer is stopped
resetThe timer is reset to zero
readyBoth hands are placed on the timer while the timer is in a reset state
unreadyOne or both hands are removed before the green light turns on and the timer is in a ready state
startingThe green light turns on while in a ready state, indicating the solve can be started
leftHandDownThe left hand is placed on the timer while the timer is in any state
leftHandUpThe left hand is removed from the timer while the timer is in any state
rightHandDownThe right hand is placed on the timer while the timer is in any state
rightHandUpThe right hand is removed from the timer while the timer is in any state

Packet Interface

Each event callback (with the exception of timerConnected and timerDisconnected) sends a Packet with the following properties:

interface Packet {
    isValid: boolean
    status: PacketStatus
    timeInMilliseconds: number
    timeAsString: string
    isLeftHandDown: boolean
    isRightHandDown: boolean
    areBothHandsDown: boolean
}

PacketStatus is an enum representing the internal code sent with each Stackmat packet:

enum PacketStatus {
    IDLE = 'I',
    STARTING = 'A',
    RUNNING = ' ',
    STOPPED = 'S',
    LEFT_HAND = 'L',
    RIGHT_HAND = 'R',
    BOTH_HANDS = 'C',
    INVALID = 'X',
}