Created
June 4, 2016 01:56
-
-
Save yasulab/663044f6d2012bd1d60cadfb0708ec61 to your computer and use it in GitHub Desktop.
モールス信号 x Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pi_piper' | |
include PiPiper | |
port = 15 | |
pin = PiPiper::Pin.new :pin => port, :direction => :out | |
dot = 0.2 | |
dash = 1.0 | |
CHAR_TIMING = { | |
"a" => [dot, dash], "b" => [dash, dot, dot, dot], "c" => [dash, dot, dash, dot], | |
"d" => [dash, dot, dot], "e" => [dot], "f" => [dot, dot, dash, dot], | |
"g" => [dash, dash, dot], "h" => [dot, dot, dot, dot], "i" => [dot, dot], | |
"j" => [dot, dash, dash, dash], "k" => [dash, dot, dash], "l" => [dot, dash, dot, dot], | |
"m" => [dash, dash], "n" => [dash, dot], "o" => [dash, dash, dash], | |
"p" => [dot, dash, dash, dot], "q" => [dash, dash, dot, dash], "r" => [dot, dash, dot], | |
"s" => [dot, dot, dot], "t" => [dash], "u" => [dot, dot, dash], | |
"v" => [dot, dot, dot, dash], "w" => [dot, dash, dash], "x" => [dash, dot, dot, dash], | |
"y" => [dash, dot, dash, dash], "z" => [dash, dash, dot, dot] | |
} | |
message = "mitou" | |
# "m"=>[dash, dash], "i"=>[dot, dot], "t"=>[dash], | |
# "o"=>[dash, dash, dash], "u" => [dot, dot, dash] | |
inter_element_gap = 1.0 | |
medium_gap = 3.0 | |
short_gap = 0.5 | |
message.each_char do |letter| | |
if letter == " " | |
pin.off | |
sleep medium_gap | |
else | |
CHAR_TIMING[letter].each do |timing| | |
pin.on | |
sleep timing | |
pin.off | |
sleep inter_element_gap | |
end | |
sleep inter_element_gap | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment