consul-vdx-term-keyboard-co.../terminal_keyboard_emulator.ino

83 lines
1.6 KiB
Arduino
Raw Normal View History

2023-03-28 13:11:50 +00:00
#include <TimerOne.h>
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;
2023-03-28 18:01:06 +00:00
dataWord = ('1' + x) << 3;
2023-03-28 15:00:33 +00:00
dataState = 10;
dataDelay = 1;
x = (x + 1) % 3;
2023-03-28 13:11:50 +00:00
clkState = HIGH;
Timer1.start();
}
if (timeNow - lastStart >= 7 && changeDiff >= 2) {
//dataWord = 0b1000000011 | (('A' + x) << 2);
}
}
void clockCycle(void)
{
2023-03-28 15:00:33 +00:00
int dataBit = HIGH;
2023-03-28 13:11:50 +00:00
if (clkState == LOW) {
clkState = HIGH;
} else {
clkState = LOW;
}
2023-03-28 15:00:33 +00:00
if (dataDelay > 0) {
dataDelay--;
} else if (dataState > 0) {
int bitToSend = (dataWord >> (dataState - 1)) & 1;
//int bitToSend = dataWord & 1;
//dataWord = dataWord >> 1;
dataBit = !bitToSend ? LOW : HIGH;
dataState--;
} else {
//digitalWrite(pinData, HIGH);
Timer1.stop();
}
digitalWrite(pinData, dataBit);
2023-03-28 13:11:50 +00:00
}
void loop(void)
{
delay(500);
/*noInterrupts();
dataWord = 0b1000000011 | (('A' + x) << 2);
dataState = 16;
x = (x + 1) % 30;
interrupts();*/
}