More reliable typing via synchronization to host clock

This commit is contained in:
Dejvino 2023-04-02 10:00:55 +02:00
parent 132552ab87
commit 1c5da6906a

View File

@ -12,8 +12,8 @@ const int dataPin = 3; // in, kbd data
const int outPin = 4; // out, kbd led const int outPin = 4; // out, kbd led
// constant config // constant config
const int slaveClockDivider = 4; const int slaveClockDivider = 8;
const int timerDelay = 528 / slaveClockDivider; const int timerDelay = 530 / slaveClockDivider;
// variables // variables
volatile int slaveClockStep = 0; volatile int slaveClockStep = 0;
@ -45,7 +45,7 @@ volatile byte nextKey = 0;
void typeKey(byte key) { void typeKey(byte key) {
nextKey = key; nextKey = key;
nextKeyReady = true; nextKeyReady = true;
Serial.print("Typing key "); Serial.println((int) key); //Serial.print("Typing key "); Serial.println((int) key);
} }
void sendKey(byte key) { void sendKey(byte key) {
@ -54,9 +54,7 @@ void sendKey(byte key) {
dataDelay = 0; dataDelay = 0;
packetDelay = 0; packetDelay = 0;
packetTail = 15; packetTail = 15;
Serial.print("Sending key "); Serial.println((int) key); //Serial.print("Sending key "); Serial.println((int) key);
//Timer1.initialize(timerDelay);
//Timer1.start();
} }
void onHostStatusChange() { void onHostStatusChange() {
@ -65,8 +63,9 @@ void onHostStatusChange() {
lastChange = timeNow; lastChange = timeNow;
if (changeDiff >= 10 && nextKeyReady) { if (changeDiff >= 10 && nextKeyReady) {
nextKeyReady = false; nextKeyReady = false;
Serial.println("Status change with key");
sendKey(nextKey); sendKey(nextKey);
Timer1.start(); // synchronize with the host
slaveClockStep = 0;
} }
} }
@ -86,7 +85,6 @@ void onHostClockCycle(void)
packetTail--; packetTail--;
dataBit = LOW; dataBit = LOW;
} else { } else {
//Timer1.stop();
} }
digitalWrite(pinData, dataBit); digitalWrite(pinData, dataBit);
} }
@ -145,10 +143,11 @@ void setupKeyMapping() {
} }
char translateKeyToChar(int key) { char translateKeyToChar(int key) {
return key; //m[key];
if (sizeof(m) <= key) { if (sizeof(m) <= key) {
return 0; return 0;
} }
return key; //m[key]; return m[key];
} }
void printChar(char keyChar) { void printChar(char keyChar) {
@ -183,10 +182,10 @@ void onTimerInterrupt()
{ {
onSlaveClockInterrupt(); onSlaveClockInterrupt();
slaveClockStep = (slaveClockStep + 1) % slaveClockDivider;
if (slaveClockStep == 0) { if (slaveClockStep == 0) {
onHostClockCycle(); onHostClockCycle();
} }
slaveClockStep = (slaveClockStep + 1) % slaveClockDivider;
} }
// ---- // ----
@ -211,7 +210,6 @@ void setup(void)
Timer1.initialize(timerDelay); Timer1.initialize(timerDelay);
Timer1.attachInterrupt(onTimerInterrupt); Timer1.attachInterrupt(onTimerInterrupt);
//Timer1.stop();
Serial.println("Keyboard ready"); Serial.println("Keyboard ready");
} }
@ -225,11 +223,19 @@ void loop(void)
typeKey(key); typeKey(key);
} }
} }
/**/
// type key from keyboard // type key from keyboard
if (counter >= numbits) { if (counter >= numbits) {
processKbdByte(data); processKbdByte(data);
data = B0; data = B0;
counter = 0; counter = 0;
} }
/**/
/*/ auto-type test
delay(500);
typeKey('B');
/**/
} }