Sound piezo buzzer for straight key

This commit is contained in:
Neale Pickett 2022-04-21 22:07:39 -06:00
parent cb19012d1a
commit 8e77322461
1 changed files with 64 additions and 37 deletions

View File

@ -11,11 +11,13 @@
# define DIT_PIN 2 # define DIT_PIN 2
# define DAH_PIN 1 # define DAH_PIN 1
# define KEY_PIN 0 # define KEY_PIN 0
# define PIEZO 7
# define LED_ON false # define LED_ON false
#else #else
# define DIT_PIN 12 # define DIT_PIN 12
# define DAH_PIN 11 # define DAH_PIN 11
# define KEY_PIN 10 # define KEY_PIN 10
# define PIEZO 8
# define LED_ON true # define LED_ON true
#endif #endif
#define LED_OFF (!LED_ON) #define LED_OFF (!LED_ON)
@ -23,39 +25,61 @@
#define STRAIGHT_KEY ',' #define STRAIGHT_KEY ','
#define DIT_KEY KEY_LEFT_CTRL #define DIT_KEY KEY_LEFT_CTRL
#define DAH_KEY KEY_RIGHT_CTRL #define DAH_KEY KEY_RIGHT_CTRL
#define TONE 660
#define MILLISECOND 1
#define SECOND (1 * MILLISECOND)
bool iambic = true;
bool keyboard = true; bool keyboard = true;
bool trs = false; // true if a TRS plug is in a TRRS jack
uint16_t iambicDelay = 80 * MILLISECOND;
Bounce dit = Bounce(); Bounce dit = Bounce();
Bounce dah = Bounce(); Bounce dah = Bounce();
Bounce key = Bounce(); Bounce key = Bounce();
void setup() { void setup() {
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(PIEZO, OUTPUT);
dit.attach(DIT_PIN, INPUT_PULLUP); dit.attach(DIT_PIN, INPUT_PULLUP);
dah.attach(DAH_PIN, INPUT_PULLUP); dah.attach(DAH_PIN, INPUT_PULLUP);
key.attach(KEY_PIN, INPUT_PULLUP); key.attach(KEY_PIN, INPUT_PULLUP);
Keyboard.begin(); Keyboard.begin();
// Straight keys need to wire the dah pin to ground somehow. // To auto-sense a straight key in a TRRS jack,
// The easiest way I can think of to do this is to use a TS connector // we just check to see if DAH is closed.
// instead of a TRS connector. // The sleeve on the straight key's TRS plug
// will short the second ring to the sleeve.
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
delay(20); delay(20);
dah.update(); dah.update();
} }
if (dah.read() == LOW) { if (dah.read() == LOW) {
iambic = false; trs = true;
}
}
// A reentrant doodad to blink out the letter V at startup
// After startup, display the status of the keyboard
#define HELLO_BITS 0b0000101010111000
void setLED() {
static bool beepin = false;
int beat = millis() / iambicDelay;
bool on = keyboard; // If we're not in intro, display status of keyboard
if (beat < 16) {
on = HELLO_BITS & (1 << (15-beat));
if (on != beepin) {
if (on) {
tone(PIEZO, TONE);
} else { } else {
iambic = true; noTone(PIEZO);
}
beepin = on;
}
} }
// Blink out a V digitalWrite(LED_BUILTIN, on?LED_ON:LED_OFF);
for (int i = 0; i < 4; i++) {
digitalWrite(LED_BUILTIN, LED_ON); delay(80 + (i/3)*160);
digitalWrite(LED_BUILTIN, LED_OFF); delay(80 + (i/3)*160);
}
} }
void midiKey(bool down, uint8_t key) { void midiKey(bool down, uint8_t key) {
@ -66,54 +90,57 @@ void midiKey(bool down, uint8_t key) {
void midiProbe() { void midiProbe() {
midiEventPacket_t event = MidiUSB.read(); midiEventPacket_t event = MidiUSB.read();
if (event.byte1 == 0x80) {
// Key release, channel 0 uint16_t msg = (event.byte1 << 8) | (event.byte2 << 0);
if (event.byte2 == 0x00) { switch (msg) {
// C0 note case 0x8B00: // Controller 0: turn keyboard mode on/off
keyboard = false; keyboard = (event.byte3 > 0x3f);
} break;
case 0x8B01: // Controller 1: set iambic speed (0-254)
iambicDelay = event.byte3 << 1;
break;
} }
} }
void loop() { void loop() {
midiProbe(); midiProbe();
digitalWrite(LED_BUILTIN, keyboard?LED_ON:LED_OFF); setLED();
// Monitor straight key pin // Monitor straight key pin
if (key.update()) { if (key.update()) {
midiKey(key.fell(), 0); midiKey(key.fell(), 0);
if (keyboard) {
if (key.fell()) { if (key.fell()) {
tone(PIEZO, TONE);
if (keyboard) {
Keyboard.press(STRAIGHT_KEY); Keyboard.press(STRAIGHT_KEY);
}
} else { } else {
noTone(PIEZO);
if (keyboard) {
Keyboard.release(STRAIGHT_KEY); Keyboard.release(STRAIGHT_KEY);
} }
} }
} }
// Monitor dit pin, which could be straight key if dah was closed on boot // If we made dit = dah, we have a straight key on the dit pin,
if (dit.update()) { // so we skip iambic polling.
uint8_t kbdKey, mKey; if (trs) {
if (iambic) { return;
kbdKey = DIT_KEY;
mKey = 1;
} else {
kbdKey = STRAIGHT_KEY;
mKey = 0;
} }
midiKey(dit.fell(), mKey); if (dit.update()) {
midiKey(dit.fell(), 1);
if (keyboard) { if (keyboard) {
if (dit.fell()) { if (dit.fell()) {
Keyboard.press(kbdKey); Keyboard.press(DIT_KEY);
} else { } else {
Keyboard.release(kbdKey); Keyboard.release(DAH_KEY);
} }
} }
} }
// Monitor dah pin // Monitor dah pin
if (iambic && dah.update()) { if (dah.update()) {
midiKey(dah.fell(), 2); midiKey(dah.fell(), 2);
if (keyboard) { if (keyboard) {