Repository navigation
Expand file tree
/
Copy pathec_matrix.c
More file actions
177 lines (150 loc) · 4.48 KB
/
Copy pathec_matrix.c
File metadata and controls
177 lines (150 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
8000
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
/**
* @file ec_matrix.c
*/
#include "matrix.h"
#include "wait.h"
#include "led.h"
#include "amk_gpio.h"
#include "amk_printf.h"
#ifndef CUSTOM_MATRIX_DEBUG
#define CUSTOM_MATRIX_DEBUG 0
#endif
#if CUSTOM_MATRIX_DEBUG
#define custom_matrix_debug amk_printf
#else
#define custom_matrix_debug(...)
#endif
#define COL_A_MASK 0x01
#define COL_B_MASK 0x02
#define COL_C_MASK 0x04
/*
#define L_MASK 0x08
#define R_MASK 0x10
*/
static pin_t custom_row_pins[] = MATRIX_ROW_PINS;
static pin_t custom_col_pins[] = MATRIX_COL_PINS;
void matrix_init_custom(void)
{
#ifdef RGB_EN_PIN
gpio_set_output_pushpull(RGB_EN_PIN);
gpio_write_pin(RGB_EN_PIN, 1);
#endif
//power on switch board
#ifdef POWER_PIN
gpio_set_output_pushpull(POWER_PIN);
gpio_write_pin(POWER_PIN, 1);
wait_ms(100);
#endif
// initialize row pins
for (int i = 0; i < MATRIX_ROWS; i++) {
gpio_set_output_pushpull(custom_row_pins[i]);
gpio_write_pin(custom_row_pins[i], 0);
}
// initialize col pins
#ifdef LEFT_EN_PIN
gpio_set_output_pushpull(LEFT_EN_PIN);
gpio_write_pin(LEFT_EN_PIN, 1);
#endif
#ifdef RIGHT_EN_PIN
gpio_set_output_pushpull(RIGHT_EN_PIN);
gpio_write_pin(RIGHT_EN_PIN, 1);
#endif
gpio_set_output_pushpull(COL_A_PIN);
gpio_write_pin(COL_A_PIN, 0);
gpio_set_output_pushpull(COL_B_PIN);
gpio_write_pin(COL_B_PIN, 0);
gpio_set_output_pushpull(COL_C_PIN);
gpio_write_pin(COL_C_PIN, 0);
// initialize opamp and capacity discharge
gpio_set_output_pushpull(OPA_EN_PIN);
gpio_write_pin(OPA_EN_PIN, 1);
gpio_set_output_pushpull(DISCHARGE_PIN);
gpio_write_pin(DISCHARGE_PIN, 1);
}
extern ADC_HandleTypeDef hadc1;
static uint32_t adc_read(void)
{
uint32_t data = 0;
if (HAL_ADC_Start(&hadc1) == HAL_OK) {
HAL_ADC_PollForConversion(&hadc1, 1);
if ((HAL_ADC_GetState(&hadc1) & HAL_ADC_STATE_REG_EOC) == HAL_ADC_STATE_REG_EOC)
{
data = HAL_ADC_GetValue(&hadc1);
} else {
custom_matrix_debug("key sense failed\n");
}
HAL_ADC_Stop(&hadc1);
}
return data;
}
static bool sense_key(pin_t row, bool on)
{
bool key_down = false;
gpio_write_pin(DISCHARGE_PIN, 1);
wait_us(DISCHARGE_WAIT_PRE);
gpio_write_pin(DISCHARGE_PIN, 0);
//gpio_set_input_floating(DISCHARGE_PIN);
gpio_write_pin(row, 1);
uint32_t data = adc_read();
// press to release
if (on) {
if (data > EC_TH_LOW) {
key_down = true;
}
} else {
if (data > EC_TH_HIGH) {
key_down = true;
}
}
if (key_down) {
custom_matrix_debug("key down: 0x%lx, data=%d\n", row, data);
//amk_printf("key down: 0x%lx, data=%d\n", row, data);
}
// clean up
gpio_write_pin(row, 0);
gpio_write_pin(DISCHARGE_PIN, 1);
wait_us(DISCHARGE_WAIT_POST);
return key_down;
}
bool matrix_scan_custom(matrix_row_t* raw)
{
bool changed = false;
#if SCAN_ONE
changed = scan_one(raw);
#else
gpio_write_pin(DISCHARGE_PIN, 1);
wait_us(SCAN_DELAY);
for (int col = 0; col < MATRIX_COLS; col++) {
gpio_write_pin(COL_A_PIN, (custom_col_pins[col]&COL_A_MASK) ? 1 : 0);
gpio_write_pin(COL_B_PIN, (custom_col_pins[col]&COL_B_MASK) ? 1 : 0);
gpio_write_pin(COL_C_PIN, (custom_col_pins[col]&COL_C_MASK) ? 1 : 0);
if (custom_col_pins[col]&L_MASK) {
gpio_write_pin(LEFT_EN_PIN, 0);
}
if (custom_col_pins[col]&R_MASK) {
gpio_write_pin(RIGHT_EN_PIN, 0);
}
for (int row = 0; row < MATRIX_ROWS; row++) {
matrix_row_t last_row_value = raw[row];
matrix_row_t current_row_value = last_row_value;
if (sense_key(custom_row_pins[row], (last_row_value&col))) {
current_row_value |= (1 << col);
} else {
current_row_value &= ~(1 << col);
}
if (last_row_value != current_row_value) {
raw[row] = current_row_value;
changed = true;
}
}
gpio_write_pin(LEFT_EN_PIN, 1);
gpio_write_pin(RIGHT_EN_PIN, 1);
}
#endif
if (changed) {
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
custom_matrix_debug("row:%d-%x\n", row, matrix_get_row(row));
}
}
return changed;
}