-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnerf-target.rb
executable file
·49 lines (40 loc) · 1.44 KB
/
nerf-target.rb
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
#!/usr/bin/env ruby
require 'bundler'
require 'logger'
Bundler.require
# The seconds after an initial hit, for which we ignore subsequent hits
# (to avoid a barrage of hits skipping through several tracks quickly).
REARM_DELAY = 10 # seconds.
# Usage: ruby nerf-target.rb /dev/tty.usbserial-A6008k35
fail 'Call this program passing the USB/Serial device as an argument.' unless ARGV.first
port_str = ARGV.first
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
logger = Logger.new STDOUT
logger.info "Trying #{port_str}..."
ignore_hits_until = Time.now.utc
SerialPort.open(port_str, baud_rate, data_bits, stop_bits, parity) do |sp|
logger.info "Listening on #{port_str}."
while true do
while (cmd = sp.gets) do
cmd.chomp!
logger.debug "Received: #{cmd}"
if cmd.start_with? 'NP'
_, timestamp, force = cmd.split ',', 3
logger.info "Hit at #{timestamp} with force #{force}."
if Time.now > ignore_hits_until
ignore_hits_until = Time.now.utc + REARM_DELAY
# This skips Spotify on OSX
# You could substitute your own commands/make a HTTP call/put a message on a queue, etc.
`osascript -e 'tell application "Spotify" to next track'`
logger.info "Skipping Spotify."
logger.debug "Ignoring hits until #{ignore_hits_until.strftime '%T %Z'}."
else
logger.debug "Ignoring this hit."
end
end
end
end
end