-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtapper.js
More file actions
63 lines (51 loc) · 1.3 KB
/
tapper.js
File metadata and controls
63 lines (51 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
let five = require('johnny-five')
class Tapper {
constructor(dirPin, solenoidPin) {
// CNC Shield - 4th Axis (A) direction pin
this.dir = new five.Pin({pin: dirPin, type: 'digital', mode: 1})
this.dir.high()
// CNC Shield - 4th Axis (A) "step" pin
this.solenoid = new five.Pin({pin: solenoidPin, type: 'digital', mode: 1})
this.tapDelay = 45
this.intervalDelay = 90
this.ringDelay = 2000
this._interval = null
this._started = false
}
toggle() {
this.solenoid.low()
this.solenoid.high()
this.solenoid.low()
this.solenoid.high()
}
tap() {
this.toggle()
setTimeout(
function (_this) {
_this.toggle()
},
this.tapDelay,
this
)
}
start() {
if (this._started === false) {
this._interval = setInterval(this.tap.bind(this), this.intervalDelay)
this._started = true
}
}
stop() {
clearInterval(this._interval)
this._started = false
}
ring(times = 1) {
let delay = this.ringDelay
if ( Number.isInteger(times) && (times > 0) ) {
for (let i = 0; i < times; i++ ) {
setTimeout(function (_this){ _this.start() }, 0 + (i * (delay*2)), this)
setTimeout(function (_this){ _this.stop() }, delay + (i * (delay*2)), this)
}
}
}
}
module.exports = Tapper