-
Notifications
You must be signed in to change notification settings - Fork 271
/
scan_loop.c
175 lines (144 loc) · 4.3 KB
/
scan_loop.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
The MIT License (MIT)
Copyright (c) 2017 Ivor Wanders
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/ScanLib.h>
// Project Includes
#include <cli.h>
#include <led.h>
#include <print.h>
#include <macro.h>
// Local Includes
#include "./scan_loop.h"
// ----- Function Declarations -----
// CLI Functions
void cliFunc_ps2Verbose(char* args);
// ----- Variables -----
CLIDict_Entry(ps2Verbose, "Output the PS/2 Scancodes received.");
CLIDict_Def(scanCLIDict, "Scan Module Commands") = {
CLIDict_Item(ps2Verbose),
{ 0, 0, 0 } // Null entry for dictionary end
};
static uint8_t verbose_state = 0;
// ----- Functions -----
uint8_t ps2data_read() {
#if defined(_teensy_3_)
return (PS2_PORT_PDIR & (1 << PS2_DATA_PIN)) != 0;
#else
#warning "Not implemented"
#endif
}
void portb_isr() {
#if defined(_teensy_3_)
PS2_PORT_ISFR = 0xFFFFFFFF; // reset the interrupt flags.
ps2interrupt(); // call the PS2 interrupt function.
#else
#warning "Not implemented"
#endif
}
void ScancodeDispatch() {
static uint8_t state = 0x01; // pressed
static uint8_t is_extended = 0;
while (1)
{
uint8_t c = get_scan_code();
if (c)
{
if (verbose_state)
{
printHex(c);
print(" ");
}
switch (c)
{
case 0xF0: // F0, is release for next scancode.
state = 0x03; // release
break;
case 0xE0: // extended scancode.
is_extended = 1;
break;
default:
// printHex(c);
// printHex(state);
// print(" ");
if (is_extended) {
// map the extended values to 0x80+c, this conveniently does not
// collide with the non-extended set.
c = 0x80 + c;
}
Macro_keyState(c, state); // send the new state
state = 0x01; // default state is pressed
is_extended = 0; // default is non extended
}
}
else
{
return;
}
}
}
// Setup
inline void Scan_setup() {
// Register Scan CLI dictionary
CLI_registerDictionary(scanCLIDict, scanCLIDictName);
#if defined(_teensy_3_)
// Enable interrupts for port B.
NVIC_ENABLE_IRQ(IRQ_PORTB);
// Set pins to input:
PS2_PORT_PDDR &= ~(1 << PS2_CLOCK_PIN);
PS2_PORT_PDDR &= ~(1 << PS2_DATA_PIN);
PS2_CLOCK_PCR = PORT_PCR_MUX(1);
PS2_DATA_PCR = PORT_PCR_MUX(1);
// Register pin PS2_CLOCK to trigger interrupt on port B:
// falling mask is 0x0A, see pins_teensy.c
// 16 is location in register, not pin
uint32_t mask = (0x0A << 16) | 0x01000000;
__disable_irq();
PS2_CLOCK_PCR |= mask;
__enable_irq();
#else
#warning "Not implemented"
#endif
}
// Main Detection Loop
inline uint8_t Scan_loop() {
// uint8_t c =get_scan_code();
// if (c != 0){
// printHex(c);
// }
ScancodeDispatch();
return 0;
}
// Signal from Macro Module that all keys have been processed (that it knows about)
inline void Scan_finishedWithMacro(uint8_t sentKeys) {
}
// Signal from Output Module that all keys have been processed (that it knows about)
inline void Scan_finishedWithOutput(uint8_t sentKeys) {
// Reset scan loop indicator (resets each key debounce state)
// TODO should this occur after USB send or Macro processing?
}
// Signal from the Output Module that the available current has changed
// current - mA
void Scan_currentChange(unsigned int current) {
// Indicate to all submodules current change
}
void cliFunc_ps2Verbose(char* args) {
verbose_state = !verbose_state;
}