<commit_before>const { Gpio } = require('onoff')
const io = require('socket.io-client')
const config = require('./config')

const socket = io(config.api)

function onPush (err, value, self, press, hold) {
  if (err) return console.error(err.stack || err)

  if (value) self.on = Date.now()
  else self.off = Date.now()

  // Is released after being pushed.
  if (!value && self.last && !self.disabled) {
    self.disabled = true

    const holdTime = self.off - self.on

    // If released 1700 milliseconds after pushed, button was held.
    if (holdTime > 1700) {
      hold && hold()
    } else {
      press()
    }

    // Only accept button pushes every 100 milliseconds.
    setTimeout(() => {
      self.disabled = false
    }, 100)
  }

  self.last = value
}

function button (gpio, press, hold) {
  const self = { on: 0, off: 0, last: 0, disabled: false }

  new Gpio(gpio, 'in', 'both')
    .watch((err, value) => {
      onPush(err, value, self, press, hold)
    })
}

const emit = event => () => socket.emit(event)

button(config.playerOne.green, emit('increment-player-one'))
button(config.playerOne.red, emit('decrement-player-one'), emit('end-game'))
button(config.playerTwo.green, emit('increment-player-two'))
button(config.playerTwo.red, emit('decrement-player-two'), emit('end-game'))<commit_msg>Add GPIO logging<commit_after>const { Gpio } = require('onoff')
const io = require('socket.io-client')
const config = require('./config')

const socket = io(config.api)

const gpioReverse = {
  [config.playerOne.green]: 'player one green',
  [config.playerOne.red]: 'player one red',
  [config.playerTwo.green]: 'player two green',
  [config.playerTwo.red]: 'player two red',
}

function onPush (err, value, self, press, hold) {
  console.log(self.name, value)

  if (err) return console.error(self.name, err.stack || err)

  if (value) self.on = Date.now()
  else self.off = Date.now()

  // Is released after being pushed.
  if (!value && self.last && !self.disabled) {
    self.disabled = true

    const holdTime = self.off - self.on

    // If released 1700 milliseconds after pushed, button was held.
    if (holdTime > 1700) {
      console.log(self.name, 'hold')
      hold && hold()
    } else {
      console.log(self.name, 'press')
      press()
    }

    // Only accept button pushes every 100 milliseconds.
    setTimeout(() => {
      self.disabled = false
    }, 100)
  }

  self.last = value
}

function button (gpio, press, hold) {
  const self = { name: gpioReverse[gpio], on: 0, off: 0, last: 0, disabled: false }

  new Gpio(gpio, 'in', 'both')
    .watch((err, value) => {
      onPush(err, value, self, press, hold)
    })
}

const emit = event => () => socket.emit(event)

button(config.playerOne.green, emit('increment-player-one'))
button(config.playerOne.red, emit('decrement-player-one'), emit('end-game'))
button(config.playerTwo.green, emit('increment-player-two'))
button(config.playerTwo.red, emit('decrement-player-two'), emit('end-game'))
{'fraction_non_alphanumeric': 0.1646586345381526, 'fraction_numerical': 0.011378848728246318, 'mean_word_length': 3.3955882352941176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5343817', 'n_tokens_mistral': 1022, 'n_tokens_neox': 974, 'n_words': 258}