Skip to content

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

License

Notifications You must be signed in to change notification settings

stilesdev/stackmat

Repository files navigation

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

TimerEvent When Fired
packetRecieved Every time a valid packet is recieved
timerConnected Valid packets start being received
timerDisconnected Valid packets stop being received
started A solve is started, and the timer starts counting up from zero
stopped A solve is completed, and the timer is stopped
reset The timer is reset to zero
ready Both hands are placed on the timer while the timer is in a reset state
unready One or both hands are removed before the green light turns on and the timer is in a ready state
starting The green light turns on while in a ready state, indicating the solve can be started
leftHandDown The left hand is placed on the timer while the timer is in any state
leftHandUp The left hand is removed from the timer while the timer is in any state
rightHandDown The right hand is placed on the timer while the timer is in any state
rightHandUp The 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',
}