-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
230 lines (218 loc) · 7.33 KB
/
Program.cs
File metadata and controls
230 lines (218 loc) · 7.33 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Threading;
using System.Text;
using System.Collections;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace MorseBuzzer
{
public class Program
{
private static string randCall()
{
// Generate a random "call sign" to play
// US call sign rules http://wireless.fcc.gov/services/index.htm?job=call_signs_1&id=amateur
Random _rnd = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
string prefix = "aknw";
string letter = "abcdefghijklmnopqrstuvwxyz";
string number = "0123456789";
int prefixLen = prefix.Length;
int letterLen = letter.Length;
int numberLen = number.Length;
StringBuilder call = new StringBuilder();
// Decide what length of call sign to generate
switch ((int)_rnd.Next(6))
{
case 0: // 1x1 call
call.Append(prefix[_rnd.Next(prefixLen)]);
call.Append(number[_rnd.Next(numberLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
break;
case 1: // 1x2 call
call.Append(prefix[_rnd.Next(prefixLen)]);
call.Append(number[_rnd.Next(numberLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
break;
case 2: // 1x3 call
call.Append(prefix[_rnd.Next(prefixLen)]);
call.Append(number[_rnd.Next(numberLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
break;
case 3: // 2x1 call
call.Append(prefix[_rnd.Next(prefixLen)]);
if (call[0].Equals("a")) // only A[A-L] are valid 2xn calls
{
call.Append(letter[_rnd.Next(13)]);
}
else
{
call.Append(letter[_rnd.Next(letterLen)]);
}
call.Append(number[_rnd.Next(numberLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
break;
case 4: // 2x2 call
call.Append(prefix[_rnd.Next(prefixLen)]);
if (call[0].Equals("a"))
{
call.Append(letter[_rnd.Next(13)]);
}
else
{
call.Append(letter[_rnd.Next(letterLen)]);
}
call.Append(number[_rnd.Next(numberLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
break;
case 5: // 2x3 call
default:
call.Append(prefix[_rnd.Next(prefixLen)]);
if (call[0].Equals("a"))
{
call.Append(letter[_rnd.Next(13)]);
}
else
{
call.Append(letter[_rnd.Next(letterLen)]);
}
call.Append(number[_rnd.Next(numberLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
call.Append(letter[_rnd.Next(letterLen)]);
break;
}
return call.ToString();
}
private static void buzz(PWM spkr, string a, string n)
{
// spkr: PWM object representing the speaker to buzz
// a: string consisting of dots (.) and dashes (-) representing a character in Morse code
// n: Note to play (contained in the scale hashtable
// Most of this code is stolen^H^H^H^H^H^Htaken from Getting Started with Netduino
// Hashtable to store the notes
Hashtable scale = new Hashtable();
scale.Add("c", 1915u);
scale.Add("d", 1700u);
scale.Add("e", 1519u);
scale.Add("f", 1432u);
scale.Add("g", 1275u);
scale.Add("a", 1136u);
scale.Add("b", 1014u);
scale.Add("C", 956u);
scale.Add("D", 851u);
scale.Add("E", 758u);
scale.Add("h", 0u);
// Tweak the value of beatsPerMinute to change the Morse code speed
int beatsPerMinute = 500;
// beatTimeInMilliseconds == one dit length
int beatTimeInMilliseconds = 60000 / beatsPerMinute;
int pauseTimeInMilliseconds = (int)(beatTimeInMilliseconds * 0.1);
uint noteDuration = (uint)scale[n];
foreach (char sym in a)
{
switch (sym)
{
case '.':
// Play a dit
spkr.SetPulse(noteDuration * 2, noteDuration);
Thread.Sleep(beatTimeInMilliseconds - pauseTimeInMilliseconds);
// pause for 1/10th of a beat
spkr.SetDutyCycle(0);
Thread.Sleep(pauseTimeInMilliseconds);
break;
case '-':
// Play a dah
spkr.SetPulse(noteDuration * 2, noteDuration);
Thread.Sleep(beatTimeInMilliseconds * 3 - pauseTimeInMilliseconds);
// pause for 1/10th of a beat
spkr.SetDutyCycle(0);
Thread.Sleep(pauseTimeInMilliseconds);
break;
case ' ':
// pause for a length of five dits
spkr.SetDutyCycle(0);
Thread.Sleep(beatTimeInMilliseconds * 5);
break;
default:
break;
}
}
// Pause for 3 dits after the character
spkr.SetDutyCycle(0);
Thread.Sleep(beatTimeInMilliseconds*3);
}
public static void Main()
{
// Text to Morse code conversion table
Hashtable morse = new Hashtable();
morse.Add('a', ".-");
morse.Add('b', "-...");
morse.Add('c', "-.-.");
morse.Add('d', "-..");
morse.Add('e', ".");
morse.Add('f', "..-.");
morse.Add('g', "--.");
morse.Add('h', "....");
morse.Add('i', "..");
morse.Add('j', ".---");
morse.Add('k', "-.-");
morse.Add('l', ".-..");
morse.Add('m', "--");
morse.Add('n', "-.");
morse.Add('o', "---");
morse.Add('p', ".--.");
morse.Add('q', "--.-");
morse.Add('r', ".-.");
morse.Add('s', "...");
morse.Add('t', "-");
morse.Add('u', "..-");
morse.Add('v', "...-");
morse.Add('w', ".--");
morse.Add('x', "-..-");
morse.Add('y', "-.--");
morse.Add('z', "--..");
morse.Add('0', "-----");
morse.Add('1', ".----");
morse.Add('2', "..---");
morse.Add('3', "...--");
morse.Add('4', "....-");
morse.Add('5', ".....");
morse.Add('6', "-....");
morse.Add('7', "--...");
morse.Add('8', "---..");
morse.Add('9', "----.");
morse.Add(' ', " ");
morse.Add('.', "·–·–·–");
morse.Add(',', "--..--");
morse.Add('?', "..--..");
morse.Add('!', "-.-.--");
morse.Add('/', "-..-.");
// Text to play in Morse code. Change this to whatever you want
// string morseText = "ab4ug";
// Play the alphabet
// string morseText = "abcdefghijklmnopqrstuvwxyz0123456789";
// Note to play Morse code in. See the buzz() routine for a list of available notes
string note = "a";
PWM speaker = new PWM(Pins.GPIO_PIN_D5);
while (true)
{
// Get a random call sign to play
string morseText = randCall();
foreach (char c in morseText)
{
// Get the Morse "song" corresponding to the current letter and send it to buzz()
buzz(speaker, (string)morse[c], note);
}
// Delay 5 seconds before repeating
speaker.SetDutyCycle(0);
Thread.Sleep(5000);
}
}
}
}