Created
September 12, 2021 16:06
-
-
Save yukilabo/f7b3c213f0210f04c98f2c478253257e to your computer and use it in GitHub Desktop.
DitDahChat用キーヤー
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 "DigiKeyboard.h" | |
// 短点は2番ピン、長点は0番ピンに接続する | |
#define DASH_PIN 0 | |
#define LED_PIN 1 | |
#define DOT_PIN 2 | |
// 値を小さくすると速くなる | |
#define DOT_LEN 100 | |
#define STATE_NONE 0 | |
#define STATE_DOT 1 | |
#define STATE_DASH 2 | |
#define KEY_UP_ARROW 0x52 | |
int state = 0; | |
bool intrDot = false; | |
bool intrDash = false; | |
unsigned long holdWhile = 0; | |
bool insertSpace = false; | |
void setup() | |
{ | |
pinMode(LED_PIN, OUTPUT); | |
pinMode(DOT_PIN, INPUT_PULLUP); | |
pinMode(DASH_PIN, INPUT_PULLUP); | |
DigiKeyboard.update(); | |
} | |
int getState(int privState, bool onDot, bool onDash, bool interDot, bool intrDash) | |
{ | |
// 長点時に短点キーが押されていたら短点 | |
if (privState == STATE_DASH && interDot) { | |
return STATE_DOT; | |
} | |
// 短点時に長点キーが押されていたら長点 | |
if (privState == STATE_DOT && intrDash) { | |
return STATE_DASH; | |
} | |
// 長点が押されている | |
if (onDash) { | |
return STATE_DASH; | |
} | |
// 短点が押されている | |
if (onDot) { | |
return STATE_DOT; | |
} | |
// 何も押されていない | |
return STATE_NONE; | |
} | |
void loop() | |
{ | |
bool onDot = !digitalRead(DOT_PIN); | |
bool onDash = !digitalRead(DASH_PIN); | |
intrDot = onDot || intrDot; | |
intrDash = onDash || intrDash; | |
if (millis() < holdWhile) { | |
return; | |
} | |
if (insertSpace) { | |
digitalWrite(LED_PIN, LOW); | |
DigiKeyboard.sendKeyPress(0, 0); | |
holdWhile = millis() + DOT_LEN; | |
insertSpace = false; | |
return; | |
} | |
state = getState(state, onDot, onDash, intrDot, intrDash); | |
if (state != STATE_NONE) { | |
digitalWrite(LED_PIN, HIGH); | |
DigiKeyboard.sendKeyPress(KEY_UP_ARROW, 0); | |
insertSpace = true; | |
holdWhile = millis() + DOT_LEN * (state == STATE_DOT ? 1 : 3); | |
} | |
// reset intruppt flag | |
intrDot = false; | |
intrDash = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment