Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Parse mode2 output as NEC(Kadenkyo) format
#!/usr/bin/env ruby
prev = nil
signal = []
ARGF.each{ |line|
line.chomp!
next if line.empty?
(command, time) = line.split
command = command.to_sym
length = (time.to_i/ 425.0).round
#p prev
if prev && prev[:command] == :pulse && command == :space
if prev[:length] <= 1 && length == 1
signal << 0
elsif prev[:length] <= 1 && length == 3
signal << 1
elsif prev[:length] == 8 && length == 4
signal << :leader
elsif prev[:length] == 1 && length > 50
signal << :stop
else
signal << [prev[:length], length]
end
end
prev = {:command => command, :length=> length}
}
count = 0
str = ""
signal.each{ |sig|
if sig == :leader || sig == :stop
puts
puts sig
str = ""
count = 0
else
print sig
count += 1
str += sig.to_s
if count % 8 == 0
puts " "+str.to_i(2).to_s(16)
str = ""
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment