Advertisement
Guest User

Untitled

a guest
May 29th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # -*- coding: utf-8 -*-
  3.  
  4. # IR code decoder for NEC format
  5.  
  6. line = ARGF.read
  7.  
  8. ar = line.split(",")
  9.  
  10. while true do
  11. v = ar.slice(0, 2)
  12.  
  13. break if v == nil || v.length != 2
  14.  
  15. v[0] = v[0].to_i
  16. v[1] = v[1].to_i
  17.  
  18. if 800 < v[0]
  19. # Leader (start of new frame)
  20. str = ""
  21.  
  22. elsif 300 < v[1]
  23. # Trailer
  24. puts str if 0 < str.length
  25. if str.length == 32
  26. c0 = "%02X" % str.slice(0, 8).reverse.to_i(2)
  27. c1 = "%02X" % str.slice(8, 8).reverse.to_i(2)
  28. d0 = "%02X" % str.slice(16, 8).reverse.to_i(2)
  29. d1 = "%02X" % str.slice(24, 8).reverse.to_i(2)
  30. puts "#{c0} #{c1} #{d0} "
  31. end
  32.  
  33. str = ""
  34. else
  35. if v[0] * 2 < v[1]
  36. str = str + "1"
  37. else
  38. str = str + "0"
  39. end
  40. end
  41.  
  42. ar = ar.slice(2, ar.length)
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement