-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.js
182 lines (154 loc) · 4.11 KB
/
board.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const Raspi = require('raspi-io')
const five = require('johnny-five')
const spawn = require('child_process').spawn
const exec = require('child_process').exec
const gm = require('gm')
const servoMin = -30
const servoMax = 180
const baseDir = '/var/www/camera/'
const imageDir = `${baseDir}snapshots/`
const snapshotPath = `${imageDir}snapshot.png`
const snapshotCmd = 'raspistill'
const snapshotArgs = ['-t', 300, '-tl', 0, '-ex', 'sports', '-awb', 'flash', '-vf', '-hf', '-o', snapshotPath, '-w', 1640, '-h', 1232, '-q', 75]
module.exports = class Board {
constructor() {
console.log('board init')
this.busy = false
this.onSnapshot = this.onSnapshot.bind(this)
this.onSaveImage = this.onSaveImage.bind(this)
this.initIo = this.initIo.bind(this)
this.initIoEvents = this.initIoEvents.bind(this)
this.init = this.init.bind(this)
this.stopRunningProcess = this.stopRunningProcess.bind(this)
this.resetServo = this.resetServo.bind(this)
this.acceptCap = this.acceptCap.bind(this)
this.rejectCap = this.rejectCap.bind(this)
this.reset = this.reset.bind(this)
}
resetServo() {
setTimeout(this.reset, 500)
}
acceptCap() {
this.servo.to(servoMin)
this.resetServo()
}
rejectCap() {
this.servo.to(servoMax)
this.resetServo()
}
reset() {
this.servo.center()
}
onSnapshot() {
this.light.off()
console.log(`image saved at ${snapshotPath}`)
gm(snapshotPath)
.crop(520, 520, 525, 565) // Crop from bottom right due to flipped snapshot (both h/v)
.modulate(110, 150) // brightness/saturation/hue
.contrast(-5)
.noProfile()
.write(`${snapshotPath}`, this.onSaveImage)
}
onSaveImage(err) {
if (err) { return }
console.log(`image cropped`)
const mask = `${baseDir}bottlecap-mask.png`
var _this = this
this.gmCompositeMask(snapshotPath, mask, () => {
console.log('image masked')
_this.busy = false
})
}
takePhoto() {
if (this.busy) { return }
this.busy = true
this.light.on()
this.process = spawn(snapshotCmd, snapshotArgs)
this.process.on('exit', this.onSnapshot)
}
gmCompositeMask(thumb, mask, next) {
var gmComposite = 'gm composite -compose in ' + thumb + ' ' + mask + ' ' + thumb
exec(gmComposite, (err) => {
if (err) throw err
next()
})
}
initIoEvents() {
this.button.on('up', () => {
this.light.off()
console.log('button up')
})
this.button.on('down', () => {
this.light.on()
console.log('button down')
})
var toggle = false
this.button2.on('up', () => {
if (toggle) {
this.servo.to(servoMin)
console.log(`Servo to ${servoMin}`)
} else {
this.servo.to(servoMax)
console.log(`Servo to ${servoMax}`)
}
toggle = !toggle
console.log('button2 up')
})
this.button2.on('down', () => {
console.log('button2', toggle)
})
this.button3.on('down', this.stopRunningProcess)
var _this = this
this.button3.on('up', () => {
console.log(`button 3 up, taking photo using command ${snapshotCmd} ${snapshotArgs.join(' ')}`)
_this.takePhoto()
})
}
initIo(cb) {
// Create servo on pin 1 (GPIO18) on P1
this.servo = new five.Servo({
pin: 1,
range: [servoMin, servoMax],
center: true
})
this.light = new five.Relay({
pin: 22
})
this.button = new five.Button({
pin: 27,
isPullup: true
})
this.button2 = new five.Button({
pin: 25,
isPullup: true
})
this.button3 = new five.Button({
pin: 4,
isPullup: true
})
this.servo.center()
this.initIoEvents()
console.log('board ready')
// Start server here
cb.call()
}
init(cb=()=>{}) {
console.log('init...')
this.board = new five.Board({
io: new Raspi(),
repl: false
})
var _this = this
this.board.on('ready', () => {
_this.initIo(cb)
})
}
stopRunningProcess() {
// Stop running tasks
if (this.process) {
this.process.stdin.pause()
this.process.kill()
this.process = undefined
}
}
}