-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathflap.c
217 lines (191 loc) · 5.16 KB
/
flap.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
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
/*
* Copyright 2013 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 with a split flap display.
*
* PB0 through PB3: unipolar stepper motor control signals
* PB0 A
* PB1 ~A
* PB2 B
* PB3 ~B
*
* PB6: normally-open push switch for zero-position adjustment.
* PB7: normally-open push switch for the input UI.
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <util/delay.h> /* for _delay_ms() */
#include "usb_keyboard.h"
// Number of flaps that appear in one cycle.
#define LETTERS 40
// Number of flaps for alphabets and numerical digits.
#define ALPHANUMERICS (26 + 10)
const uchar extra_character_keycodes[LETTERS - ALPHANUMERICS] = {
KEYCODE_HYPHEN, KEYCODE_COMMA, KEYCODE_PERIOD, KEYCODE_SPACE
};
#define IDLE_TIMER_FREQUENCY 100
#define DELAY_BEFORE_INPUT (1 * IDLE_TIMER_FREQUENCY)
#define MAX_SPEED 9
#ifdef _FULL_STEP
#define STEPPER_PHASES 4
/* Stepper excitation pattern. Invert the order if necessary.
* Bits 7 and 6 must be set to enable internal pullups for the switches.
*/
const uchar stepper_pattern[STEPPER_PHASES] = {0xc1, 0xc4, 0xc2, 0xc8};
#define STEPPER_PHASE_MASK 3
#define STEPS_PER_LETTER 5
#else
/* half-step mode */
#define STEPPER_PHASES 8
const uchar stepper_pattern[STEPPER_PHASES] = {
0xc1, 0xc5, 0xc4, 0xc6, 0xc2, 0xca, 0xc8, 0xc9};
#define STEPPER_PHASE_MASK 7
#define STEPS_PER_LETTER 10
#endif
#define button_pressed() bit_is_clear(PINB, PINB7)
#define adjust_button_pressed() bit_is_clear(PINB, PINB6)
const unsigned int intervals[MAX_SPEED + 1] = {
(unsigned int)(F_CPU / 8 / IDLE_TIMER_FREQUENCY),
(unsigned int)(F_CPU / 8 / 25),
(unsigned int)(F_CPU / 8 / 43),
(unsigned int)(F_CPU / 8 / 56),
(unsigned int)(F_CPU / 8 / 66),
(unsigned int)(F_CPU / 8 / 75),
(unsigned int)(F_CPU / 8 / 82),
(unsigned int)(F_CPU / 8 / 90),
(unsigned int)(F_CPU / 8 / 97),
(unsigned int)(F_CPU / 8 / 103)
};
uchar stepper_phase = 0;
void advance_stepper_phase() {
#ifdef STEPPER_PHASE_MASK
stepper_phase = (stepper_phase + 1) & STEPPER_PHASE_MASK;
#else
stepper_phase ++;
if (stepper_phase >= STEPPER_PHASES) {
stepper_phase = 0;
}
#endif
PORTB = stepper_pattern[stepper_phase];
}
int __attribute__((noreturn)) main(void)
{
DDRB = 0x3f;
PORTB = stepper_pattern[stepper_phase];
wdt_enable(WDTO_1S);
initUsbKeyboard();
uchar key = 0;
uchar lastKey = 0;
uchar character = 0;
uchar substep = 0;
uchar stop_count = 0;
uchar speed = 0;
enum {INIT, ACTIVE, PAUSED, ADJUST};
uchar state = INIT;
uchar on_count = 0;
/* sets up timer for stepper interval */
/* timer 1 CTC mode, counts up at f[clock]/8 [Hz] */
TCCR1B = (1 << WGM12) | (1 << CS11);
TCNT1 = 0;
OCR1A = intervals[speed];
while(1) {
wdt_reset();
usbPoll();
if (bit_is_clear(TIFR, OCF1A)) {
continue;
}
TIFR |= _BV(OCF1A); /* clear OCF1A */
switch(state) {
case ADJUST:
speed = 0;
if (adjust_button_pressed()) {
} else {
state = INIT;
}
break;
case INIT:
if (button_pressed()) {
speed = 1;
on_count = 0;
state = ACTIVE;
}
if (adjust_button_pressed()) {
state = ADJUST;
advance_stepper_phase();
continue;
}
break;
case ACTIVE:
if (button_pressed()) {
if (on_count < STEPS_PER_LETTER) {
on_count ++;
} else if (speed < MAX_SPEED) {
speed ++;
}
} else {
if (speed > 1) {
speed --;
} else if (speed == 1) {
if (substep == 0) {
speed = 0;
stop_count = 0;
state = PAUSED;
}
}
}
break;
case PAUSED:
default:
if (button_pressed()) {
speed = 1;
state = ACTIVE;
} else {
if (stop_count == DELAY_BEFORE_INPUT) {
if (character >= ALPHANUMERICS) {
key = extra_character_keycodes[character - ALPHANUMERICS];
} else {
key = KEYCODE_A + character;
}
state = INIT;
} else {
stop_count ++;
}
}
}
OCR1A = intervals[speed];
if (speed > 0) {
substep ++;
if (substep >= STEPS_PER_LETTER) {
substep = 0;
character ++;
if (character >= LETTERS) {
character = 0;
}
}
advance_stepper_phase();
}
if (usbInterruptIsReady()) {
if (key != lastKey) {
lastKey = key;
prepareInterruptReport(key);
key = KEYCODE_NONE;
}
}
}
}