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
// 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');
/**/
}