2023-03-28 13:11:50 +00:00
|
|
|
#include <TimerOne.h>
|
|
|
|
|
2023-03-30 03:47:30 +00:00
|
|
|
// config:
|
2023-03-28 13:11:50 +00:00
|
|
|
const int pinData = 6;
|
|
|
|
const int pinStatus = 7;
|
|
|
|
|
2023-03-30 03:47:30 +00:00
|
|
|
// fixed values
|
2023-03-30 13:58:00 +00:00
|
|
|
const int timerDelay = 530;
|
2023-03-29 18:40:53 +00:00
|
|
|
|
2023-03-28 13:11:50 +00:00
|
|
|
void setup(void)
|
|
|
|
{
|
|
|
|
pinMode(pinData, OUTPUT);
|
2023-03-30 04:23:06 +00:00
|
|
|
digitalWrite(pinData, HIGH);
|
2023-03-30 03:47:30 +00:00
|
|
|
|
2023-03-29 18:40:53 +00:00
|
|
|
Timer1.initialize(timerDelay);
|
2023-03-28 13:11:50 +00:00
|
|
|
Timer1.attachInterrupt(clockCycle);
|
|
|
|
Timer1.stop();
|
|
|
|
|
|
|
|
pinMode(pinStatus, INPUT_PULLUP);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(pinStatus), statusCycle, CHANGE);
|
|
|
|
|
|
|
|
Serial.begin(9600);
|
|
|
|
}
|
|
|
|
|
|
|
|
volatile long lastChange = 0;
|
|
|
|
volatile int x = 0;
|
|
|
|
volatile int dataWord = 0;
|
|
|
|
volatile int dataState = 0;
|
|
|
|
volatile int dataDelay = 0;
|
2023-03-29 18:40:53 +00:00
|
|
|
volatile int packetDelay = 0;
|
|
|
|
volatile int packetTail = 0;
|
2023-03-28 13:11:50 +00:00
|
|
|
|
2023-03-30 04:23:06 +00:00
|
|
|
volatile bool nextKeyReady = false;
|
|
|
|
volatile byte nextKey = 0;
|
|
|
|
|
|
|
|
void typeKey(byte key) {
|
2023-03-30 13:58:00 +00:00
|
|
|
//noInterrupts();
|
2023-03-30 04:23:06 +00:00
|
|
|
nextKey = key;
|
2023-03-30 13:58:00 +00:00
|
|
|
nextKeyReady = true;
|
|
|
|
//interrupts();
|
2023-03-30 04:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sendKey(byte key) {
|
2023-03-30 13:58:00 +00:00
|
|
|
//noInterrupts();
|
2023-03-30 04:23:06 +00:00
|
|
|
dataWord = key;
|
2023-03-29 19:25:28 +00:00
|
|
|
dataState = 8;
|
2023-03-29 18:40:53 +00:00
|
|
|
dataDelay = 0;
|
|
|
|
packetDelay = 0;
|
2023-03-30 13:58:00 +00:00
|
|
|
packetTail = 15;
|
|
|
|
//interrupts();
|
2023-03-29 18:40:53 +00:00
|
|
|
Timer1.initialize(timerDelay);
|
2023-03-28 13:11:50 +00:00
|
|
|
Timer1.start();
|
2023-03-30 04:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void statusCycle() {
|
|
|
|
long timeNow = millis();
|
|
|
|
long changeDiff = timeNow - lastChange;
|
|
|
|
lastChange = timeNow;
|
|
|
|
if (changeDiff >= 10 && nextKeyReady) {
|
|
|
|
nextKeyReady = false;
|
2023-03-30 13:58:00 +00:00
|
|
|
sendKey(nextKey);
|
2023-03-28 13:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clockCycle(void)
|
|
|
|
{
|
2023-03-28 15:00:33 +00:00
|
|
|
int dataBit = HIGH;
|
2023-03-29 18:40:53 +00:00
|
|
|
if (packetDelay > 0) {
|
|
|
|
packetDelay--;
|
|
|
|
} else if (dataDelay > 0) {
|
2023-03-28 15:00:33 +00:00
|
|
|
dataDelay--;
|
2023-03-29 18:40:53 +00:00
|
|
|
dataBit = LOW;
|
2023-03-28 15:00:33 +00:00
|
|
|
} else if (dataState > 0) {
|
|
|
|
int bitToSend = (dataWord >> (dataState - 1)) & 1;
|
|
|
|
dataBit = !bitToSend ? LOW : HIGH;
|
|
|
|
dataState--;
|
2023-03-29 18:40:53 +00:00
|
|
|
} else if (packetTail > 0) {
|
|
|
|
packetTail--;
|
|
|
|
dataBit = LOW;
|
2023-03-28 15:00:33 +00:00
|
|
|
} else {
|
|
|
|
Timer1.stop();
|
|
|
|
}
|
|
|
|
digitalWrite(pinData, dataBit);
|
2023-03-28 13:11:50 +00:00
|
|
|
}
|
2023-03-30 13:58:00 +00:00
|
|
|
int xx = 0;
|
2023-03-28 13:11:50 +00:00
|
|
|
void loop(void)
|
|
|
|
{
|
2023-03-30 13:58:00 +00:00
|
|
|
delay(500);
|
|
|
|
typeKey(44 + xx);
|
|
|
|
xx = (xx+1) % 20;
|
2023-03-30 04:23:06 +00:00
|
|
|
|
2023-03-30 13:58:00 +00:00
|
|
|
/*if (!nextKeyReady && Serial.available() > 0) {
|
2023-03-30 04:23:06 +00:00
|
|
|
long key = Serial.parseInt(SKIP_ALL);
|
|
|
|
if (key != 0) {
|
|
|
|
typeKey(key);
|
|
|
|
}
|
2023-03-30 13:58:00 +00:00
|
|
|
}*/
|
2023-03-28 13:11:50 +00:00
|
|
|
}
|
2023-03-30 04:23:06 +00:00
|
|
|
|