This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
foxbox.c
182 lines (159 loc) · 4.59 KB
/
foxbox.c
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
/*
* foxbox.c - An Arduino Fox-Hunt system.
* (c) 2011-present, W8UPD - the Univ. Akron Amat. Radio Club
* Authored by Ricky Elrod, N8SQL
* Co-Authored by Jimmy Carter, KG4SGP and Chris Egeland, KD8LCV
* Released under a two-clause BSD license. See LICENSE for details.
*/
// This is not technically C, but syntax-highlights decently as it.
// ============== Configuration of FoxBox ===================
// These values are in miliseconds
const int DIT = 60;
const int DAH = DIT * 3;
const int LETTER_SPACE = DIT * 3;
const int WORD_SPACE = DIT * 7;
// Random number of seconds to delay between identifying.
const int ID_DELAY_MINIMUM = 60;
const int ID_DELAY_MAXIMUM = ID_DELAY_MINIMUM * 3;
// Miliseconds to wait after keying PTT, before sending audio.
const int PRE_ID = 1000;
// What to send
const String MESSAGE = "de w8upd/b";
// ========= Pin assignments ==========
// The pin to blink morse, as sending.
const int LED_PIN = 13;
// The pin to activate PTT.
const int PTT_PIN = 5;
// The pin to send audio out of.
const int TONE_PIN = 6;
/**
* Convert a char/letter to appropriate dots and dashes.
* @param char letter The letter to encode
* @return String Morse code representation (ASCII "." and "-") of the given
char.
*/
String convert_morse(char letter) {
switch (letter) {
case 'a': return ".-";
case 'b': return "-...";
case 'c': return "-.-.";
case 'd': return "-..";
case 'e': return ".";
case 'f': return "..-.";
case 'g': return "--.";
case 'h': return "....";
case 'i': return "..";
case 'j': return ".---";
case 'k': return "-.-";
case 'l': return ".-..";
case 'm': return "--";
case 'n': return "-.";
case 'o': return "---";
case 'p': return ".--.";
case 'q': return "--.-";
case 'r': return ".-.";
case 's': return "...";
case 't': return "-";
case 'u': return "..-";
case 'v': return "...-";
case 'w': return ".--";
case 'x': return "-..-";
case 'y': return "-.--";
case 'z': return "--..";
case '1': return ".----";
case '2': return "..---";
case '3': return "...--";
case '4': return "....-";
case '5': return ".....";
case '6': return "-....";
case '7': return "--...";
case '8': return "---..";
case '9': return "----.";
case '0': return "-----";
case '?': return "..--..";
case '.': return ".-.-.-";
case ',': return "--..--";
case '-': return "-....-";
case '/': return "-..-.";
default: return "";
}
}
/**
* Blink the LED and sound the tone for one "dit" or "dah"
*
* @param char type_delay A char representing a dit or dah ('.' or '-') which
determines how long the action happens.
*/
void blink(char type_delay) {
analogWrite(TONE_PIN, 127);
digitalWrite(LED_PIN, HIGH);
if (type_delay == '.') {
delay(DIT);
} else if (type_delay == '-') {
delay(DAH);
}
analogWrite(TONE_PIN, 0);
digitalWrite(LED_PIN, LOW);
delay(DIT);
}
/**
* Iterate through a given string and perform blink() on each character.
*
* @param String text The string of letters to encode to morse.
*/
void perform_blink(String text) {
for (int i = 0; i <= text.length(); i++) {
// Handle special cases here.
if (text.charAt(i) == ' ') {
delay(WORD_SPACE);
continue;
}
// Convert the letter to morse, ...
String morseified = convert_morse(text.charAt(i));
// For each char of the morse string, ...
for (int j = 0; j <= morseified.length(); j++) {
if (morseified.charAt(j) == '.') {
blink('.');
} else if (morseified.charAt(j) == '-') {
blink('-');
}
}
delay(LETTER_SPACE);
}
}
/**
* Delays for a random amount of seconds, based on given arguments.
*
* @param int minimum Minimum number of miliseconds to delay
* @param int maximum Maximum number of miliseconds to delay
* @return int The number of seconds actually delayed
*/
int randomDelay(int minimum, int maximum) {
// Random noise on unconnected pin 0.
randomSeed(analogRead(0));
int randomSeconds = random(minimum, maximum);
// This math has to be done in the function call to prevent an overflow.
delay(randomSeconds * 1000);
return randomSeconds;
}
/**
* Enter the program here.
*/
void setup() {
// LED stuff.
pinMode(LED_PIN, OUTPUT);
// PTT stuff.
pinMode(PTT_PIN, OUTPUT);
// Audio output.
pinMode(TONE_PIN, OUTPUT);
}
/**
* Do this as the program runs.
*/
void loop() {
digitalWrite(PTT_PIN, HIGH);
delay(PRE_ID);
perform_blink(MESSAGE);
digitalWrite(PTT_PIN, LOW);
randomDelay(ID_DELAY_MINIMUM, ID_DELAY_MAXIMUM);
}