Advertisement
jsbsan

morse

Feb 8th, 2014
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GAMBAS 3.00 KB | None | 0 0
  1. ' Gambas class file
  2.  
  3. Private morseTable As Collection
  4.  
  5. Public Sub Form_Open()
  6.  
  7.   Me.center
  8.   Music.Load("beep-01a.wav")
  9.   setupMorse()
  10.  
  11. End
  12.  
  13. Public Sub setupMorse()
  14.  
  15.   morseTable = New Collection
  16.   morseTable.Add(".-", "a")
  17.   morseTable.Add("-...", "b")
  18.   morseTable.Add("-.-.", "c")
  19.   morseTable.Add("-..", "d")
  20.   morseTable.Add(".", "e")
  21.   morseTable.Add("..-.", "f")
  22.   morseTable.Add("--.", "g")
  23.   morseTable.Add("....", "h")
  24.   morseTable.Add("..", "i")
  25.   morseTable.Add(".---", "j")
  26.   morseTable.Add("-.-", "k")
  27.   morseTable.Add(".-..", "l")
  28.   morseTable.Add("--", "m")
  29.   morseTable.Add("-.", "n")
  30.   morseTable.Add("---", "o")
  31.   morseTable.Add(".--.", "p")
  32.   morseTable.Add("--.-", "q")
  33.   morseTable.Add(".-.", "r")
  34.   morseTable.Add("...", "s")
  35.   morseTable.Add("-", "t")
  36.   morseTable.Add("..-", "u")
  37.   morseTable.Add("...-", "v")
  38.   morseTable.Add(".--", "w")
  39.   morseTable.Add("-..-", "x")
  40.   morseTable.Add("-.--", "y")
  41.   morseTable.Add("--..", "z")
  42.   morseTable.Add("-----", "0")
  43.   morseTable.Add(".----", "1")
  44.   morseTable.Add("..---", "2")
  45.   morseTable.Add("...--", "3")
  46.   morseTable.Add("....-", "4")
  47.   morseTable.Add(".....", "5")
  48.   morseTable.Add("-....", "6")
  49.   morseTable.Add("--...", "7")
  50.   morseTable.Add("---..", "8")
  51.   morseTable.Add("----.", "9")
  52.   morseTable.Add(".-.-.-", ".")
  53.   morseTable.Add("--..--", ",")
  54.   morseTable.Add("..--..", "?")
  55.   morseTable.Add(".----.", "'")
  56.   morseTable.Add("-.-.--", "!")
  57.   morseTable.Add("-..-.", "/")
  58.   morseTable.Add("-.--.", "(")
  59.   morseTable.Add("-.--.-", ")")
  60.   morseTable.Add(".-...", "&")
  61.   morseTable.Add("---...", ":")
  62.   morseTable.Add("-.-.-.", ";")
  63.   morseTable.Add("-...-", "=")
  64.   morseTable.Add(".-.-.", "+")
  65.   morseTable.Add("-....-", "-")
  66.   morseTable.Add("..--.-", "_")
  67.   morseTable.Add(".-..-.", "\"")
  68.   morseTable.Add("...-..-", "$")
  69.   morseTable.Add(".--.-.", "@")
  70.  
  71. End
  72.  
  73. Public Sub Button1_Click()
  74.  
  75.   Dim idx As Integer
  76.   Dim aChar As String
  77.  
  78.   For idx = 1 To Len(TextBox1.text)
  79.     aChar = Mid$(TextBox1.text, idx, 1)
  80.     If aChar = " " Then
  81.       Print "Break between words"
  82.       ' Pause between words (perhaps do something to eliminate multiple      spaces?)
  83.       Wait 0.7
  84.       Continue
  85.     Endif
  86.     playChar(aChar)
  87.   Next
  88.  
  89. End Sub
  90.  
  91. Public Sub playChar(aChar As String)
  92.  
  93.   Dim charAsMorse As String
  94.   Dim idx As Integer
  95.  
  96.   charAsMorse = morseTable[Lower$(aChar)]
  97.   Print "Playing: " & charAsMorse
  98.   For idx = 1 To Len(charAsMorse)
  99.     TextLabelLetra.text = "Letra: \"" & achar & "\" código: \"" & charAsMorse & "\""
  100.     If Mid$(charAsMorse, idx, 1) = "." Then
  101.       playDot()
  102.     Else
  103.       playDash()
  104.     Endif
  105.     ' Pause between individual dots/dashes of a single character
  106.     Wait 0.2
  107.   Next
  108.   ' Pause between characters of a word
  109.   Wait 0.5
  110.  
  111. End
  112.  
  113. Public Sub playDot()
  114.  
  115.   playTone(0.1)
  116.  
  117. End
  118.  
  119. Public Sub playDash()
  120.  
  121.   playTone(0.4)
  122.  
  123. End
  124.  
  125. Public Sub playTone(duration As Float)
  126.  
  127.   Music.Play()
  128.   Wait duration
  129.   Music.Stop()
  130.  
  131. End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement