From 1c5da6906a71669096b3f95c4d358b1f1c7e0f78 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Sun, 2 Apr 2023 10:00:55 +0200 Subject: [PATCH] More reliable typing via synchronization to host clock --- terminal_keyboard_emulator.ino | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/terminal_keyboard_emulator.ino b/terminal_keyboard_emulator.ino index 4833ecc..45d304a 100644 --- a/terminal_keyboard_emulator.ino +++ b/terminal_keyboard_emulator.ino @@ -12,8 +12,8 @@ const int dataPin = 3; // in, kbd data const int outPin = 4; // out, kbd led // constant config -const int slaveClockDivider = 4; -const int timerDelay = 528 / slaveClockDivider; +const int slaveClockDivider = 8; +const int timerDelay = 530 / slaveClockDivider; // variables volatile int slaveClockStep = 0; @@ -45,7 +45,7 @@ volatile byte nextKey = 0; void typeKey(byte key) { nextKey = key; nextKeyReady = true; - Serial.print("Typing key "); Serial.println((int) key); + //Serial.print("Typing key "); Serial.println((int) key); } void sendKey(byte key) { @@ -54,9 +54,7 @@ void sendKey(byte key) { dataDelay = 0; packetDelay = 0; packetTail = 15; - Serial.print("Sending key "); Serial.println((int) key); - //Timer1.initialize(timerDelay); - //Timer1.start(); + //Serial.print("Sending key "); Serial.println((int) key); } void onHostStatusChange() { @@ -65,8 +63,9 @@ void onHostStatusChange() { lastChange = timeNow; if (changeDiff >= 10 && nextKeyReady) { nextKeyReady = false; - Serial.println("Status change with key"); sendKey(nextKey); + Timer1.start(); // synchronize with the host + slaveClockStep = 0; } } @@ -86,7 +85,6 @@ void onHostClockCycle(void) packetTail--; dataBit = LOW; } else { - //Timer1.stop(); } digitalWrite(pinData, dataBit); } @@ -145,10 +143,11 @@ void setupKeyMapping() { } char translateKeyToChar(int key) { + return key; //m[key]; if (sizeof(m) <= key) { return 0; } - return key; //m[key]; + return m[key]; } void printChar(char keyChar) { @@ -183,10 +182,10 @@ void onTimerInterrupt() { onSlaveClockInterrupt(); - slaveClockStep = (slaveClockStep + 1) % slaveClockDivider; if (slaveClockStep == 0) { onHostClockCycle(); } + slaveClockStep = (slaveClockStep + 1) % slaveClockDivider; } // ---- @@ -211,7 +210,6 @@ void setup(void) Timer1.initialize(timerDelay); Timer1.attachInterrupt(onTimerInterrupt); - //Timer1.stop(); Serial.println("Keyboard ready"); } @@ -225,11 +223,19 @@ void loop(void) typeKey(key); } } + /**/ + // type key from keyboard if (counter >= numbits) { processKbdByte(data); data = B0; counter = 0; } + /**/ + + /*/ auto-type test + delay(500); + typeKey('B'); + /**/ }