-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadPins.ino
161 lines (156 loc) · 4.64 KB
/
ReadPins.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#define BUFFER_SIZE 100
// https://github.com/Locoduino/RingBuffer
#include <RingBuf.h>
RingBuf<byte, BUFFER_SIZE> data;
unsigned long overflowCount = 0;
unsigned long priorOverflowCount = 0;
enum { FOUR_BIT = 0, EIGHT_BIT = 1 } sizeMode = EIGHT_BIT;
enum { CONTROL = 0, DATA = 1 } dataMode = CONTROL;
void setup() {
delay(200); // https://forum.arduino.cc/index.php?topic=600452.0
Serial.begin(2000000);
Serial.println("Read pins 2-7 as RS, E, and 4-7 of 1602 LCD device");
// Uncommenting these lines causes subsequent Serial.print() calls to truncate output
// This indicates a significant limit to this approach
// Serial.println("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
// Serial.println("01234567890123456789012345678901234567890123456789012345678901234567890123456789");
for (int i = 2; i < 8; ++i) {
pinMode(i, INPUT_PULLUP);
}
// https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
attachInterrupt(digitalPinToInterrupt(3), pin3falling, FALLING);
}
byte getPins() {
byte pins = 0;
for (int i = 7; i >= 2; --i) {
pins = pins * 2 + digitalRead(i);
}
return pins;
}
void pin3falling() {
if (data.isFull()) {
++overflowCount;
} else {
data.lockedPush(getPins());
}
}
void loop() {
unsigned long newOverflowCount = overflowCount;
if (priorOverflowCount != newOverflowCount) {
priorOverflowCount = newOverflowCount;
Serial.print("Overflow: ");
Serial.println(newOverflowCount);
}
if ((sizeMode == EIGHT_BIT && !data.isEmpty()) || (data.size() >= 2)) {
bool isData;
byte value;
if (sizeMode == EIGHT_BIT) {
data.lockedPop(value);
isData = value & 0x1;
value = value >> 2 << 4;
} else {
byte temp1, temp2;
data.lockedPop(temp1);
data.lockedPop(temp2);
isData = temp1 & 0x1;
value = (temp1 >> 2 << 4) + (temp2 >> 2);
}
if (isData) {
if (dataMode == CONTROL) {
Serial.print('"');
dataMode = DATA;
}
char character = value;
Serial.print(character);
} else {
if (dataMode == DATA) {
Serial.println('"');
dataMode = CONTROL;
}
if (value & 0x80) { // set DDRAM address
Serial.print("DDRAM: 0x");
Serial.println(value & 0x7F, HEX);
} else if (value & 0x40) { // set CGRAM address
Serial.print("CGRAM: 0x");
Serial.println(value & 0x3F, HEX);
} else if (value & 0x20) { // function set
if (value & 0x10) {
sizeMode = EIGHT_BIT;
Serial.print('8');
} else {
if (sizeMode == EIGHT_BIT) {
while (data.isEmpty()) {
delay(1);
}
byte temp;
if (!data.lockedPop(temp)) {
Serial.println("MISSING DATA");
}
value = value + (temp >> 2);
}
sizeMode = FOUR_BIT;
Serial.print('4');
}
Serial.print("-bit; ");
if (value & 0x08) {
Serial.print("2 lines; ");
} else {
Serial.print("1 line; ");
}
if (value & 0x04) {
Serial.println("5x10");
} else {
Serial.println("5x8");
}
} else if (value & 0x10) { // cursor/display shift
if (value & 0x08) {
Serial.print("shift display; ");
} else {
Serial.print("move cursor; ");
}
Serial.print("shift ");
if (value & 0x04) {
Serial.println("right");
} else {
Serial.println("left");
}
} else if (value & 0x08) { // display on/off control
Serial.print("display ");
if (value & 0x04) {
Serial.print("on");
} else {
Serial.print("off");
}
Serial.print("; cursor ");
if (value & 0x02) {
Serial.print("on");
} else {
Serial.print("off");
}
Serial.print("; blink ");
if (value & 0x01) {
Serial.println("on");
} else {
Serial.println("off");
}
} else if (value & 0x04) { // entry mode set
if (value & 0x02) {
Serial.print("increment");
} else {
Serial.print("decrement");
}
Serial.print(" cursor position; ");
if (!(value & 0x01)) {
Serial.print("no ");
}
Serial.println("display shift");
} else if (value & 0x02) { // cursor home
Serial.println("cursor home");
} else if (value & 0x01) { // clear display
Serial.println("clear display");
} else {
Serial.println("invalid command (0x00)");
}
}
}
}