From 6cdcceefddd2fef28bd1bd4174b2edc22925bd1a Mon Sep 17 00:00:00 2001 From: Dejvino Date: Thu, 30 Mar 2023 06:23:06 +0200 Subject: [PATCH] Key typing over serial --- terminal_keyboard_emulator.ino | 51 +++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/terminal_keyboard_emulator.ino b/terminal_keyboard_emulator.ino index c91d393..48230b0 100644 --- a/terminal_keyboard_emulator.ino +++ b/terminal_keyboard_emulator.ino @@ -10,6 +10,7 @@ const int timerDelay = 500; void setup(void) { pinMode(pinData, OUTPUT); + digitalWrite(pinData, HIGH); Timer1.initialize(timerDelay); Timer1.attachInterrupt(clockCycle); @@ -28,22 +29,38 @@ volatile int dataState = 0; volatile int dataDelay = 0; volatile int packetDelay = 0; volatile int packetTail = 0; -volatile int clkState = LOW; +volatile int clkState = HIGH; -void statusCycle() { - long timeNow = millis(); - long changeDiff = timeNow - lastChange; - lastChange = timeNow; - if (changeDiff >= 10) { - dataWord = (0 + x); +volatile bool nextKeyReady = false; +volatile byte nextKey = 0; + +void typeKey(byte key) { + noInterrupts(); + nextKeyReady = true; + nextKey = key; + interrupts(); +} + +void sendKey(byte key) { + noInterrupts(); + dataWord = key; dataState = 8; dataDelay = 0; packetDelay = 0; - packetTail = 100; - x = (x + 1) % 8; + packetTail = 10; clkState = HIGH; + interrupts(); Timer1.initialize(timerDelay); Timer1.start(); +} + +void statusCycle() { + long timeNow = millis(); + long changeDiff = timeNow - lastChange; + lastChange = timeNow; + if (changeDiff >= 10 && nextKeyReady) { + sendKey(nextKey); + nextKeyReady = false; } } @@ -75,11 +92,13 @@ void clockCycle(void) void loop(void) { - delay(500); - - /*noInterrupts(); - dataWord = 0b1000000011 | (('A' + x) << 2); - dataState = 16; - x = (x + 1) % 30; - interrupts();*/ + delay(50); + + if (Serial.available() > 0) { + long key = Serial.parseInt(SKIP_ALL); + if (key != 0) { + typeKey(key); + } + } } +