summaryrefslogtreecommitdiffstats
path: root/keyboards/converter/palm_usb/matrix.c
blob: 49212f2ef147da002210cede891c7a5f9d396535 (plain)
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
/*
Copyright 2018 milestogo 
with elements Copyright 2014 cy384 under a modified BSD license
building on qmk structure Copyright 2012 Jun Wako <wakojun@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include QMK_KEYBOARD_H
#include "protocol/serial.h"
#include "timer.h"
#include "pincontrol.h"


/*
 * Matrix Array usage:
 *
 * ROW: 12(4bits)
 * COL:  8(3bits)
 *
 *   +---------+
 *  0|00 ... 07|
 *  1|00 ... 07|
 *  :|   ...   |
 *  :|   ...   |
 *  A|         |
 *  B|         |
 *   +---------+
 */
static uint8_t matrix[MATRIX_ROWS];


// we're going to need a sleep timer
static uint16_t last_activity ;
// and a byte to track duplicate up events signalling all keys up. 
static uint16_t last_upKey ;
// serial device can disconnect. Check every MAXDROP characters. 
static uint16_t disconnect_counter = 0;


// bitmath masks. 
#define KEY_MASK 0b10000000
#define COL_MASK 0b00000111
#define ROW_MASK 0b01111000


#define ROW(code)    (( code & ROW_MASK ) >>3)
#define COL(code)    ((code & COL_MASK) )
#define KEYUP(code) ((code & KEY_MASK) >>7 )

static bool is_modified = false;

__attribute__ ((weak))
void matrix_init_kb(void) {
    matrix_init_user();
}

__attribute__ ((weak))
void matrix_scan_kb(void) {
    matrix_scan_user();
}

__attribute__ ((weak))
void matrix_init_user(void) {
}

__attribute__ ((weak))
void matrix_scan_user(void) {
}

inline
uint8_t matrix_rows(void)
{
    return MATRIX_ROWS;
}

inline
uint8_t matrix_cols(void)
{
    return MATRIX_COLS;
}


void pins_init(void) {
 // set pins for pullups, Rts , power &etc. 

    //print ("pins setup\n");
    pinMode(VCC_PIN, PinDirectionOutput);
    digitalWrite(VCC_PIN, PinLevelLow);

#if ( HANDSPRING == 0)

#ifdef CY835
    pinMode(GND_PIN, PinDirectionOutput);
    digitalWrite(GND_PIN, PinLevelLow);

    pinMode(PULLDOWN_PIN, PinDirectionOutput);
    digitalWrite(PULLDOWN_PIN, PinLevelLow);
#endif

    pinMode(DCD_PIN, PinDirectionInput);
    pinMode(RTS_PIN, PinDirectionInput); 
#endif

/* check that the other side isn't powered up. 
    test=digitalRead(DCD_PIN);
    xprintf("b%02X:", test);
    test=digitalRead(RTS_PIN);
    xprintf("%02X\n", test);
*/
 
}

uint8_t rts_reset(void) {
    static uint8_t firstread ;
/* bounce RTS so device knows it is rebooted */

// On boot, we keep rts as input, then switch roles here
// on leaving sleep, we toggle the same way

    firstread=digitalRead(RTS_PIN);
   // printf("r%02X:", firstread);

    pinMode(RTS_PIN, PinDirectionOutput);

    if (firstread == PinLevelHigh) {
        digitalWrite(RTS_PIN, PinLevelLow);
    } 
     _delay_ms(10);
    digitalWrite(RTS_PIN, PinLevelHigh);  
    

/* the future is Arm 
    if (palReadPad(RTS_PIN_IOPRT) == PinLevelLow)
  {
    _delay_ms(10);
    palSetPadMode(RTS_PINn_IOPORT, PinDirectionOutput_PUSHPULL);
    palSetPad(RTS_PORT, RTS_PIN);
  }
  else
  {
    palSetPadMode(RTS_PIN_RTS_PORT, PinDirectionOutput_PUSHPULL);
    palSetPad(RTS_PORT, RTS_PIN);
    palClearPad(RTS_PORT, RTS_PIN);
    _delay_ms(10);
    palSetPad(RTS_PORT, RTS_PIN);
  }
*/


 _delay_ms(5);  
 //print("rts\n");
 return 1;
}

uint8_t get_serial_byte(void) {
    static uint8_t code;
    while(1) {
        code = serial_recv();
        if (code) { 
            debug_hex(code); debug(" ");
            return code;
        }
    }
}

uint8_t palm_handshake(void) {
    // assumes something has seen DCD go high, we've toggled RTS 
    // and we now need to verify handshake. 
    // listen for up to 4 packets before giving up. 
    // usually I get the sequence FF FA FD
    static uint8_t codeA=0;
 
    for (uint8_t i=0; i < 5; i++) {
        codeA=get_serial_byte();
        if ( 0xFA == codeA) {
            if( 0xFD == get_serial_byte()) {
                return 1;
            }
        }
    }
    return 0;
}

