diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/MorseKeyboard_CircuitPlaygroundExpress.ino b/MorseKeyboard_CircuitPlaygroundExpress.ino new file mode 100644 index 0000000..9548c79 --- /dev/null +++ b/MorseKeyboard_CircuitPlaygroundExpress.ino @@ -0,0 +1,38 @@ +#include +#include "morseKeyboard.h" + +int buzzerPin = A0; +int debounceTime = 400; // prepare for noisy buttons +int ditPin = A2; +int dahPin = A3; +int ditSound = 880; +int dahSound = 500; +int ditSoundDuration = 500; +int dahSoundDuration = 550; +int speedTyper = 400; // time for key repeats +int speedSense = 3; +bool activeButtonValue = LOW; + +char ditKey = 46; +char dahKey = 45; +char altDitKey = 204; +char altDahKey = 205; + +KeyboardKey shortKey(ditPin, ditKey, altDitKey, debounceTime, buzzerPin, ditSound, ditSoundDuration, activeButtonValue); +KeyboardKey longKey(dahPin, dahKey, altDahKey, debounceTime, buzzerPin, dahSound, dahSoundDuration, activeButtonValue); +//AccessKey accessButton(accessPin, debounceTime, activeButtonValue); + +void setup() { + // put your setup code here, to run once: + Serial.begin(9600); + Keyboard.begin(); +} + +void loop() { + + //bool shiftValue = bleAccessButton.Check(); + bool shiftValue = false; + + shortKey.Press(shiftValue, speedTyper); + longKey.Press(shiftValue, speedTyper); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..d6a057b --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# MorseKeyboard_CircuitPlaygroundExpress diff --git a/morseKeyboard.h b/morseKeyboard.h new file mode 100644 index 0000000..2c06be7 --- /dev/null +++ b/morseKeyboard.h @@ -0,0 +1,163 @@ +class KeyboardKey { + int keyPin; // the number of the button pin + char modifierValue; + char keyValue; + char letterValue; + char altLetterValue; + int pressSpeed; + int debounceDelay; + int freq; + int sndLength; + int sndPin; + bool buttonTriggerState; + + bool keyFirstPress = true; + + unsigned long lastDebounceTime = 0; + + public: + KeyboardKey (int pin, char primaryKeyValue, char secondaryKeyValue, int debouncerTime, int speakerPin, int freqValue, int sndDuration, bool buttonValueState) + { + keyPin = pin; + letterValue = primaryKeyValue; + altLetterValue = secondaryKeyValue; + keyValue = letterValue; + debounceDelay = debouncerTime; + freq = freqValue; + sndLength = sndDuration; + sndPin = speakerPin; + buttonTriggerState = buttonValueState; + int resistorType; + + if (buttonTriggerState == LOW){ + resistorType = INPUT_PULLUP; + } + else{ + resistorType = INPUT_PULLDOWN; + } + + pinMode(keyPin, resistorType); + } + + void Press(boolean isShiftPressed, int ditdahTimer) + { + int keyState = digitalRead(keyPin); + bool shiftCheck = isShiftPressed; + int playLength = sndLength - pressSpeed; + pressSpeed = ditdahTimer; + + if (isShiftPressed) { + keyValue = altLetterValue; + } + else { + keyValue = letterValue; + } + + if (keyState == buttonTriggerState) { + if (millis() - lastDebounceTime > debounceDelay) { + if (keyFirstPress == true) { + tone(sndPin, freq, playLength / 3); + Keyboard.press(keyValue); + Keyboard.release(keyValue); + keyFirstPress = false; + lastDebounceTime = millis(); + } + while (digitalRead(keyPin) == buttonTriggerState && millis() - lastDebounceTime > debounceDelay) { + if (millis() - lastDebounceTime > pressSpeed) { + tone(sndPin, freq, playLength / 3); + Keyboard.press(keyValue); + Keyboard.release(keyValue); + lastDebounceTime = millis(); + } + } + lastDebounceTime = millis(); + } + } + else { + keyFirstPress = true; + } + } +}; + +class AccessKey { + int keyPin; // the number of the button pin + char keyValue; + int debounceDelay; + bool buttonTriggerState; + + unsigned long lastDebounceTime = 0; + + bool lastButtonState = HIGH; + bool buttonState; + + bool modifierState = false; + + public: + AccessKey (int pin, int debouncerTime, bool buttonStateValue) + { + keyPin = pin; + debounceDelay = debouncerTime; + buttonTriggerState = buttonStateValue; + + int resistorValue; + + if (buttonTriggerState == LOW){ + resistorValue = INPUT_PULLUP; + } + else{ + resistorValue = INPUT_PULLDOWN; + } + pinMode(keyPin, INPUT_PULLDOWN); + } + + void Press() + { + int keyState = digitalRead(keyPin); + + if (keyState == buttonTriggerState) { + if (millis() - lastDebounceTime > debounceDelay) { + Keyboard.press(131); + Keyboard.press(176); + Keyboard.releaseAll(); + Keyboard.press(131); + Keyboard.press(176); + Keyboard.releaseAll(); + Keyboard.press(131); + Keyboard.press(176); + Keyboard.releaseAll(); + lastDebounceTime = millis(); + } + lastDebounceTime = millis(); + } + } + + boolean Check() + { + int keyState = digitalRead(keyPin); + + if (keyState != lastButtonState) { + lastDebounceTime = millis(); + } + + if ((millis() - lastDebounceTime) > debounceDelay) { + if (keyState != buttonState) { + buttonState = keyState; + + if (buttonState == HIGH) { + modifierState = !modifierState; + Keyboard.press(131); + Keyboard.press(176); + Keyboard.releaseAll(); + Keyboard.press(131); + Keyboard.press(176); + Keyboard.releaseAll(); + Keyboard.press(131); + Keyboard.press(176); + Keyboard.releaseAll(); + } + } + } + lastButtonState = keyState; + return modifierState; + } +};