-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyboard44.ino
56 lines (52 loc) · 1.57 KB
/
keyboard44.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
#include "keyboard44.h"
//从左到右写针脚
keyboard44::keyboard44(const uint8_t pin1, const uint8_t pin2, const uint8_t pin3, const uint8_t pin4, const uint8_t pin5, const uint8_t pin6, const uint8_t pin7, const uint8_t pin8)
{
KBcolPins[0] = pin1;
KBcolPins[1] = pin2;
KBcolPins[2] = pin3;
KBcolPins[3] = pin4;
KBrowPins[0] = pin5;
KBrowPins[1] = pin6;
KBrowPins[2] = pin7;
KBrowPins[3] = pin8;
for (int i = 0; i < KBcol; i++) //列设为输出高电平
{
pinMode(KBcolPins[i], OUTPUT);
digitalWrite(KBcolPins[i], HIGH);
Serial.print(KBcolPins[i]);
Serial.println(" Setup finished"); //调试用语句
}
for (int i = 0; i < KBrow; i++) //行设为读取,开启上拉电阻(为了复位)
{
pinMode(KBrowPins[i], INPUT);
digitalWrite(KBrowPins[i], HIGH);
Serial.print(KBrowPins[i]);
Serial.println(" Setup finished"); //调试用语句
}
}
//从注意零开始算行列
void keyboard44::setKey(char key, char row, char col)
{
KBkey[row][col] = key;
}
char keyboard44::getKey()
{
char input = 0;
for (int i = 0; i < KBcol; i++)
{
digitalWrite(KBcolPins[i], LOW);
for (int j = 0; j < KBrow; j++)
{
if (digitalRead(KBrowPins[j]) == LOW)
{
delay(KBdebounce);
while (digitalRead(KBrowPins[j]) == LOW)
; //一个有风险的死循环
input = KBkey[i][j];
}
}
digitalWrite(KBcolPins[i], HIGH);
}
return input;
}