-
Notifications
You must be signed in to change notification settings - Fork 146
/
dit-dah.c
162 lines (145 loc) · 4.21 KB
/
dit-dah.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
/*
* Copyright 2012 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/*
* USB keyboard to input by Morse code with help of special romaji table.
* The device transmits keycodes based on a single button input:
*
* "." for a short tap
* "-" for a long tap
* "/" for a pause after a tap
* [SPACE][ENTER] for a long pause after taps (convert and commit)
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <util/delay.h> /* for _delay_ms() */
#include "usb_keyboard.h"
/*
* Thresholds for button pressing duration.
*
* A counter is increased every 10 milliseconds in the main loop.
* These time lengths are specified by the units of this counter.
*
* FYI: WPM = words per minute is commonly used unit of morse code speed.
* 1 word stands for 50 dots. For example, 10 WPM means 1 dot=0.12s
*/
#define T_DIT 12 /* 10WPM */
/* minimum length of button press duration for a "dah" */
#define THRESHOLD_DIT_DAH (T_DIT + 7)
/* minimum length of time for a character separator */
#define THRESHOLD_GAP (T_DIT * 3)
/* length of time to wait before convert and commit */
#define THRESHOLD_LONG_GAP 100 /* 1 second */
#define EMPTY 255
int __attribute__((noreturn)) main(void)
{
PORTB = 0xff;
wdt_enable(WDTO_1S);
initUsbKeyboard();
/* sets up timer for button polling interval */
/* timer 1 CTC mode, counts up at f[clock]/8 [Hz] */
TCCR1B = (1 << WGM12) | (1 << CS11);
TCNT1 = 0;
OCR1A = F_CPU / 8 / 100; /* 100Hz, T=10 milliseconds */
uchar key = 0;
/* Keyboard keys to send.
* Since we have only 1 byte buffer, the first key is dropped if the second
* key is pushed before submitting it.
*/
uchar cmdBuf = EMPTY;
uchar state = 0;
unsigned int count = 0;
while(1) { /* main event loop */
wdt_reset();
usbPoll();
if(usbInterruptIsReady()) {
if (cmdBuf != EMPTY) {
prepareInterruptReport(cmdBuf);
if (cmdBuf == KEYCODE_NONE) {
cmdBuf = EMPTY;
} else {
/* release all keys */
cmdBuf = KEYCODE_NONE;
}
}
}
/* samples the key input every 10 milliseconds to avoid chattering */
if (bit_is_clear(TIFR, OCF1A)) {
continue;
}
TIFR |= _BV(OCF1A); /* clear OCF1A */
key = bit_is_clear(PINB, PINB0);
switch(state) {
case 0: /* initial state or after a long gap */
if (key) {
state = 1;
count = 0;
}
break;
case 1: /* key is on */
if (!key) {
cmdBuf = KEYCODE_PERIOD; /* dot */
state = 3;
count = 0;
break;
}
if (count++ > THRESHOLD_DIT_DAH) {
state = 2;
count = 0;
}
break;
case 2: /* key has been pressed long enough to input dash */
if (!key) {
cmdBuf = KEYCODE_HYPHEN; /* dash */
state = 3;
count = 0;
}
break;
case 3: /* key was released. */
if (key) {
state = 1;
count = 0;
break;
}
if (count++ > THRESHOLD_GAP) {
cmdBuf = KEYCODE_SLASH; /* word separator */
state = 4;
}
break;
case 4: /* short gap, or long gap */
if (key) {
state = 1;
count = 0;
break;
}
if (count++ > THRESHOLD_LONG_GAP) {
cmdBuf = KEYCODE_SPACE; /* start conversion */
state = 5;
count = 0;
}
break;
case 5: /* long gap. pause before sending Enter key. */
if (count++ > T_DIT) {
cmdBuf = KEYCODE_ENTER; /* commit conversion */
state = 0;
count = 0;
}
break;
}
}
}