2018-08-10 10:13:45 +00:00
|
|
|
#include <Arduino.h>
|
2014-10-18 11:33:48 +00:00
|
|
|
#include "ps2drv.h"
|
|
|
|
|
2019-03-03 17:07:47 +00:00
|
|
|
#if !defined(KBD_BUFFER)
|
|
|
|
#define KBD_BUFFER 16
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static volatile uint8_t buffer[KBD_BUFFER];
|
2014-10-18 11:33:48 +00:00
|
|
|
static volatile uint8_t head, tail;
|
|
|
|
static uint8_t DataPin;
|
|
|
|
|
|
|
|
// The ISR for the external interrupt
|
2020-07-08 13:33:52 +00:00
|
|
|
#if defined(ESP32) || defined(ESP8266)
|
|
|
|
ICACHE_RAM_ATTR
|
|
|
|
#endif
|
2014-10-18 11:33:48 +00:00
|
|
|
void ps2interrupt(void)
|
|
|
|
{
|
2014-12-15 14:36:52 +00:00
|
|
|
static uint8_t bitcount=0;
|
|
|
|
static uint8_t incoming=0;
|
|
|
|
static uint32_t prev_ms=0;
|
|
|
|
uint32_t now_ms;
|
|
|
|
uint8_t n, val;
|
2014-10-18 11:33:48 +00:00
|
|
|
|
2014-12-15 14:36:52 +00:00
|
|
|
val = digitalRead(DataPin);
|
|
|
|
now_ms = millis();
|
|
|
|
if (now_ms - prev_ms > 250) {
|
|
|
|
bitcount = 0;
|
|
|
|
incoming = 0;
|
|
|
|
}
|
|
|
|
prev_ms = now_ms;
|
|
|
|
n = bitcount - 1;
|
|
|
|
if (n <= 7) {
|
|
|
|
incoming |= (val << n);
|
|
|
|
}
|
|
|
|
bitcount++;
|
|
|
|
if (bitcount == 11) {
|
|
|
|
uint8_t i = head + 1;
|
2019-03-03 17:07:47 +00:00
|
|
|
if (i == KBD_BUFFER) i = 0;
|
2014-12-15 14:36:52 +00:00
|
|
|
if (i != tail) {
|
|
|
|
buffer[i] = incoming;
|
|
|
|
head = i;
|
|
|
|
}
|
|
|
|
bitcount = 0;
|
|
|
|
incoming = 0;
|
|
|
|
}
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PS2Driver::available() {
|
2014-12-15 14:36:52 +00:00
|
|
|
if (head == tail)
|
|
|
|
return false;
|
2014-10-18 11:33:48 +00:00
|
|
|
|
2014-12-15 14:36:52 +00:00
|
|
|
uint8_t i = tail+1;
|
2019-03-03 17:07:47 +00:00
|
|
|
if (i == KBD_BUFFER) i = 0;
|
2014-12-15 14:36:52 +00:00
|
|
|
if (buffer[i] == 0xf0)
|
|
|
|
return i != head;
|
|
|
|
return true;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 14:36:52 +00:00
|
|
|
unsigned PS2Driver::read2() {
|
|
|
|
if (head == tail)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint8_t i = tail+1;
|
2019-03-03 17:07:47 +00:00
|
|
|
if (i == KBD_BUFFER) i = 0;
|
2014-12-15 14:36:52 +00:00
|
|
|
tail = i;
|
|
|
|
if (buffer[i] != 0xf0)
|
|
|
|
return buffer[i];
|
2014-12-16 17:16:56 +00:00
|
|
|
return 0xf000 | read2();
|
2014-12-15 14:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PS2Driver::peek() {
|
|
|
|
if (head == tail)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint8_t i = tail+1;
|
2019-03-03 17:07:47 +00:00
|
|
|
if (i == KBD_BUFFER) i = 0;
|
2014-12-15 14:36:52 +00:00
|
|
|
if (buffer[i] == 0xf0) {
|
2019-03-03 17:07:47 +00:00
|
|
|
if (++i == KBD_BUFFER) i = 0;
|
2014-12-16 17:16:56 +00:00
|
|
|
return 0xf000 | buffer[i];
|
2014-12-15 14:36:52 +00:00
|
|
|
}
|
|
|
|
return buffer[i];
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PS2Driver::begin(uint8_t data_pin, uint8_t irq_pin)
|
|
|
|
{
|
2014-12-15 14:36:52 +00:00
|
|
|
DataPin = data_pin;
|
|
|
|
pinMode(irq_pin, INPUT_PULLUP);
|
|
|
|
pinMode(data_pin, INPUT_PULLUP);
|
|
|
|
attachInterrupt(irq_pin, ps2interrupt, FALLING);
|
|
|
|
head = tail = 0;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|