-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtappy_glove.c
375 lines (287 loc) · 6.01 KB
/
tappy_glove.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/** example_blink_led app:
This app blinks the red LED.
For a precompiled version of this app and a tutorial on how to load this app
onto your Wixel, see the Pololu Wixel User's Guide:
http://www.pololu.com/docs/0J46
*/
#include <wixel.h>
#include <usb.h>
#include <usb_com.h>
#include <radio_com.h>
#include <radio_link.h>
#include <stdio.h>
//Comment out to run in non-USB mode.
//#define USB_MODE 1
#define TX_BUFF_LEN 100
#define RX_BUFF_LEN 4
#define DOT_TIME 1000
#define DASH_TIME 2000
#define GAP_TIME 1000
#define SPACE_TIME 1000
#define HEARTBEAT_TIME 250
typedef enum { false = 0, true = 1} bool;
typedef enum MorseSymbol{INVALID = 0, DOT = 1, DASH = 2, SPACE = 3} morse_t;
/*** TRANSMIT MESSAGE BUFFER ***/
morse_t txMessage[TX_BUFF_LEN] = {0};
void initTxMessage(void);
void addToTxMessage(morse_t m);
morse_t popFromTxMessage(void);
uint8 nextSymbol(uint8 index);
uint8 txMessageStart = 0;
uint8 txMessageEnd = 0;
bool txReadyToSend = false;
/*** DEBUG FUNCTIONS ***/
void debugPrintByte(uint8 george);
/*** RECEIVE MESSAGE BUFFER ***/
morse_t rxMessage[RX_BUFF_LEN] = {0};
uint8 rxIndex;
void initRxMessage(void);
/*** TIMER VARIABLES ***/
uint32 lastHeartbeatTime = 0;
uint32 startTime;
uint32 symbolTime;
bool symbolInProgress = false;
bool gapInProgress = false;
/*** OTHER FORWARD DECLARATIONS ***/
void setupInterrupts(void);
void buzzerOff(void);
void buzzerOn(void);
/*
* P1_2 = DOT
* P1_1 = DASH
* P1_7 = SPACE
* P1_6 = SEND
*/
//Note: For port interrupts, MUST clear the module interrupt flag before
//the CPU interrupt flag.
ISR(P1INT, 0)
{
if(P1IFG & 0x2) //Pin 1
{
P1IFG &= ~0x2; //Clear flag
addToTxMessage(DASH);
//debugPrintByte(130);
IRCON2 &= ~P1IF; //Clear CPU interrupt flag
return;
}
if(P1IFG & 0x4) //Pin 2
{
P1IFG &= ~0x4; //Clear flag
addToTxMessage(DOT);
//debugPrintByte(129);
IRCON2 &= ~P1IF; //Clear CPU interrupt flag
return;
}
if(P1IFG & 0x40) //Pin 6
{
P1IFG &= ~0x40; //Clear flag
txReadyToSend = true;
IRCON2 &= ~P1IF; //Clear CPU interrupt flag
return;
}
if(P1IFG & 0x80) //Pin 7
{
P1IFG &= ~0x80; //Clear flag
addToTxMessage(SPACE);
//debugPrintByte(131);
IRCON2 &= ~P1IF; //Clear CPU interrupt flag
return;
}
IRCON2 &= ~P1IF; //Clear CPU interrupt flag
}
uint8 nextSymbol(uint8 index)
{
return (index + 1) % TX_BUFF_LEN;
}
void addToTxMessage(morse_t m)
{
if(nextSymbol(txMessageEnd) != txMessageStart)
{
txMessage[txMessageEnd] = m;
txMessageEnd = nextSymbol(txMessageEnd);
}
}
morse_t popFromTxMessage(void)
{
if(txMessageStart != txMessageEnd)
{
morse_t returnVal = txMessage[txMessageStart];
txMessage[txMessageStart] = INVALID;
txMessageStart = nextSymbol(txMessageStart);
return returnVal;
}
else return INVALID;
}
void transmit(void)
{
bool valid = true;
uint8 byteToSend = 0;
while(valid == true)
{
uint8 i;
morse_t temp;
byteToSend = 0;
for(i = 0; i < 4; i++)
{
temp = popFromTxMessage();
byteToSend |= temp << (2*i);
if(temp == INVALID) valid = false;
}
if(radioComTxAvailable() && byteToSend != 0)
{
radioComTxSendByte(byteToSend);
debugPrintByte(byteToSend);
}
}
initTxMessage();
}
void setupInterrupts(void)
{
IRCON2 &= ~P1IF; //Clear P1 interrupt flag.
//Interrupt Port 1, Pin 1, 2, 6, 7 interrupts.
P1IEN |= 0xC6;
IEN2 |= 0x10; //Enable Port 1 interrupts.
IEN0 |= EA; //Set global interrupt enable.
}
void buzzerOn()
{
setDigitalOutput(10,HIGH);
LED_YELLOW(1);
}
void buzzerOff()
{
setDigitalOutput(10,LOW);
LED_YELLOW(0);
}
void initRxMessage()
{
//This makes sure we don't receive a message of 0's.
rxIndex = RX_BUFF_LEN;
}
void debugPrintByte(uint8 george)
{
if(usbComTxAvailable())
{
int8 j;
usbComTxSendByte('0');
usbComTxSendByte('b');
for(j = 7; j >= 0 ; j--)
{
if((george >> j) & 0x1)
usbComTxSendByte('1');
else
usbComTxSendByte('0');
}
usbComTxSendByte('\n');
}
}
void initTxMessage()
{
uint8 i;
for(i = 0; i < TX_BUFF_LEN; i++)
{
txMessage[i] = 0;
}
txMessageStart = 0;
txMessageEnd = 0;
txReadyToSend = false;
}
void receiveMessage(uint8 message)
{
uint8 i;
rxIndex = 0;
for(i = 0; i < RX_BUFF_LEN; i++)
{
uint8 george = (message >> 2*i) & 0x3;
rxMessage[i] = george;
debugPrintByte(george);
}
}
void processSymbol(morse_t m)
{
if(m == INVALID) return;
startTime = getMs();
switch(m)
{
case DOT:
symbolTime = DOT_TIME;
buzzerOn();
break;
case DASH:
symbolTime = DASH_TIME;
buzzerOn();
break;
case SPACE:
symbolTime = SPACE_TIME;
break;
}
symbolInProgress = true;
}
void main()
{
systemInit();
usbInit();
radioComInit();
initTxMessage();
initRxMessage();
setupInterrupts();
buzzerOff();
while(1)
{
/*** TIMER UPDATES ***/
if(getMs() - lastHeartbeatTime > HEARTBEAT_TIME)
{
LED_RED_TOGGLE();
lastHeartbeatTime = getMs();
}
if(symbolInProgress &&((getMs() - startTime) > symbolTime))
{
buzzerOff();
symbolInProgress = false;
gapInProgress = true;
startTime = getMs();
}
if(gapInProgress && ((getMs() - startTime) > GAP_TIME))
{
gapInProgress = false;
}
if(rxIndex < RX_BUFF_LEN && !symbolInProgress && !gapInProgress)
{
processSymbol(rxMessage[rxIndex]);
rxIndex++;
}
/*** OTHER SERVICES ***/
#ifdef USB_MODE
if(usbComRxAvailable() && rxIndex == RX_BUFF_LEN)
{
uint8 modeDifferentiation = usbComRxReceiveByte();
if(modeDifferentiation == 'r')
receiveMessage(usbComRxReceiveByte());
else
{
if(radioComTxAvailable())
{
radioComTxSendByte(usbComRxReceiveByte());
}
}
}
#else
if(radioComRxAvailable() && rxIndex == RX_BUFF_LEN
&& !symbolInProgress && !gapInProgress)
{
receiveMessage(radioComRxReceiveByte());
}
#endif
//Transmit data that was input via buttons.
if(txReadyToSend)
{
transmit();
txReadyToSend = false;
}
radioComTxService();
//Make sure we can strap to the bootloader if necessary.
boardService();
//Run USB Communication Tasks
usbComService();
}
}