Last active
June 28, 2022 19:16
-
-
Save ragingcomputer/948ebbc036ab29822b0dfb5058ac6eb2 to your computer and use it in GitHub Desktop.
CNC Keypad / Custom USB Keyboard for LinuxCNC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Keyboard.h" | |
#include <Keypad.h> | |
const int KEYDELAY = 100; | |
const int SEQDELAY = 500; | |
const byte ROWS = 2; | |
const byte COLS = 4; | |
// Define the Keymap | |
/* | |
Cycle Start | Resume | Touch Off | Pause | |
Up | Down | Left | Right | |
*/ | |
char keys[ROWS][COLS] = { | |
{'1','2','3','4'}, | |
{'5','6','7','8'} | |
}; | |
byte rowPins[ROWS] = { 10, 16 }; | |
byte colPins[COLS] = { 6, 7, 8, 9 }; | |
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); | |
void setup() { | |
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad | |
} | |
void loop() { | |
char key = keypad.getKey(); | |
if(key) // Check for a valid key. | |
{ | |
switch (key) | |
{ | |
case '1': | |
// Cycle Start | |
Keyboard.begin(); | |
Keyboard.press('r'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
Keyboard.end(); | |
break; | |
case '2': | |
// Resume | |
Keyboard.begin(); | |
Keyboard.press('s'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
Keyboard.end(); | |
break; | |
case '3': | |
// Touch Off | |
Keyboard.begin(); | |
Keyboard.press('x'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(200); | |
Keyboard.press(KEY_END); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(SEQDELAY); | |
Keyboard.press('0'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(200); | |
Keyboard.press(KEY_RETURN); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(SEQDELAY); | |
Keyboard.press('y'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(200); | |
Keyboard.press(KEY_END); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(SEQDELAY); | |
Keyboard.press('0'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(200); | |
Keyboard.press(KEY_RETURN); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(SEQDELAY); | |
Keyboard.press('z'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(200); | |
Keyboard.press(KEY_END); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(SEQDELAY); | |
Keyboard.press('0'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(200); | |
Keyboard.press(KEY_RETURN); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
delay(SEQDELAY); | |
Keyboard.press('x'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
Keyboard.end(); | |
break; | |
case '4': | |
// Pause | |
Keyboard.begin(); | |
Keyboard.press('p'); | |
delay(KEYDELAY); | |
Keyboard.releaseAll(); | |
Keyboard.end(); | |
break; | |
} | |
} | |
} | |
// Taking care of some special events. | |
void keypadEvent(KeypadEvent key){ | |
switch (keypad.getState()){ | |
case PRESSED: | |
if (key == '5') { | |
Keyboard.begin(); | |
Keyboard.press(KEY_UP_ARROW); | |
} else if (key == '6') { | |
Keyboard.begin(); | |
Keyboard.press(KEY_DOWN_ARROW); | |
} else if (key == '7') { | |
Keyboard.begin(); | |
Keyboard.press(KEY_LEFT_ARROW); | |
} else if (key == '8') { | |
Keyboard.begin(); | |
Keyboard.press(KEY_RIGHT_ARROW); | |
} | |
break; | |
case RELEASED: | |
if (key == '5') { | |
Keyboard.releaseAll(); | |
Keyboard.end(); | |
} else if (key == '6') { | |
Keyboard.releaseAll(); | |
Keyboard.end(); | |
} else if (key == '7') { | |
Keyboard.releaseAll(); | |
Keyboard.end(); | |
} else if (key == '8') { | |
Keyboard.releaseAll(); | |
Keyboard.end(); | |
} | |
break; | |
case HOLD: | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment