summaryrefslogtreecommitdiffstats
path: root/keyboards/hhkb/yang/matrix.c
blob: f0eccc899dc74cb5bb5d60b6d905ff8366ffcab4 (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
/*
Copyright 2011 Jun Wako <wakojun@gmail.com>
Copyright 2020 Kan-Ru Chen <kanru@kanru.info>

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 "quantum.h"

#ifdef BLUETOOTH_ENABLE
#    include "adafruit_ble.h"
#endif

#define RELAX_TIME_US 5
#define ADC_READ_TIME_US 5

uint8_t power_save_level;

static uint32_t matrix_last_modified = 0;

static inline void key_strobe_high(void) { writePinLow(B6); }
static inline void key_strobe_low(void) { writePinHigh(B6); }
static inline bool key_state(void) { return readPin(D7); }
static inline void key_prev_on(void) { writePinHigh(B7); }
static inline void key_prev_off(void) { writePinLow(B7); }
static inline bool key_power_state(void) { return !readPin(D6); }

static inline void suspend_power_down_longer(void) {
    uint8_t times = 60;
    while (--times) suspend_power_down();
}

void matrix_power_up(void) {
    dprint("[matrix_on]\n");
    // change pins output
    DDRB  = 0xFF;
    PORTB = 0x40;
    // switch MOS FET on
    setPinOutput(D6);
    writePinLow(D6);
}

void matrix_power_down(void) {
    dprint("[matrix_off]\n");
    // input with pull-up consumes less than without it when pin is open
    DDRB  = 0x00;
    PORTB = 0xFF;
    // switch MOS FET off
    setPinOutput(D6);
    writePinHigh(D6);
}

static inline void key_select_row(uint8_t row) { PORTB = (PORTB & 0b11111000) | ((row)&0b111); }
static inline void key_select_col(uint8_t col) { PORTB = (PORTB & 0b11000111) | (((col)&0b111) << 3); }
static inline bool key_prev_was_on(matrix_row_t matrix[], uint8_t row, uint8_t col) { return matrix[row] & (1 << col); }

void matrix_init_custom(void) { power_save_level = 0; }

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool matrix_has_changed = false;

    // power on
    if (!key_power_state()) {
        matrix_power_up();
    }
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        matrix_row_t last_row_value = current_matrix[row];

        key_select_row(row);
        wait_us(RELAX_TIME_US);

        for (uint8_t col = 0; col < MATRIX_COLS; col++) {
            // Hysteresis control: assert(1) when previous key state is on
            if (key_prev_was_on(current_matrix, row, col)) {
                key_prev_on();
            } else {
                key_prev_off();
            }

            // Disable interrupts to encure the ADC timing is correct
            cli();

            // strobe
            key_select_col(col);
            key_strobe_high();

            // Wait for ADC to outputs its value.
            // 1us was ok on one HHKB, but not worked on another.
            // no   wait doesn't work on Teensy++ with pro(1us works)
            // no   wait does    work on tmk PCB(8MHz) with pro2
            // 1us  wait does    work on both of above
            // 1us  wait doesn't work on tmk(16MHz)
            // 5us  wait does    work on tmk(16MHz)
            // 5us  wait does    work on tmk(16MHz/2)
            // 5us  wait does    work on tmk(8MHz)
            // 10us wait does    work on Teensy++ with pro
            // 10us wait does    work on 328p+iwrap with pro
            // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
            wait_us(ADC_READ_TIME_US);

            if (key_state()) {
                current_matrix[row] &= ~(1 << col);
            } else {
                current_matrix[row] |= (1 << col);
            }

            key_strobe_low();
            sei();

            // Make sure enough time has elapsed since the last call
            // This is to ensure the matrix voltages have relaxed
            wait_us(RELAX_TIME_US);
        }
        if (current_matrix[row] ^ last_row_value) {
            matrix_has_changed   = true;
            matrix_last_modified = timer_read32();
        }
    }

    // Power saving
    uint32_t time_diff = timer_elapsed32(matrix_last_modified);
    if (time_diff > MATRIX_POWER_SAVE_TIMEOUT_L3_MS) {
        power_save_level = 3;
        suspend_power_down_longer();
    } else if (time_diff > MATRIX_POWER_SAVE_TIMEOUT_L2_MS) {
        power_save_level = 2;
#ifdef BLUETOOTH_ENABLE
        if (!adafruit_ble_is_connected()) {
            power_save_level = 3;
        }
#endif
        suspend_power_down_longer();
    } else if (time_diff > MATRIX_POWER_SAVE_TIMEOUT_MS) {
        power_save_level = 1;
        suspend_power_down();
    } else {
        if (power_save_level != 0) {
            power_save_level = 0;
            suspend_wakeup_init();
        }
    }

    return matrix_has_changed;
}

bool adafruit_ble_delbonds(void);
bool adafruit_ble_reconnect(void);

bool command_extra(uint8_t code) {
    switch (code) {
#ifdef BLUETOOTH_ENABLE
        case KC_R:
            adafruit_ble_delbonds();
            return true;
        case KC_S:
            adafruit_ble_reconnect();
            return true;
#endif
        default:
            return false;
    }
}