-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIR_Keyboard.ino
732 lines (509 loc) · 13 KB
/
IR_Keyboard.ino
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
IR Keyboard for LG Blu-ray.
LG key codes: http://lirc.sourceforge.net/remotes/lg/AKB72915207
IR library: http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
*/
#include <IRremote.h>
#include "irkeyboard.h"
#include <LiquidCrystal.h>
#include <PS2Keyboard.h>
// IR interface
IRsend irsend;
const int GRID_WIDTH = 8;
const int GRID_HEIGHT = 7;
int cursorRow; // cursor postion in grid
int cursorCol;
// PS/2 interface
const int DataPin = 7;
const int IRQpin = 8;
// cursor size
const int lcdRows = 4;
const int lcdCols = 20;
// keyboard buffer
char searchBuffer[(lcdRows*lcdCols)+1];
PS2Keyboard keyboard; // keyboard object
// point at buffer
char *sPtr = searchBuffer;
// create LCD object
//LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
LiquidCrystal lcd(2, 3, 4, 5, 6, 9);
boolean remoteMode = false; // flag for manual operation via arrows and enter
/*
sendCmd
Send passed commend to IR reciever.
*/
void sendCmd(COMMAND cmd)
{
// combine pre data with specific code
irsend.sendNEC(PRE_DATA | (long)(tv_codes[cmd] & 0xffff),32);
// we must move deliberatly to avoid missed clicks
delay(300);
}
/*
posAtA
Postions cursor at the 'a' character. Call in startup to position at a known place.
We assume that the cursor is somewhere in the
search grid and there is no wrapping at the grid edges.
*/
void posAtA()
{
int i;
// move all the way left
for(i=0; i < GRID_WIDTH; i++) sendCmd(left);
delay(40);
// move all the way down
for(i=0; i < GRID_HEIGHT; i++) sendCmd(down);
delay(40);
// now move up to 'a'
for(i=0; i < (GRID_HEIGHT- 2); i++) sendCmd(up);
delay(40);
// init current posiiton
cursorRow = 1;
cursorCol = 0;
}
/*
posAtChar
Position at character in passed row, column
*/
void posAtChar(int gridRow, int gridCol)
{
// posiiton at proper row
while( cursorRow != gridRow )
{
if( cursorRow > gridRow )
{
sendCmd(up);
cursorRow--;
}
else {
sendCmd(down);
cursorRow++;
} // else
} // while
// posiiton at proper column
while( cursorCol != gridCol )
{
if( cursorCol < gridCol )
{
sendCmd(right);
cursorCol++;
}
else {
sendCmd(left);
cursorCol--;
} // else
} // while
}
/* typeChar
Type character at passed positon in grid.
*/
void typeChar(int gridRow, int gridCol)
{
// position at character
posAtChar(gridRow, gridCol);
// choose character
sendCmd(ok);
delay(40);
}
const int quoteval = '"'; // compiler blows up if '"" is in array
int specialChars[] = {
'.',',','_','-','\'',quoteval,'/','&','@'};
/*
checkForSpecialChar
If passed char is a special character (appears after '9' on screen)
return its grid posiiton, else return -1.
*/
int checkForSpecialChar(char c)
{
int retVal = (-1);
// while more special chars
for(int idx=0; specialChars[idx]; idx++)
{
// if this is the special char
if( c == specialChars[idx] )
{
// set position in grid
retVal = 'z' - 'a' + '9' - '0' + idx + 2;
// stop looking
break;
} // if
} // for
return(retVal);
}
//
// getGridPos
//
// Return char position on screen grid or -1 for illegal character.
//
int getGridPos(char c)
{
int gridPos = (-1); // init to illegal value
// if this is a letter
if( ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) )
{
// calculate grid position
((c >= 'a') && (c <= 'z')) ? gridPos = c - 'a' : gridPos = c - 'A';
}
else if( (c >= '0') && (c <= '9') ) {
// if this is a number
// numbers are after letters on grid
gridPos = ('z' - 'a') + (c - '0') + 1;
}
else {
// this character might be a special character
gridPos = checkForSpecialChar(c);
} // else
return(gridPos);
}
// stringToLCD
//
// Print string to LCD begining at passed location in searchBuffer
//
void stringToLCD(char *p)
{
byte colNo;
byte rowNo;
// while more charactres
while( *p )
{
colNo = (p - searchBuffer) % lcdCols;
rowNo = (p - searchBuffer) /lcdCols;
lcd.setCursor(colNo, rowNo);
lcd.print(*p++);
} // while
}
/* restoreLCD
A status message has been on screen, clear it and write the
search buffer.
*/
void restoreLCD()
{
// print buffer to lcd
lcd.clear();
// lcd.print(searchBuffer);
stringToLCD(searchBuffer);
// Turn on the cursor:
lcd.cursor();
lcd.blink();
// restore cursor position
lcd.setCursor((sPtr-searchBuffer) % lcdCols, (sPtr-searchBuffer) / lcdCols);
}
/*
processBuffer
Write ir coammnds to spell contents of buffer.
*/
void processBuffer()
{
char *c = searchBuffer; // point at buffer
int gridPos; // offset from letter 'a'
int gridRow, gridCol;
// while more characters in buffer
while( *c )
{
// get position on grid
gridPos = getGridPos(*c);
// if we have a legal character
if( gridPos >= 0 ) {
// calculate row and column
gridRow = (gridPos / GRID_WIDTH) + 1;
gridCol = gridPos % GRID_WIDTH;
// type the character
typeChar(gridRow,gridCol);
}
else {
// must be a top row character
if( *c == ' ')
{
// first position at 'b'
gridRow = 1;
gridCol = 1;
posAtChar(gridRow, gridCol); // positon at a
sendCmd(up); // up one
sendCmd(ok); // type it
sendCmd(down); // back to grid
// down one from space is 'b'
cursorRow = 1;
cursorCol = 1;
} // if
} // else
// advance pointer
c++;
// if there is a keystroke available
if (keyboard.available()) {
char inChar;
// read it
inChar = keyboard.read();
// if escape was clicked, bail out
if( inChar == PS2_ESC )
{
// write message to LCD
lcd.clear();
lcd.noCursor();
lcd.print("Quitting and resetting ...");
// position at known place
posAtA();
// back to buffer on LCD
restoreLCD();
break;
} // if
} // if
} // while
}
// insertChar
//
// Insert passed character into search buffer.
//
void insertChar(char c)
{
// if at end of buffer
if( !*sPtr )
{
// if buffer is full
if( (sPtr - searchBuffer) == (lcdRows * lcdCols) )
{
// overwrite last character
*(sPtr-1) = c;
}
else {
// insert into buffer, increment pointer
*sPtr++ = c;
} // else
}
else {
// shift right!
char *bufStart, *bufEnd;
// init pointers insert position
bufStart = bufEnd = sPtr;
// find end of buffer
while( *bufEnd ) bufEnd++;
// if buffer is full, overwrite last char
if( (bufEnd - searchBuffer) == (lcdRows * lcdCols) ) bufEnd--;
// copy chars
while( bufEnd > bufStart )
{
// copy one character
*bufEnd = *(bufEnd - 1);
// move end pointer left
bufEnd--;
} // while
// insert character
*sPtr++ = c;
} // else
}
// shiftLeft
//
// Shift all chars in search buffer left.
// Update string on LCD.
//
void shiftLeft()
{
char *p1, *p2;
// copy characters one slot left
for(p2=sPtr,p1=sPtr-1; *p2; p1++, p2++) *p1 = *p2;
// terminate string
*p1 = '\0';
// move pointer left
if( sPtr > searchBuffer ) --sPtr;
// lcd clear
lcd.clear();
// print on LCD
//char *p = searchBuffer;
//while( *p ) lcd.print(*p++);
stringToLCD(searchBuffer);
// position cursor
lcd.setCursor((sPtr-searchBuffer) % lcdCols, (sPtr-searchBuffer) / lcdCols);
}
// setup
//
//
void setup() {
delay(1000);
// startup keyboard
keyboard.begin(DataPin, IRQpin);
Serial.begin(9600);
// startup LCD
lcd.begin(lcdCols, lcdRows);
delay(1000);
// wait for character
lcd.print(" Any Key to Begin ");
while( !keyboard.available() );
// read ikeyt
char c = keyboard.read();
lcd.clear();
lcd.print("One Moment ...");
// position at known place in search window
posAtA();
lcd.clear();
// Turn on the cursor:
lcd.cursor();
lcd.blink();
// place curser at first char of first row
lcd.setCursor(0,0);
}
char c;
void loop() {
// if there is a keystroke available
if (keyboard.available()) {
// read it
char c = keyboard.read();
// if not in remote mode
if( !remoteMode )
{
// if this is a legal character
if( (c == ' ') || (getGridPos(c) != (-1)) )
{
// insert it into buffer
insertChar(c);
// print from inserted char to end of string on LCD
char *p = sPtr-1;
//while( *p ) lcd.print(*p++);
stringToLCD(p);
// if buffer is full
if( (sPtr - searchBuffer) == (lcdRows * lcdCols) )
{
// cursor at last char
lcd.setCursor(lcdCols-1,lcdRows-1);
}
else {
// cursor at next char
lcd.setCursor((sPtr-searchBuffer) % lcdCols, (sPtr-searchBuffer) / lcdCols);
} // else
} // if
// check for some of the special keys
else if (c == PS2_ENTER) {
// time to type search criteria
processBuffer();
}
else if (c == PS2_BACKSPACE) {
// if we are not at the first character, shift left
if( sPtr != searchBuffer )
{
shiftLeft();
} // if
}
else if (c == PS2_F1 ) {
// enter control mode
// shut off cursor
lcd.noCursor();
lcd.noBlink();
// indicate that we are in direct mode
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Use arrows and enter");
lcd.setCursor(0,1);
lcd.print("F1 when done");
// flip flag
remoteMode = true;
}
else if (c == PS2_F2 ) {
// clear search field on TV
posAtChar(1, 2); // positon at c
sendCmd(up); // up one
sendCmd(ok); // type it
sendCmd(down); // back to grid
// down one from clear is 'c'
cursorRow = 1;
cursorCol = 2;
}
else if (c == PS2_TAB) {
Serial.print("[Tab]");
}
else if (c == PS2_ESC) {
// set buffer to nulls
for( sPtr=searchBuffer; *sPtr; sPtr++ ) *sPtr = '\0';
// point to top of buffer
sPtr = searchBuffer;
// clear the LCD
lcd.clear();
}
else if (c == PS2_PAGEDOWN) {
Serial.print("[PgDn]");
}
else if (c == PS2_PAGEUP) {
Serial.print("[PgUp]");
}
else if (c == PS2_LEFTARROW) {
// if not alreay at top of string
if( sPtr > searchBuffer )
{
// move pointer left
sPtr--;
// move cursor
lcd.setCursor((sPtr-searchBuffer) % lcdCols, (sPtr-searchBuffer) / lcdCols);
} // if
}
else if (c == PS2_RIGHTARROW)
{
// if not at end of string
if( *sPtr && ((sPtr+1) - searchBuffer) < ((lcdRows*lcdCols)) )
{
// move pointer right
sPtr++;
// move cursor
lcd.setCursor((sPtr-searchBuffer) % lcdCols, (sPtr-searchBuffer) / lcdCols);
} // if
}
else if (c == PS2_UPARROW) {
// if there is a row above the cursor
if( (sPtr - searchBuffer) / lcdCols )
{
// position one row up
sPtr -= lcdCols;
lcd.setCursor((sPtr-searchBuffer) % lcdCols, (sPtr-searchBuffer) / lcdCols);
} // if
}
else if (c == PS2_DOWNARROW) {
Serial.print("[DOWN ARROW]");
// if there is a row below the cursor
if( ((sPtr - searchBuffer)/lcdCols) < lcdRows )
{
// count chars in buffer
int len = 0;
char *p = searchBuffer;
while( *p++ ) len++;
// calculate position one row down
int newPos = (sPtr - searchBuffer) + lcdCols;
// new position is one row down or end of string
sPtr = searchBuffer + ((newPos < len) ? newPos : len);
lcd.setCursor((sPtr-searchBuffer) % lcdCols, (sPtr-searchBuffer) / lcdCols);
} // if
}
else if (c == 10) {
Serial.print("[^Enter]");
}
}
else {
// remote mode - only respond to arrows and enter
switch( c)
{
case PS2_LEFTARROW: // up arrow
sendCmd(left);
break;
case PS2_RIGHTARROW: // right arrow
sendCmd(right);
break;
case PS2_UPARROW: // up arrow
sendCmd(up);
break;
case PS2_DOWNARROW: // down arrow
sendCmd(down);
break;
case PS2_ENTER: // enter
sendCmd(ok);
break;
case PS2_F1: // end remote mode
// position at known place on grid
lcd.clear();
lcd.print("One Moment ...");
posAtA();
// back to normal
restoreLCD();
// clear flag
remoteMode = false;
break;
default:
break;
} // switch
} // else
} // if
}