-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
154 lines (139 loc) · 4.36 KB
/
index.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const five = require('johnny-five')
const chalk = require('chalk')
const green = chalk.green
const blue = chalk.cyan
const debug = require('debug')('catbot:board')
const utils = require('./lib/utils')
const applyDeadzone = utils.applyDeadzone
const rng = utils.convertRange
const getConf = require('./lib/config')
function makeCat (catCb, opts) {
console.log(green(msg))
if (!catCb) {
catCb = function () {
debug('no cb provided')
}
}
if (!opts) opts = {}
if (!opts.boardOpt) opts.boardOpt = {repl: false}
if (!opts.catConf) {
opts.catConf = getConf()
} else {
opts.catConf = getConf(opts.catConf)
}
if (!opts.inputRange) {
debug('inputRange missing')
if (opts.catConf.inputRange) {
opts.inputRange = [opts.catConf.inputRange[0], opts.catConf.inputRange[1]]
} else {
opts.inputRange = [0, 180]
}
}
if (!opts.servoRange) {
debug('servorange missing')
if (opts.catConf.servoRange) {
opts.servoRange = [opts.catConf.servoRange[0], opts.catConf.servoRange[1]]
} else {
opts.servoRange = [0, 180]
}
}
debug('input', opts.inputRange, 'servo', opts.servoRange)
// debug('opts')
// debug(opts.catConf.hw)
const hw = opts.catConf.hw
const cat = {hw: {}}
cat.board = new five.Board(opts.boardOpt)
cat.board.on('ready', function () {
debug('board is ready')
console.log(blue('catbot is ready to use'))
const laser = new five.Led(hw.laser.pin)
const servoX = new five.Servo({pin: hw.servoX.pin, startAt: 90, range: opts.servoRange})
const servoY = new five.Servo({pin: hw.servoY.pin, startAt: 90, range: opts.servoRange})
// if analog joystick is enabled
if (opts.hwJoystick || opts.catConf.hwJstk) {
let joyTimer
let isJoyOn = false
debug('init hwJoystick')
console.log(green('Joystick configured'))
console.log(blue('Press the joystick button for 2 sec to enable/disable it, quick press to toggle laser'))
const hwJoy = new five.Joystick({
pins: [hw.jstk.x, hw.jstk.y]
})
const button = new five.Button({
pin: 9,
isPullup: hw.jstk.isPullup
})
hwJoy.on('change', function () {
if (isJoyOn === true) {
const X = applyDeadzone(this.x, hw.jstk.deadZone)
const Y = applyDeadzone(this.y, hw.jstk.deadZone)
debug(X, Y)
if (X !== 0 || Y !== 0) {
servoX.to(rng(this.x, [1, -1], opts.servoRange))
servoY.to(rng(this.y, [1, -1], opts.servoRange))
} else {
servoX.to(rng(0, [1, -1], [10, 170]))
servoY.to(rng(0, [1, -1], [10, 170]))
}
}
})
button.on('up', function () {
clearTimeout(joyTimer)
laser.toggle()
})
button.on('down', function () {
console.log(blue('hold 2 sec to toggle joymode'))
joyTimer = setTimeout(function () {
isJoyOn = !isJoyOn
console.log(blue('joyMode:'), isJoyOn)
}, 2000)
})
// export a ref to the hardware to the cat object
cat.hwJoy = hwJoy
}
/**
* orient the turret to the passed positions
*
* @param {Array} pos array of 2 value pos X and Y {numbers}
* @param {Array} [servos] optional array of j5 servos obj
*/
cat.to = function (pos, servos) {
debug('cat.to called')
if (!servos) servos = [servoX, servoY]
servos[0].to(rng(pos[0], opts.inputRange, opts.servoRange))
servos[1].to(rng(pos[1], opts.inputRange, opts.servoRange))
}
// enable inject hardware to REPL if REPL is enabled
if (opts.boardOpt.repl === true) {
console.log(green('REPL mode enabled'))
cat.board.repl.inject({
x: servoX,
y: servoY,
laser: laser,
to: cat.to
})
}
// export a ref to the hardware to the cat object
cat.laser = laser
cat.x = servoX
cat.y = servoY
return catCb(null, cat)
})
}
const msg = `
\`*-.
) _\`-.
. : \`. .
: _ ' \\
; *\` _. \`*-._
\`-.-' \`-.
; \` \`.
:. . \\
. \\ . : .-' .
' \`+.; ; ' :
: ' | ; ;-.
; ' : :\`-: _.\`*
.*' / .*' ; .*\`- +' \`*'
. \`*-* \`*-* \`*-*'\`)
`
module.exports = makeCat