{"size": 1271, "ext": "js", "max_stars_repo_path": "server/controllers/hardware.js", "max_stars_repo_name": "chevalvert/lpa-datafossile", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/controllers/hardware.js", "max_issues_repo_name": "chevalvert/lpa-datafossile", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/controllers/hardware.js", "max_forks_repo_name": "chevalvert/lpa-datafossile", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.9137931034, "max_line_length": 81, "alphanum_fraction": 0.6247049567}
const configuration = require('@configuration')
const Serialed = require('serialed')

const ctrlr = new Serialed.SerialedController()
ctrlr.addNode(
  configuration['hardware'].address,
  150
)

const observers = []
let now = Date.now()
let last = now
let frameCount = 0
ctrlr.on('next', () => {
  now = Date.now()
  frameCount++
  for (let i = 0; i < observers.length; i++) {
    observers[i](now - last, frameCount)
  }
  last = now
})

process.on('SIGINT', kill)
process.on('SIGTERM', kill)
process.on('uncaughtException', err => {
  console.error(err)
  kill()
})

function kill () {
  ctrlr.close()
  process.exit(1)
}

module.exports = {
  kill,
  clear: ctrlr.clear.bind(ctrlr),
  segment: (sliceIndex, color = configuration['hardware'].white) => {
    const segmentsLength = configuration['hardware'].segmentsLength
    const segmentLength = configuration['hardware'].pixelsLength / segmentsLength

    color = color.map(c => Math.floor(c))

    for (let i = 0; i < segmentsLength; i++) {
      const x = i % 2
        ? (segmentLength * (i + 1) - 1) - sliceIndex
        : sliceIndex + (segmentLength) * i
      ctrlr.led(x, ...color)
    }
  },

  raf: fn => {
    if (typeof fn === 'function' && !~observers.indexOf(fn)) {
      observers.push(fn)
    }
  }
}
