-
Notifications
You must be signed in to change notification settings - Fork 1
/
morse.lua
executable file
·132 lines (121 loc) · 3.4 KB
/
morse.lua
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
-------------------------------------------------------------------------------
-- eLua Morse Demo
--
-- Dado Sutter sep 2008
-------------------------------------------------------------------------------
local pwmid, tmrid, ledpin
if pd.board() == "EK-LM3S8962" or pd.board() == "EK-LM3S6965" then
pwmid, tmrid, ledpin = 1, 1, pio.PF_0
elseif pd.board() == "SAM7-EX256" then
pwmid, tmrid, ledpin = 0, 1, pio.PB_20
tmr.setclock( 1, 100000 )
else
print( pd.board() .. " not supported with this example" )
return
end
------------ User Adjusted Variables ------------
local dotDelay, playFreq, playFreqSave = 90000, 880, 880
local freqStep, dotDelayStep = 220, 10000
-- Morse Alphabet
local Morse = {
A='.-',
B='-...',
C='-.-.',
D='-..',
E='.',
F='..-.',
G='--.',
H='....',
I='..',
J='.---',
K='-.-',
L='.-..',
M='--',
N='-.',
O='---',
P='.--.',
Q='--.-',
R='.-.',
S='...',
T='-',
U='..-',
V='...-',
W='.--',
X='-..-',
Y='-.--',
Z='--..'
}
------------ Auxiliar Functions ------------
local function play(m)
term.print(m)
if m == ' ' then
tmr.delay(tmrid, 2 * dotDelay)
else
pio.pin.sethigh( ledpin )
pwm.start(pwmid)
tmr.delay(tmrid, m == '.' and dotDelay or 3 * dotDelay)
pwm.stop(pwmid)
pio.pin.setlow( ledpin )
tmr.delay(tmrid, dotDelay)
end
end
local function HandleKbd(k)
if k == term.KC_ESC then
return true
elseif k == term.KC_UP then -- Speed up
dotDelay = dotDelay + dotDelayStep
elseif k == term.KC_DOWN then -- Speed down
dotDelay = dotDelay - dotDelayStep
elseif k > 0 and k < 256 then
if string.char(k) == '+' then -- Frequency up
playFreq = playFreq + freqStep
elseif string.char(k) == '-' then -- Frequency down
playFreq = playFreq - freqStep
elseif string.char(k) == 's' then -- Sound on/off
if playFreq == 0 then
playFreq = playFreqSave
else
playFreqSave = playFreq
playFreq = 0
end
end
end
pwm.setup(pwmid, playFreq, 50)
end
------------ Main Program ------------
pio.pin.setdir( pio.OUTPUT, ledpin )
pwm.setup( pwmid, playFreq, 50 )
while true do
term.clrscr()
term.moveto(1, 1)
print("Welcome to eLua Morse Playing on " .. pd.cpu())
io.write("Enter phrase (empty phrase to exit): ")
local msg, enabled = io.read(), true
if #msg == 0 then break end
term.print(' ')
while term.getchar(term.NOWAIT) ~= -1 do end -- flush
while enabled do -- Main Loop
for i = 1, #msg do -- msg loop
local ch = msg:sub(i, i):upper()
term.print(ch) -- show what will be played
if ch ~= ' ' and Morse[ch] then
for j = 1, #Morse[ch] do -- Morse symbol loop
play(Morse[ch]:sub(j,j)) -- play each morse symbol
end
else
play(' ') play(' ') -- Between words
end
play(' ') -- Extra between words & lett
key = term.getchar(term.NOWAIT) -- Handle UI actions
if key ~= -1 then
if HandleKbd(key) then
enabled = false
break
end
end
end
if not enabled then break end
print()
play(' ') play(' ') play(' ') -- Between sentences
end
end