Skip to content

Instantly share code, notes, and snippets.

@ueman
Created July 26, 2018 16:59
Show Gist options
  • Save ueman/19a14569c07ba37ec557d0e7cf8f009c to your computer and use it in GitHub Desktop.
Save ueman/19a14569c07ba37ec557d0e7cf8f009c to your computer and use it in GitHub Desktop.
6key arduino macro pad
#include <HID-Project.h>
#include <HID-Settings.h>
/**
HIGH = 1
LOW = 0
*/
#define PRESSED 1
#define RELEASED 0
// pin[xx] on led matrix connected to nn on Arduino
int pins[6] = { 10, 18, 19, 20, 21 };
// col[xx] of leds = pin yy on led matrix
int cols[3] = { 19, 18, 10 };
// row[xx] of leds = pin yy on led matrix
int rows[2] = { 21, 20 };
//debounce in milliseconds
const int debounceTime = 5;
//Switch Pins
const byte buttonPin[6] = {4, 5, 6, 7, 8, 9};
//Switch status
boolean buttonStatus[6] = {0, 0, 0, 0, 0, 0};
int layer = 0;
// hier sind eigentlich die buttonpresses aus der keyActions-Datei gewesen
void setup()
{
Serial.begin(115200);
Keyboard.begin();
Consumer.begin();
//setup inputs, turn on pullups
for (int i = 0; i <= 5; i++)
{
pinMode(buttonPin[i], INPUT);
digitalWrite(buttonPin[i], HIGH);
}
setupLeds();
}
void loop()
{
CheckKeys();
refreshLights();
delay(debounceTime);
}
void CheckKeys()
{
for (int i = 0; i <= 5; i++)
{
if (digitalRead(buttonPin[i]) == LOW)
{
checkPressedKey(i);
}
else
{
checkReleasedKey(i);
}
}
}
void checkPressedKey(int i)
{
if (buttonStatus[i] == PRESSED)
{
return;
}
pressKey(i);
buttonStatus[i] = 1;
}
void checkReleasedKey(int i)
{
if (buttonStatus[i] == RELEASED)
{
return;
}
releaseKey(i);
buttonStatus[i] = 0;
}
void pressKey(int i)
{
if (layer == 1) {
i = i + 6;
}
switch (i) {
case 0: pressButton1(); break;
case 1: pressButton2(); break;
case 2: pressButton3(); break;
case 3: pressButton4(); break;
case 4: pressButton5(); break;
case 5: pressButton6(); break;
case 6: pressButton7(); break;
case 7: pressButton8(); break;
case 8: pressButton9(); break;
case 9: pressButton10(); break;
case 10: pressButton11(); break;
case 11: pressButton12(); break;
default: break;
}
}
void releaseKey(int i)
{
if (layer == 1) {
i = i + 6;
}
switch (i) {
case 0: releaseButton1(); break;
case 1: releaseButton2(); break;
case 2: releaseButton3(); break;
case 3: releaseButton4(); break;
case 4: releaseButton5(); break;
case 5: releaseButton6(); break;
case 6: releaseButton7(); break;
case 7: releaseButton8(); break;
case 8: releaseButton9(); break;
case 9: releaseButton10(); break;
case 10: releaseButton11(); break;
case 11: releaseButton12(); break;
default: break;
}
}
void changeLayer() {
if (layer == 0) {
layer = 1;
} else {
layer = 0;
}
}
void refreshLights() {
if (layer == 0) {
turnOnLeds();
TXLED1;
RXLED1;
} else {
turnOffLeds();
TXLED0;
RXLED0;
}
}
void setupLeds() {
for (int thisPin = 0; thisPin < 6; thisPin++) {
// initialize the output pins:
pinMode(pins[thisPin], OUTPUT);
digitalWrite(pins[thisPin], LOW);
}
/*
digitalWrite(cols[0], HIGH);
digitalWrite(cols[1], HIGH);
digitalWrite(rows[0], LOW);
digitalWrite(rows[1], HIGH);
*/
/*
for (int i = 0; i < 3; i++) {
digitalWrite(cols[i], HIGH);
}
for (int i = 0; i < 2; i++) {
digitalWrite(rows[i], LOW);
}
/*
for (int thisPin = 0; thisPin < 6; thisPin++) {
// initialize the output pins:
pinMode(pins[thisPin], OUTPUT);
// take the col pins (i.e. the cathodes) high to ensure that the LEDS are off:
digitalWrite(pins[thisPin], HIGH);
}
*/
}
void turnOnLed(int row, int col) {
digitalWrite(cols[0], HIGH);
digitalWrite(rows[0], LOW);
/*
digitalWrite(rows[row], HIGH);
digitalWrite(cols[col], LOW);
*/
}
void turnOffLed(int row, int col) {
digitalWrite(cols[0], LOW);
digitalWrite(rows[0], LOW);
/*
digitalWrite(rows[row], LOW);
digitalWrite(cols[col], LOW);
*/
}
void turnOnLeds() {
for (int i = 0; i < 3; i++) {
digitalWrite(cols[i], HIGH);
}
for (int i = 0; i < 2; i++) {
digitalWrite(rows[i], LOW);
}
}
void turnOffLeds() {
for (int i = 0; i < 2; i++) {
digitalWrite(rows[i], HIGH);
}
}
install this arduino lib
https://github.com/NicoHood/HID
void pressButton1() {
changeLayer();
}
void releaseButton1() {
}
void pressButton2() {
Consumer.press(MEDIA_VOLUME_MUTE);
}
void releaseButton2() {
Consumer.release(MEDIA_VOLUME_MUTE);
}
void pressButton3() {
Keyboard.press(KEY_ESC);
}
void releaseButton3() {
Keyboard.release(KEY_ESC);
}
void pressButton4() {
Consumer.press(MEDIA_VOLUME_DOWN);
}
void releaseButton4() {
Consumer.release(MEDIA_VOLUME_DOWN);
}
void pressButton5() {
Consumer.press(MEDIA_VOLUME_UP);
}
void releaseButton5() {
Consumer.release(MEDIA_VOLUME_UP);
}
void pressButton6() {
Consumer.press(CONSUMER_EXPLORER);
}
void releaseButton6() {
Consumer.release(CONSUMER_EXPLORER);
}
void pressButton7() {
pressButton1();
}
void releaseButton7() {
}
void pressButton8() {
Serial.print("1\n");
}
void releaseButton8() {}
void pressButton9() {
}
void releaseButton9() {}
void pressButton10() {
}
void releaseButton10() {}
void pressButton11() {
}
void releaseButton11() {}
void pressButton12() {
}
void releaseButton12() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment