This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
openKeyboard.c
518 lines (419 loc) · 15.4 KB
/
openKeyboard.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// FIRMWARE REPLACEMENT FOR CHATPAD's pic16f883
// see attached documents to learn more
// release 0.3.1 from jean : giancarlotodone at yahoo dot it
// for the Open Keyboard Project
// thanks alot to Gidi (who owns an half of my chatpad, call him "sponsor")
// and my sweethart piceta; they helped me handling
// cable knots while reversing and testing.
// Thanks as well to all people who demonstrated interest in this project!
#define CONFIG_BAUDRATE 1
#define CONFIG_SENDMODE 2
#define CONFIG_LIGHTMODE 3
#define CONFIG_LIGHTSTRENGTH 4
#define CONFIG_LIGHTFADEOFFTIME 5
#define CONFIG_REPEAT 6 // not used yet
#define CONFIG_ACCEPTCOMMANDS 7 // not used yet
#define CONFIG_MACROS 8 // not used yet
#define SENDMODE_SERIAL 0
#define SENDMODE_SCAN 1
#define SENDMODE_PS2 2 // not used yet
#define LIGHTMODE_ALWAYSOFF 0
#define LIGHTMODE_ALWAYSON 1
#define LIGHTMODE_FADE 2
#define LIGHTMODE_LASTMODE 2
#define BAUDRATE_300 1
#define BAUDRATE_1200 2
#define BAUDRATE_2400 3
#define BAUDRATE_4800 4
#define BAUDRATE_9600 5
#define BAUDRATE_19200 6
#define BAUDRATE_38400 7
#define BAUDRATE_57600 8
#define BAUDRATE_115200 9
const char scanMap[50] = { // i could output directly idx,
8, 9, 10, 40, 20, 30, 47, // but maybe a translation map
41, 42, 43, 39, 44, 38, 45, // can prove useful in future.
31, 32, 33, 35, 34, 36, 37, // starts from 1...think 0
21, 22, 23, 25, 24, 26, 27, // should be reserved for
11, 12, 13, 15, 14, 16, 17, // future use
1, 2, 3, 5, 4, 6, 7,
28, 18, 19, 0, 0, 29, 46 };
const char serialMap[4][49] = { // CHARACTER MAPPING MATRIX for "pure serial mode"
// normal char press
{56, 57, 48, 13, 112, 44, 0,
0, 0, 0, 46, 32, 109, 0,
0, 122, 120, 118, 99, 98, 110,
97, 115, 100, 103, 102, 104, 106,
113, 119, 101, 116, 114, 121, 117,
49, 50, 51, 53, 52, 54, 55,
107, 105, 111, 0, 0, 108, 8},
// shifted
{56, 57, 48, 13, 80, 44, 0,
0, 0, 0, 46, 32, 77, 0,
0, 90, 88, 86, 67, 66, 78,
65, 83, 68, 71, 70, 72, 74,
81, 87, 69, 84, 82, 89, 85,
49, 50, 51, 53, 52, 54, 55,
75, 73, 79, 0, 0, 76, 8},
// green alt
{56, 57, 48, 13, 41, 58, 0,
0, 0, 0, 63, 32, 62, 0,
0, 96, 39, 45, 187, 124, 60,
126, 154, 123, 168, 125, 47, 39,
33, 64, 128, 37, 35, 94, 38,
49, 50, 51, 53, 52, 54, 55,
91, 42, 40, 0, 0, 93, 8},
// red alt
{56, 57, 48, 13, 61, 59, 0,
0, 0, 0, 191, 32, 181, 0,
0, 230, 156, 95, 231, 43, 241,
225, 223, 240, 165, 163, 92, 34,
161, 229, 232, 254, 36, 253, 249,
49, 50, 51, 53, 52, 54, 55,
169, 236, 242, 0, 0, 248, 8}
}; ////////// END OF MAPPING MATRIX
unsigned char data[7] = {0,0,0,0,0,0,0};
unsigned char oldData[7] = {0,0,0,0,0,0,0};
unsigned char justPressed[7] = {0,0,0,0,0,0,0};
unsigned char newlyPressd = 0;
unsigned char AMask = 0;
unsigned char BMask = 0;
unsigned char somethingChanged = 0;
unsigned long int timer = 0xFFFF;
unsigned long int lightFadeOffTime = 0x8000;
// alternates (shift,green,red) mapping to translation map
// 0 1 2 3 4 5 6 7
unsigned char alts [] = {0, 1, 2, 2, 3, 3, 3, 3};
unsigned char alt;
void (*send)() = 0;
void (*lightHandler)() = 0;
unsigned char baudrate = 0;
unsigned char sendMode = 0;
unsigned char lightMode = 0;
unsigned char lightStrenght = 0;
unsigned char pwmRunning = 0;
void enterPowerSave(){
PORTA = 0;
INTCON.RBIE = 1; // enables PORTB on-change interrupt
asm{
sleep;
}
}
void exitPowerSave(){
INTCON.RBIE = 0; // disables PORTB on-change interrupt
}
void setupUsart(unsigned char br){
switch(br){ // done so, because you can't call USART_Init(variable); but only USART_Init(literal);
case BAUDRATE_300: USART_Init(300);
break;
case BAUDRATE_1200: USART_Init(1200);
break;
case BAUDRATE_2400: USART_Init(2400);
break;
case BAUDRATE_4800: USART_Init(4800);
break;
case BAUDRATE_9600: USART_Init(9600);
break;
case BAUDRATE_19200: USART_Init(19200);
break;
case BAUDRATE_38400: USART_Init(38400);
break;
case BAUDRATE_57600: USART_Init(57600);
break;
case BAUDRATE_115200: USART_Init(115200);
break;
default: EEprom_write(CONFIG_BAUDRATE, BAUDRATE_4800); // if no config found, set a default 4800
br = BAUDRATE_4800;
USART_Init(4800);
}
baudrate = br;
}
void turnBacklightOn(){
timer = lightFadeOffTime;
if (!pwmRunning) {
PWM1_Start();
PWM2_Start();
pwmRunning = 1;
}
PWM1_Change_Duty(lightStrenght);
PWM2_Change_Duty(lightStrenght);
}
void nop(){
}
void lightFade(){
unsigned char s;
if (newlyPressd) {
turnBacklightOn();
return;
}
if (timer>0) timer --;
else {
PWM1_Stop;
PWM2_Stop;
PORTC = 255;
pwmRunning = 0;
enterPowerSave();
}
if (timer<(~lightStrenght)){
PWM1_Change_Duty(~((unsigned char)timer));
PWM2_Change_Duty(~((unsigned char)timer));
}
}
void setupLightMode(unsigned char lm){
if (pwmRunning){
PWM1_Stop();
PWM2_Stop();
}
switch (lm){
case LIGHTMODE_ALWAYSOFF: lightHandler = &nop;
lightStrenght = 0xFF;
lightMode = LIGHTMODE_ALWAYSOFF;
break;
case LIGHTMODE_ALWAYSON: lightHandler = &nop;
turnBacklightOn();
lightMode = LIGHTMODE_ALWAYSON;
break;
case LIGHTMODE_FADE: lightHandler = &lightFade;
turnBacklightOn();
lightStrenght = 0x80; // restore middle luminosity
lightMode = LIGHTMODE_FADE;
break;
default : EEprom_write(CONFIG_LIGHTMODE, LIGHTMODE_ALWAYSOFF);
lightMode = LIGHTMODE_ALWAYSOFF;
lightHandler = &nop;
lightStrenght = 0xFF;
}
PORTC = 255;
// PWM init
if (lightMode != LIGHTMODE_ALWAYSOFF) {
PWM1_Init(40000);
PWM1_Change_Duty(lightStrenght);
PWM1_Start();
PWM2_Init(40000);
PWM2_Change_Duty(lightStrenght);
PWM2_Start();
pwmRunning = 1;
}
}
void doScan(){
unsigned char sc = 0;
newlyPressd = 0;
for (AMask=1; AMask<128; AMask<<=1){
oldData[sc] = data[sc]; // save old data
PORTA = ~AMask; // put a 0 on the line we want to inspect
data[sc] = PORTB; // read PORTB (pulled up)...if a line goes to 0, bingo!
data[sc] = data[sc] | PORTB; // antibounce
data[sc] = data[sc] | PORTB; // antibounce
data[sc] = ~data[sc]; // return to positive logic
justPressed[sc] = data[sc]&(~oldData[sc]);
newlyPressd = newlyPressd | justPressed[sc];
sc++;
}
}
void serialSend(){
unsigned char idx = 0;
unsigned char sc = 0;
for (AMask=1; AMask<128; AMask<<=1){
for (BMask=1; BMask<128; BMask<<=1){
if (justPressed[sc] & BMask) USART_Write(serialMap[alt][idx]);
idx++;
}
sc++;
}
alt = alts[data[2]&1|((data[1]&1)<<1)|((data[0]&64)>>4)]; // next scan can use alt retrieved here
}
void scanSend(){
unsigned char idx = 0;
unsigned char sc = 0;
unsigned char bs = 0;
for (AMask=1; AMask<128; AMask<<=1){
somethingChanged = data[sc] ^ oldData[sc];
bs = 0;
for (BMask=1; BMask<128; BMask<<=1){
if (someThingChanged & BMask)
USART_Write(scanMap[idx] | (((data[sc]&BMask)>>bs)<<6)); // | 64 if pressed
bs++;
idx++;
}
sc++;
}
}
void ps2Send(){
// not implemented yet
}
void setupSendMode(unsigned char sm){
switch (sm){
case SENDMODE_SCAN : send = &scanSend;
sendMode = SENDMODE_SCAN;
break;
case SENDMODE_SERIAL: send = &serialSend;
sendMode = SENDMODE_SERIAL;
break;
// case SENDMODE_PS2: send = &ps2send;
// sendMode = SENDMODE_PS2;
// break;
default : send = &serialSend;
EEprom_write(CONFIG_SENDMODE, SENDMODE_SERIAL);
sendMode = SENDMODE_SERIAL;
}
}
void config(){
timer = lightFadeOffTime;
if ((data[1]&4)&&(lightStrenght<0xff)) { // <
lightStrenght++;
PWM1_Change_Duty(lightStrenght);
PWM2_Change_Duty(lightStrenght);
if (lightMode==LIGHTMODE_ALWAYSOFF) lightMode = LIGHTMODE_FADE;
} // decrease light strenght
if ((data[1]&64)&&(lightStrenght>0)) { // >
lightStrenght--;
PWM1_Change_Duty(lightStrenght);
PWM2_Change_Duty(lightStrenght);
if (lightMode==LIGHTMODE_ALWAYSOFF) lightMode = LIGHTMODE_FADE;
} // increase light strenght
if (justPressed[5]&1){ // 1
// if (data[1]&1) // this buttion combination just doesn't work...think that's because of the buttons staying on the same matrix line.
// setupUsart(BAUDRATE_4800);
// else {
setupLightMode(LIGHTMODE_FADE);
lightFadeOffTime = 2<<8;
// }
}
if (justPressed[5]&2){ // 2
if (data[1]&1)
;//setupUsart(BAUDRATE_300); //all setupUsart commented out are to save code words space...otherwise i'm running into demo limit
else { //consider donating some money to me to purchase a full version of mikroC and continue development
setupLightMode(LIGHTMODE_FADE);
lightFadeOffTime = 4<<8; // more or less 5 sec
}
}
if (justPressed[5]&4){ // 3
if (data[1]&1)
;//setupUsart(BAUDRATE_1200);
else {
setupLightMode(LIGHTMODE_FADE);
lightFadeOffTime = 8<<8;
}
}
if (justPressed[5]&16){ // 4
if (data[1]&1)
;//setupUsart(BAUDRATE_2400);
else {
setupLightMode(LIGHTMODE_FADE);
lightFadeOffTime = 16<<8;
}
}
if (justPressed[5]&8){ // 5
if (data[1]&1)
setupUsart(BAUDRATE_4800);
else {
setupLightMode(LIGHTMODE_FADE);
lightFadeOffTime = 32<<8;
}
}
if (justPressed[5]&32){ // 6
if (data[1]&1)
setupUsart(BAUDRATE_9600);
else {
setupLightMode(LIGHTMODE_FADE);
lightFadeOffTime = 64<<8;
}
}
if (justPressed[5]&64){ // 7
if (data[1]&1)
;//setupUsart(BAUDRATE_19200);
else {
setupLightMode(LIGHTMODE_FADE);
lightFadeOffTime = 128<<8;
}
}
if (justPressed[0]&1){ // 8
if (data[1]&1)
;//setupUsart(BAUDRATE_38400);
else {
setupLightMode(LIGHTMODE_FADE);
lightFadeOffTime = 255<<8;
}
}
if (justPressed[0]&2){ // 9
if (data[1]&1)
;//setupUsart(BAUDRATE_57600);
else
setupLightMode(LIGHTMODE_ALWAYSON);
}
if (justPressed[0]&4){ // 0
if (data[1]&1)
;//setupUsart(BAUDRATE_115200);
else
setupLightMode(LIGHTMODE_ALWAYSOFF);
}
if (justPressed[3]&1){ // A
setupSendMode(SENDMODE_SCAN);
}
if (justPressed[3]&2){ // S
setupSendMode(SENDMODE_SERIAL);
}
Delay_5ms();
}
void saveConfig(){
EEprom_write(CONFIG_BAUDRATE, baudrate);
EEprom_write(CONFIG_SENDMODE, sendMode);
EEprom_write(CONFIG_LIGHTMODE, lightMode);
EEprom_write(CONFIG_LIGHTSTRENGTH, lightStrenght);
EEprom_write(CONFIG_LIGHTFADEOFFTIME, lightFadeOffTime>>8);
}
void interrupt() {
if (INTCON.RBIF) { // SOMETHING CHANGED ON PORTB...
exitPowerSave(); // just wakeup and so some scanning...
INTCON.RBIF = 0; // clear intr flag
}
// if (PIR1.RCIF) { // USART RECEIVE BUFFER () FULL...
//
// }
}
void main() {
OSCCON = 0x67; // 01100111 - 0110 stands for 4Mhz internal clock
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
PORTA = 0; // init port A
TRISA = 0; // 1 = input
PORTB = 0; // initialize PORTB
TRISB = 255; // designate PORTB as all input
PORTC = 0; // initialize PORTC
TRISC = 0; // designate PORTB 0-7 as output
OPTION_REG = 0; // pull-ups enabled on PORTB....no external pull-downs
// present, so we have to run negative logic...
//WPUB = 127; // weak pull-ups enabled on PORTB 0-6 // AAARGH!! this caused random Light-On
INTCON.RBIE = 0; // disables PORTB on-change interrupt
INTCON.RBIF = 0; // clear PORTB mismatch values
IOCB = 127; // pin 0-6 of PORTB configured to raise interrupt when changed
PIE1 = 32; // RCIE = 1; enables EUSART on-receive interrupt
INTCON.GIE = 1; // Global Interrupt Enable/disable
// SEND MODE ------------------
Delay_ms(20); // required for eeprom_read to work correctly
sendMode = EEprom_read(CONFIG_SENDMODE);
setupSendMode(sendMode);
// TODO: if in PS2 mode don't initialize usart
// USART ---------------
Delay_ms(20); // required for eeprom_read to work correctly
baudrate = EEprom_read(CONFIG_BAUDRATE);
setupUsart(baudrate); // Initalize USART (xxxx baud rate, 1 stop bit, no parity...)
// LIGHTS ----------------
Delay_ms(20); // required for eeprom_read to work correctly
lightFadeOffTime = EEprom_read(CONFIG_LIGHTFADEOFFTIME) << 8;
lightStrenght = EEprom_read(CONFIG_LIGHTSTRENGTH);
Delay_ms(20); // required for eeprom_read to work correctly
lightMode = EEprom_read(CONFIG_LIGHTMODE);
setUpLightMode(lightMode);
do {
doScan();
if (data[1]&2) {
config(); // people button pressed -> config mode
}else{
if (oldData[1]&2) saveConfig(); // ppl btn just released... save config
send();
}
lightHandler();
Delay_80us(); // without this delay, pic hangs on sleep...
} while (1); // endless loop
}