Sha256: 590e95b3a7b2bdd5234d7e803d4b135538a15d1d6584ed4459369875be9246be

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

require 'bundler/setup'
require 'wall_e'

class MorseCode

  LETTERS = {
    "a" => ".-",
    "b" => "-...",
    "c" => "-.-.",
    "d" => "-..",
    "e" => ".",
    "f" => "..-.",
    "g" => "--.",
    "h" => "....",
    "i" => "..",
    "j" => ".---",
    "k" => "-.-",
    "l" => ".-..",
    "m" => "--",
    "o" => "---",
    "p" => ".--.",
    "q" => "--.-",
    "r" => ".-.",
    "s" => "...",
    "t" => "-",
    "u" => "..-",
    "v" => "...-",
    "w" => ".--",
    "x" => "-..-",
    "y" => "-.--",
    "z" => "--.."
  }

  def initialize(piezo)
    @piezo = piezo
    @dit_length = 0.15
    @dah_length = @dit_length * 3
    @space_length = @dit_length * 7
    @tone = 1275
  end

  def dit
    puts '.'
    output(@dit_length)
  end

  def dah
    puts '-'
    output(@dah_length)
  end

  def output(duration)
    @piezo.on(@tone)
    sleep(duration)
    @piezo.off
    sleep(@dit_length)
  end

  def say(string)
    string.each_char do |letter|
      pattern = LETTERS[letter.downcase]
      if pattern.nil?
        sleep @space_length
      else
        pattern.each_char do |p|
          case p
          when '.' then dit
          when '-' then dah
          end
        end
        sleep @dah_length
      end
    end
  end
end

WallE::Assembler.build do
  piezo = Piezo(6)

  mc = MorseCode.new(piezo)

  mc.say('Hello World')
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wall_e-0.1.0 cookbook/Piezo/morse_code.rb
wall_e-0.0.4 cookbook/Piezo/morse_code.rb
wall_e-0.0.3 cookbook/Piezo/morse_code.rb
wall_e-0.0.2 cookbook/Piezo/morse_code.rb