From 829246c33252ccb7beb830cc8948a320218a3129 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Tue, 28 Mar 2023 15:11:50 +0200 Subject: [PATCH] Responding like crazy --- terminal_keyboard_emulator.ino | 85 ++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 terminal_keyboard_emulator.ino diff --git a/terminal_keyboard_emulator.ino b/terminal_keyboard_emulator.ino new file mode 100644 index 0000000..987123c --- /dev/null +++ b/terminal_keyboard_emulator.ino @@ -0,0 +1,85 @@ +#include + +const int pinData = 6; +const int pinStatus = 7; + +void setup(void) +{ + pinMode(pinData, OUTPUT); + //Timer1.initialize(60); + //Timer1.attachInterrupt(clockCycle); + + Timer1.initialize(1100/2); + Timer1.attachInterrupt(clockCycle); + Timer1.stop(); + + pinMode(pinStatus, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(pinStatus), statusCycle, CHANGE); + + Serial.begin(9600); +} + +volatile long lastChange = 0; +volatile long lastStart = 0; +volatile int x = 0; +volatile int dataWord = 0; +volatile int dataState = 0; +volatile int dataDelay = 0; +volatile int clkState = LOW; + +void statusCycle() { + long timeNow = millis(); + long changeDiff = timeNow - lastChange; + lastChange = timeNow; + if (changeDiff >= 10) { + lastStart = timeNow; + + dataWord = (' ' + x); + dataState = 8; + dataDelay = 0; + x = (x + 1) % 100; + clkState = HIGH; + Timer1.start(); + } + if (timeNow - lastStart >= 7 && changeDiff >= 2) { + //dataWord = 0b1000000011 | (('A' + x) << 2); + + } +} + +void clockCycle(void) +{ + if (clkState == LOW) { + clkState = HIGH; + //digitalWrite(pinData, (dataState > 0) ? LOW : HIGH); + } else { + clkState = LOW; + if (dataDelay > 0) { + dataDelay--; + } else if (dataState > 0) { + int bitToSend = (dataWord >> (dataState - 1)) & 1; + //digitalWrite(pinData, bitToSend ? LOW : HIGH); + dataState--; + if (dataState <= 0) { + Timer1.stop(); + } + } else { + //digitalWrite(pinData, HIGH); + } + } + if (dataDelay <= 0 && dataState > 0) + digitalWrite(pinData, clkState); + else + digitalWrite(pinData, HIGH); +} + +void loop(void) +{ + delay(500); + + /*noInterrupts(); + dataWord = 0b1000000011 | (('A' + x) << 2); + dataState = 16; + x = (x + 1) % 30; + interrupts();*/ +}