-
Notifications
You must be signed in to change notification settings - Fork 18
/
print.c
206 lines (182 loc) · 4.91 KB
/
print.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "print.h"
#include "crd.h"
#define STRBUF_SIZE (5u)
typedef enum
{
INIT = 0,
PRESSED,
RELEASED,
IDLE,
DELETE,
DELETE_PRESSED,
}kbd_status_t;
typedef struct
{
char *strPtr;
uint8_t info;
uint8_t len;
uint8_t callback;
}strPtr_t;
strPtr_t strBuf[STRBUF_SIZE];
uint8_t actString; //Cadena actual
uint8_t cCount = 0; //Delete counter
uint8_t EOS=0; //EndOfString
uint8_t debounceCount = 0; //counter
keyboard_report_t reportBuffer;
kbd_status_t kbd_status = INIT;
/** Private Function prototype */
static void print_pressKey(char);
static void print_releaseKey(void);
static void print(char * str,uint8_t isRam, uint8_t len);
void print_pressKey(char ascii){
//ascii to key
char key = KEYBOARD_READ_BYTE((void*)(keyboardLUT_ES+ascii));
//setup reportBuffer
reportBuffer.modifier = ((SHIFT_MASK) &key) >> 6;
reportBuffer.modifier |= (ALTGR_MASK) &key;
// remapping of KEY_EUROPE_2 to original value
key = (~(SHIFT_MASK|ALTGR_MASK)) & key;
if(key == KEY_EUROPE_2) {
reportBuffer.keycode = KEY_EUROPE_2_ORG;
} else {
reportBuffer.keycode = key;
}
}
static void print_releaseKey(void){
//Initializing
reportBuffer.modifier=0;
reportBuffer.keycode=0;
}
static void print(char * str,uint8_t isRam, uint8_t len){
uint8_t cont=0;
uint8_t index;
while(cont < sizeof(strBuf))
{
index = (actString+cont)%STRBUF_SIZE;
if(strBuf[index].info == EMPTY)
{
strBuf[index].info = isRam;
strBuf[index].strPtr = str;
strBuf[index].len = len;
if(len != 0) {
strBuf[index].callback = 1;
}
else{
strBuf[index].callback = 0;
}
break;
}
cont++;
}
}
void printStr_RAM(char *str){
print((void*)str, RAM, 0);
}
void printStr_FLASH(char *str){
print((void*)str, FLASH, 0);
}
void print_nStr(char* str, uint8_t len){
print((void*)str, RAM, len);
}
void printUpdate(void){
char ascii = 0;
if(strBuf[actString].info != EMPTY)
{
if(strBuf[actString].info == RAM) {
ascii = *(strBuf[actString].strPtr); //READ CHAR
}else if(strBuf[actString].info == FLASH) {
ascii = pgm_read_byte(strBuf[actString].strPtr);//READ CHAR
}
}
switch(kbd_status) {
case INIT: //Initial debouncing
if(debounceCount < INIT_DEBOUNCE)
{
print_releaseKey();
debounceCount++;
}else{
kbd_status=RELEASED;
debounceCount = 0;
}
break;
case PRESSED: //We release
print_releaseKey();
kbd_status = RELEASED;
if(strBuf[actString].len > 0) {
strBuf[actString].len--;
}
break;
case RELEASED: //We can PROCESS CHAR
if( (strBuf[actString].callback == 0) && (ascii == 0) ) //EOS
{
EOS=1;
strBuf[actString].info = EMPTY;
actString = (actString+1)%STRBUF_SIZE;
}
else if( (strBuf[actString].callback != 0) && (strBuf[actString].len == 0) )
{
strBuf[actString].info = EMPTY;
CRD_apply();
if(strBuf[actString].info == EMPTY)
{
EOS=1;
}
}
else{
strBuf[actString].strPtr++; //Increment pointer
//Do we have to enter IDLE state?
if(ascii == SYNCHRONOUS_IDLE_KEY)
{
kbd_status = IDLE;
}else{
print_pressKey(ascii);
if(EOS==0) { //not first char
cCount++; //new char to delete
}else{ //first char of string
EOS=0;
cCount = 1;
}
kbd_status = PRESSED;
}
}
break;
case IDLE:
if(debounceCount < IDLE_TIMEOUT)
{
print_releaseKey();
debounceCount++;
}else{
kbd_status=RELEASED;
debounceCount = 0;
if(strBuf[actString].len > 0) {
strBuf[actString].len--;
}
}
break;
case DELETE:
if(cCount > 0)
{ //DELETING
print_pressKey(0x08);
cCount--;
kbd_status=DELETE_PRESSED;
}else{ //DONE ERASING
print_releaseKey();
//strBuf[actString].info = EMPTY;
kbd_status=RELEASED;
if(EOS==0) //if half word printing
{
EOS=1; //INVALIDATE CURRENT TYPING
strBuf[actString].info = EMPTY;
actString = (actString+1)%STRBUF_SIZE;
}
}
break;
case DELETE_PRESSED:
print_releaseKey();
kbd_status=DELETE;
break;
}
}
void print_deleteStr(void){
kbd_status=DELETE;
}