Skip to content

Commit

Permalink
fix(Gpio): Reset timer on every change
Browse files Browse the repository at this point in the history
  • Loading branch information
ovaar committed Dec 30, 2019
1 parent 9c3ee49 commit 3f46355
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 7 additions & 10 deletions src/gpio-proximity.ts
Expand Up @@ -13,28 +13,25 @@ export default class SignalProximity extends EventEmitter {
this.timer.elapsed = function() {
console.log('timer elapsed')
}
this._onGpioWatch(null, this.gpio.readSync())
this.gpio.watch(this._onGpioWatch.bind(this))
}

private _onGpioWatch(err: Error | null | undefined, value: BinaryValue) {
try {
if (err) {
console.log(err)
throw err
}
console.log(`Watch = ${value}`)

this.timer.restart()
} catch (error) {
console.error(error)
if (err) {
console.error(err)
return
}

this.timer.restart()
}

public start() {
this.timer.start()
}

public stop() {
this.timer.stop()
this.gpio.unexport()
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -10,9 +10,9 @@ export namespace Proximity {

bluetooth.start()

const gpio_4 = new Gpio(4, 'in', 'rising')
const gpio4 = new Gpio(4, 'in', 'both')

const signalProximity = new SignalProximity(gpio_4)
const signalProximity = new SignalProximity(gpio4)
signalProximity.start()

process.on('SIGINT', () => {
Expand Down

0 comments on commit 3f46355

Please sign in to comment.