December 04, 2020, 10:22:25 AM

Author Topic: [bb] Morse Code Converter by superStruct [ 1+ years ago ]  (Read 753 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Morse Code Converter by superStruct [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Morse Code Converter
Author : superStruct
Posted : 1+ years ago

Description : This program takes the text that you type in and writes onto the screen in Morse code. It also used the systembeep DLL to create the audible dots and dashs of Morse code.

EDIT: Thanks to everybody who commented. Some of those were put into this edit. Keep them coming.


Code :
Code: BlitzBasic
  1. Graphics 1200,400,0,2
  2. SetBuffer BackBuffer()
  3.  
  4. Global PriMsg$
  5. Global SndMsg$
  6. Global command$
  7. Global TKey%
  8. Global MorsMsg$
  9.  
  10. Const dot = 50
  11. Const dash = 150
  12. Const freq = 500
  13.  
  14. Dim textline$(100)
  15.  
  16. Print ""
  17.  
  18. While Not KeyDown(1)
  19.         Cls
  20.         Check_Keys()
  21.         Text 0,0,">:" + PriMsg$
  22.         Text 0,24,MorsMsg
  23.         Flip
  24. Wend
  25.  
  26. Function Check_Keys()
  27.         ;Grab any keys that get pressed
  28.         TKey% = GetKey()
  29.        
  30.         If TKey <> 0 Then
  31.                 If Tkey <> 8 Then ;8 = backspace key
  32.                         ;convert pressed key To the actual character
  33.                         TChr$ = Chr$(TKey)
  34.                         MorsMsg = ""
  35.                         ;append String with Last key pressed
  36.                         PriMsg = PriMsg + TChr
  37.                 EndIf
  38.         EndIf
  39.        
  40.         If KeyDown(14)
  41.                 If Len(PriMsg) > 0 Then PriMsg = Left(PriMsg,Len(PriMsg)-1)
  42.                 Delay(100)
  43.         EndIf
  44.        
  45.         If KeyDown(200) Or KeyDown(208)
  46.                 PriMsg = PriMsg + ""
  47.         EndIf
  48.        
  49.         ;hitting enter key will send contents of sndmsg to client/server
  50.         If KeyHit(28) Then ;Enter key
  51.                 SndMsg = PriMsg
  52.                 MorseConvert()
  53.                 MorseSound()
  54.                 PriMsg = ""
  55.         EndIf
  56.        
  57. End Function
  58.  
  59. Function MorseConvert()
  60.         For i = 1 To Len(PriMsg)
  61.                 temp$ = Right(Left(PriMsg,i),1)
  62.                 If temp = "a" Or temp = "A"
  63.                         MorsMsg = MorsMsg + ".- "
  64.                 ElseIf temp = "b" Or temp = "B"
  65.                         MorsMsg = MorsMsg + "-... "
  66.                 ElseIf temp = "c" Or temp = "C"
  67.                         MorsMsg = MorsMsg + "-.-. "
  68.                 ElseIf temp = "d" Or temp = "D"
  69.                         MorsMsg = MorsMsg + "-.. "
  70.                 ElseIf temp = "e" Or temp = "E"
  71.                         MorsMsg = MorsMsg + ". "
  72.                 ElseIf temp = "f" Or temp = "F"
  73.                         MorsMsg = MorsMsg + "..-. "
  74.                 ElseIf temp = "g" Or temp = "G"
  75.                         MorsMsg = MorsMsg + "--. "
  76.                 ElseIf temp = "h" Or temp = "H"
  77.                         MorsMsg = MorsMsg + ".... "
  78.                 ElseIf temp = "i" Or temp = "I"
  79.                         MorsMsg = MorsMsg + ".. "
  80.                 ElseIf temp = "j" Or temp = "J"
  81.                         MorsMsg = MorsMsg + ".--- "
  82.                 ElseIf temp = "k" Or temp = "K"
  83.                         MorsMsg = MorsMsg + "-.- "
  84.                 ElseIf temp = "l" Or temp = "L"
  85.                         MorsMsg = MorsMsg + ".-.. "
  86.                 ElseIf temp = "m" Or temp = "M"
  87.                         MorsMsg = MorsMsg + "-- "
  88.                 ElseIf temp = "n" Or temp = "N"
  89.                         MorsMsg = MorsMsg + "-. "
  90.                 ElseIf temp = "o" Or temp = "O"
  91.                         MorsMsg = MorsMsg + "--- "
  92.                 ElseIf temp = "p" Or temp = "P"
  93.                         MorsMsg = MorsMsg + ".--. "
  94.                 ElseIf temp = "q" Or temp = "Q"
  95.                         MorsMsg = MorsMsg + "--.- "
  96.                 ElseIf temp = "r" Or temp = "R"
  97.                         MorsMsg = MorsMsg + ".-. "
  98.                 ElseIf temp = "s" Or temp = "S"
  99.                         MorsMsg = MorsMsg + "... "
  100.                 ElseIf temp = "t" Or temp = "T"
  101.                         MorsMsg = MorsMsg + "- "
  102.                 ElseIf temp = "u" Or temp = "U"
  103.                         MorsMsg = MorsMsg + "..- "
  104.                 ElseIf temp = "v" Or temp = "V"
  105.                         MorsMsg = MorsMsg + ".-- "
  106.                 ElseIf temp = "w" Or temp = "W"
  107.                         MorsMsg = MorsMsg + ".--.. "
  108.                 ElseIf temp = "x" Or temp = "X"
  109.                         MorsMsg = MorsMsg + "-..- "
  110.                 ElseIf temp = "y" Or temp = "Y"
  111.                         MorsMsg = MorsMsg + "-.-- "
  112.                 ElseIf temp = "z" Or temp = "Z"
  113.                         MorsMsg = MorsMsg + "--.. "
  114.                 ElseIf temp = " "
  115.                         MorsMsg = MorsMsg + "| "
  116.                 ElseIf temp = "1"
  117.                         MorsMsg = MorsMsg + ".---- "
  118.                 ElseIf temp = "2"
  119.                         MorsMsg = MorsMsg + "..--- "
  120.                 ElseIf temp = "3"
  121.                         MorsMsg = MorsMsg + "...-- "
  122.                 ElseIf temp = "4"
  123.                         MorsMsg = MorsMsg + "....- "
  124.                 ElseIf temp = "5"
  125.                         MorsMsg = MorsMsg + "..... "
  126.                 ElseIf temp = "6"
  127.                         MorsMsg = MorsMsg + "-.... "
  128.                 ElseIf temp = "7"
  129.                         MorsMsg = MorsMsg + "--... "
  130.                 ElseIf temp = "8"
  131.                         MorsMsg = MorsMsg + "---.. "
  132.                 ElseIf temp = "9"
  133.                         MorsMsg = MorsMsg + "----. "
  134.                 ElseIf temp = "0"
  135.                         MorsMsg = MorsMsg + "----- "
  136.                 EndIf          
  137.         Next
  138. End Function
  139.  
  140. Function MorseSound()
  141.   For i = 0 To Len(MorsMsg)
  142.         temp$ = Right(Left(MorsMsg,i),1)
  143.         If temp = "-"
  144.                 SystemBeep(freq, dash)
  145.         ElseIf temp = "."
  146.                 SystemBeep(freq, dot)
  147.         ElseIf temp = " "
  148.                 Delay(50)
  149.         EndIf
  150.         Delay(50)
  151.   next
  152. End Function


Comments :


gosse(Posted 1+ years ago)

 Instead of having a giant If statement, you could just parse the string you just created.


Jim Teeuwen(Posted 1+ years ago)

 Nice little toy :)  I agree with Gosse though. parsing the string to generate the sounds would save a ton of code.pseudo code:
Code: [Select]
Function MorseSound(code$)
  For every character in code
if character is "-" then
SystemBeep(freq, dash)
else
SystemBeep(freq, dot)
end if
Delay(50)
  next
end Function



xlsior(Posted 1+ years ago)

 You can also save a bunch of checks by changing this line:
Code: [Select]
temp$ = Right(Left(PreMsg,i),1)to say this instead:
Code: [Select]
temp$ = Upper(Right(Left(PreMsg,i),1))That way everything will always be in uppercase, and you don't have to perform two checks for each character to account for both upper and lowercase possibilities.So instead of this:
Code: [Select]
if temp = "a" or temp = "A"you can do this:
Code: [Select]
if temp = "A"


Shortwind(Posted 1+ years ago)

 Just a couple questions and possibly a idea or two to shorten, and optimize your routines a bit.First, why are you using:  temp$ = Right(Left(PreMsg,i),1)Will not this do the same thing, and make more sense:  temp$ = Mid(PreMsg,i,1)???Optimize:You can kill a few birds with the following idea:        Use an array (read from data statements) of all your input text to morse equivalent.        Create a string such as "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ."        Use InStr in your Key input routine, and Morse Convert function.  In this way you can except only valid text input.... Also you can eliminate the entire If..ElseIf.. construct into just a few lines of code in the for loop.If you need example code, or if I need to explain these ideas better, then please feel free to respond either here, or email.*I think I thunk a thought that thunk a thought to think.


Bobysait(Posted 1+ years ago)

 
Code: [Select]
Dim Morse$(37)

Function InitMorse()
Restore MorseData
For i = 0 To 36 : Read v$ : Morse(i)=v : Next
End Function

Function MorseConvert$(msg$)
Local temp$="",I%=0,V%=0
For I = 1 To Len(msg)
V=Asc(Upper(Mid(msg,I,1)))
If V>=65 And V<=90 ; "A" to "Z"
temp$=temp + Morse(V-65)+" "
ElseIf V=32 ; Space
temp$=temp+Morse( 26 )
ElseIf V>=48 And V<=57 ; 0 to 9
temp$=temp+Morse( V-21 )+" "
EndIf
Next
Return temp
End Function

.MorseData
; A to Z
Data ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--"
Data "-.","---",".--.","--.-",".-.","...","-","..-",".--",".--..","-..-","-.--","--.."
;Space then 0 to 1
Data "|","-----",".----","..---","...--","....-",".....","-....","--...","---..","----."



Graphics 400,300,0,2
SetBuffer BackBuffer()

Local Msg$="",Quit%=False

InitMorse()

Repeat
Cls
Color 150,200,255 : Locate 000,024 : Print MorseConvert(Msg)
Color 000,200,000 : Locate 015,280 : Print "<Exit> To Quit."
Color 255,200,050 : Locate 000,000 : Msg$=Input(">:")
If Upper(Msg)="EXIT" Exit
Flip
Forever
End
the "Lite" version :)


