1
0

Cipulot Updates (#24539)

This commit is contained in:
Cipulot
2024-10-28 20:54:05 +01:00
committed by GitHub
parent 06b7dce565
commit 2aa186873e
68 changed files with 292 additions and 171 deletions

View File

@@ -0,0 +1,8 @@
CUSTOM_MATRIX = lite
ANALOG_DRIVER_REQUIRED = yes
VPATH += keyboards/cipulot/common
SRC += matrix.c ec_board.c ec_switch_matrix.c
ifeq ($(strip $(VIA_ENABLE)), yes)
SRC += via_ec.c
endif

View File

@@ -17,6 +17,10 @@
#include "ec_switch_matrix.h"
#include "keyboard.h"
#ifdef SPLIT_KEYBOARD
# include "transactions.h"
#endif
void eeconfig_init_kb(void) {
// Default values
eeprom_ec_config.actuation_mode = DEFAULT_ACTUATION_MODE;
@@ -57,8 +61,14 @@ void keyboard_post_init_kb(void) {
ec_config.rescaled_mode_0_actuation_threshold[row][col] = rescale(ec_config.mode_0_actuation_threshold, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
ec_config.rescaled_mode_0_release_threshold[row][col] = rescale(ec_config.mode_0_release_threshold, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
ec_config.rescaled_mode_1_initial_deadzone_offset[row][col] = rescale(ec_config.mode_1_initial_deadzone_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
ec_config.rescaled_mode_1_actuation_offset[row][col] = rescale(ec_config.mode_1_actuation_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
ec_config.rescaled_mode_1_release_offset[row][col] = rescale(ec_config.mode_1_release_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
}
}
#ifdef SPLIT_KEYBOARD
transaction_register_rpc(RPC_ID_VIA_CMD, via_cmd_slave_handler);
#endif
keyboard_post_init_user();
}

View File

@@ -37,8 +37,14 @@ const pin_t amux_en_pins[] = AMUX_EN_PINS;
const pin_t amux_n_col_sizes[] = AMUX_COL_CHANNELS_SIZES;
const pin_t amux_n_col_channels[][AMUX_MAX_COLS_COUNT] = {AMUX_COL_CHANNELS};
#ifdef UNUSED_POSITIONS_LIST
const uint8_t UNUSED_POSITIONS[][2] = UNUSED_POSITIONS_LIST;
# define UNUSED_POSITIONS_COUNT ARRAY_SIZE(UNUSED_POSITIONS)
#endif
#define AMUX_SEL_PINS_COUNT ARRAY_SIZE(amux_sel_pins)
#define EXPECTED_AMUX_SEL_PINS_COUNT ceil(log2(AMUX_MAX_COLS_COUNT)
// Checks for the correctness of the configuration
_Static_assert(ARRAY_SIZE(amux_en_pins) == AMUX_COUNT, "AMUX_EN_PINS doesn't have the minimum number of bits required to enable all the multiplexers available");
// Check that number of select pins is enough to select all the channels
@@ -70,6 +76,16 @@ void init_amux(void) {
}
}
// Disable all the unused rows
void disable_unused_row(uint8_t row) {
// disable all the other rows apart from the current selected one
for (uint8_t idx = 0; idx < MATRIX_ROWS; idx++) {
if (idx != row) {
gpio_write_pin_low(row_pins[idx]);
}
}
}
// Select the multiplexer channel of the specified multiplexer
void select_amux_channel(uint8_t channel, uint8_t col) {
// Get the channel for the specified multiplexer
@@ -158,6 +174,10 @@ void ec_noise_floor(void) {
sum += amux_n_col_sizes[i];
uint8_t adjusted_col = col + sum;
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
#ifdef UNUSED_POSITIONS_LIST
if (is_unused_position(row, adjusted_col)) continue;
#endif
disable_unused_row(row);
ec_config.noise_floor[row][adjusted_col] += ec_readkey_raw(amux, row, col);
}
}
@@ -180,11 +200,15 @@ bool ec_matrix_scan(matrix_row_t current_matrix[]) {
for (uint8_t amux = 0; amux < AMUX_COUNT; amux++) {
disable_unused_amux(amux);
for (uint8_t col = 0; col < amux_n_col_sizes[amux]; col++) {
uint8_t sum = 0;
for (uint8_t i = 0; i < (amux > 0 ? amux : 0); i++)
sum += amux_n_col_sizes[i];
uint8_t adjusted_col = col + sum;
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
uint8_t sum = 0;
for (uint8_t i = 0; i < (amux > 0 ? amux : 0); i++)
sum += amux_n_col_sizes[i];
uint8_t adjusted_col = col + sum;
#ifdef UNUSED_POSITIONS_LIST
if (is_unused_position(row, adjusted_col)) continue;
#endif
disable_unused_row(row);
sw_value[row][adjusted_col] = ec_readkey_raw(amux, row, col);
if (ec_config.bottoming_calibration) {
@@ -266,7 +290,7 @@ bool ec_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t
uprintf("Key pressed: %d, %d, %d\n", row, col, sw_value);
}
// Has key moved up enough to be released?
else if (sw_value < ec_config.extremum[row][col] - ec_config.mode_1_release_offset) {
else if (sw_value < ec_config.extremum[row][col] - ec_config.rescaled_mode_1_release_offset[row][col]) {
ec_config.extremum[row][col] = sw_value;
*current_row &= ~(1 << col);
uprintf("Key released: %d, %d, %d\n", row, col, sw_value);
@@ -280,7 +304,7 @@ bool ec_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t
ec_config.extremum[row][col] = sw_value;
}
// Has key moved down enough to be pressed?
else if (sw_value > ec_config.extremum[row][col] + ec_config.mode_1_actuation_offset) {
else if (sw_value > ec_config.extremum[row][col] + ec_config.rescaled_mode_1_actuation_offset[row][col]) {
ec_config.extremum[row][col] = sw_value;
*current_row |= (1 << col);
uprintf("Key pressed: %d, %d, %d\n", row, col, sw_value);
@@ -312,6 +336,18 @@ void ec_print_matrix(void) {
print("\n");
}
// Check if the position is unused
#ifdef UNUSED_POSITIONS_LIST
bool is_unused_position(uint8_t row, uint8_t col) {
for (uint8_t i = 0; i < UNUSED_POSITIONS_COUNT; i++) {
if (UNUSED_POSITIONS[i][0] == row && UNUSED_POSITIONS[i][1] == col) {
return true;
}
}
return false;
}
#endif
// Rescale the value to a different range
uint16_t rescale(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

View File

@@ -37,11 +37,13 @@ typedef struct {
uint16_t mode_0_actuation_threshold; // threshold for key press in mode 0
uint16_t mode_0_release_threshold; // threshold for key release in mode 0
uint16_t mode_1_initial_deadzone_offset; // threshold for key press in mode 1 (initial deadzone)
uint8_t mode_1_actuation_offset; // offset for key press in mode 1 (1-255)
uint8_t mode_1_release_offset; // offset for key release in mode 1 (1-255)
uint16_t rescaled_mode_0_actuation_threshold[MATRIX_ROWS][MATRIX_COLS]; // threshold for key press in mode 0 rescaled to actual scale
uint16_t rescaled_mode_0_release_threshold[MATRIX_ROWS][MATRIX_COLS]; // threshold for key release in mode 0 rescaled to actual scale
uint16_t rescaled_mode_1_initial_deadzone_offset[MATRIX_ROWS][MATRIX_COLS]; // threshold for key press in mode 1 (initial deadzone) rescaled to actual scale
uint8_t mode_1_actuation_offset; // offset for key press in mode 1 (1-255)
uint8_t mode_1_release_offset; // offset for key release in mode 1 (1-255)
uint8_t rescaled_mode_1_actuation_offset[MATRIX_ROWS][MATRIX_COLS]; // offset for key press in mode 1 rescaled to actual scale
uint8_t rescaled_mode_1_release_offset[MATRIX_ROWS][MATRIX_COLS]; // offset for key release in mode 1 rescaled to actual scale
uint16_t extremum[MATRIX_ROWS][MATRIX_COLS]; // extremum values for mode 1
uint16_t noise_floor[MATRIX_ROWS][MATRIX_COLS]; // noise floor detected during startup
bool bottoming_calibration; // calibration mode for bottoming out values (true: calibration mode, false: normal mode)
@@ -58,6 +60,7 @@ extern ec_config_t ec_config;
void init_row(void);
void init_amux(void);
void disable_unused_row(uint8_t row);
void select_amux_channel(uint8_t channel, uint8_t col);
void disable_unused_amux(uint8_t channel);
void discharge_capacitor(void);
@@ -71,3 +74,11 @@ bool ec_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint
void ec_print_matrix(void);
uint16_t rescale(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max);
#ifdef UNUSED_POSITIONS_LIST
bool is_unused_position(uint8_t row, uint8_t col);
#endif
#ifdef SPLIT_KEYBOARD
void via_cmd_slave_handler(uint8_t m2s_size, const void* m2s_buffer, uint8_t s2m_size, void* s2m_buffer);
#endif

View File

@@ -19,6 +19,10 @@
#include "print.h"
#include "via.h"
#ifdef SPLIT_KEYBOARD
# include "transactions.h"
#endif
#ifdef VIA_ENABLE
void ec_rescale_values(uint8_t item);
@@ -50,6 +54,12 @@ void via_config_set_value(uint8_t *data) {
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
# ifdef SPLIT_KEYBOARD
if (is_keyboard_master()) {
transaction_rpc_send(RPC_ID_VIA_CMD, 30, data);
}
# endif
switch (*value_id) {
case id_actuation_mode: {
eeprom_ec_config.actuation_mode = value_data[0];
@@ -115,6 +125,8 @@ void via_config_set_value(uint8_t *data) {
ec_rescale_values(0);
ec_rescale_values(1);
ec_rescale_values(2);
ec_rescale_values(3);
ec_rescale_values(4);
uprintf("#############################\n");
uprintf("# Noise floor data acquired #\n");
uprintf("#############################\n");
@@ -124,13 +136,14 @@ void via_config_set_value(uint8_t *data) {
case id_show_calibration_data: {
if (value_data[0] == 0) {
ec_show_calibration_data();
break;
}
break;
}
case id_clear_bottoming_calibration_data: {
if (value_data[0] == 0) {
ec_clear_bottoming_calibration_data();
}
break;
}
default: {
// Unhandled value.
@@ -240,6 +253,22 @@ void ec_rescale_values(uint8_t item) {
}
}
break;
// Rescale the Rapid Trigger mode actuation offsets
case 3:
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
ec_config.rescaled_mode_1_actuation_offset[row][col] = rescale(ec_config.mode_1_actuation_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
}
}
break;
// Rescale the Rapid Trigger mode release offsets
case 4:
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
ec_config.rescaled_mode_1_release_offset[row][col] = rescale(ec_config.mode_1_release_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
}
}
break;
default:
// Unhandled item.
@@ -258,9 +287,11 @@ void ec_save_threshold_data(uint8_t option) {
// Save Rapid Trigger mode thresholds and rescale them for runtime usage
else if (option == 1) {
eeprom_ec_config.mode_1_initial_deadzone_offset = ec_config.mode_1_initial_deadzone_offset;
eeprom_ec_config.mode_1_actuation_offset = ec_config.mode_1_actuation_offset;
eeprom_ec_config.mode_1_release_offset = ec_config.mode_1_release_offset;
eeprom_ec_config.mode_1_actuation_offset = ec_config.mode_1_actuation_offset;
eeprom_ec_config.mode_1_release_offset = ec_config.mode_1_release_offset;
ec_rescale_values(2);
ec_rescale_values(3);
ec_rescale_values(4);
}
eeconfig_update_kb_datablock(&eeprom_ec_config);
uprintf("####################################\n");
@@ -272,11 +303,12 @@ void ec_save_threshold_data(uint8_t option) {
void ec_save_bottoming_reading(void) {
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
// If the bottom reading doesn't go over the noise floor by BOTTOMING_CALIBRATION_THRESHOLD, it is likely that:
// 1. The key is not actually in the matrix
// 2. The key is on an alternative layout, therefore not being pressed
// 3. The key in in the current layout but not being pressed
if (ec_config.bottoming_reading[row][col] < (ec_config.noise_floor[row][col] + BOTTOMING_CALIBRATION_THRESHOLD)) {
// If the calibration starter flag is still set on the key, it indicates that the key was skipped during the scan because it is not physically present.
// If the flag is not set, it means a bottoming reading was taken. If this reading doesn't exceed the noise floor by the BOTTOMING_CALIBRATION_THRESHOLD, it likely indicates one of the following:
// 1. The key is part of an alternative layout and is not being pressed.
// 2. The key is in the current layout but is not being pressed.
// In both conditions we should set the bottoming reading to the maximum value to avoid false positives.
if (ec_config.bottoming_calibration_starter[row][col] || ec_config.bottoming_reading[row][col] < (ec_config.noise_floor[row][col] + BOTTOMING_CALIBRATION_THRESHOLD)) {
eeprom_ec_config.bottoming_reading[row][col] = 1023;
} else {
eeprom_ec_config.bottoming_reading[row][col] = ec_config.bottoming_reading[row][col];
@@ -287,6 +319,8 @@ void ec_save_bottoming_reading(void) {
ec_rescale_values(0);
ec_rescale_values(1);
ec_rescale_values(2);
ec_rescale_values(3);
ec_rescale_values(4);
eeconfig_update_kb_datablock(&eeprom_ec_config);
}
@@ -360,4 +394,14 @@ void ec_clear_bottoming_calibration_data(void) {
uprintf("######################################\n");
}
# ifdef SPLIT_KEYBOARD
void via_cmd_slave_handler(uint8_t m2s_size, const void *m2s_buffer, uint8_t s2m_size, void *s2m_buffer) {
if (m2s_size == (RAW_EPSIZE-2)) {
via_config_set_value((uint8_t *)m2s_buffer);
} else {
uprintf("Unexpected response in slave handler\n");
}
}
# endif
#endif // VIA_ENABLE