View difference between Paste ID: 2q8Sh7Y6 and dwwGuvwe
SHOW: | | - or go back to the newest paste.
1
#include <Arduino.h>
2
#include <HardwareSerial.h>
3
4
#define systemRequest 84
5
#define modCode       96
6
#define altModCode    97
7
8
#define CLOCK_PIN  10
9
#define DATA_PIN   23
10
#define SWITCH_PIN 22
11
12
#define CLOCK digitalRead(CLOCK_PIN)
13
#define DATA  digitalRead(DATA_PIN)
14
15
void setup()
16
{
17
  Serial.begin(19200);
18
  Serial.println("Connected");
19
  pinMode(CLOCK_PIN, INPUT);
20
  pinMode(DATA_PIN, INPUT);
21
  pinMode(SWITCH_PIN, OUTPUT);
22
  digitalWrite(SWITCH_PIN, LOW);
23
  Keyboard.set_modifier(0);
24
  Keyboard.set_key1(0);
25
  Keyboard.set_key2(0);
26
  Keyboard.set_key3(0);
27
  Keyboard.set_key4(0);
28
  Keyboard.set_key5(0);
29
  Keyboard.set_key6(0);
30
  Keyboard.send_now();
31
  while (CLOCK == HIGH)
32
    ;
33
}
34
35
const int max_buffer   = 100;
36
int buffer[max_buffer] = {};
37
int buffer_index       = 0;
38
39
void clear_buffer()
40
{
41
  for (int i = 0; i < buffer_index; ++i)
42
  {
43
    Serial.println(buffer[i]);
44
    Serial.println(buffer[i]);
45
    Serial.println(buffer[i]);
46
  }
47
  buffer_index = 0;
48
}
49
50
void append_buffer(int value)
51
{
52
  buffer[buffer_index++] = value;
53
}
54
55
void loop()
56
{
57
  if (buffer_index + 10 >= max_buffer) clear_buffer();
58
59
  unsigned long t = micros();
60-
  while (CLOCK == HIGH && (micros() - t) < 200)
60+
  while (CLOCK == LOW && (micros() - t) < 200)
61
    ;
62-
  if (CLOCK == HIGH)
62+
  if (CLOCK == LOW)
63
  {
64
    append_buffer(-2);
65
    clear_buffer();
66
  }
67
  while (CLOCK == LOW)
68
    ;
69
  t = micros();
70-
  while (CLOCK == LOW && (micros() - t) < 60)
70+
  while (CLOCK == HIGH && (micros() - t) < 60)
71
    ;
72-
  if (CLOCK == LOW) append_buffer(2);
72+
  if (CLOCK == HIGH) append_buffer(2);
73
  while (CLOCK == HIGH)
74
    ;
75-
  append_buffer(DATA);
75+
  append_buffer(1 - DATA);
76
}
77