-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProMicroCode
163 lines (156 loc) · 5.33 KB
/
ProMicroCode
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
#include <Keyboard.h>
byte button =9; // Set what port to look for input signal
byte piezo =3; // Set what port to send tone signal
const long letterInterval = 700; // interval microseconds at which to evaluate morse into alphabet after button up
const long wordInterval = 4000; // interval microseconds at which time a space will be added
unsigned long startPressTime;
unsigned long endPressTime;
unsigned long pressDuration;
unsigned long upDuration;
unsigned long currentMillis;
unsigned long wordTimer;
unsigned long wordDuration;
byte ignoreSpace = 0;
String morseString = "";
byte charCheck = 0;
byte morseChecked = 0;
byte timerState;
byte heldCommand = 0;
// this function will be called to find what ASCII character to type
// fairly inefficient way to do this but works for now since such a
// limited character set
void MorseLibrary(String& a){
char output = 0;
ignoreSpace = 0;
if (a == ".-") output = 'a';
if (a == "-...") output = 'b';
if (a == "-.-.") output = 'c';
if (a == "-..") output = 'd';
if (a == ".") output = 'e';
if (a == "..-.") output = 'f';
if (a == "--.") output = 'g';
if (a == "....") output = 'h';
if (a == "..") output = 'i';
if (a == ".---") output = 'j';
if (a == "-.-") output = 'k';
if (a == ".-..") output = 'l';
if (a == "--") output = 'm';
if (a == "-.") output = 'n';
if (a == "---") output = 'o';
if (a == ".--.") output = 'p';
if (a == "--.-") output = 'q';
if (a == ".-.") output = 'r';
if (a == "...") output = 's';
if (a == "-") output = 't';
if (a == "..-") output = 'u';
if (a == "...-") output = 'v';
if (a == ".--") output = 'w';
if (a == "-..-") output = 'x';
if (a == "-.--") output = 'y';
if (a == "--..") output = 'z';
if (a == "-----") output = '0';
if (a == ".----") output = '1';
if (a == "..---") output = '2';
if (a == "...--") output = '3';
if (a == "....-") output = '4';
if (a == ".....") output = '5';
if (a == "-....") output = '6';
if (a == "--...") output = '7';
if (a == "---..") output = '8';
if (a == "----.") output = '9';
if (a == "----"){
output = KEY_BACKSPACE;
ignoreSpace = 1; // no space character sent after backspace
}
if (a == ".-.-.-") output = '.';
if (a == "-.-.--") output = '!';
// MOAAR
if (a == "--..--") output = ',';
if (a == "..--..") output = '?';
if (a == ".----.") output = 39; // apostrophe
if (a == "-..-.") output = '/';
if (a == "-.--.") output = '(';
if (a == "-.--.-") output = ')';
if (a == ".-...") output = '&';
if (a == "---...") output = ':';
if (a == "-.-.-.") output = ';';
if (a == "-...-") output = '=';
if (a == ".-.-.") output = '+';
if (a == "-....-") output = '-';
if (a == "..--.-") output = '_';
if (a == ".--.-.") output = '@';
if (a == ".-..-.") output = '"';
if (a == "...-.") output = '*';
if (a == "-.-.-") output = 92; // backslash
if (a == "---.-") output = '%';
if (a == "--.-.") output = '#';
if (a == "--.-.-") output = '|';
if (a == "......") output = '^';
if (a == ".---..") output = '~';
if (a == "-..-.-") output = '`';
if (a == "...-..") output = '$';
if (a == ".--..") output = '[';
if (a == ".--..-") output = ']';
if (a == ".--.-") output = '{';
if (a == ".--.--") output = '}';
if (a == "-.---") output = '<';
if (a == "-.----") output = '>';
if (a == "..--") output = ' ';
if (a == ".-.-"){
Keyboard.write(KEY_RETURN); // Enter
ignoreSpace = 1; // no space character sent after enter
return; // exit this function
}
if (a == "....-."){
Keyboard.press(KEY_LEFT_SHIFT); // Shift - .press will hold key
ignoreSpace = 1; // no space
heldCommand = 1;
return;
}
Serial.println (output);
Keyboard.write (output);
if (heldCommand == 1){ // release held key if done last cycle
Keyboard.releaseAll();
heldCommand = 0;
}
}
void setup(){
pinMode (button, INPUT_PULLUP);
Serial.begin(9600);
Keyboard.begin();
}
void loop(){
if (timerState == 0 && digitalRead(button) == LOW){ // button pressed
startPressTime = millis();
tone(piezo, 880); // sends A880 to piezo speaker
timerState = 1;
charCheck = 0;
}
if (timerState == 1 && digitalRead(button) == HIGH){ // button released
endPressTime = millis();
noTone(piezo);
pressDuration = endPressTime - startPressTime;
if (pressDuration > 10 && pressDuration <= 200){
morseString += "."; // add dot to string
Serial.print (" . ");
}
if (pressDuration > 200){
morseString += "-"; // add dash to string
Serial.print(" - ");
}
timerState = 0;
charCheck = 1;
}
currentMillis = millis();
upDuration = currentMillis - endPressTime;
if (upDuration >= letterInterval && charCheck == 1) {
MorseLibrary (morseString); // call the function that outputs character
charCheck = 0; // reset this variable to 0 so that this if statement does not run again until next button release
morseString = ""; // reset string used for collecting morse
morseChecked = 1;
}
if (upDuration >= wordInterval && charCheck == 0 && morseChecked == 1 && ignoreSpace == 0 && timerState == 0) {
morseChecked = 0;
Keyboard.write(' ');
}
}