Skip to content

Instantly share code, notes, and snippets.

@entozoon
Created February 22, 2022 15:09
Show Gist options
  • Save entozoon/126d04db59dbc73be628dda20bad7b71 to your computer and use it in GitHub Desktop.
Save entozoon/126d04db59dbc73be628dda20bad7b71 to your computer and use it in GitHub Desktop.
#include "DigiKeyboard.h" // These two are modified from the originals to get the caps lock functionality.
#include "hidkeys.h"
int myDebugLevel = 0;
int delayLevel = 250;
//-------------------------------------------------------------
void setup() {
pinMode(1, OUTPUT); // BuiltIn LED as output
DigiKeyboard.sendKeyStroke(
0); // Evidently this helps at the start of digispark sketches.
// Check the state of Caps Lock and change led accordingly
if (DigiKeyboard.getLEDs() & CAPS_LOCK)
digitalWrite(1, HIGH);
else
digitalWrite(1, LOW);
DigiKeyboard.delay(10);
if (myDebugLevel == 1) {
DigiKeyboard.println("Setup complete.");
}
}
//-------------------------------------------------------------
void loop() {
if (myDebugLevel == 1) {
DigiKeyboard.println("Loop began.");
}
bool capsLockState = (DigiKeyboard.getLEDs() & CAPS_LOCK);
// Give it time to detect a change.
DigiKeyboard.delay(delayLevel);
bool thisCapsLockState = (DigiKeyboard.getLEDs() & CAPS_LOCK);
int clickCount = 0;
DigiKeyboard.delay(10);
if (thisCapsLockState != capsLockState) {
if (myDebugLevel == 1) {
DigiKeyboard.println("Detected change in Caps Lock.");
}
flash(3);
capsLockState = !capsLockState;
clickCount = capsCounter(0);
flash(2);
flash(2);
if (myDebugLevel == 2) {
DigiKeyboard.print("Click count: ");
DigiKeyboard.println(clickCount);
}
myMacros(clickCount);
}
}
//-------------------------------------------------------------
int capsCounter(int myCount) {
flash(3);
bool capsLockState = (DigiKeyboard.getLEDs() & CAPS_LOCK);
if (myDebugLevel == 1) {
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.print("My count is: ");
DigiKeyboard.println(myCount);
}
for (int k = 0; k <= 5; k++) {
DigiKeyboard.delay(delayLevel);
bool thisCapsLockState = (DigiKeyboard.getLEDs() & CAPS_LOCK);
if (thisCapsLockState != capsLockState) {
capsLockState = !capsLockState;
k = 10;
myCount = capsCounter(myCount + 1);
}
}
return myCount;
}
//-------------------------------------------------------------
void flash(int flashes) {
for (int m = 0; m <= flashes; m++) {
digitalWrite(1, HIGH);
DigiKeyboard.delay(delayLevel / 10);
digitalWrite(1, LOW);
DigiKeyboard.delay(delayLevel / 10);
}
if (DigiKeyboard.getLEDs() & CAPS_LOCK) {
digitalWrite(1, HIGH);
} else {
digitalWrite(1, LOW);
}
}
//-------------------------------------------------------------
void myMacros(int macroNumber) {
boolean capsLockInitialState;
if (digitalRead(1) == HIGH) {
capsLockInitialState = true;
} else {
capsLockInitialState = false;
}
switch (macroNumber) {
case 0:
// Accidental press. Exit.
break;
// COPY ALL INFO FROM EXISTING DISPATCH TO NOTEPAD
case 1:
// turnOffCapsLock();
copyInfoExistingOrder();
// if (capsLockInitialState == true){
// turnOnCapsLock();
// }
break;
// PASTE ALL INFO FROM NOTEPAD TO EXISTING DISPATCH
case 2:
pasteInfoExistingOrder();
break;
}
}
//-------------------------------------------------------------
void turnOffCapsLock() {
if (digitalRead(1) == HIGH) {
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.sendKeyStroke(KEY_CAPS_LOCK);
digitalWrite(1, LOW);
}
}
//-------------------------------------------------------------
void turnOnCapsLock() {
if (digitalRead(1) == LOW) {
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.sendKeyStroke(KEY_CAPS_LOCK);
digitalWrite(1, HIGH);
}
}
//-------------------------------------------------------------
void tabKey(int pressCount) {
int i;
for (i = 0; i <= pressCount; i++) {
DigiKeyboard.sendKeyStroke(KEY_TAB);
}
}
//-------------------------------------------------------------
void copyKey() { DigiKeyboard.sendKeyStroke(KEY_C, MOD_CONTROL_LEFT); }
//-------------------------------------------------------------
void altTab() { DigiKeyboard.sendKeyStroke(KEY_TAB, MOD_CONTROL_LEFT); }
//-------------------------------------------------------------
void pasteKey() { DigiKeyboard.sendKeyStroke(KEY_V, MOD_CONTROL_LEFT); }
//-------------------------------------------------------------
void selectAllKey() { DigiKeyboard.sendKeyStroke(KEY_A, MOD_CONTROL_LEFT); }
//-------------------------------------------------------------
void endKey() { DigiKeyboard.sendKeyStroke(KEY_END); }
//-------------------------------------------------------------
void copyFunction(int tabCount, int delayTime) {
tabKey(tabCount);
selectAllKey;
copyKey;
altTab;
pasteKey;
DigiKeyboard.sendKeyStroke(KEY_ENTER);
altTab;
DigiKeyboard.delay(delayTime);
}
//-------------------------------------------------------------
void pasteFunction(int tabCount, int delayTime) {
DigiKeyboard.sendKeyStroke(KEY_END);
DigiKeyboard.sendKeyStroke(KEY_HOME, KEY_L_SHIFT);
copyKey;
altTab;
tabKey(tabCount);
pasteKey;
altTab;
DigiKeyboard.sendKeyStroke(KEY_DOWN_ARROW);
DigiKeyboard.delay(delayTime);
}
//-------------------------------------------------------------
void openNotepad(int delayTime) {
DigiKeyboard.sendKeyStroke(KEY_R, MOD_GUI_LEFT);
DigiKeyboard.println("Notepad.exe");
DigiKeyboard.delay(delayTime);
}
//-------------------------------------------------------------
void copyInfoExistingOrder() {
openNotepad(1000);
altTab;
// DISPATCH NUMBER
copyFunction(0, 0);
// CUSTOMER ID
copyFunction(3, 0);
// PARENT ORDER
copyFunction(1, 0);
// JOB NAME
copyFunction(1, 0);
// ADDRESS
copyFunction(1, 0);
// PARENT ORDER COMMENT 3
copyFunction(1, 0);
// PO
copyFunction(1, 0);
// COMMENTS LINE 1
copyFunction(3, 0);
// COMMENTS LINE 2
copyFunction(1, 0);
// COMMENTS LINE 3
copyFunction(1, 0);
// COORDS, IF AVAILABLE
copyFunction(2, 0);
// CONTACT 1
copyFunction(8, 0);
// CONTACT 2
copyFunction(1, 0);
// CONTACT 3
copyFunction(2, 0);
// PRODUCT
copyFunction(1, 0);
// QUANTITY
copyFunction(2, 0);
// COMPLETE BY
copyFunction(1, 0);
// FREIGHT/FOB
copyFunction(1, 0);
// INTERNAL MEMO
copyFunction(1, 0);
// SPECIAL COMMENT
copyFunction(1, 0);
// S/O LAT
copyFunction(1, 0);
// S/O LONG
copyFunction(1, 0);
// START TIME
copyFunction(1, 0);
// LOADS PER HOUR
copyFunction(5, 0);
// LOAD SIZE
copyFunction(1, 0);
// MILES
copyFunction(6, 0);
}
//-------------------------------------------------------------
void pasteInfoExistingOrder() {
// WITH NOTEPAD OPEN, WITH THE INFO ABOVE OPEN IN THE FORMAT MADE ABOVE
// CLICK ANYWHERE ON THE FIRST LINE (DISPATCH #)
// DISPATCH #
pasteFunction(0, 0);
// CUSTOMER NUMBER
pasteFunction(3, 0);
// PARENT ORDER
pasteFunction(1, 0);
// PARENT ORDER COMMENT 1
pasteFunction(1, 0);
// PARENT ORDER COMMENT 2
pasteFunction(1, 0);
// PARENT ORDER COMMENT 3
pasteFunction(1, 0);
// PO
pasteFunction(1, 0);
// COMMENT 1
pasteFunction(3, 0);
// COMMENT 2
pasteFunction(1, 0);
// COMMENT 3
pasteFunction(1, 0);
// COORDS
pasteFunction(2, 0);
// CONTACT 1
pasteFunction(8, 0);
// CONTACT 2
pasteFunction(1, 0);
// CONTACT 3
pasteFunction(2, 0);
// PRODUCT
pasteFunction(1, 0);
// QTY/LOADS
pasteFunction(1, 0);
// COMPLETE BY
pasteFunction(1, 0);
// FREIGHT/FOB
pasteFunction(1, 0);
// INTERNAL MEMO
pasteFunction(1, 0);
// SPECIAL COMMENT
pasteFunction(1, 0);
// S/O LAT
pasteFunction(1, 0);
// S/O LONG
pasteFunction(1, 0);
// START TIME
pasteFunction(1, 0);
// LOADS PER HOUR
pasteFunction(5, 0);
// LOAD SIZE
pasteFunction(1, 0);
// MILEAGE
pasteFunction(6, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment