-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard.c
153 lines (148 loc) · 4.63 KB
/
keyboard.c
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
#include "keyboard.h"
//#define KEYBOARD_DEBUG
#ifdef KEYBOARD_DEBUG
#include <stdio.h>
#endif
/**
* @breif: key_state transfer
* @param: sig: array for signal
* 0----press of the key(W/A/S/D)
* 1----in/not in high-speed-mode
* 2----swing or not
* 3----detecting whether G & C have same direction
* prevsig: array for the signal in the iteration before
* s: key-state
* x: current speed
* @reval
*/
void kb_stateTransfer(const keysignal_t *key, const keysignal_t *high, bool_t *keyState, int *speed)
{
if(keyState[0] == 0 && keyState[1] == 1)
{
//stop state
if(key->n) //P
{
if(high->n) //P & H
{
//10: fast speeding up, the speed is initialized
keyState[0] = 1;
keyState[1] = 0;
*speed += *speed == 0 ? INI_SPEED : 0;
}
else //P and ~H
{
if(*speed < LOW_SPEED) //speed is not as high as LOW_SPEED
{
//11: slowly speeding up
keyState[0] = 1;
keyState[1] = 1;
*speed += *speed == 0 ? INI_SPEED : 0;
}
else if(*speed == LOW_SPEED) //speed meets the LOW_SPEED
{
//00: holding the speed
keyState[0] = 0;
keyState[1] = 0;
}
//else: the speed is higher: stay in the stop mode so that the speed goed down
}
}
//else (~P): stay in the stop mode to maintain the speed to be 0
}
else if(keyState[0] == 1 && keyState[1] == 1)
{
//Slowly speeding up
if(!key->n) //~P
{
//01: stop state
keyState[0] = 0;
keyState[1] = 1;
}
else if(key->n && high->n) //P & H
{
//10: fast speeding up
keyState[0] = 1;
keyState[1] = 0;
}
else //P & ~H
{
if (*speed >= LOW_SPEED) //the speed is as high as LOW_SPEED
{
//00: holding the speed
keyState[0] = keyState[1] = 0;
}
//else: remain in this state
}
}
else if(keyState[0] == 1 && keyState[1] == 0)
{
//Fast speeding up
if (!key->n || !high->n) //~P or ~H
{
//01: stop state to slow down
keyState[0] = 0;
keyState[1] = 1;
}
else //P and H
{
if (*speed >= HIGH_SPEED) //the speed is as high as the HIGH_SPEED
{
//00: holding the speed
keyState[0] = 0;
keyState[1] = 0;
}
//else: remain in the same state to speed up
}
}
else
{
//s[0]==s[1]==0
//holding speed
if (key->n != key->p || high->n != high->p) //change: bakc to stop mode
{
keyState[0] = 0;
keyState[1] = 1;
}
//else: remain
}
#ifdef KEYBOARD_DEBUG
printf("INNER %d", *speed);
#endif
}
/**
* @breif: W/A/S/D control
* @param: signal: array for signal
* 0----press of the key(W/A/S/D)
* 1----in/not in high-speed-mode
* 2----swing or not
* 3----detecting whether G & C have same direction
* prevSig: array for the signal in the iteration before
* keyState: array for key-state
* speed: current speed
* @reval: the output speed
*/
int kb_keyControl(const keysignal_t *key, const keysignal_t *high, bool_t *keyState, int speed)
{
bool_t keySig = (bool_t)key->n;
bool_t keyPrev= (bool_t)key->p;
bool_t highSig= (bool_t)high->n;
bool_t highPrev=(bool_t)high->p;
kb_stateTransfer(key,high, keyState, &speed);
speed = ((1 + keyState[0] + keyState[0] + keyState[0]) * speed) >> keyState[1];
speed = speed > HIGH_SPEED ? HIGH_SPEED : speed;
return speed;
}
void kb_printState(char key, const bool_t *keyState)
{
printf("\tKey %c: ", key);
if (keyState[0] == 0 && keyState[1] == 1)
printf("STOP ");
else if (keyState[0] == 0 && keyState[1] == 0)
printf("HOLDING SPEED ");
else if (keyState[0] == 1 && keyState[1] == 1)
printf("SLOWLY SPEEDING UP");
else if (keyState[0] == 1 && keyState[1] == 0)
printf("FAST SPEEDING UP ");
else
printf("ERROR!");
}