Skip to content

Instantly share code, notes, and snippets.

@disolovyov
Created September 8, 2010 16:30
Show Gist options
  • Save disolovyov/570376 to your computer and use it in GitHub Desktop.
Save disolovyov/570376 to your computer and use it in GitHub Desktop.
Good for making your networks homework, if you're a TTI student. ;)
require 'rmagick'
module LineCode
class Turtle
# Creates a signal turtle with binary +data+ on a given +level+.
# Byte labels are aligned to +byte_width+ peaks.
def initialize(data, level, byte_width = 1)
@data = data.dup
@x, @y = 0, level == 1 ? 0 : 20
@level = level == 1 ? 1 : 0
@line_width = 0
@drop_y = 0
@peaks = 0
@byte_width = byte_width
@gc = Magick::Draw.new
@gc.text_align(Magick::CenterAlign)
end
# Draws a signal peak.
def draw(level)
if @level != level
@level = level
y = 20 - @y
@gc.line(@x, @y + @drop_y, @x, y + @drop_y)
@y = y
end
@gc.line(@x, @y + @drop_y, x = @x + 10, @y + @drop_y)
if @peaks % @byte_width == 0
@gc.text(@byte_width * 5 + @x, 40 + @drop_y, @data[0] || ' ')
@data.slice!(0)
end
@peaks += 1
@x = x
if (@line_width += 1) == 52
@x = 0
@drop_y += 60
@line_width = 0
end
end
# Writes current canvas to file.
def write(name)
imgl = Magick::ImageList.new
fill = Magick::HatchFill.new('white', 'LightCyan2', @byte_width * 10)
imgl.new_image(@drop_y > 0 ? 520 : @x, @drop_y + 40, fill)
@gc.draw(imgl)
imgl.write(outfile = File.join(Dir.pwd, "#{name}.png"))
File.size(outfile)
end
# Draws a signal of a given binary string +raw+ to file +name+.png.
# Binary +data+ is used for byte labels.
# Byte labels are aligned to +byte_width+ peaks.
def self.crawl(raw, data, name, byte_width = 1)
turtle = self.new(data, raw[0].to_i, byte_width)
raw.each_char {|c| turtle.draw(c.to_i) }
turtle.write(name)
end
end # Turtle
class Encoder
# Returns a raw binary string (unencoded).
attr_reader :raw
# Creates new line code encoder from string +word+.
def initialize(word)
@raw = ''
word.each_char {|c| @raw << c.ord.to_s(2).rjust(8, '0') }
end
# Returns a non-return-to-zero, inverted binary string.
def nrzi
nrzi = ''
level = 1
@raw.each_char do |c|
level = 1 - level if c == '1'
nrzi << level.to_s
end
nrzi
end
# Returns a binary string in manchester encoding.
def manchester
mc = ''
@raw.each_char {|c| mc << (1 - c.to_i).to_s << c }
mc
end
# Returns a binary string in differential manchester encoding.
def diff_manchester
dmc = ''
level = 0
@raw.each_char do |c|
level = 1 - level if c == '1'
dmc << level.to_s << (1 - level).to_s
end
dmc
end
end # Encoder
end # LineCode
if __FILE__ == $0
begin print 'Encode whaaat? ' end while gets.chomp.empty?
lc = LineCode::Encoder.new($_.chomp)
File.open(outfile = File.join(Dir.pwd, 'out.txt'), 'w') do |f|
f.puts "raw: #{@raw = lc.raw}"
f.puts "nrzi: #{@nrzi = lc.nrzi}"
f.puts "manchester: #{@manchester = lc.manchester}"
f.puts "diff manchester: #{@diff_manchester = lc.diff_manchester}"
end
bytes = File.size(outfile)
bytes += LineCode::Turtle.crawl(@raw, @raw, 'raw')
bytes += LineCode::Turtle.crawl(@nrzi, @raw, 'nrzi')
bytes += LineCode::Turtle.crawl(@manchester, @raw, 'manchester', 2)
bytes += LineCode::Turtle.crawl(@diff_manchester, @raw, 'diff_manchester', 2)
puts "#{bytes} byte(s) written!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment