-
Notifications
You must be signed in to change notification settings - Fork 3
/
realforce.c
108 lines (96 loc) · 3.14 KB
/
realforce.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
/*
Copyright 2015 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 <string.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "matrix.h"
#include "led.h"
#include "keymap.h"
#include "capsense.h"
#include "debug.h"
uint8_t avg[MATRIX_X][MATRIX_Y];
uint16_t counts[MATRIX_X][MATRIX_Y];
uint16_t key[MATRIX_X];
static void calibrate(void)
{
memset(avg, 0, sizeof(avg));
for (uint8_t i = 0; i < 64; i++) {
for (uint8_t x = 0; x < MATRIX_X; x++) {
// To reduce cross-talk between adjacent Y lines burst them alternately.
burst(x, 0x55); burst(x, 0xAA);
for (uint8_t y = 0; y < MATRIX_Y; y++) {
avg[x][y] = avg[x][y] - (avg[x][y]>>2) + (sense(y)>>2);
_delay_us(10);
}
discharge_all();
}
}
}
uint8_t matrix_rows(void) { return MATRIX_ROWS; }
uint8_t matrix_cols(void) { return MATRIX_COLS; }
void matrix_setup(void) {
//debug_enable = true;
//debug_matrix = true;
}
void matrix_init(void) {
memset(counts, 0, sizeof(counts));
memset(key, 0, sizeof(key));
calibrate();
}
uint8_t matrix_scan(void) {
uint16_t s;
// Scan matrix
for (uint8_t x = 0; x < MATRIX_X; x++) {
// To reduce cross-talk between adjacent Y lines burst them alternately.
burst(x, 0x55); burst(x, 0xAA);
for (uint8_t y = 0; y < MATRIX_Y; y++) {
s = sense(y);
counts[x][y] = s;
if (s > THRESHOLD_ON) key[x] |= (1<<y);
if (s < THRESHOLD_ON - HYSTERESIS) key[x] &= ~(1<<y);
if (key[x] & (1<<y)) counts[x][y] |= 0x1000;
}
discharge_all();
}
return 0;
}
bool matrix_is_on(uint8_t row, uint8_t col) {
return key[row] & (1<<col);
}
matrix_row_t matrix_get_row(uint8_t row) {
return key[row];
}
void matrix_print(void) {
for (uint8_t x = 0; x < MATRIX_X; x++) {
for (uint8_t y = 0; y < MATRIX_Y; y++) {
xprintf("%04X(%02X)%c", counts[x][y], avg[x][y], (key[x] & (1<<y) ? '*' : ' '));
}
xprintf("\r\n");
}
}
void matrix_power_up(void) {}
void matrix_power_down(void) {}
void led_set(uint8_t usb_led) {}
/* translates key to keycode */
extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
extern const uint16_t fn_actions[];
uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
{
return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
}
/* translates Fn keycode to action */
action_t keymap_fn_to_action(uint8_t keycode)
{
return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
}