1 | /* keyPadHiduino Example Code
|
2 | modified by: Chregu Müller
|
3 | */
|
4 |
|
5 |
|
6 | // Pins 1-7 of the keypad connected to the Arduino respectively:
|
7 | int keypadPins[7] = {2, 10, 3, 16, 14, 4, 5};
|
8 | int keypadStatus; // Used to monitor which buttons are pressed.
|
9 | int timeout; // timeout variable used in loop
|
10 |
|
11 | void setup()
|
12 | {
|
13 | for (int i=0; i<7; i++)
|
14 | {
|
15 | pinMode(keypadPins[i], INPUT); // Set all keypad pins as inputs
|
16 | digitalWrite(keypadPins[i], HIGH); // pull all keypad pins high
|
17 | }
|
18 | }
|
19 |
|
20 | void loop()
|
21 | {
|
22 | keypadStatus = getKeypadStatus(); // read which buttons are pressed
|
23 | if (keypadStatus != 0) // If a button is pressed go into here
|
24 | {
|
25 | sendKeyPress(keypadStatus); // send the button over USB
|
26 | timeout = 2000; // top of the repeat delay
|
27 | while ((getKeypadStatus() == keypadStatus) && (--timeout)) // Decrement timeout and check if key is being held down
|
28 | delayMicroseconds(1);
|
29 | while (getKeypadStatus() == keypadStatus) // while the same button is held down
|
30 | {
|
31 | sendKeyPress(keypadStatus); // continue to send the button over USB
|
32 | delay(50); // 50ms repeat rate
|
33 | }
|
34 | }
|
35 | }
|
36 |
|
37 |
|
38 | void sendKeyPress(int key)
|
39 | {
|
40 | switch(key)
|
41 | {
|
42 | case 1: // 0x001
|
43 | Keyboard.write('1');
|
44 | break;
|
45 | case 2: // 0x002
|
46 | // Keyboard.write('ü');
|
47 | Keyboard.press(KEY_LEFT_ALT);
|
48 | //Keyboard.write(0xD5);
|
49 | //Keyboard.write(0xD9);
|
50 | //Keyboard.write(0xD3);
|
51 | Keyboard.write('a');
|
52 | Keyboard.release(KEY_LEFT_ALT);
|
53 | break;
|
54 | case 4: // 0x004
|
55 | Keyboard.write('@');
|
56 | break;
|
57 | case 16: // 0x010
|
58 | Keyboard.write('ä');
|
59 | break;
|
60 | case 128: // 0x080
|
61 | Keyboard.write('ö');
|
62 | break;
|
63 | case 256: // 0x100
|
64 | Keyboard.write(' ');
|
65 | break;
|
66 | case 2048: // 0x800
|
67 | Keyboard.write('Ü');
|
68 | break;
|
69 | }
|
70 | }
|
71 |
|
72 | /* getKeypadStatus(): This function returns an int that represents
|
73 | the status of the 12-button keypad. Only the 12 LSb's of the return
|
74 | value hold any significange. Each bit represents the status of a single
|
75 | key on the button pad. '1' is bit 0, '2' is bit 1, '3' is bit 2, ...,
|
76 | '#' is bit 11.
|
77 |
|
78 | This function doesn't work for multitouch.
|
79 | */
|
80 | int getKeypadStatus()
|
81 | {
|
82 | int rowPins[4] = {keypadPins[2], keypadPins[6], keypadPins[5], keypadPins[0]}; // row pins are 2, 7, 6, and 1 of the keypad
|
83 | int columnPins[3] = {keypadPins[1], keypadPins[3], keypadPins[4]}; // column pins are pins 2, 4, and 5 of the keypad
|
84 | int keypadStatus = 0; // this will be what's returned
|
85 |
|
86 | /* initialize all pins, inputs w/ pull-ups */
|
87 | for (int i=0; i<7; i++)
|
88 | {
|
89 | pinMode(keypadPins[i], INPUT);
|
90 | digitalWrite(keypadPins[i], HIGH);
|
91 | }
|
92 |
|
93 | for (int row=0; row<4; row++)
|
94 | { // initial for loop to check all 4 rows
|
95 | pinMode(rowPins[row], OUTPUT); // set the row pin as an output
|
96 | digitalWrite(rowPins[row], LOW); // pull the row pins low
|
97 | for (int col=0; col<3; col++)
|
98 | { // embedded for loop to check all 3 columns of each row
|
99 | if (!digitalRead(columnPins[col]))
|
100 | {
|
101 | keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4); // set the status bit of the keypad return value
|
102 | }
|
103 | }
|
104 | pinMode(rowPins[row], INPUT); // reset the row pin as an input
|
105 | digitalWrite(rowPins[row], HIGH); // pull the row pin high
|
106 | }
|
107 |
|
108 | return keypadStatus;
|
109 | }
|