const int dataPin = 9; /* DS */
const int clockPin = 7; /* SHCP */
const int latchPin = 8; /* STCP */
uint8_t *scan[] = {A5, A4, A3, A2, A1, A0, 13, 12, 11,10};
uint8_t data[10] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uint8_t pos[2];
char *d[] = {"1 ","2 ","3 ","4 ","5 ","6 ","7 ","8 ","9 ","0 "};
int8_t power[] = {128,64,32,16,8,4,2,1};
String readString;
void clear() {
for (int i = 0; i < 10; i++) {
data[i] = 0;
}
}
void printBin(byte aByte) {
for (int8_t aBit = 7; aBit >= 0; aBit--)
Serial.write(bitRead(aByte, aBit) ? '1' : '0');
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Keyboard Emulation");
for (int i = 0; i < 8; i++) {
pinMode(scan[i], INPUT);
}
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
uint8_t pattern = 0;
while (Serial.available()) {
delay(3); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
clear();
readString.toUpperCase();
Serial.println(readString); //see what was received
pos[0] = 255;
pos[1] = 255;
char *str = readString.c_str();
for(int i =0; i < strlen(str); i++ ) {
char c = str[i];
switch(c) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
pos[1] = c - 'A';
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
pos[0] = c - '1';
break;
case '0':
pos[0] = 9;
break;
}
if (pos[0] != 255 && pos[1] != 255) {
Serial.print(pos[0], DEC);
Serial.print(" - ");
Serial.println(pos[1], DEC);
data[pos[0]] = data[pos[0]] | power[pos[1]];
pos[0] = pos[1] = 255;
}
}
Serial.println(" ABCDEFGH");
for (int i = 0; i < 10; i++ ) {
Serial.print(d[i]);
printBin(data[i]);
Serial.println("");
}
readString = "";
}
int button = 0;
for (int i = 0; i < 10; i++) {
int buttonState = digitalRead(scan[i]);
pattern = 255;
if (buttonState == LOW) {
pattern = ~data[i];
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, pattern);
digitalWrite(latchPin, HIGH);
button = 1;
}
}
if (!button) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, pattern);
digitalWrite(latchPin, HIGH);
}
}