uint8_t palm_reset(void) {
    print("@");
    rts_reset();  // shouldn't need to power cycle. 

    if ( palm_handshake() ) {
        last_activity = timer_read();
        return 1;
    } else { 
        print("failed reset");
        return 0;
    }

}

uint8_t handspring_handshake(void) {
    // should be sent 15 ms after power up. 
    // listen for up to 4 packets before giving up. 
    static uint8_t codeA=0;
 
    for (uint8_t i=0; i < 5; i++) {
        codeA=get_serial_byte();
        if ( 0xF9 == codeA) {
            if( 0xFB == get_serial_byte()) {
                return 1;
            }
        }
    }
    return 0;
}

uint8_t handspring_reset(void) {
    digitalWrite(VCC_PIN, PinLevelLow);
    _delay_ms(5);
    digitalWrite(VCC_PIN, PinLevelHigh);

    if ( handspring_handshake() ) {
        last_activity = timer_read();
        disconnect_counter=0;
        return 1;
    } else { 
        print("-HSreset");
        return 0;   
    }
}

void matrix_init(void)
{
    debug_enable = true;
    //debug_matrix =true;
    
    serial_init(); // arguments all #defined 
 
#if (HANDSPRING == 0)
    pins_init(); // set all inputs and outputs. 
#endif

    print("power up\n");
    digitalWrite(VCC_PIN, PinLevelHigh);

    // wait for DCD strobe from keyboard - it will do this 
    // up to 3 times, then the board needs the RTS toggled to try again
  
#if ( HANDSPRING == 1)
    if ( handspring_handshake() ) {
        last_activity = timer_read();
    } else { 
        print("failed handshake");
        _delay_ms(1000);
        //BUG /should/ power cycle or toggle RTS & reset, but this usually works. 
    }

#else  /// Palm / HP  device with DCD
    while( digitalRead(DCD_PIN) != PinLevelHigh ) {;} 
    print("dcd\n");

    rts_reset(); // at this point the keyboard should think all is well. 

    if ( palm_handshake() ) {
        last_activity = timer_read();
    } else { 
        print("failed handshake");
        _delay_ms(1000);
        //BUG /should/ power cycle or toggle RTS & reset, but this usually works. 
    }

#endif

    // initialize matrix state: all keys off
    for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;

    matrix_init_quantum();
    return;
    
    
}


uint8_t matrix_scan(void)
{
    uint8_t code;
    code = serial_recv();
    if (!code) {
/*         
        disconnect_counter ++;
        if (disconnect_counter > MAXDROP) {
            //  set all keys off
             for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; 
        }
*/
        // check if the keyboard is asleep. 
        if (timer_elapsed(last_activity) > SLEEP_TIMEOUT) {
#if(HANDSPRING ==0 )
            palm_reset();
#else
            handspring_reset();
#endif
            return 0;
        } 

    }

   last_activity = timer_read();
   disconnect_counter=0; // if we are getting serial data, we're connected. 

    debug_hex(code); debug(" ");


    switch (code) {
        case 0xFD:  // unexpected reset byte 2
             print("rstD ");
            return 0;
        case 0xFA:  // unexpected reset
            print("rstA ");
            return 0;
    }

    if (KEYUP(code)) {
        if (code == last_upKey) {
            // all keys are not pressed. 
            // Manual says to disable all modifiers left open now. 
            // but that could defeat sticky keys. 
            // BUG? dropping this byte. 
            last_upKey=0;
            return 0;
        }
        // release
        if (matrix_is_on(ROW(code), COL(code))) {
            matrix[ROW(code)] &= ~(1<<COL(code));
            last_upKey=code;
        }
    } else {
       // press
        if (!matrix_is_on(ROW(code), COL(code))) {
            matrix[ROW(code)] |= (1<<COL(code));

        }
    }

    matrix_scan_quantum();
    return code;
}

bool matrix_is_modified(void)
{
    return is_modified;
}

inline
bool matrix_has_ghost(void)
{
    return false;
}

inline
bool matrix_is_on(uint8_t row, uint8_t col)
{
    return (matrix[row] & (1<<col));
}

inline
uint8_t matrix_get_row(uint8_t row)
{
    return matrix[row];
}

void matrix_print(void)
{
    print("\nr/c 01234567\n");
    for (uint8_t row = 0; row < matrix_rows(); row++) {
        phex(row); print(": ");
        pbin_reverse(matrix_get_row(row));
        print("\n");
    }
}

uint8_t matrix_key_count(void)
{
    uint8_t count = 0;
    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
        count += bitpop(matrix[i]);
    }
    return count;
}