Shortwind(Posted 1+ years ago)

 :DHa ha.  Good one Bobysait!!!   Compact and to the point!  Nice work...:)


Shortwind(Posted 1+ years ago)

 But I like this better:Function MorseConvert$(msg$)   Local temp$="",i%=0,v%=0   Local teststring$="ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789"   For i = 1 To Len(msg)      v=Instr(teststring,Upper(Mid(msg,i,1)))      If v Then temp$=temp + Morse(v-1)+" "   Next   Return tempEnd Function:) (By the way... (dumb question time...) how do you put the code in a editable, nice looking window in the post???


Bobysait(Posted 1+ years ago)

 [ code] your code [/ code]for the full size codeor [ codebox] your code [/ codebox]for a fixed height code box, with a scrollbar( without the spaces )ps : "Instr" is slower than computing asc()as the Instr function search from start to end of the string, and compute the ascci code for the char, and compare them to find the first identical with the one you pass in parameter. It's very faster to use asc.


Shortwind(Posted 1+ years ago)

 Thanks for the [ code ] info. :DInstr is probably slower... I was just trying to show superStruct that there ARE many ways of doing the same thing.And isn't a bit of healthy competition fun?  ha haI'm sure someone out there will see this post and put us all to shame! ha haLaters, Be Safe! :))P.S.  Lets see this in the BF Language! ha ha


Bobysait(Posted 1+ years ago)

 <div class="quote"> And isn't a bit of healthy competition fun? ha ha </div>Of course :)But mine is better :PThere is probably another "better" way to do it, that also involve a bit more code.1> Store the chars ( " ", "A" to "Z" and "0" to "9" ) with a dim like :Dim CharsV(256)i=0For n=asc("A") to asc("Z") CharsV(n) = i i=i+1next2> instead of If v=XXX statement, just replace the V with CharsV(v), that is a direct access to all chars.Then, we'll probably get very near to the otpimal solution :) [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal