/
morse_code.rb
83 lines (73 loc) · 1.32 KB
/
morse_code.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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