Skip to content

Instantly share code, notes, and snippets.

@Sawtaytoes
Created September 20, 2018 02:49
Show Gist options
  • Save Sawtaytoes/564aadb81db85aacf3e00cb92d387c47 to your computer and use it in GitHub Desktop.
Save Sawtaytoes/564aadb81db85aacf3e00cb92d387c47 to your computer and use it in GitHub Desktop.
Flic Button listener for single- and double-button presses.
import {
BUTTON_DOWN,
doublePressAction,
singlePressAction,
} from './actions'
const timeLimit = 300
let startTime = Date.now()
let timeoutId = 0
const isWithinTimeLimit = () => Date.now() - startTime < timeLimit
const handleButtonPress = buttonState => {
if (buttonState !== 'ButtonDown') {
return
}
if (isWithinTimeLimit()) {
clearTimeout(timeoutId)
startTime = 0
doublePressAction()
}
startTime = Date.now()
timeoutId = setTimeout(singlePressAction, timeLimit)
}
const listenForButtonPress = bluetoothAddress => {
new FlicConnectionChannel(bluetoothAddress)
.on('buttonUpOrDown', handleButtonPress)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment