Skip to content

Commit

Permalink
breaking: new 6-key layout!
Browse files Browse the repository at this point in the history
  • Loading branch information
centerorbit committed Dec 27, 2023
1 parent 559e251 commit ef78b85
Showing 1 changed file with 109 additions and 52 deletions.
161 changes: 109 additions & 52 deletions drivekeys.ino
Original file line number Diff line number Diff line change
@@ -1,52 +1,109 @@
#include <Arduino.h>
#include <BleKeyboard.h>

#define BUTTON1 26
#define BUTTON2 25
#define BUTTON3 13

BleKeyboard bleKeyboard(
"DriveKeys",
"ShipLift LLC",
100
);

int button1State = HIGH;
int button2State = HIGH;
int button3State = HIGH;

void setup() {
bleKeyboard.begin();
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
pinMode(BUTTON3, INPUT_PULLUP);
}

void stateChecker(int button, int *state, uint8_t key){
int currentButton1State = digitalRead(button);
if (currentButton1State != *state)
{
if (currentButton1State == LOW)
{
bleKeyboard.press(key);
}
else
{
bleKeyboard.release(key);
}
}
*state = currentButton1State;
}

void loop() {
if(bleKeyboard.isConnected()) {
stateChecker(BUTTON1, &button1State, KEY_LEFT_CTRL); // SW
stateChecker(BUTTON2, &button2State, KEY_LEFT_ALT); // SE
stateChecker(BUTTON3, &button3State, KEY_LEFT_SHIFT); // NE

// Or, actual keys (for debugging etc)
// stateChecker(BUTTON1, &button1State, 'a'); // SW
// stateChecker(BUTTON2, &button2State, 'b'); // SE
// stateChecker(BUTTON3, &button3State, 'c'); // NE
}
}

#include "Keyboard.h"


/**
* DriveKey Physical Layout
*
* +---------------------+
* | A | B | C | |
* | D | E | |
* | F | OOOO |
* |---+ OOOOOO |
* | OOOOOOOO |
* | OOOOOO |
* | OOOO |
* |...
*
**/

// Aesthetics
#define TAP_MS 200 // When a tap becomes a hold in milliseconds

// MAX_KEYS must be larger than the highest IO pin on the board used for keymapping
// In this case it's digital pin 13. 20 > 13, so MAX_KEYS of 20 is fine.
// 13 would also be fine, but this memory is cheap, and this project simple.

#define MAX_KEYS 20
static char KEY_MAP[MAX_KEYS];
static bool STATE_MAP[MAX_KEYS];

// Maps the diagram from above to the physical/electrically wired pins.
// This names will be used extensively throughtout this code. Know them.
#define KEY_A 13
#define KEY_B 12
#define KEY_C 11
#define KEY_D 10
#define KEY_E 9
#define KEY_F 8

void setup() {
// Assign each key to produce a character output.
KEY_MAP[KEY_A] = 'a';
KEY_MAP[KEY_B] = 'b';
KEY_MAP[KEY_C] = 'c';
KEY_MAP[KEY_D] = 'd';
KEY_MAP[KEY_E] = 'e';
KEY_MAP[KEY_F] = 'f';

// Set the initial state of all keys
// This map will be used to:
// * Know when a key is presses
// * Track when it is released
// * Know if it's being held
// * Know if combos are being pressed
// * Know the current state of any one key
// * And I think that's it.
//

STATE_MAP[KEY_A] = HIGH;
STATE_MAP[KEY_B] = HIGH;
STATE_MAP[KEY_C] = HIGH;
STATE_MAP[KEY_D] = HIGH;
STATE_MAP[KEY_E] = HIGH;
STATE_MAP[KEY_F] = HIGH;

// Since the design has no resistors, we use INPUT_PULLUP for us, HIGH means no-pressing
pinMode(KEY_A, INPUT_PULLUP);
pinMode(KEY_B, INPUT_PULLUP);
pinMode(KEY_C, INPUT_PULLUP);
pinMode(KEY_D, INPUT_PULLUP);
pinMode(KEY_E, INPUT_PULLUP);
pinMode(KEY_F, INPUT_PULLUP);



// Keyboard.begin();
Serial.begin(9600);
delay(5000);
Serial.println("Begin!");
}

void stateCheck(int incomingKey, char* output) {
bool incoming = digitalRead(incomingKey);

if (incoming == STATE_MAP[incomingKey]) {
return;
}

strcat(output, &KEY_MAP[incomingKey]);

STATE_MAP[incomingKey] = incoming;
}

void loop() {
char output[12] = {'\0'};
stateCheck(KEY_A, output);
stateCheck(KEY_B, output);
stateCheck(KEY_C, output);
stateCheck(KEY_D, output);
stateCheck(KEY_E, output);
stateCheck(KEY_F, output);

Serial.println(output);
delay(TAP_MS);

// Keyboard.print("You pressed the button ");
// Keyboard.print(counter);
// Keyboard.println(" times.");
}

0 comments on commit ef78b85

Please sign in to comment.