-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoteKeyboard.c
More file actions
668 lines (605 loc) · 17.2 KB
/
remoteKeyboard.c
File metadata and controls
668 lines (605 loc) · 17.2 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
// $Id: remoteKeyboard.c 13 2009-05-20 13:32:39Z ned $
#include "remoteKeyboard.h"
#include <avr/io.h>
#if 0
FUSES =
{
//
.low = LFUSE_DEFAULT,
// enable SPI (default) as well as saving EEPROM over chip erase and 4.5V BOD level
.high = (/*FUSE_DWEN & */ FUSE_SPIEN & FUSE_EESAVE & FUSE_BODLEVEL1 & FUSE_BODLEVEL0),
// no reset vector
.extended = EFUSE_DEFAULT
};
#endif
// forward declarations
static column_mask_t readColumnInputs(void);
static row_mask_t readRowStates(void);
static void processRowInputs(row_mask_t rowInputs, uint8_t activeColumn);
static row_mask_t readAuxSwitchStates(void);
static void assertRowOutputs(row_mask_t mask, row_mask_t quiescent);
static void assertAuxOutputs(row_mask_t mask);
static uint8_t countBits(uint8_t number, uint8_t *lastBitnumSet);
static void printHexByte(uint8_t );
static void dumpState(void);
static uint8_t doPressOrReleaseRC(char *command);
static SerialCommandState processSerialCommand(void);
static void initIO(void);
static void initTimers(void);
static void initPCInterrupts(void);
// RAM storage:
// bitmap for which switches are forced
volatile row_mask_t forcedSwitches[N_COLUMNS+1];
volatile row_mask_t activeSwitches[N_COLUMNS+1]; // first time we notice a switch change
volatile row_mask_t priorActiveSwitches[N_COLUMNS+1]; // second time
// DEBUG
volatile column_mask_t seenColumnsHigh = 0;
volatile column_mask_t seenColumnsLow = 0xFF;
volatile row_mask_t seenRowsHigh = 0;
volatile row_mask_t seenRowsLow = 0xFF;
volatile uint16_t columnStrobes[N_COLUMNS+1];
// LED flashing: bits shifted out from LS bit; 1 = LED ON
// each bit is worth LED_BIT_TICKS
volatile uint8_t ledPattern;
// read column inputs, return raw value
// CALLED FROM ISR
static column_mask_t readColumnInputs(void)
{
uint8_t retval = 0;
#if PB_COL_MASK != 0
retval |= PB_TO_COL(PINB);
#endif
#if PC_COL_MASK != 0
retval |= PC_TO_COL(PINC);
#endif
#if PD_COL_MASK != 0
retval |= PD_TO_COL(PIND);
#endif
seenColumnsHigh |= retval; // DEBUG
seenColumnsHigh |= UNUSED_COLUMNS_MASK;
seenColumnsLow &= retval;
seenColumnsLow &= ~UNUSED_COLUMNS_MASK;
return retval;
}
// read row inputs, return raw value
// CALLED FROM ISR
static row_mask_t readRowStates(void)
{
uint8_t retval = 0;
#if PB_ROW_MASK != 0
uint8_t oldDDRB = DDRB; // save direction
if (oldDDRB & PB_ROW_MASK)
DDRB = oldDDRB & ~PB_ROW_MASK; // reset DDR for inputs
#endif
#if PC_ROW_MASK != 0
uint8_t oldDDRC = DDRC; // save direction
if (oldDDRC & PC_ROW_MASK)
DDRC = oldDDRC & ~PC_ROW_MASK; // reset DDR for inputs
#endif
#if PD_ROW_MASK != 0
uint8_t oldDDRD = DDRD; // save direction
if (oldDDRD & PD_ROW_MASK)
DDRD = oldDDRD & ~PD_ROW_MASK; // reset DDR for inputs
#endif
_delay_us(10);
#if PB_ROW_MASK != 0
retval |= PB_TO_ROW(PINB); // and read current values
if (oldDDRB & PB_ROW_MASK)
DDRB = oldDDRB; // restore direction
#endif
#if PC_ROW_MASK != 0
retval |= PC_TO_ROW(PINC); // and read current values
if (oldDDRC & PC_ROW_MASK)
DDRC = oldDDRC; // restore direction
#endif
#if PD_ROW_MASK != 0
retval |= PD_TO_ROW(PIND); // and read current values
if (oldDDRD & PD_ROW_MASK)
DDRD = oldDDRD; // restore direction
#endif
seenRowsHigh |= retval; // DEBUG
seenRowsHigh |= UNUSED_ROWS_MASK;
seenRowsLow &= retval;
seenRowsLow &= ~UNUSED_ROWS_MASK;
return retval;
}
// CALLED FROM ISR
static void processRowInputs(row_mask_t rowInputs, uint8_t activeColumn)
{
// priorActive active rowInputs ch ch2 valid
// 0 0 x x 0 0
// 0 1 0 1 1 0 (glitch)
// 0 1 1 0 1 1 pressed
// 1 0 0 0 1 1 released
// 1 0 1 1 1 0 (glitch)
// 1 1 x x 0 0
//
// changed = active ^ rowInputs
// changed2 = priorActive ^ active
// valid = changed2 & ~changed
// then:
// priorActive = active
// active = rowInputs
//
// compare with last samples for this column to find changed ones (1 == changed)
row_mask_t changed = activeSwitches[activeColumn] ^ rowInputs;
// compare last with prior to detect glitches (1 == changed last time)
row_mask_t changed2
=activeSwitches[activeColumn] ^ priorActiveSwitches[activeColumn];
// valid ones are ones that have just changed but were stable before that
// for at least one sample.
row_mask_t valid = changed2 & ~changed;
row_mask_t mask = 1;
for (uint8_t rowBitNum = 0; rowBitNum < N_ROWS; rowBitNum++, mask <<= 1)
{
// for each row input with a valid change
if (valid & mask)
{
uint8_t state = (rowInputs & mask) ? 'p' : 'r'; // pressed/released
// send out p00 or r00 type codes
uart_putc(state);
uart_putc(rowBitNum + '0');
uart_putc(activeColumn + '0');
uart_putc('\r');
uart_putc('\n');
}
}
priorActiveSwitches[activeColumn] = activeSwitches[activeColumn];
activeSwitches[activeColumn] = rowInputs;
}
// read individual AUX switch states as if they were row inputs
// CALLED FROM ISR
static row_mask_t readAuxSwitchStates(void)
{
uint8_t bit;
row_mask_t retval = 0;
#if N_AUX_OUTPUTS >= 1
uint8_t oldDDR0 = AUX0_DDREG;
AUX0_DDREG &= ~AUX0_MASK; // set as input
#endif
#if N_AUX_OUTPUTS >= 2
uint8_t oldDDR1 = AUX1_DDREG;
AUX1_DDREG &= ~AUX1_MASK; // set as input
#endif
_delay_us(10);
#if N_AUX_OUTPUTS >= 1
bit = AUX0_PINREG & AUX0_MASK; // read input
if (!AUX0_ON_STATE) // invert if active low
{
bit ^= AUX0_MASK;
}
AUX0_DDREG = oldDDR0;
if (bit)
{
retval = 0x01;
}
#endif
#if N_AUX_OUTPUTS >= 2
bit = AUX1_PINREG & AUX1_MASK; // read input
if (!AUX1_ON_STATE) // invert if active low
{
bit ^= AUX1_MASK;
}
AUX1_DDREG = oldDDR1;
if (bit)
{
retval |= 0x02;
}
#endif
return retval;
}
// set row outputs corresponding to bits in mask as outputs
// quiescent=0: set PORTx bits in mask
// quiescent=1: reset PORTx bits in mask
// to set all to inputs pass a 0
// leaves DDRx set
// CALLED FROM ISR
static void assertRowOutputs(row_mask_t mask, row_mask_t quiescent)
{
uint8_t bits;
#if PB_ROW_MASK != 0
bits = PB_FROM_ROW(mask);
DDRB &= ~PB_ROW_MASK; // reset to inputs
PORTB &= ~PB_ROW_MASK; // and don't pull up
if (bits)
{
DDRB |= bits; // turn into outputs
if (quiescent)
{
PORTB &= ~bits;
}
else
{
PORTB |= bits;
}
}
#endif
#if PC_ROW_MASK != 0
bits = PC_FROM_ROW(mask);
DDRC &= ~PC_ROW_MASK; // reset to inputs
PORTC &= ~PC_ROW_MASK; // and don't pull up
if (bits)
{
DDRC |= bits;
if (quiescent)
{
PORTC &= ~bits;
}
else
{
PORTC |= bits;
}
}
#endif
#if PD_ROW_MASK != 0
bits = PD_FROM_ROW(mask);
DDRD &= ~PD_ROW_MASK; // reset to inputs
PORTD &= ~PD_ROW_MASK; // and don't pull up
if (bits)
{
DDRD |= bits;
if (quiescent)
{
PORTD &= ~bits;
}
else
{
PORTD |= bits;
}
}
#endif
}
// set row outputs corresponding to bits in mask as outputs
// and set those outputs to their ON state
// to set all to inputs pass a 0
// leaves DDRx bits set
// CALLED FROM timer tick ISR
static void assertAuxOutputs(row_mask_t mask)
{
#if N_AUX_OUTPUTS >= 1
if (mask & 1)
{
if (AUX0_ON_STATE)
{
AUX0_PORTREG |= AUX0_MASK;
}
else
{
AUX0_PORTREG &= ~AUX0_MASK;
}
AUX0_DDREG |= AUX0_MASK; // set as output
}
else
{
AUX0_PORTREG &= ~AUX0_MASK; // ensure pullups off
AUX0_DDREG &= ~AUX0_MASK; // set as input
}
#endif
#if N_AUX_OUTPUTS >= 2
if (mask & 2)
{
if (AUX1_ON_STATE)
{
AUX1_PORTREG |= AUX1_MASK;
}
else
{
AUX1_PORTREG &= ~AUX1_MASK;
}
AUX1_DDREG |= AUX1_MASK; // set as output
}
else
{
AUX1_PORTREG &= ~AUX1_MASK; // ensure pullups off
AUX1_DDREG &= ~AUX1_MASK; // set as input
}
#endif
}
// CALLED FROM ISR
static uint8_t countBits(uint8_t number, uint8_t *lastBitnumSet)
{
static struct BitDecode_t
{
uint8_t nBits : 4;
uint8_t highestBit : 4;
} const usedBits[16] = {
{ 0, 0 }, // 0
{ 1, 0 }, // 1
{ 1, 1 }, // 2
{ 2, 1 }, // 3
{ 1, 2 }, // 4
{ 2, 2 }, // 5
{ 2, 2 }, // 6
{ 3, 2 }, // 7
{ 1, 3 }, // 8
{ 2, 3 }, // 9
{ 2, 3 }, // A
{ 3, 3 }, // B
{ 2, 3 }, // C
{ 3, 3 }, // D
{ 3, 3 }, // E
{ 4, 3 }, // F
};
uint8_t numberSet = 0;
uint8_t highestBit = 0;
uint8_t nybble = number & 0x0F;
struct BitDecode_t const *p = usedBits + nybble;
if ((numberSet = p->nBits))
{
highestBit = p->highestBit;
}
number >>= 4;
p = usedBits + number;
uint8_t nb;
if ((nb = p->nBits))
{
numberSet += nb;
highestBit = 4 + p->highestBit;
}
if (numberSet)
{
*lastBitnumSet = highestBit;
}
return numberSet;
}
// 30.5 Hz periodic interrupt
ISR(TIMER0_OVF_vect)
{
// read aux switches
processRowInputs(readAuxSwitchStates(), N_COLUMNS);
// handle forced aux switches
assertAuxOutputs(forcedSwitches[N_COLUMNS]);
}
// pin change interrupt vector PCINT1
// triggered by any logic change on enabled PCINTxx pins (inputs from host column strobe pins)
ISR(PCINT1_vect)
{
// get the column inputs (at least one of which has just changed)
column_mask_t columnInputs = readColumnInputs();
static mask_t quiescentState = 0xFF; // debug
again:
columnInputs ^= quiescentState; // invert if necessary
columnInputs &= ~UNUSED_COLUMNS_MASK; // ignore unused columns
uint8_t activeColumn = 0;
uint8_t nSetBits = countBits(columnInputs, &activeColumn);
switch (nSetBits)
{
case 1: // single active column line
{
// read the row inputs and convert to logical levels (1 == active)
row_mask_t rowInputs = readRowStates() ^ quiescentState;
// handle transitions and report on changes
columnStrobes[activeColumn]++; // DEBUG
processRowInputs(rowInputs, activeColumn);
// now force any switches that we're forcing
assertRowOutputs(forcedSwitches[activeColumn], quiescentState);
}
break;
case 0: // no active column lines
// set all row outputs as inputs
assertRowOutputs(0, 0);
break;
case N_COLUMNS - 1: // quiescent state wrong; one active
columnInputs ^= quiescentState; // restore flipped bits
quiescentState = ~quiescentState;
goto again;
break;
case N_COLUMNS: // quiescent state wrong; nothing active
quiescentState = ~quiescentState;
break;
}
}
// print byte as 2 hex chars
static void printHexByte(uint8_t val)
{
uint8_t v = (val & 0xF0) >> 4;
v = (v > 0x09) ? ('A' - 0x0a + v) : ('0' + v);
uart_putc(v);
v = val & 0x0F;
v = (v > 0x09) ? ('A' - 0x0a + v) : ('0' + v);
uart_putc(v);
}
// debug: dump state of interrupt handler observations
static void dumpState(void)
{
uart_puts_P("\r\nchi: ");
printHexByte(seenColumnsHigh);
uart_puts_P(" clo: ");
printHexByte(seenColumnsLow);
uart_puts_P("\r\nrhi: ");
printHexByte(seenRowsHigh);
uart_puts_P(" rlo: ");
printHexByte(seenRowsLow);
uart_puts_P("\r\nCo Fo Ac Pr CSTR\r\n");
for (uint8_t i = 0; i <= N_COLUMNS; i++)
{
printHexByte(i);
uart_putc(' ');
printHexByte(forcedSwitches[i]);
uart_putc(' ');
printHexByte(activeSwitches[i]);
uart_putc(' ');
printHexByte(priorActiveSwitches[i]);
uart_putc(' ');
printHexByte(columnStrobes[i] >> 8);
printHexByte(columnStrobes[i] & 0xFF);
uart_puts_P("\r\n");
columnStrobes[i] = 0; // reset count
}
}
void pressSwitch(uint8_t row, uint8_t column)
{
cli();
forcedSwitches[column] |= (1 << row);
sei();
}
void releaseSwitch(uint8_t row, uint8_t column)
{
cli();
forcedSwitches[column] &= ~(1 << row);
sei();
}
// Set or clear bit in forcedSwitches[] in response to command string
// Returns 0 if OK, else error
// Handles 3-character strings (p00 or r00 style) from serial port
// 1st character: 'p' == press; 'r' == release
// 2nd character: row, 0 .. (N_ROWS-1)
// aux. switch I/O starts at row 0
// 3rd character: column
// 0 .. (N_COLUMNS-1) are matrix switches
// aux. switch I/O is at column (N_COLUMNS)
// so (if N_COLUMNS==6):
// p00 press row 0, column 0
// p05 press row 0, column 5
// p06 press aux 0
static uint8_t doPressOrReleaseRC(char *command)
{
uint8_t row = command[1];
if ((row >= '0') && (row < '0' + N_ROWS))
row -= '0';
else
goto error;
uint8_t column = command[2];
if ((column >= '0') && (column <= '0' + N_COLUMNS))
column -= '0';
else
goto error;
if (command[0] == 'p')
pressSwitch(row, column);
else if (command[0] == 'r')
releaseSwitch(row, column);
else
goto error;
return 0;
error:
return 1;
}
// check for serial command
// and process it if it's complete
// Returns SERIAL_CMD_OK for complete and recognized command
// SERIAL_CMD_INCOMPLETE for incomplete (<4 chars) or
// SERIAL_CMD_ERROR for anything else
// Serial commands:
//
// p00 or r00 type codes
// pRC\r press row R, column C
// rRC\r release row R, column C
//
// R for reset
static SerialCommandState processSerialCommand(void)
{
static char command[8];
static uint8_t bytesReceived;
// while there's still data to read
// and no errors
while ((bytesReceived < sizeof(command) - 1))
{
unsigned int c = uart_getc();
if ((c & 0xFF00) == 0)
{
command[bytesReceived++] = (uint8_t) c;
}
else
{
// serial error or no data?
if ((c & 0xFF00) == UART_NO_DATA)
return SERIAL_CMD_INCOMPLETE;
else
return SERIAL_CMD_ERROR;
}
if (c == '\r') // gotten a full command line?
{
SerialCommandState retval = SERIAL_CMD_ERROR;
switch (command[0])
{
case 'p': // press
// fall through
case 'r': // release
if (bytesReceived == 4)
{
command[3] = '\0'; // mark end of string
if (doPressOrReleaseRC(command) == 0)
{
retval = SERIAL_CMD_OK; // on no error
}
else
{
uart_puts(command);
uart_puts_P("?\r\n");
}
}
break;
case 'R': // reset
if (bytesReceived == 2)
{
// Proper software reset using watchdog or jump to bootloader
asm volatile ("jmp 0");
}
break;
case '\r': // empty line: dump state
dumpState();
break;
}
bytesReceived = 0; // reset counter
return retval;
}
}
// here when buffer is full
bytesReceived = 0;
return SERIAL_CMD_ERROR;
}
static void initIO(void)
{
// init IO
// set unused pins as outputs pulled low
PORTB = PB_OUTPUT_INIT;
DDRB = PB_OTHER_OUTPUTS;
PORTC = PC_OUTPUT_INIT;
DDRC = PC_OTHER_OUTPUTS;
PORTD = PD_OUTPUT_INIT;
DDRD = PD_OTHER_OUTPUTS;
}
static void initTimers(void)
{
// init 8-bit Timer0 for periodic interrupts
// max period = 30.5 Hz with 8 MHz clock and clk/1024 prescaler
// min period = 31250 Hz with 8 MHz clock and clk/1 prescaler
TCCR0A = 0;
TCCR0B = 5 << CS00; // clk/1024 = 30.5 Hz
TIMSK0 = _BV(TOIE0);
}
static void initPCInterrupts(void)
{
// init pin-change interrupts
#if PB_COL_MASK != 0
PCMSK0 = PB_COL_MASK;
PCICR |= _BV(PCIE0);
#endif
#if PC_COL_MASK != 0
PCMSK1 = PC_COL_MASK;
PCICR |= _BV(PCIE1);
#endif
#if PD_COL_MASK != 0
PCMSK2 = PD_COL_MASK;
PCICR |= _BV(PCIE2);
#endif
}
int main(void)
{
initIO();
initTimers();
initPCInterrupts();
uart_init(UART_BAUD_SELECT(BAUD, F_CPU));
sei(); // enable IRQ globally
// debug: print wakeup message
uart_puts_P("RemoteKeyboard v1.0 by Ned Konz\r\n");
// main loop: process serial commands and go to sleep
for (;; )
{
processSerialCommand();
// nothing else to do: go to sleep
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_mode();
}
return 0;
}