lotus: Further adapt custom matrix scanning code

Following instructions here: https://docs.qmk.fm/#/custom_matrix
And looking at other keyboards.

Signed-off-by: Daniel Schaefer <dhs@frame.work>
This commit is contained in:
Daniel Schaefer 2022-12-20 18:05:17 +08:00
parent 3f4d933a6c
commit fb8b49818b
3 changed files with 118 additions and 73 deletions

View File

@ -60,76 +60,3 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BL_TOGG, KC_TRNS, KC_TRNS, KC_HOME, KC_PGDN, KC_PGUP, KC_RGHT
)
};
/**
* Keyscan mockup/pseudo-code below. Prefixed with zoid_ to avoid hooking into system
*/
#define LOTUS_COLS 14
void adc_gpio_init(int gpio) {
// TODO: Implement
}
void adc_select_input(int adc_channel) {
// TODO: Implement
}
// Mux GPIOs
#define MUX_A 1
#define MUX_B 2
#define MUX_C 3
#define MUX_ENABLE 4
// Mux output
#define ADC_IN 28
void gpio_set(int gpio, int enable) {
// TODO: Implement
}
/**
* Tell the mux to select a specific column
*
* Splits the positive integer (<=7) into its three component bits.
*/
void mux_select_col(int col) {
int bits[] = {
(col & 0x1) > 0,
(col & 0x4) > 0,
(col & 0x8) > 0
};
gpio_set(MUX_A, bits[0]);
gpio_set(MUX_B, bits[1]);
gpio_set(MUX_C, bits[2]);
}
uint16_t adc_read(void) { return 0; }
void zoid_keyscan(void) {
for (int col = 0; col < LOTUS_COLS; col++) {
// TODO: Map col index to GPIO
// Drive column high so we can measure the resistors
gpio_set(col, 1);
// Read ADC for this row
mux_select_col(col);
uint16_t adc_value = adc_read();
// Interpret ADC value as rows
printf("%04X", adc_value);
// Drive column low again
gpio_set(col, 1);
}
}
void adc_init(void) {
gpio_set(MUX_ENABLE, 1);
}
// TODO: Do we need a de-init? Probably not.
void zoid_keyscan_init(void) {
adc_init();
// Make sure GPIO is high-impedance, no pullups etc
adc_gpio_init(26);
// Select ADC input 0 (GPIO26)
adc_select_input(0);
}

114
keyboards/lotus/matrix.c Normal file
View File

@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdint.h>
#include "debug.h"
#include "matrix.h"
#include "print.h"
#include "quantum.h"
#define LOTUS_COLS 14
/**
* Tell RP2040 ADC controller to initialize a specific GPIO for ADC input
*/
void adc_gpio_init(int gpio) {
assert(gpio >= 26 && gpio <= 29);
// TODO: Implement
}
/**
* Tell RP2040 ADC controller to read from a specific ADC channel
*/
void adc_select_input(int adc_channel) {
assert(adc_channel >= 0 && adc_channel <= 4);
// TODO: Implement
}
// Mux GPIOs
#define MUX_A 1
#define MUX_B 2
#define MUX_C 3
#define MUX_ENABLE 4
// Mux output
#define ADC_IN 28
/**
* Tell the mux to select a specific column
*
* Splits the positive integer (<=7) into its three component bits.
*/
static void mux_select_col(int col) {
int bits[] = {
(col & 0x1) > 0,
(col & 0x4) > 0,
(col & 0x8) > 0
};
writePin(MUX_A, bits[0]);
writePin(MUX_B, bits[1]);
writePin(MUX_C, bits[2]);
}
static uint16_t adc_read(void) { return 0; }
/**
* Based on the adc value, update the matrix for this column
* */
static bool interpret_adc_col(matrix_row_t cur_matrix[], uint16_t adc_value, int col) {
bool changed = false;
printf("Col %d - ADC value:%04X", col, adc_value);
for(uint8_t row = 0; row < MATRIX_ROWS; row++) {
// TODO: Decode adc_value and set each row in this column
uint8_t key_state = 0;
cur_matrix[row] |= key_state ? 0 : (1 << col);
}
return changed;
}
/**
* Overriding behavior of matrix_scan from quantum/matrix.c
*/
bool matrix_scan_custom(matrix_row_t current_matrix[]) {
bool changed = false;
for (int col = 0; col < LOTUS_COLS; col++) {
// TODO: Map col index to GPIO
// Drive column high so we can measure the resistors
writePinHigh(col);
wait_us(30); // Probably good idea to wait a bit
// Read ADC for this row
mux_select_col(col);
uint16_t adc_value = adc_read();
// Interpret ADC value as rows
changed |= interpret_adc_col(current_matrix, adc_value, col);
// Drive column low again
writePinLow(col);
}
return changed;
}
/**
* Enable the ADC MUX
*
* TODO: Do we need a de-init? Probably not.
*/
static void adc_mux_init(void) {
writePinHigh(MUX_ENABLE);
}
/**
* Overriding behavior of matrix_init from quantum/matrix.c
*/
void matrix_init_custom(void) {
adc_mux_init();
// Make sure GPIO is high-impedance, no pullups etc
adc_gpio_init(26);
// Select ADC input 0 (GPIO26)
adc_select_input(0);
}

View File

@ -2,3 +2,7 @@ WS2812_DRIVER = vendor
SERIAL_DRIVER = vendor
#RGB_MATRIX_ENABLE = false
RGB_MATRIX_DRIVER = IS31FL3745
# Custom matrix scanning code via ADC
#CUSTOM_MATRIX = lite
#SRC += matrix.c