|
1 |
| -#include <Arduino.h> |
2 |
| -#include <BleKeyboard.h> |
3 |
| - |
4 |
| -#define BUTTON1 26 |
5 |
| -#define BUTTON2 25 |
6 |
| -#define BUTTON3 13 |
7 |
| - |
8 |
| -BleKeyboard bleKeyboard( |
9 |
| - "DriveKeys", |
10 |
| - "ShipLift LLC", |
11 |
| - 100 |
12 |
| -); |
13 |
| - |
14 |
| -int button1State = HIGH; |
15 |
| -int button2State = HIGH; |
16 |
| -int button3State = HIGH; |
17 |
| - |
18 |
| -void setup() { |
19 |
| - bleKeyboard.begin(); |
20 |
| - pinMode(BUTTON1, INPUT_PULLUP); |
21 |
| - pinMode(BUTTON2, INPUT_PULLUP); |
22 |
| - pinMode(BUTTON3, INPUT_PULLUP); |
23 |
| -} |
24 |
| - |
25 |
| -void stateChecker(int button, int *state, uint8_t key){ |
26 |
| - int currentButton1State = digitalRead(button); |
27 |
| - if (currentButton1State != *state) |
28 |
| - { |
29 |
| - if (currentButton1State == LOW) |
30 |
| - { |
31 |
| - bleKeyboard.press(key); |
32 |
| - } |
33 |
| - else |
34 |
| - { |
35 |
| - bleKeyboard.release(key); |
36 |
| - } |
37 |
| - } |
38 |
| - *state = currentButton1State; |
39 |
| -} |
40 |
| - |
41 |
| -void loop() { |
42 |
| - if(bleKeyboard.isConnected()) { |
43 |
| - stateChecker(BUTTON1, &button1State, KEY_LEFT_CTRL); // SW |
44 |
| - stateChecker(BUTTON2, &button2State, KEY_LEFT_ALT); // SE |
45 |
| - stateChecker(BUTTON3, &button3State, KEY_LEFT_SHIFT); // NE |
46 |
| - |
47 |
| - // Or, actual keys (for debugging etc) |
48 |
| - // stateChecker(BUTTON1, &button1State, 'a'); // SW |
49 |
| - // stateChecker(BUTTON2, &button2State, 'b'); // SE |
50 |
| - // stateChecker(BUTTON3, &button3State, 'c'); // NE |
51 |
| - } |
52 |
| -} |
| 1 | + |
| 2 | +#include "Keyboard.h" |
| 3 | + |
| 4 | + |
| 5 | +/** |
| 6 | +* DriveKey Physical Layout |
| 7 | +* |
| 8 | +* +---------------------+ |
| 9 | +* | A | B | C | | |
| 10 | +* | D | E | | |
| 11 | +* | F | OOOO | |
| 12 | +* |---+ OOOOOO | |
| 13 | +* | OOOOOOOO | |
| 14 | +* | OOOOOO | |
| 15 | +* | OOOO | |
| 16 | +* |... |
| 17 | +* |
| 18 | +**/ |
| 19 | + |
| 20 | +// Aesthetics |
| 21 | +#define TAP_MS 200 // When a tap becomes a hold in milliseconds |
| 22 | + |
| 23 | +// MAX_KEYS must be larger than the highest IO pin on the board used for keymapping |
| 24 | +// In this case it's digital pin 13. 20 > 13, so MAX_KEYS of 20 is fine. |
| 25 | +// 13 would also be fine, but this memory is cheap, and this project simple. |
| 26 | + |
| 27 | +#define MAX_KEYS 20 |
| 28 | +static char KEY_MAP[MAX_KEYS]; |
| 29 | +static bool STATE_MAP[MAX_KEYS]; |
| 30 | + |
| 31 | +// Maps the diagram from above to the physical/electrically wired pins. |
| 32 | +// This names will be used extensively throughtout this code. Know them. |
| 33 | +#define KEY_A 13 |
| 34 | +#define KEY_B 12 |
| 35 | +#define KEY_C 11 |
| 36 | +#define KEY_D 10 |
| 37 | +#define KEY_E 9 |
| 38 | +#define KEY_F 8 |
| 39 | + |
| 40 | +void setup() { |
| 41 | + // Assign each key to produce a character output. |
| 42 | + KEY_MAP[KEY_A] = 'a'; |
| 43 | + KEY_MAP[KEY_B] = 'b'; |
| 44 | + KEY_MAP[KEY_C] = 'c'; |
| 45 | + KEY_MAP[KEY_D] = 'd'; |
| 46 | + KEY_MAP[KEY_E] = 'e'; |
| 47 | + KEY_MAP[KEY_F] = 'f'; |
| 48 | + |
| 49 | + // Set the initial state of all keys |
| 50 | + // This map will be used to: |
| 51 | + // * Know when a key is presses |
| 52 | + // * Track when it is released |
| 53 | + // * Know if it's being held |
| 54 | + // * Know if combos are being pressed |
| 55 | + // * Know the current state of any one key |
| 56 | + // * And I think that's it. |
| 57 | + // |
| 58 | + |
| 59 | + STATE_MAP[KEY_A] = HIGH; |
| 60 | + STATE_MAP[KEY_B] = HIGH; |
| 61 | + STATE_MAP[KEY_C] = HIGH; |
| 62 | + STATE_MAP[KEY_D] = HIGH; |
| 63 | + STATE_MAP[KEY_E] = HIGH; |
| 64 | + STATE_MAP[KEY_F] = HIGH; |
| 65 | + |
| 66 | + // Since the design has no resistors, we use INPUT_PULLUP for us, HIGH means no-pressing |
| 67 | + pinMode(KEY_A, INPUT_PULLUP); |
| 68 | + pinMode(KEY_B, INPUT_PULLUP); |
| 69 | + pinMode(KEY_C, INPUT_PULLUP); |
| 70 | + pinMode(KEY_D, INPUT_PULLUP); |
| 71 | + pinMode(KEY_E, INPUT_PULLUP); |
| 72 | + pinMode(KEY_F, INPUT_PULLUP); |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + // Keyboard.begin(); |
| 77 | + Serial.begin(9600); |
| 78 | + delay(5000); |
| 79 | + Serial.println("Begin!"); |
| 80 | +} |
| 81 | + |
| 82 | +void stateCheck(int incomingKey, char* output) { |
| 83 | + bool incoming = digitalRead(incomingKey); |
| 84 | + |
| 85 | + if (incoming == STATE_MAP[incomingKey]) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + strcat(output, &KEY_MAP[incomingKey]); |
| 90 | + |
| 91 | + STATE_MAP[incomingKey] = incoming; |
| 92 | +} |
| 93 | + |
| 94 | +void loop() { |
| 95 | + char output[12] = {'\0'}; |
| 96 | + stateCheck(KEY_A, output); |
| 97 | + stateCheck(KEY_B, output); |
| 98 | + stateCheck(KEY_C, output); |
| 99 | + stateCheck(KEY_D, output); |
| 100 | + stateCheck(KEY_E, output); |
| 101 | + stateCheck(KEY_F, output); |
| 102 | + |
| 103 | + Serial.println(output); |
| 104 | + delay(TAP_MS); |
| 105 | + |
| 106 | + // Keyboard.print("You pressed the button "); |
| 107 | + // Keyboard.print(counter); |
| 108 | + // Keyboard.println(" times."); |
| 109 | +} |
0 commit comments