Ruby LCD Display

Ruby LCD Display

README

The LCD-Display its a common code contest from ACM Chanllenges, basically the code should display a Number (input) in LCD-Display format (like digital clocks). I decided to develop this script in Ruby (because is easily) and after test the Input/Output of the script with a simple UNIX Date command output.

A friend of yours has just bought a new computer. Before this, the most powerful machine he ever used was a pocket calculator. He is a little disappointed because he liked the LCD display of his calculator more than the screen on his new computer! To make him happy, write a program that prints numbers in LCD display style.

The input file contains several lines, one for each number to be displayed. Each line contains integers s and n, where n is the number to be displayed (0 <= n <= 99, 999, 999) and s is the size in which it shall be displayed (1 <= s <= 10). The input will be terminated by a line containing two zeros, which should not be processed.

Print the numbers specified in the input file in an LCD display-style using s “-” signs for the horizontal segments and s “|” signs for the vertical ones. Each digit occupies exactly s + 2 columns and 2s + 3 rows. Be sure to fill all the white space occupied by the digits with blanks, including the last digit. There must be exactly one column of blanks between two digits.

*** Example:
[+] Input:
1234567890

[+] Output:
      --  --        --   --  --   --   --   --
   |    |   | |  | |    |      | |  | |  | |  |
   |    |   | |  | |    |      | |  | |  | |  |
      --  --   --   --   --       --   --
   | |      |    |    | |  |   | |  |    | |  |
   | |      |    |    | |  |   | |  |    | |  |
      --  --        --   --       --   --   --
eight = [
    [0, 1, 1, 0], #  --
    [2, 0, 0, 2], # |  |
    [2, 0, 0, 2], # |  |
    [0, 1, 1, 0], #  --
    [2, 0, 0, 2], # |  |
    [2, 0, 0, 2], # |  |
    [0, 1, 1, 0]  #  --
]

I stupidly wrote a dictionary file, but maybe you can made this step with a better solution.

$ echo "IC0tIAp8ICB8CnwgIHwKICAgIAp8ICB8CnwgIHwKIC0tIAogICAgCiAgIHwKICAgfAogICAgCiAgIHwKICAgfAogICAgCiAtLSAKICAgfAogICB8CiAtLSAKfCAgIAp8ICAgCiAtLSAKIC0tIAogICB8CiAgIHwKIC0tIAogICB8CiAgIHwKIC0tIAogICAgCnwgIHwKfCAgfAogLS0gCiAgIHwKICAgfAogICAgCiAtLSAKfCAgIAp8ICAgCiAtLSAKICAgfAogICB8CiAtLSAKIC0tIAp8ICAgCnwgICAKIC0tIAp8ICB8CnwgIHwKIC0tIAogLS0gCiAgIHwKICAgfAogICAgCiAgIHwKICAgfAogICAgCiAtLSAKfCAgfAp8ICB8CiAtLSAKfCAgfAp8ICB8CiAtLSAKIC0tIAp8ICB8CnwgIHwKIC0tIAogICB8CiAgIHwKIC0tIAoK" | base64 -d &gt; RubyLCDDisplay.dict

Code

#!/usr/bin/env ruby
def get_dictionary()
  numbers = Array.new
  counter = 1
  number = 0
  fileresource = File.new('RubyLCDDisplay.dict', 'r')
  fileresource.each do |linetext|
    chars = linetext.gsub("\n", '').split('')
    if !chars.empty?
      numbers[number] = Array.new if counter==1
      chars2number = Array.new
      chars.each do |char|
        chars2number.push(0) if char==' '
        chars2number.push(1) if char=='-'
        chars2number.push(2) if char=='|'
      end
      numbers[number].push(chars2number)
      number = number+1 if counter==7
      counter = counter==7 ? 1 : counter+1
    end
  end
  return numbers
end
def puts_lcd_number(number)
  number_a = number.to_s.split('')
  dictionary = get_dictionary()
  0.upto(6) do |index|
    number_a.each do |number|
      dictionary[number.to_i][index].each do |number_char|
        print ' ' if number_char==0
        print '-' if number_char==1
        print '|' if number_char==2
      end
      print ' ' # Number per number separator
    end
    puts '' # New line separator
  end
end
puts_lcd_number( !ARGV[0].nil? ? ARGV[0] : 1234567890 )

Tips and Tricks

This isn’t really a usefull program, but I could use it to display a dynamic digital clock in my terminal:

$ for i in {1..60}; do
    clear;
    ruby ./RubyLCDDisplay.rb $(date +'%H%M%S');
    sleep 1;
  done
Do you have a project idea? Let's make it together!