|
| 1 | +/* Copyright 2020 sekigon-gonnoc |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU General Public License as published by |
| 5 | + * the Free Software Foundation, either version 2 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <string.h> |
| 18 | + |
| 19 | +#include "crkbd_ecwl.h" |
| 20 | +#include "quantum.h" |
| 21 | +#include "bmp_matrix.h" |
| 22 | + |
| 23 | +#include "debug.h" |
| 24 | +#include "i2c_master.h" |
| 25 | + |
| 26 | +#include "apidef.h" |
| 27 | +#include "debug.h" |
| 28 | + |
| 29 | +#define POWER_PIN 11 |
| 30 | +#define APLEX_EN_PIN 10 |
| 31 | +#define OPA_SHDN_PIN 20 |
| 32 | +#define ANALOG_PORT 6 |
| 33 | +#define DISCHARGE_PIN 16 |
| 34 | +const uint8_t row_pins[] = {7, 8, 9}; |
| 35 | +const uint8_t col_pins[] = {15, 14, 13}; |
| 36 | +const uint8_t col_sels[] = {6, 4, 3, 0, 1, 2, 5}; |
| 37 | + |
| 38 | +#define ERROR_TH 3000 |
| 39 | +#define HIGH_TH 1000 |
| 40 | +#define LOW_TH 800 |
| 41 | + |
| 42 | +#define LEN(x) (sizeof(x) / sizeof(x[0])) |
| 43 | + |
| 44 | +bool bmp_config_overwrite(bmp_api_config_t const *const config_on_storage, |
| 45 | + bmp_api_config_t *const keyboard_config) { |
| 46 | + // User can overwrite partial settings |
| 47 | + keyboard_config->mode = config_on_storage->mode; |
| 48 | + keyboard_config->startup = config_on_storage->startup; |
| 49 | + keyboard_config->matrix.debounce = config_on_storage->matrix.debounce; |
| 50 | + keyboard_config->matrix.is_left_hand = |
| 51 | + config_on_storage->matrix.is_left_hand; |
| 52 | + keyboard_config->param_central = config_on_storage->param_central; |
| 53 | + keyboard_config->param_peripheral = config_on_storage->param_peripheral; |
| 54 | + keyboard_config->keymap = config_on_storage->keymap; |
| 55 | + keyboard_config->reserved[2] = config_on_storage->reserved[2]; |
| 56 | + |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +void keyboard_post_init_kb() { |
| 61 | + debug_enable = false; |
| 62 | + debug_keyboard = false; |
| 63 | + debug_mouse = false; |
| 64 | +} |
| 65 | + |
| 66 | +const bmp_ecs_config_t ecs_config = {.pins = row_pins, |
| 67 | + .cnt = LEN(row_pins), |
| 68 | + .adc_port = ANALOG_PORT, |
| 69 | + .shdn_pin = OPA_SHDN_PIN, |
| 70 | + .discharge_pin = DISCHARGE_PIN}; |
| 71 | + |
| 72 | +void ecs_matrix_init() { |
| 73 | + // init driver |
| 74 | + BMPAPI->ecs.init(&ecs_config); |
| 75 | + |
| 76 | + // init col_pins |
| 77 | + for (int idx = 0; idx < LEN(col_pins); idx++) { |
| 78 | + setPinOutput(col_pins[idx]); |
| 79 | + writePinLow(col_pins[idx]); |
| 80 | + } |
| 81 | + |
| 82 | + // power_on |
| 83 | + setPinOutput(POWER_PIN); |
| 84 | + writePinHigh(POWER_PIN); |
| 85 | + |
| 86 | + // enable multiplexer |
| 87 | + setPinOutput(APLEX_EN_PIN); |
| 88 | + writePinLow(APLEX_EN_PIN); |
| 89 | +} |
| 90 | + |
| 91 | +uint16_t sw_read[32] = {0}; |
| 92 | +uint32_t ecs_get_device_row() { return MATRIX_ROWS_DEFAULT; } |
| 93 | +uint32_t ecs_get_device_col() { return MATRIX_COLS_DEFAULT; } |
| 94 | +uint32_t ecs_matrix_scan(matrix_row_t *matrix_raw) { |
| 95 | + uint16_t sw_idx = 0; |
| 96 | + |
| 97 | + for (int col_sel_idx = 0; col_sel_idx < LEN(col_sels); col_sel_idx++) { |
| 98 | + BMPAPI->ecs.discharge_capacitor(); |
| 99 | + |
| 100 | + writePinHigh(APLEX_EN_PIN); |
| 101 | + // select col |
| 102 | + for (int col_pin_idx = 0; col_pin_idx < LEN(col_pins); col_pin_idx++) { |
| 103 | + writePin(col_pins[col_pin_idx], |
| 104 | + col_sels[col_sel_idx] & (1 << col_pin_idx) ? 1 : 0); |
| 105 | + } |
| 106 | + writePinLow(APLEX_EN_PIN); |
| 107 | + |
| 108 | + for (int row = 0; row < sizeof(row_pins); row++) { |
| 109 | + BMPAPI->ecs.discharge_capacitor(); |
| 110 | + |
| 111 | + // assign_ecs_drive_pin |
| 112 | + BMPAPI->ecs.assign_drive_pin(row_pins[row]); |
| 113 | + |
| 114 | + // drive prev row low |
| 115 | + BMPAPI->ecs.clear_drive_pins(&ecs_config); |
| 116 | + |
| 117 | + // read switch value |
| 118 | + BMPAPI->ecs.sw_read(&sw_read[col_sel_idx + row * LEN(col_sels)]); |
| 119 | + sw_idx++; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + BMPAPI->ecs.discharge_capacitor(); |
| 124 | + |
| 125 | + // select first col |
| 126 | + for (int col_pin_idx = 0; col_pin_idx < LEN(col_pins); col_pin_idx++) { |
| 127 | + writePin(col_pins[col_pin_idx], |
| 128 | + col_sels[0] & (1 << col_pin_idx) ? 1 : 0); |
| 129 | + } |
| 130 | + |
| 131 | + // clear all row |
| 132 | + BMPAPI->ecs.clear_drive_pins(&ecs_config); |
| 133 | + |
| 134 | + // stop_adc |
| 135 | + BMPAPI->ecs.schedule_next_scan(); |
| 136 | + |
| 137 | + uint8_t matrix_changed = 0; |
| 138 | + |
| 139 | + for (int row = 0; row < THIS_DEVICE_ROWS; row++) { |
| 140 | + for (int col = 0; col < THIS_DEVICE_COLS; col++) { |
| 141 | + if (sw_read[col + row * LEN(col_sels)] > ERROR_TH) { |
| 142 | + // error value |
| 143 | + |
| 144 | + dprintf("ERROR(%d,%d):%d", row, col, |
| 145 | + sw_read[col + row * LEN(col_sels)]); |
| 146 | + |
| 147 | + sw_read[col + row * LEN(col_sels)] = 0; |
| 148 | + } |
| 149 | + |
| 150 | + if (matrix_raw[row] & (1 << col)) { |
| 151 | + if (sw_read[col + row * LEN(col_sels)] < LOW_TH) { |
| 152 | + matrix_raw[row] &= ~(1 << col); |
| 153 | + matrix_changed++; |
| 154 | + |
| 155 | + dprintf("UP(%d,%d):%d", row, col, |
| 156 | + sw_read[col + row * LEN(col_sels)]); |
| 157 | + } |
| 158 | + } else { |
| 159 | + if (sw_read[col + row * LEN(col_sels)] > HIGH_TH) { |
| 160 | + matrix_raw[row] |= (1 << col); |
| 161 | + matrix_changed++; |
| 162 | + |
| 163 | + dprintf("DOWN(%d,%d):%d", row, col, |
| 164 | + sw_read[col + row * LEN(col_sels)]); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + static int cnt = 0; |
| 171 | + if (cnt++ == 30) { |
| 172 | + cnt = 0; |
| 173 | + for (int row = 0; row < sizeof(row_pins); row++) { |
| 174 | + for (int col_sel_idx = 0; col_sel_idx < LEN(col_sels); |
| 175 | + col_sel_idx++) { |
| 176 | + dprintf("%5d", sw_read[col_sel_idx + row * LEN(col_sels)]); |
| 177 | + } |
| 178 | + dprintf("\n"); |
| 179 | + } |
| 180 | + dprintf("\n"); |
| 181 | + } |
| 182 | + |
| 183 | + return matrix_changed; |
| 184 | +} |
| 185 | + |
| 186 | +static const bmp_matrix_func_t matrix_func = { |
| 187 | + ecs_matrix_init, ecs_get_device_row, ecs_get_device_col, ecs_matrix_scan}; |
| 188 | + |
| 189 | +const bmp_matrix_func_t *get_matrix_func_user() { return &matrix_func; } |
| 190 | + |
| 191 | +void bmp_before_sleep() {} |
| 192 | + |
| 193 | +bool checkSafemodeFlag(bmp_api_config_t const *const config) { return false; } |
0 commit comments