~ruther/qmk_firmware

ed791972e1bf812c432655de31e3cf92f3bcf397 — Cipulot 1 year, 2 months ago 34a113c
Cipulot refactoring (#22368)

Co-authored-by: Drashna Jaelre <drashna@live.com>
Co-authored-by: Joel Challis <git@zvecr.com>
156 files changed, 1456 insertions(+), 4396 deletions(-)

A keyboards/cipulot/common/ec_board.c
A keyboards/cipulot/common/ec_switch_matrix.c
A keyboards/cipulot/common/ec_switch_matrix.h
R keyboards/cipulot/{ec_23u/ec_switch_matrix.h => common/eeprom_tools.h}
R keyboards/cipulot/{ec_23u/matrix.c => common/matrix.c}
D keyboards/cipulot/common/via_apc.c
A keyboards/cipulot/common/via_ec.c
M keyboards/cipulot/ec_23u/config.h
D keyboards/cipulot/ec_23u/ec_switch_matrix.c
M keyboards/cipulot/ec_23u/halconf.h
M keyboards/cipulot/ec_23u/info.json
M keyboards/cipulot/ec_23u/keymaps/default/keymap.c
M keyboards/cipulot/ec_23u/keymaps/via/config.h
M keyboards/cipulot/ec_23u/keymaps/via/keymap.c
M keyboards/cipulot/ec_23u/keymaps/via/rules.mk
D keyboards/cipulot/ec_23u/keymaps/via/via_apc.c
M keyboards/cipulot/ec_23u/mcuconf.h
A keyboards/cipulot/ec_23u/post_rules.mk
M keyboards/cipulot/ec_23u/readme.md
M keyboards/cipulot/ec_23u/rules.mk
M keyboards/cipulot/ec_60/config.h
D keyboards/cipulot/ec_60/ec_switch_matrix.c
D keyboards/cipulot/ec_60/ec_switch_matrix.h
M keyboards/cipulot/ec_60/halconf.h
M keyboards/cipulot/ec_60/info.json
M keyboards/cipulot/ec_60/keymaps/60_ansi_tsangan/keymap.c
M keyboards/cipulot/ec_60/keymaps/60_iso_tsangan/keymap.c
M keyboards/cipulot/ec_60/keymaps/60_jis/keymap.c
M keyboards/cipulot/ec_60/keymaps/default/keymap.c
M keyboards/cipulot/ec_60/keymaps/via/config.h
M keyboards/cipulot/ec_60/keymaps/via/keymap.c
M keyboards/cipulot/ec_60/keymaps/via/rules.mk
D keyboards/cipulot/ec_60/keymaps/via/via_apc.c
D keyboards/cipulot/ec_60/matrix.c
M keyboards/cipulot/ec_60/mcuconf.h
A keyboards/cipulot/ec_60/post_rules.mk
M keyboards/cipulot/ec_60/readme.md
M keyboards/cipulot/ec_60/rules.mk
M keyboards/cipulot/ec_alveus/1_0_0/config.h
D keyboards/cipulot/ec_alveus/1_0_0/ec_switch_matrix.c
D keyboards/cipulot/ec_alveus/1_0_0/ec_switch_matrix.h
M keyboards/cipulot/ec_alveus/1_0_0/halconf.h
M keyboards/cipulot/ec_alveus/1_0_0/info.json
M keyboards/cipulot/ec_alveus/1_0_0/keymaps/default/keymap.c
M keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/config.h
M keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/keymap.c
M keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/rules.mk
D keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/via_apc.c
D keyboards/cipulot/ec_alveus/1_0_0/matrix.c
M keyboards/cipulot/ec_alveus/1_0_0/mcuconf.h
A keyboards/cipulot/ec_alveus/1_0_0/post_rules.mk
M keyboards/cipulot/ec_alveus/1_0_0/readme.md
M keyboards/cipulot/ec_alveus/1_0_0/rules.mk
M keyboards/cipulot/ec_alveus/1_2_0/config.h
D keyboards/cipulot/ec_alveus/1_2_0/ec_switch_matrix.c
D keyboards/cipulot/ec_alveus/1_2_0/ec_switch_matrix.h
M keyboards/cipulot/ec_alveus/1_2_0/halconf.h
M keyboards/cipulot/ec_alveus/1_2_0/info.json
M keyboards/cipulot/ec_alveus/1_2_0/keymaps/default/keymap.c
M keyboards/cipulot/ec_alveus/1_2_0/keymaps/tkl_nofrow_ansi_tsangan_wkl_split_bs/keymap.c
M keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/config.h
M keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/keymap.c
M keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/rules.mk
D keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/via_apc.c
D keyboards/cipulot/ec_alveus/1_2_0/matrix.c
M keyboards/cipulot/ec_alveus/1_2_0/mcuconf.h
A keyboards/cipulot/ec_alveus/1_2_0/post_rules.mk
M keyboards/cipulot/ec_alveus/1_2_0/readme.md
M keyboards/cipulot/ec_alveus/1_2_0/rules.mk
M keyboards/cipulot/ec_pro2/config.h
D keyboards/cipulot/ec_pro2/ec_switch_matrix.c
D keyboards/cipulot/ec_pro2/ec_switch_matrix.h
M keyboards/cipulot/ec_pro2/halconf.h
M keyboards/cipulot/ec_pro2/info.json
M keyboards/cipulot/ec_pro2/keymaps/60_hhkb/keymap.c
M keyboards/cipulot/ec_pro2/keymaps/default/keymap.c
M keyboards/cipulot/ec_pro2/keymaps/via/config.h
M keyboards/cipulot/ec_pro2/keymaps/via/keymap.c
M keyboards/cipulot/ec_pro2/keymaps/via/rules.mk
D keyboards/cipulot/ec_pro2/keymaps/via/via_apc.c
D keyboards/cipulot/ec_pro2/matrix.c
M keyboards/cipulot/ec_pro2/mcuconf.h
A keyboards/cipulot/ec_pro2/post_rules.mk
M keyboards/cipulot/ec_pro2/readme.md
M keyboards/cipulot/ec_pro2/rules.mk
M keyboards/cipulot/ec_prox/ansi_iso/config.h
D keyboards/cipulot/ec_prox/ansi_iso/ec_switch_matrix.c
D keyboards/cipulot/ec_prox/ansi_iso/ec_switch_matrix.h
M keyboards/cipulot/ec_prox/ansi_iso/halconf.h
M keyboards/cipulot/ec_prox/ansi_iso/info.json
M keyboards/cipulot/ec_prox/ansi_iso/keymaps/60_hhkb/keymap.c
M keyboards/cipulot/ec_prox/ansi_iso/keymaps/default/keymap.c
M keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/config.h
M keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/keymap.c
M keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/rules.mk
D keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/via_apc.c
D keyboards/cipulot/ec_prox/ansi_iso/matrix.c
M keyboards/cipulot/ec_prox/ansi_iso/mcuconf.h
A keyboards/cipulot/ec_prox/ansi_iso/post_rules.mk
M keyboards/cipulot/ec_prox/ansi_iso/readme.md
M keyboards/cipulot/ec_prox/ansi_iso/rules.mk
M keyboards/cipulot/ec_prox/jis/config.h
D keyboards/cipulot/ec_prox/jis/ec_switch_matrix.c
D keyboards/cipulot/ec_prox/jis/ec_switch_matrix.h
M keyboards/cipulot/ec_prox/jis/halconf.h
M keyboards/cipulot/ec_prox/jis/info.json
M keyboards/cipulot/ec_prox/jis/keymaps/default/keymap.c
M keyboards/cipulot/ec_prox/jis/keymaps/via/config.h
M keyboards/cipulot/ec_prox/jis/keymaps/via/keymap.c
M keyboards/cipulot/ec_prox/jis/keymaps/via/rules.mk
D keyboards/cipulot/ec_prox/jis/keymaps/via/via_apc.c
D keyboards/cipulot/ec_prox/jis/matrix.c
M keyboards/cipulot/ec_prox/jis/mcuconf.h
A keyboards/cipulot/ec_prox/jis/post_rules.mk
M keyboards/cipulot/ec_prox/jis/readme.md
M keyboards/cipulot/ec_prox/jis/rules.mk
M keyboards/cipulot/ec_theca/config.h
D keyboards/cipulot/ec_theca/ec_switch_matrix.c
D keyboards/cipulot/ec_theca/ec_switch_matrix.h
M keyboards/cipulot/ec_theca/halconf.h
M keyboards/cipulot/ec_theca/info.json
M keyboards/cipulot/ec_theca/keymaps/default/keymap.c
M keyboards/cipulot/ec_theca/keymaps/tkl_ansi_tsangan/keymap.c
M keyboards/cipulot/ec_theca/keymaps/tkl_ansi_tsangan_wkl/keymap.c
M keyboards/cipulot/ec_theca/keymaps/via/config.h
M keyboards/cipulot/ec_theca/keymaps/via/keymap.c
M keyboards/cipulot/ec_theca/keymaps/via/rules.mk
D keyboards/cipulot/ec_theca/matrix.c
M keyboards/cipulot/ec_theca/mcuconf.h
A keyboards/cipulot/ec_theca/post_rules.mk
M keyboards/cipulot/ec_theca/readme.md
M keyboards/cipulot/ec_theca/rules.mk
M keyboards/cipulot/kawayo/config.h
M keyboards/cipulot/kawayo/keymaps/default/keymap.c
M keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker/keymap.c
M keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker_tsangan/keymap.c
M keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker_tsangan_split_bs/keymap.c
M keyboards/cipulot/kawayo/keymaps/via/keymap.c
M keyboards/cipulot/rf_r1_8_9xu/config.h
D keyboards/cipulot/rf_r1_8_9xu/ec_switch_matrix.c
D keyboards/cipulot/rf_r1_8_9xu/ec_switch_matrix.h
M keyboards/cipulot/rf_r1_8_9xu/halconf.h
M keyboards/cipulot/rf_r1_8_9xu/info.json
M keyboards/cipulot/rf_r1_8_9xu/keymaps/default/keymap.c
M keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_ansi_tsangan/keymap.c
M keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_iso_tsangan/keymap.c
M keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_jis/keymap.c
M keyboards/cipulot/rf_r1_8_9xu/keymaps/via/config.h
M keyboards/cipulot/rf_r1_8_9xu/keymaps/via/keymap.c
M keyboards/cipulot/rf_r1_8_9xu/keymaps/via/rules.mk
D keyboards/cipulot/rf_r1_8_9xu/keymaps/via/via_apc.c
D keyboards/cipulot/rf_r1_8_9xu/matrix.c
M keyboards/cipulot/rf_r1_8_9xu/mcuconf.h
A keyboards/cipulot/rf_r1_8_9xu/post_rules.mk
M keyboards/cipulot/rf_r1_8_9xu/readme.md
M keyboards/cipulot/rf_r1_8_9xu/rules.mk
A keyboards/cipulot/common/ec_board.c => keyboards/cipulot/common/ec_board.c +64 -0
@@ 0,0 1,64 @@
/* Copyright 2023 Cipulot
 *
 * 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 3 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 "ec_switch_matrix.h"
#include "quantum.h"

void eeconfig_init_kb(void) {
    // Default values
    eeprom_ec_config.actuation_mode                 = DEFAULT_ACTUATION_MODE;
    eeprom_ec_config.mode_0_actuation_threshold     = DEFAULT_MODE_0_ACTUATION_LEVEL;
    eeprom_ec_config.mode_0_release_threshold       = DEFAULT_MODE_0_RELEASE_LEVEL;
    eeprom_ec_config.mode_1_initial_deadzone_offset = DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET;
    eeprom_ec_config.mode_1_actuation_offset        = DEFAULT_MODE_1_ACTUATION_OFFSET;
    eeprom_ec_config.mode_1_release_offset          = DEFAULT_MODE_1_RELEASE_OFFSET;

    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS; col++) {
            eeprom_ec_config.bottoming_reading[row][col] = DEFAULT_BOTTOMING_READING;
        }
    }
    // Write default value to EEPROM now
    eeconfig_update_kb_datablock(&eeprom_ec_config);

    eeconfig_init_user();
}

// On Keyboard startup
void keyboard_post_init_kb(void) {
    // Read custom menu variables from memory
    eeconfig_read_kb_datablock(&eeprom_ec_config);

    // Set runtime values to EEPROM values
    ec_config.actuation_mode                 = eeprom_ec_config.actuation_mode;
    ec_config.mode_0_actuation_threshold     = eeprom_ec_config.mode_0_actuation_threshold;
    ec_config.mode_0_release_threshold       = eeprom_ec_config.mode_0_release_threshold;
    ec_config.mode_1_initial_deadzone_offset = eeprom_ec_config.mode_1_initial_deadzone_offset;
    ec_config.mode_1_actuation_offset        = eeprom_ec_config.mode_1_actuation_offset;
    ec_config.mode_1_release_offset          = eeprom_ec_config.mode_1_release_offset;
    ec_config.bottoming_calibration          = false;
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS; col++) {
            ec_config.bottoming_calibration_starter[row][col]           = true;
            ec_config.bottoming_reading[row][col]                       = eeprom_ec_config.bottoming_reading[row][col];
            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]);
        }
    }

    keyboard_post_init_user();
}

A keyboards/cipulot/common/ec_switch_matrix.c => keyboards/cipulot/common/ec_switch_matrix.c +312 -0
@@ 0,0 1,312 @@
/* Copyright 2023 Cipulot
 *
 * 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 3 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "math.h"
#include "print.h"
#include "wait.h"

#if defined(__AVR__)
#    error "AVR platforms not supported due to a variety of reasons. Among them there are limited memory, limited number of pins and ADC not being able to give satisfactory results."
#endif

#define OPEN_DRAIN_SUPPORT defined(PAL_MODE_OUTPUT_OPENDRAIN)

eeprom_ec_config_t eeprom_ec_config;
ec_config_t        ec_config;

// Pin and port array
const pin_t row_pins[]                                 = MATRIX_ROW_PINS;
const pin_t amux_sel_pins[]                            = AMUX_SEL_PINS;
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};

#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
_Static_assert(AMUX_SEL_PINS_COUNT == EXPECTED_AMUX_SEL_PINS_COUNT), "AMUX_SEL_PINS doesn't have the minimum number of bits required address all the channels");
// Check that number of elements in AMUX_COL_CHANNELS_SIZES is enough to specify the number of channels for all the multiplexers available
_Static_assert(ARRAY_SIZE(amux_n_col_sizes) == AMUX_COUNT, "AMUX_COL_CHANNELS_SIZES doesn't have the minimum number of elements required to specify the number of channels for all the multiplexers available");

static uint16_t sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

// Initialize the row pins
void init_row(void) {
    // Set all row pins as output and low
    for (uint8_t idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

// Initialize the multiplexers
void init_amux(void) {
    for (uint8_t idx = 0; idx < AMUX_COUNT; idx++) {
        setPinOutput(amux_en_pins[idx]);
        writePinLow(amux_en_pins[idx]);
    }
    for (uint8_t idx = 0; idx < AMUX_SEL_PINS_COUNT; idx++) {
        setPinOutput(amux_sel_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
    uint8_t ch = amux_n_col_channels[channel][col];
    // momentarily disable specified multiplexer
    writePinHigh(amux_en_pins[channel]);
    // Select the multiplexer channel
    for (uint8_t i = 0; i < AMUX_SEL_PINS_COUNT; i++) {
        writePin(amux_sel_pins[i], ch & (1 << i));
    }
    // re enable specified multiplexer
    writePinLow(amux_en_pins[channel]);
}

// Disable all the unused multiplexers
void disable_unused_amux(uint8_t channel) {
    // disable all the other multiplexers apart from the current selected one
    for (uint8_t idx = 0; idx < AMUX_COUNT; idx++) {
        if (idx != channel) {
            writePinHigh(amux_en_pins[idx]);
        }
    }
}
// Discharge the peak hold capacitor
void discharge_capacitor(void) {
#ifdef OPEN_DRAIN_SUPPORT
    writePinLow(DISCHARGE_PIN);
#else
    writePinLow(DISCHARGE_PIN);
    setPinOutput(DISCHARGE_PIN);
#endif
}

// Charge the peak hold capacitor
void charge_capacitor(uint8_t row) {
#ifdef OPEN_DRAIN_SUPPORT
    writePinHigh(DISCHARGE_PIN);
#else
    setPinInput(DISCHARGE_PIN);
#endif
    writePinHigh(row_pins[row]);
}

// Initialize the peripherals pins
int ec_init(void) {
    // Initialize ADC
    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    // Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
#ifdef OPEN_DRAIN_SUPPORT
    setPinOutputOpenDrain(DISCHARGE_PIN);
#else
    setPinOutput(DISCHARGE_PIN);
#endif

    // Initialize drive lines
    init_row();

    // Initialize AMUXs
    init_amux();

    return 0;
}

// Get the noise floor
void ec_noise_floor(void) {
    // Initialize the noise floor
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS; col++) {
            ec_config.noise_floor[row][col] = 0;
        }
    }

    // Sample the noise floor
    for (uint8_t i = 0; i < DEFAULT_NOISE_FLOOR_SAMPLING_COUNT; i++) {
        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 adjusted_col = amux == 0 ? col : col + amux_n_col_sizes[amux - 1];
                for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
                    ec_config.noise_floor[row][adjusted_col] += ec_readkey_raw(amux, row, col);
                }
            }
        }
        wait_ms(5);
    }

    // Average the noise floor
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS; col++) {
            ec_config.noise_floor[row][col] /= DEFAULT_NOISE_FLOOR_SAMPLING_COUNT;
        }
    }
}

// Scan key values and update matrix state
bool ec_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    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++) {
            for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
                uint8_t adjusted_col        = amux == 0 ? col : col + amux_n_col_sizes[amux - 1];
                sw_value[row][adjusted_col] = ec_readkey_raw(amux, row, col);

                if (ec_config.bottoming_calibration) {
                    if (ec_config.bottoming_calibration_starter[row][adjusted_col]) {
                        ec_config.bottoming_reading[row][adjusted_col]             = sw_value[row][adjusted_col];
                        ec_config.bottoming_calibration_starter[row][adjusted_col] = false;
                    } else if (sw_value[row][adjusted_col] > ec_config.bottoming_reading[row][adjusted_col]) {
                        ec_config.bottoming_reading[row][adjusted_col] = sw_value[row][adjusted_col];
                    }
                } else {
                    updated |= ec_update_key(&current_matrix[row], row, adjusted_col, sw_value[row][adjusted_col]);
                }
            }
        }
    }

    return ec_config.bottoming_calibration ? false : updated;
}

// Read the capacitive sensor value
uint16_t ec_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    select_amux_channel(channel, col);

    // Set the row pin to low state to avoid ghosting
    writePinLow(row_pins[row]);

    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ec_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Real Time Noise Floor Calibration
    if (sw_value < (ec_config.noise_floor[row][col] - NOISE_FLOOR_THRESHOLD)) {
        uprintf("Noise Floor Change: %d, %d, %d\n", row, col, sw_value);
        ec_config.noise_floor[row][col]                             = sw_value;
        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]);
    }

    // Normal board-wide APC
    if (ec_config.actuation_mode == 0) {
        if (current_state && sw_value < ec_config.rescaled_mode_0_release_threshold[row][col]) {
            *current_row &= ~(1 << col);
            uprintf("Key released: %d, %d, %d\n", row, col, sw_value);
            return true;
        }
        if ((!current_state) && sw_value > ec_config.rescaled_mode_0_actuation_threshold[row][col]) {
            *current_row |= (1 << col);
            uprintf("Key pressed: %d, %d, %d\n", row, col, sw_value);
            return true;
        }
    }
    // Rapid Trigger
    else if (ec_config.actuation_mode == 1) {
        // Is key in active zone?
        if (sw_value > ec_config.rescaled_mode_1_initial_deadzone_offset[row][col]) {
            // Is key pressed while in active zone?
            if (current_state) {
                // Is the key still moving down?
                if (sw_value > ec_config.extremum[row][col]) {
                    ec_config.extremum[row][col] = sw_value;
                    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) {
                    ec_config.extremum[row][col] = sw_value;
                    *current_row &= ~(1 << col);
                    uprintf("Key released: %d, %d, %d\n", row, col, sw_value);
                    return true;
                }
            }
            // Key is not pressed while in active zone
            else {
                // Is the key still moving up?
                if (sw_value < ec_config.extremum[row][col]) {
                    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) {
                    ec_config.extremum[row][col] = sw_value;
                    *current_row |= (1 << col);
                    uprintf("Key pressed: %d, %d, %d\n", row, col, sw_value);
                    return true;
                }
            }
        }
        // Key is not in active zone
        else {
            // Check to avoid key being stuck in pressed state near the active zone threshold
            if (sw_value < ec_config.extremum[row][col]) {
                ec_config.extremum[row][col] = sw_value;
                *current_row &= ~(1 << col);
                return true;
            }
        }
    }
    return false;
}

// Print the matrix values
void ec_print_matrix(void) {
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS - 1; col++) {
            uprintf("%4d,", sw_value[row][col]);
        }
        uprintf("%4d\n", sw_value[row][MATRIX_COLS - 1]);
    }
    print("\n");
}

// 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;
}

A keyboards/cipulot/common/ec_switch_matrix.h => keyboards/cipulot/common/ec_switch_matrix.h +72 -0
@@ 0,0 1,72 @@
/* Copyright 2023 Cipulot
 *
 * 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 3 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>
#include "matrix.h"
#include "eeconfig.h"

typedef struct PACKED {
    uint8_t  actuation_mode;                              // 0: normal board-wide APC, 1: Rapid trigger from specific board-wide actuation point, 2: Rapid trigger from resting point
    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
    uint8_t  mode_1_actuation_offset;                     // offset for key press in mode 1 and 2 (1-255)
    uint8_t  mode_1_release_offset;                       // offset for key release in mode 1 and 2 (1-255)
    uint16_t bottoming_reading[MATRIX_ROWS][MATRIX_COLS]; // bottoming reading
} eeprom_ec_config_t;

typedef struct {
    uint8_t  actuation_mode;                                                    // 0: normal board-wide APC, 1: Rapid trigger from specific board-wide actuation point (it can be very near that baseline noise and be "full travel")
    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)
    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)
    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)
    bool     bottoming_calibration_starter[MATRIX_ROWS][MATRIX_COLS];           // calibration mode for bottoming out values (true: calibration mode, false: normal mode)
    uint16_t bottoming_reading[MATRIX_ROWS][MATRIX_COLS];                       // bottoming reading
} ec_config_t;

// Check if the size of the reserved persistent memory is the same as the size of struct eeprom_ec_config_t
_Static_assert(sizeof(eeprom_ec_config_t) == EECONFIG_KB_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

extern eeprom_ec_config_t eeprom_ec_config;

extern ec_config_t ec_config;

void init_row(void);
void init_amux(void);
void select_amux_channel(uint8_t channel, uint8_t col);
void disable_unused_amux(uint8_t channel);
void discharge_capacitor(void);
void charge_capacitor(uint8_t row);

int      ec_init(void);
void     ec_noise_floor(void);
bool     ec_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ec_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ec_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
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);

R keyboards/cipulot/ec_23u/ec_switch_matrix.h => keyboards/cipulot/common/eeprom_tools.h +8 -18
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 13,24 13,14 @@
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;
#include "eeprom.h"

ecsm_config_t ecsm_config;
#if (EECONFIG_KB_DATA_SIZE) > 0
#    define EEPROM_KB_PARTIAL_UPDATE(__struct, __field) eeprom_update_block(&(__struct.__field), (void *)((void *)(EECONFIG_KB_DATABLOCK) + offsetof(typeof(__struct), __field)), sizeof(__struct.__field))
#endif

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);
#if (EECONFIG_USER_DATA_SIZE) > 0
#    define EEPROM_USER_PARTIAL_UPDATE(__struct, __field) eeprom_update_block(&(__struct.__field), (void *)((void *)(EECONFIG_USER_DATABLOCK) + offsetof(typeof(__struct), __field)), sizeof(__struct.__field))
#endif

R keyboards/cipulot/ec_23u/matrix.c => keyboards/cipulot/common/matrix.c +13 -15
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 17,28 17,26 @@
#include "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

// Custom matrix init function
void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Initialize EC
    ec_init();

    ecsm_init(&ecsm_config);
    // Get the noise floor at boot
    ec_noise_floor();
}

// Custom matrix scan function
bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);
    bool updated = ec_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

// Bootmagic overriden to avoid conflicts with EC
void bootmagic_scan(void) {
    ;
}

D keyboards/cipulot/common/via_apc.c => keyboards/cipulot/common/via_apc.c +0 -158
@@ 1,158 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

#ifdef VIA_ENABLE

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}
#endif // VIA_ENABLE

A keyboards/cipulot/common/via_ec.c => keyboards/cipulot/common/via_ec.c +363 -0
@@ 0,0 1,363 @@
/* Copyright 2023 Cipulot
 *
 * 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 3 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 "eeprom_tools.h"
#include "ec_switch_matrix.h"
#include "action.h"
#include "print.h"
#include "via.h"

#ifdef VIA_ENABLE

void ec_rescale_values(uint8_t item);
void ec_save_threshold_data(uint8_t option);
void ec_save_bottoming_reading(void);
void ec_show_calibration_data(void);
void ec_clear_bottoming_calibration_data(void);

// Declaring enums for VIA config menu
enum via_enums {
    // clang-format off
    id_actuation_mode = 1,
    id_mode_0_actuation_threshold = 2,
    id_mode_0_release_threshold = 3,
    id_save_threshold_data = 4,
    id_mode_1_initial_deadzone_offset = 5,
    id_mode_1_actuation_offset = 6,
    id_mode_1_release_offset = 7,
    id_bottoming_calibration = 8,
    id_noise_floor_calibration = 9,
    id_show_calibration_data = 10,
    id_clear_bottoming_calibration_data = 11
    // clang-format on
};

// Handle the data received by the keyboard from the VIA menus
void via_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_actuation_mode: {
            eeprom_ec_config.actuation_mode = value_data[0];
            ec_config.actuation_mode        = eeprom_ec_config.actuation_mode;
            if (ec_config.actuation_mode == 0) {
                uprintf("#########################\n");
                uprintf("#  Actuation Mode: APC  #\n");
                uprintf("#########################\n");
            } else if (ec_config.actuation_mode == 1) {
                uprintf("#################################\n");
                uprintf("# Actuation Mode: Rapid Trigger #\n");
                uprintf("#################################\n");
            }
            EEPROM_KB_PARTIAL_UPDATE(eeprom_ec_config, actuation_mode);
            break;
        }
        case id_mode_0_actuation_threshold: {
            ec_config.mode_0_actuation_threshold = value_data[1] | (value_data[0] << 8);
            uprintf("APC Mode Actuation Threshold: %d\n", ec_config.mode_0_actuation_threshold);
            break;
        }
        case id_mode_0_release_threshold: {
            ec_config.mode_0_release_threshold = value_data[1] | (value_data[0] << 8);
            uprintf("APC Mode Release Threshold: %d\n", ec_config.mode_0_release_threshold);
            break;
        }
        case id_mode_1_initial_deadzone_offset: {
            ec_config.mode_1_initial_deadzone_offset = value_data[1] | (value_data[0] << 8);
            uprintf("Rapid Trigger Mode Initial Deadzone Offset: %d\n", ec_config.mode_1_initial_deadzone_offset);
            break;
        }
        case id_mode_1_actuation_offset: {
            ec_config.mode_1_actuation_offset = value_data[0];
            uprintf("Rapid Trigger Mode Actuation Offset: %d\n", ec_config.mode_1_actuation_offset);
            break;
        }
        case id_mode_1_release_offset: {
            ec_config.mode_1_release_offset = value_data[0];
            uprintf("Rapid Trigger Mode Release Offset: %d\n", ec_config.mode_1_release_offset);
            break;
        }
        case id_bottoming_calibration: {
            if (value_data[0] == 1) {
                ec_config.bottoming_calibration = true;
                uprintf("##############################\n");
                uprintf("# Bottoming calibration mode #\n");
                uprintf("##############################\n");
            } else {
                ec_config.bottoming_calibration = false;
                ec_save_bottoming_reading();
                uprintf("## Bottoming calibration done ##\n");
                ec_show_calibration_data();
            }
            break;
        }
        case id_save_threshold_data: {
            ec_save_threshold_data(value_data[0]);
            break;
        }
        case id_noise_floor_calibration: {
            if (value_data[0] == 0) {
                ec_noise_floor();
                ec_rescale_values(0);
                ec_rescale_values(1);
                ec_rescale_values(2);
                uprintf("#############################\n");
                uprintf("# Noise floor data acquired #\n");
                uprintf("#############################\n");
                break;
            }
        }
        case id_show_calibration_data: {
            if (value_data[0] == 0) {
                ec_show_calibration_data();
                break;
            }
        }
        case id_clear_bottoming_calibration_data: {
            if (value_data[0] == 0) {
                ec_clear_bottoming_calibration_data();
            }
        }
        default: {
            // Unhandled value.
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void via_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_actuation_mode: {
            value_data[0] = eeprom_ec_config.actuation_mode;
            break;
        }
        case id_mode_0_actuation_threshold: {
            value_data[0] = eeprom_ec_config.mode_0_actuation_threshold >> 8;
            value_data[1] = eeprom_ec_config.mode_0_actuation_threshold & 0xFF;
            break;
        }
        case id_mode_0_release_threshold: {
            value_data[0] = eeprom_ec_config.mode_0_release_threshold >> 8;
            value_data[1] = eeprom_ec_config.mode_0_release_threshold & 0xFF;
            break;
        }
        case id_mode_1_initial_deadzone_offset: {
            value_data[0] = eeprom_ec_config.mode_1_initial_deadzone_offset >> 8;
            value_data[1] = eeprom_ec_config.mode_1_initial_deadzone_offset & 0xFF;
            break;
        }
        case id_mode_1_actuation_offset: {
            value_data[0] = eeprom_ec_config.mode_1_actuation_offset;
            break;
        }
        case id_mode_1_release_offset: {
            value_data[0] = eeprom_ec_config.mode_1_release_offset;
            break;
        }
        default: {
            // Unhandled value.
            break;
        }
    }
}

// Handle the commands sent and received by the keyboard with VIA
void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                via_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                via_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                // Bypass the save function in favor of pinpointed saves
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Rescale the values received by VIA to fit the new range
void ec_rescale_values(uint8_t item) {
    switch (item) {
        // Rescale the APC mode actuation thresholds
        case 0:
            for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
                for (uint8_t col = 0; col < MATRIX_COLS; col++) {
                    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]);
                }
            }
            break;
        // Rescale the APC mode release thresholds
        case 1:
            for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
                for (uint8_t col = 0; col < MATRIX_COLS; 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]);
                }
            }
            break;
        // Rescale the Rapid Trigger mode initial deadzone offsets
        case 2:
            for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
                for (uint8_t col = 0; col < MATRIX_COLS; 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]);
                }
            }
            break;

        default:
            // Unhandled item.
            break;
    }
}

void ec_save_threshold_data(uint8_t option) {
    // Save APC mode thresholds and rescale them for runtime usage
    if (option == 0) {
        eeprom_ec_config.mode_0_actuation_threshold = ec_config.mode_0_actuation_threshold;
        eeprom_ec_config.mode_0_release_threshold   = ec_config.mode_0_release_threshold;
        ec_rescale_values(0);
        ec_rescale_values(1);
    }
    // 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;
        ec_rescale_values(2);
    }
    eeconfig_update_kb_datablock(&eeprom_ec_config);
    uprintf("####################################\n");
    uprintf("# New thresholds applied and saved #\n");
    uprintf("####################################\n");
}

// Save the bottoming reading
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)) {
                eeprom_ec_config.bottoming_reading[row][col] = 1023;
            } else {
                eeprom_ec_config.bottoming_reading[row][col] = ec_config.bottoming_reading[row][col];
            }
        }
    }
    // Rescale the values to fit the new range for runtime usage
    ec_rescale_values(0);
    ec_rescale_values(1);
    ec_rescale_values(2);
    eeconfig_update_kb_datablock(&eeprom_ec_config);
}

// Show the calibration data
void ec_show_calibration_data(void) {
    uprintf("\n###############\n");
    uprintf("# Noise Floor #\n");
    uprintf("###############\n");
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS - 1; col++) {
            uprintf("%4d,", ec_config.noise_floor[row][col]);
        }
        uprintf("%4d\n", ec_config.noise_floor[row][MATRIX_COLS - 1]);
    }

    uprintf("\n######################\n");
    uprintf("# Bottoming Readings #\n");
    uprintf("######################\n");
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS - 1; col++) {
            uprintf("%4d,", eeprom_ec_config.bottoming_reading[row][col]);
        }
        uprintf("%4d\n", eeprom_ec_config.bottoming_reading[row][MATRIX_COLS - 1]);
    }

    uprintf("\n######################################\n");
    uprintf("# Rescaled APC Mode Actuation Points #\n");
    uprintf("######################################\n");
    uprintf("Original APC Mode Actuation Point: %4d\n", ec_config.mode_0_actuation_threshold);
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS - 1; col++) {
            uprintf("%4d,", ec_config.rescaled_mode_0_actuation_threshold[row][col]);
        }
        uprintf("%4d\n", ec_config.rescaled_mode_0_actuation_threshold[row][MATRIX_COLS - 1]);
    }

    uprintf("\n######################################\n");
    uprintf("# Rescaled APC Mode Release Points   #\n");
    uprintf("######################################\n");
    uprintf("Original APC Mode Release Point: %4d\n", ec_config.mode_0_release_threshold);
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS - 1; col++) {
            uprintf("%4d,", ec_config.rescaled_mode_0_release_threshold[row][col]);
        }
        uprintf("%4d\n", ec_config.rescaled_mode_0_release_threshold[row][MATRIX_COLS - 1]);
    }

    uprintf("\n#######################################################\n");
    uprintf("# Rescaled Rapid Trigger Mode Initial Deadzone Offset #\n");
    uprintf("#######################################################\n");
    uprintf("Original Rapid Trigger Mode Initial Deadzone Offset: %4d\n", ec_config.mode_1_initial_deadzone_offset);
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        for (uint8_t col = 0; col < MATRIX_COLS - 1; col++) {
            uprintf("%4d,", ec_config.rescaled_mode_1_initial_deadzone_offset[row][col]);
        }
        uprintf("%4d\n", ec_config.rescaled_mode_1_initial_deadzone_offset[row][MATRIX_COLS - 1]);
    }
    print("\n");
}

// Clear the calibration data
void ec_clear_bottoming_calibration_data(void) {
    // Clear the EEPROM data
    eeconfig_init_kb();

    // Reset the runtime values to the EEPROM values
    keyboard_post_init_kb();

    uprintf("######################################\n");
    uprintf("# Bottoming calibration data cleared #\n");
    uprintf("######################################\n");
}

#endif // VIA_ENABLE

M keyboards/cipulot/ec_23u/config.h => keyboards/cipulot/ec_23u/config.h +36 -14
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,29 19,51 @@
#define MATRIX_ROWS 4
#define MATRIX_COLS 6

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { B13, B12, A7, B0 }
#define MATRIX_COL_CHANNELS \
    { 4, 6, 3, 2, 0, 1 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 1
#define AMUX_MAX_COLS_COUNT 6

#define AMUX_EN_PINS \
    { C15 }

#define AMUX_SEL_PINS \
    { C14, C13, B6 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN C15
#define AMUX_COL_CHANNELS_SIZES \
    { 6 }

#define AMUX_0_COL_CHANNELS \
    { 4, 6, 3, 2, 0, 1 }

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS

#define DISCHARGE_PIN A3
#define ANALOG_PORT A2

/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
//#define DEBOUNCE 5
#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 100
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE

#define EECONFIG_KB_DATA_SIZE 58

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 550
#define DEFAULT_RELEASE_LEVEL 500

#define DISCHARGE_TIME 10

D keyboards/cipulot/ec_23u/ec_switch_matrix.c => keyboards/cipulot/ec_23u/ec_switch_matrix.c +0 -165
@@ 1,165 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    //Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN);
    writePinLow(APLEX_EN_PIN);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    writePinHigh(APLEX_EN_PIN);
    select_mux(col);
    writePinLow(APLEX_EN_PIN);

    // Set strobe pins to low state
    writePinLow(row_pins[row]);
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

M keyboards/cipulot/ec_23u/halconf.h => keyboards/cipulot/ec_23u/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/ec_23u/info.json => keyboards/cipulot/ec_23u/info.json +1 -7
@@ 8,19 8,13 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "audio": false,
        "backlight": false,
        "bootmagic": true,
        "command": false,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,
        "nkro": true,
        "rgblight": true
    },
    "mouse_key": {
        "enabled": true
    },
    "indicators": {
        "num_lock": "B14"
    },

M keyboards/cipulot/ec_23u/keymaps/default/keymap.c => keyboards/cipulot/ec_23u/keymaps/default/keymap.c +3 -20
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 28,27 28,10 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {

    [1] = LAYOUT_all(
        RGB_TOG, RGB_VAD, RGB_VAI, _______,
        _______, _______, _______, NK_TOGG,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, QK_BOOT, _______),


    [2] = LAYOUT_all(
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______),

    [3] = LAYOUT_all(
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______)
        _______, _______, QK_BOOT, _______)
    // clang-format on
};

M keyboards/cipulot/ec_23u/keymaps/via/config.h => keyboards/cipulot/ec_23u/keymaps/via/config.h +3 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,7 @@

#pragma once

// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1
// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4

M keyboards/cipulot/ec_23u/keymaps/via/keymap.c => keyboards/cipulot/ec_23u/keymaps/via/keymap.c +3 -20
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 28,27 28,10 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {

    [1] = LAYOUT_all(
        RGB_TOG, RGB_VAD, RGB_VAI, _______,
        _______, _______, _______, NK_TOGG,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, QK_BOOT, _______),


    [2] = LAYOUT_all(
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______),

    [3] = LAYOUT_all(
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______,
        _______, _______, _______, _______)
        _______, _______, QK_BOOT, _______)
    // clang-format on
};

M keyboards/cipulot/ec_23u/keymaps/via/rules.mk => keyboards/cipulot/ec_23u/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += via_apc.c

D keyboards/cipulot/ec_23u/keymaps/via/via_apc.c => keyboards/cipulot/ec_23u/keymaps/via/via_apc.c +0 -156
@@ 1,156 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

M keyboards/cipulot/ec_23u/mcuconf.h => keyboards/cipulot/ec_23u/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/ec_23u/post_rules.mk => keyboards/cipulot/ec_23u/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/ec_23u/readme.md => keyboards/cipulot/ec_23u/readme.md +3 -4
@@ 20,8 20,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical Boot0 pins**: Short the Boot0 pins on the back of the PCB while plugging in the keyboard
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available
* **Physical reset**: Long short the exposed pads on the top of the PCB
* **Keycode in layout**: Press the key mapped to QK_BOOT if it is available

M keyboards/cipulot/ec_23u/rules.mk => keyboards/cipulot/ec_23u/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 2

M keyboards/cipulot/ec_60/config.h => keyboards/cipulot/ec_60/config.h +40 -13
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,27 19,54 @@
#define MATRIX_ROWS 5
#define MATRIX_COLS 15

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { B15, A8, B0, A7, B1 }
#define MATRIX_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6, 4 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 2
#define AMUX_MAX_COLS_COUNT 8

#define AMUX_EN_PINS \
    { B7, B3 }

#define AMUX_SEL_PINS \
    { B6, B5, B4 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN_0 B7
#define APLEX_EN_PIN_1 B3
#define AMUX_COL_CHANNELS_SIZES \
    { 8, 7 }

#define AMUX_0_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6, 4 }

#define AMUX_1_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6 }

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS, AMUX_1_COL_CHANNELS

#define DISCHARGE_PIN A6
#define ANALOG_PORT A3

#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 100
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE

#define EECONFIG_KB_DATA_SIZE 160

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 550
#define DEFAULT_RELEASE_LEVEL 500

#define DISCHARGE_TIME 10

D keyboards/cipulot/ec_60/ec_switch_matrix.c => keyboards/cipulot/ec_60/ec_switch_matrix.c +0 -183
@@ 1,183 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    //Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN_0);
    writePinLow(APLEX_EN_PIN_0);
    setPinOutput(APLEX_EN_PIN_1);
    writePinLow(APLEX_EN_PIN_1);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    if (channel == 0) {
        writePinHigh(APLEX_EN_PIN_0);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_0);
    } else {
        writePinHigh(APLEX_EN_PIN_1);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_1);
    }

    // Set strobe pins to low state
    writePinLow(row_pins[row]);
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_1);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_0);
    for (int col = 0; col < (sizeof(col_channels) - 1); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
        }
    }
    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

D keyboards/cipulot/ec_60/ec_switch_matrix.h => keyboards/cipulot/ec_60/ec_switch_matrix.h +0 -36
@@ 1,36 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;

ecsm_config_t ecsm_config;

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);

M keyboards/cipulot/ec_60/halconf.h => keyboards/cipulot/ec_60/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/ec_60/info.json => keyboards/cipulot/ec_60/info.json +2 -9
@@ 8,18 8,11 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "audio": false,
        "backlight": false,
        "bootmagic": true,
        "command": false,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,
        "nkro": true,
        "rgblight": false
    },
    "mouse_key": {
        "enabled": true
        "nkro": true
    },
    "processor": "STM32F401",
    "url": "https://www.github.com/Cipulot/EC60",

M keyboards/cipulot/ec_60/keymaps/60_ansi_tsangan/keymap.c => keyboards/cipulot/ec_60/keymaps/60_ansi_tsangan/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 32,14 32,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        _______, _______, _______,                             _______,                                     _______, _______, _______),

    [2] = LAYOUT_60_ansi_tsangan(
        QK_BOOT, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______,                            _______,                                     _______, _______, _______),

    [3] = LAYOUT_60_ansi_tsangan(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/ec_60/keymaps/60_iso_tsangan/keymap.c => keyboards/cipulot/ec_60/keymaps/60_iso_tsangan/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 32,14 32,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        _______, _______, _______,                             _______,                                     _______, _______, _______),

    [2] = LAYOUT_60_iso_tsangan(
        QK_BOOT, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______,                            _______,                                     _______, _______, _______),

    [3] = LAYOUT_60_iso_tsangan(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/ec_60/keymaps/60_jis/keymap.c => keyboards/cipulot/ec_60/keymaps/60_jis/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 34,14 34,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        _______, _______, _______, _______,                             _______,          _______, _______, _______, _______, _______),

    [2] = LAYOUT_60_jis(
        QK_BOOT, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______,                            _______,          _______, _______, _______, _______, _______),

    [3] = LAYOUT_60_jis(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/ec_60/keymaps/default/keymap.c => keyboards/cipulot/ec_60/keymaps/default/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 34,14 34,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        _______,  _______,  _______,  _______,        _______,  _______,       _______,  _______,  _______,  _______,  _______),

    [2] = LAYOUT_all(
        QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,       _______,  _______,       _______,  _______,  _______,  _______,  _______),

    [3] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_60/keymaps/via/config.h => keyboards/cipulot/ec_60/keymaps/via/config.h +3 -3
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,5 @@

#pragma once

// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4
// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1

M keyboards/cipulot/ec_60/keymaps/via/keymap.c => keyboards/cipulot/ec_60/keymaps/via/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 34,14 34,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        _______,  _______,  _______,  _______,        _______,  _______,       _______,  _______,  _______,  _______,  _______),

    [2] = LAYOUT_all(
        QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,       _______,  _______,       _______,  _______,  _______,  _______,  _______),

    [3] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_60/keymaps/via/rules.mk => keyboards/cipulot/ec_60/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += via_apc.c

D keyboards/cipulot/ec_60/keymaps/via/via_apc.c => keyboards/cipulot/ec_60/keymaps/via/via_apc.c +0 -156
@@ 1,156 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

D keyboards/cipulot/ec_60/matrix.c => keyboards/cipulot/ec_60/matrix.c +0 -44
@@ 1,44 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;

    ecsm_init(&ecsm_config);
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

M keyboards/cipulot/ec_60/mcuconf.h => keyboards/cipulot/ec_60/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/ec_60/post_rules.mk => keyboards/cipulot/ec_60/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/ec_60/readme.md => keyboards/cipulot/ec_60/readme.md +1 -2
@@ 20,8 20,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical Boot0 pins**: Short the Boot0 pins on the back of the PCB while plugging in the keyboard
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available

M keyboards/cipulot/ec_60/rules.mk => keyboards/cipulot/ec_60/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 3

M keyboards/cipulot/ec_alveus/1_0_0/config.h => keyboards/cipulot/ec_alveus/1_0_0/config.h +39 -13
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,27 19,53 @@
#define MATRIX_ROWS 5
#define MATRIX_COLS 16

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { A14, B3, A15, B5, B4 }
#define MATRIX_COL_CHANNELS \
    { 3, 0, 1, 2, 6, 5, 7, 4 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 2
#define AMUX_MAX_COLS_COUNT 8

#define AMUX_EN_PINS \
    { C13, C14 }

#define AMUX_SEL_PINS \
    { B7, B8, B9 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN_0 C13
#define APLEX_EN_PIN_1 C14
#define AMUX_COL_CHANNELS_SIZES \
    { 8, 8 }

#define AMUX_0_COL_CHANNELS \
    { 3, 0, 1, 2, 6, 5, 7, 4 }

#define AMUX_1_COL_CHANNELS AMUX_0_COL_CHANNELS

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS, AMUX_1_COL_CHANNELS

#define DISCHARGE_PIN B1
#define ANALOG_PORT A3

#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 100
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE

#define EECONFIG_KB_DATA_SIZE 170

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 550
#define DEFAULT_RELEASE_LEVEL 500

#define DISCHARGE_TIME 10

D keyboards/cipulot/ec_alveus/1_0_0/ec_switch_matrix.c => keyboards/cipulot/ec_alveus/1_0_0/ec_switch_matrix.c +0 -183
@@ 1,183 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    // Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN_0);
    writePinLow(APLEX_EN_PIN_0);
    setPinOutput(APLEX_EN_PIN_1);
    writePinLow(APLEX_EN_PIN_1);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    if (channel == 0) {
        writePinHigh(APLEX_EN_PIN_0);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_0);
    } else {
        writePinHigh(APLEX_EN_PIN_1);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_1);
    }

    // Set strobe pins to low state
    writePinLow(row_pins[row]);
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_1);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_0);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
        }
    }
    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

D keyboards/cipulot/ec_alveus/1_0_0/ec_switch_matrix.h => keyboards/cipulot/ec_alveus/1_0_0/ec_switch_matrix.h +0 -36
@@ 1,36 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;

ecsm_config_t ecsm_config;

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);

M keyboards/cipulot/ec_alveus/1_0_0/halconf.h => keyboards/cipulot/ec_alveus/1_0_0/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/ec_alveus/1_0_0/info.json => keyboards/cipulot/ec_alveus/1_0_0/info.json +2 -9
@@ 8,18 8,11 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "audio": false,
        "backlight": false,
        "bootmagic": true,
        "command": false,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,
        "nkro": true,
        "rgblight": false
    },
    "mouse_key": {
        "enabled": true
        "nkro": true
    },
    "processor": "STM32F401",
    "usb": {

M keyboards/cipulot/ec_alveus/1_0_0/keymaps/default/keymap.c => keyboards/cipulot/ec_alveus/1_0_0/keymaps/default/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 33,14 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
    _______,            _______,                      _______,                                                    _______,            _______,   _______,  _______,  _______),

    [2] = LAYOUT_tkl_nofrow_ansi_tsangan_wkl(
    QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                  _______,
    _______,            _______,                      _______,                                                    _______,            _______,   _______,  _______,  _______),

    [3] = LAYOUT_tkl_nofrow_ansi_tsangan_wkl(
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                  _______,

M keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/config.h => keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/config.h +3 -3
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,5 @@

#pragma once

// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4
// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1

M keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/keymap.c => keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 33,14 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        _______,  _______,  _______,                      _______,                                                    _______,  _______,  _______,   _______,  _______,  _______),

    [2] = LAYOUT_all(
        QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                  _______,
        _______,  _______,  _______,                      _______,                                                    _______,  _______,  _______,   _______,  _______,  _______),

    [3] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                  _______,

M keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/rules.mk => keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += via_apc.c

D keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/via_apc.c => keyboards/cipulot/ec_alveus/1_0_0/keymaps/via/via_apc.c +0 -156
@@ 1,156 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

D keyboards/cipulot/ec_alveus/1_0_0/matrix.c => keyboards/cipulot/ec_alveus/1_0_0/matrix.c +0 -44
@@ 1,44 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;

    ecsm_init(&ecsm_config);
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

M keyboards/cipulot/ec_alveus/1_0_0/mcuconf.h => keyboards/cipulot/ec_alveus/1_0_0/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/ec_alveus/1_0_0/post_rules.mk => keyboards/cipulot/ec_alveus/1_0_0/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/ec_alveus/1_0_0/readme.md => keyboards/cipulot/ec_alveus/1_0_0/readme.md +1 -2
@@ 20,8 20,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical reset**: Long short the exposed pads on the top of the PCB
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available

M keyboards/cipulot/ec_alveus/1_0_0/rules.mk => keyboards/cipulot/ec_alveus/1_0_0/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 3

M keyboards/cipulot/ec_alveus/1_2_0/config.h => keyboards/cipulot/ec_alveus/1_2_0/config.h +39 -13
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,27 19,53 @@
#define MATRIX_ROWS 5
#define MATRIX_COLS 16

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { A14, B3, A15, B5, B4 }
#define MATRIX_COL_CHANNELS \
    { 3, 0, 1, 2, 6, 5, 7, 4 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 2
#define AMUX_MAX_COLS_COUNT 8

#define AMUX_EN_PINS \
    { C13, C14 }

#define AMUX_SEL_PINS \
    { B7, B8, B9 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN_0 C13
#define APLEX_EN_PIN_1 C14
#define AMUX_COL_CHANNELS_SIZES \
    { 8, 8 }

#define AMUX_0_COL_CHANNELS \
    { 3, 0, 1, 2, 6, 5, 7, 4 }

#define AMUX_1_COL_CHANNELS AMUX_0_COL_CHANNELS

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS, AMUX_1_COL_CHANNELS

#define DISCHARGE_PIN B1
#define ANALOG_PORT A3

#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 100
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE

#define EECONFIG_KB_DATA_SIZE 170

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 550
#define DEFAULT_RELEASE_LEVEL 500

#define DISCHARGE_TIME 10

D keyboards/cipulot/ec_alveus/1_2_0/ec_switch_matrix.c => keyboards/cipulot/ec_alveus/1_2_0/ec_switch_matrix.c +0 -183
@@ 1,183 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    // Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN_0);
    writePinLow(APLEX_EN_PIN_0);
    setPinOutput(APLEX_EN_PIN_1);
    writePinLow(APLEX_EN_PIN_1);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    if (channel == 0) {
        writePinHigh(APLEX_EN_PIN_0);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_0);
    } else {
        writePinHigh(APLEX_EN_PIN_1);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_1);
    }

    // Set strobe pins to low state
    writePinLow(row_pins[row]);
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_1);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_0);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
        }
    }
    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

D keyboards/cipulot/ec_alveus/1_2_0/ec_switch_matrix.h => keyboards/cipulot/ec_alveus/1_2_0/ec_switch_matrix.h +0 -36
@@ 1,36 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;

ecsm_config_t ecsm_config;

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);

M keyboards/cipulot/ec_alveus/1_2_0/halconf.h => keyboards/cipulot/ec_alveus/1_2_0/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/ec_alveus/1_2_0/info.json => keyboards/cipulot/ec_alveus/1_2_0/info.json +2 -9
@@ 8,18 8,11 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "audio": false,
        "backlight": false,
        "bootmagic": true,
        "command": false,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,
        "nkro": true,
        "rgblight": false
    },
    "mouse_key": {
        "enabled": true
        "nkro": true
    },
    "processor": "STM32F401",
    "usb": {

M keyboards/cipulot/ec_alveus/1_2_0/keymaps/default/keymap.c => keyboards/cipulot/ec_alveus/1_2_0/keymaps/default/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 33,14 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
    _______,            _______,                      _______,                                                    _______,            _______,   _______,  _______,  _______),

    [2] = LAYOUT_tkl_nofrow_ansi_tsangan_wkl(
    QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                  _______,
    _______,            _______,                      _______,                                                    _______,            _______,   _______,  _______,  _______),

    [3] = LAYOUT_tkl_nofrow_ansi_tsangan_wkl(
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                  _______,

M keyboards/cipulot/ec_alveus/1_2_0/keymaps/tkl_nofrow_ansi_tsangan_wkl_split_bs/keymap.c => keyboards/cipulot/ec_alveus/1_2_0/keymaps/tkl_nofrow_ansi_tsangan_wkl_split_bs/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 33,14 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        _______,            _______,                      _______,                                                    _______,            _______,             _______,  _______,  _______),

    [2] = LAYOUT_tkl_nofrow_ansi_tsangan_wkl_split_bs(
        QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,             _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                            _______,
        _______,            _______,                      _______,                                                    _______,    _______,             _______,  _______,  _______),

    [3] = LAYOUT_tkl_nofrow_ansi_tsangan_wkl_split_bs(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,             _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                            _______,

M keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/config.h => keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/config.h +3 -3
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,5 @@

#pragma once

// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4
// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1

M keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/keymap.c => keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/keymap.c +2 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 33,14 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        _______,  _______,  _______,                      _______,                                                    _______,  _______,  _______,             _______,  _______,  _______),

    [2] = LAYOUT_all(
        QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,             _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                            _______,
        _______,  _______,  _______,                      _______,                                                    _______,  _______,  _______,             _______,  _______,  _______),

    [3] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,   _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,             _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,       _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,                 _______,                            _______,

M keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/rules.mk => keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += via_apc.c

D keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/via_apc.c => keyboards/cipulot/ec_alveus/1_2_0/keymaps/via/via_apc.c +0 -156
@@ 1,156 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

D keyboards/cipulot/ec_alveus/1_2_0/matrix.c => keyboards/cipulot/ec_alveus/1_2_0/matrix.c +0 -44
@@ 1,44 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;

    ecsm_init(&ecsm_config);
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

M keyboards/cipulot/ec_alveus/1_2_0/mcuconf.h => keyboards/cipulot/ec_alveus/1_2_0/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/ec_alveus/1_2_0/post_rules.mk => keyboards/cipulot/ec_alveus/1_2_0/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/ec_alveus/1_2_0/readme.md => keyboards/cipulot/ec_alveus/1_2_0/readme.md +1 -2
@@ 20,8 20,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical reset**: Long short the exposed pads on the top of the PCB
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available

M keyboards/cipulot/ec_alveus/1_2_0/rules.mk => keyboards/cipulot/ec_alveus/1_2_0/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 3

M keyboards/cipulot/ec_pro2/config.h => keyboards/cipulot/ec_pro2/config.h +40 -13
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,27 19,54 @@
#define MATRIX_ROWS 5
#define MATRIX_COLS 15

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { B15, A8, B0, A7, B1 }
#define MATRIX_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6, 4 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 2
#define AMUX_MAX_COLS_COUNT 8

#define AMUX_EN_PINS \
    { B7, B3 }

#define AMUX_SEL_PINS \
    { B6, B5, B4 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN_0 B7
#define APLEX_EN_PIN_1 B3
#define AMUX_COL_CHANNELS_SIZES \
    { 8, 7 }

#define AMUX_0_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6, 4 }

#define AMUX_1_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6 }

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS, AMUX_1_COL_CHANNELS

#define DISCHARGE_PIN A6
#define ANALOG_PORT A3

#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 100
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE

#define EECONFIG_KB_DATA_SIZE 160

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 550
#define DEFAULT_RELEASE_LEVEL 500

#define DISCHARGE_TIME 10

D keyboards/cipulot/ec_pro2/ec_switch_matrix.c => keyboards/cipulot/ec_pro2/ec_switch_matrix.c +0 -183
@@ 1,183 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    // Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN_0);
    writePinLow(APLEX_EN_PIN_0);
    setPinOutput(APLEX_EN_PIN_1);
    writePinLow(APLEX_EN_PIN_1);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    if (channel == 0) {
        writePinHigh(APLEX_EN_PIN_0);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_0);
    } else {
        writePinHigh(APLEX_EN_PIN_1);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_1);
    }

    // Set strobe pins to low state
    writePinLow(row_pins[row]);
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_1);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_0);
    for (int col = 0; col < (sizeof(col_channels) - 1); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
        }
    }
    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

D keyboards/cipulot/ec_pro2/ec_switch_matrix.h => keyboards/cipulot/ec_pro2/ec_switch_matrix.h +0 -36
@@ 1,36 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;

ecsm_config_t ecsm_config;

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);

M keyboards/cipulot/ec_pro2/halconf.h => keyboards/cipulot/ec_pro2/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/ec_pro2/info.json => keyboards/cipulot/ec_pro2/info.json +2 -9
@@ 1,6 1,6 @@
{
    "manufacturer": "Cipulot",
    "keyboard_name": "EC Pro2",
    "keyboard_name": "EC Pro 2",
    "maintainer": "Cipulot",
    "bootloader": "stm32-dfu",
    "build": {


@@ 8,19 8,13 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "audio": false,
        "backlight": false,
        "bootmagic": true,
        "command": false,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,
        "nkro": true,
        "rgblight": true
    },
    "mouse_key": {
        "enabled": true
    },
    "processor": "STM32F401",
    "rgblight": {
        "led_count": 22,


@@ 40,7 34,6 @@
    "ws2812": {
        "pin": "B14"
    },
    "url": "https://www.github.com/Cipulot/EC-Pro-2",
    "usb": {
        "device_version": "0.0.1",
        "pid": "0x6B8E",

M keyboards/cipulot/ec_pro2/keymaps/60_hhkb/keymap.c => keyboards/cipulot/ec_pro2/keymaps/60_hhkb/keymap.c +4 -10
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 22,8 22,8 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_ESC,   KC_1,     KC_2,     KC_3,     KC_4,   KC_5,    KC_6,   KC_7,     KC_8,     KC_9,     KC_0,     KC_MINS,   KC_EQL,   KC_BSLS,  KC_GRV,
        KC_TAB,   KC_Q,     KC_W,     KC_E,     KC_R,   KC_T,    KC_Y,   KC_U,     KC_I,     KC_O,     KC_P,     KC_LBRC,   KC_RBRC,  KC_DEL,
        KC_LCTL,  KC_A,     KC_S,     KC_D,     KC_F,   KC_G,    KC_H,   KC_J,     KC_K,     KC_L,     KC_SCLN,  KC_QUOT,   KC_ENTER,
        KC_LSFT,  KC_Z,     KC_X,     KC_C,   KC_V,    KC_B,   KC_N,     KC_M,     KC_COMM,  KC_DOT,   KC_SLSH,  KC_RSFT,   MO(1),
                  KC_LGUI,  KC_LALT,                             KC_SPC,                               KC_RALT,  KC_RGUI),
        KC_LSFT,  KC_Z,     KC_X,     KC_C,     KC_V,   KC_B,    KC_N,   KC_M,     KC_COMM,  KC_DOT,   KC_SLSH,  KC_RSFT,   MO(1),
                  KC_LALT,  KC_LGUI,                             KC_SPC,                               KC_RGUI,  KC_RALT),

    [1] = LAYOUT_60_hhkb(
        _______,  KC_F1,    KC_F2,    KC_F3,    KC_F4,    KC_F5,    KC_F6,    KC_F7,      KC_F8,    KC_F9,    KC_F10,   KC_F11,    KC_F12,   KC_INS,  KC_DEL,


@@ 33,13 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
                  _______,  _______,                                _______,                                  _______,  MO(2)),

    [2] = LAYOUT_60_hhkb(
        RGB_TOG,  RGB_VAD,  RGB_VAI,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  QK_BOOT,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
                  _______,  _______,                                _______,                                _______,  _______),
    [3] = LAYOUT_60_hhkb(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_pro2/keymaps/default/keymap.c => keyboards/cipulot/ec_pro2/keymaps/default/keymap.c +3 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 23,7 23,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_TAB,   KC_Q,     KC_W,     KC_E,     KC_R,   KC_T,    KC_Y,   KC_U,     KC_I,     KC_O,     KC_P,     KC_LBRC,   KC_RBRC,  KC_DEL,
        KC_LCTL,  KC_A,     KC_S,     KC_D,     KC_F,   KC_G,    KC_H,   KC_J,     KC_K,     KC_L,     KC_SCLN,  KC_QUOT,   KC_NUHS,  KC_ENTER,
        KC_LSFT,  KC_BSLS,  KC_Z,     KC_X,     KC_C,   KC_V,    KC_B,   KC_N,     KC_M,     KC_COMM,  KC_DOT,   KC_SLSH,   KC_RSFT,  MO(1),
                  KC_LGUI,  KC_LALT,                             KC_SPC,                               KC_RALT,  KC_RGUI),
                  KC_LALT,  KC_LGUI,                             KC_SPC,                               KC_RGUI,  KC_RALT),

    [1] = LAYOUT_all(
        _______,  KC_F1,    KC_F2,    KC_F3,    KC_F4,    KC_F5,    KC_F6,    KC_F7,      KC_F8,    KC_F9,    KC_F10,   KC_F11,    KC_F12,   KC_INS,  KC_DEL,


@@ 33,13 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
                  _______,  _______,                                _______,                                  _______,  MO(2)),

    [2] = LAYOUT_all(
        RGB_TOG,  RGB_VAD,  RGB_VAI,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  QK_BOOT,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
                  _______,  _______,                                _______,                                _______,  _______),
    [3] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_pro2/keymaps/via/config.h => keyboards/cipulot/ec_pro2/keymaps/via/config.h +3 -3
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,5 @@

#pragma once

// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4
// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1

M keyboards/cipulot/ec_pro2/keymaps/via/keymap.c => keyboards/cipulot/ec_pro2/keymaps/via/keymap.c +3 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 23,7 23,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_TAB,   KC_Q,     KC_W,     KC_E,     KC_R,   KC_T,    KC_Y,   KC_U,     KC_I,     KC_O,     KC_P,     KC_LBRC,   KC_RBRC,  KC_DEL,
        KC_LCTL,  KC_A,     KC_S,     KC_D,     KC_F,   KC_G,    KC_H,   KC_J,     KC_K,     KC_L,     KC_SCLN,  KC_QUOT,   KC_NUHS,  KC_ENTER,
        KC_LSFT,  KC_BSLS,  KC_Z,     KC_X,     KC_C,   KC_V,    KC_B,   KC_N,     KC_M,     KC_COMM,  KC_DOT,   KC_SLSH,   KC_RSFT,  MO(1),
                  KC_LGUI,  KC_LALT,                             KC_SPC,                               KC_RALT,  KC_RGUI),
                  KC_LALT,  KC_LGUI,                             KC_SPC,                               KC_RGUI,  KC_RALT),

    [1] = LAYOUT_all(
        _______,  KC_F1,    KC_F2,    KC_F3,    KC_F4,    KC_F5,    KC_F6,    KC_F7,      KC_F8,    KC_F9,    KC_F10,   KC_F11,    KC_F12,   KC_INS,  KC_DEL,


@@ 33,13 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
                  _______,  _______,                                _______,                                  _______,  MO(2)),

    [2] = LAYOUT_all(
        RGB_TOG,  RGB_VAD,  RGB_VAI,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  QK_BOOT,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
                  _______,  _______,                                _______,                                _______,  _______),
    [3] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_pro2/keymaps/via/rules.mk => keyboards/cipulot/ec_pro2/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += via_apc.c

D keyboards/cipulot/ec_pro2/keymaps/via/via_apc.c => keyboards/cipulot/ec_pro2/keymaps/via/via_apc.c +0 -156
@@ 1,156 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

D keyboards/cipulot/ec_pro2/matrix.c => keyboards/cipulot/ec_pro2/matrix.c +0 -44
@@ 1,44 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;

    ecsm_init(&ecsm_config);
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

M keyboards/cipulot/ec_pro2/mcuconf.h => keyboards/cipulot/ec_pro2/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/ec_pro2/post_rules.mk => keyboards/cipulot/ec_pro2/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/ec_pro2/readme.md => keyboards/cipulot/ec_pro2/readme.md +1 -2
@@ 20,8 20,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical Boot0 pins**: Short the Boot0 pins on the back of the PCB while plugging in the keyboard
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available

M keyboards/cipulot/ec_pro2/rules.mk => keyboards/cipulot/ec_pro2/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 2

M keyboards/cipulot/ec_prox/ansi_iso/config.h => keyboards/cipulot/ec_prox/ansi_iso/config.h +40 -13
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,27 19,54 @@
#define MATRIX_ROWS 5
#define MATRIX_COLS 15

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { A7, B0, A4, A5, A6 }
#define MATRIX_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6, 4 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 2
#define AMUX_MAX_COLS_COUNT 8

#define AMUX_EN_PINS \
    { B7, B3 }

#define AMUX_SEL_PINS \
    { B4, B5, B6 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN_0 B7
#define APLEX_EN_PIN_1 B3
#define AMUX_COL_CHANNELS_SIZES \
    { 8, 7 }

#define AMUX_0_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6, 4 }

#define AMUX_1_COL_CHANNELS \
    { 0, 3, 1, 2, 5, 7, 6 }

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS, AMUX_1_COL_CHANNELS

#define DISCHARGE_PIN A2
#define ANALOG_PORT A1

#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 100
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE

#define EECONFIG_KB_DATA_SIZE 160

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 550
#define DEFAULT_RELEASE_LEVEL 500

#define DISCHARGE_TIME 10

D keyboards/cipulot/ec_prox/ansi_iso/ec_switch_matrix.c => keyboards/cipulot/ec_prox/ansi_iso/ec_switch_matrix.c +0 -183
@@ 1,183 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    // Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN_0);
    writePinLow(APLEX_EN_PIN_0);
    setPinOutput(APLEX_EN_PIN_1);
    writePinLow(APLEX_EN_PIN_1);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    if (channel == 0) {
        writePinHigh(APLEX_EN_PIN_0);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_0);
    } else {
        writePinHigh(APLEX_EN_PIN_1);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_1);
    }

    // Set strobe pins to low state
    writePinLow(row_pins[row]);
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_1);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_0);
    for (int col = 0; col < (sizeof(col_channels) - 1); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
        }
    }
    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

D keyboards/cipulot/ec_prox/ansi_iso/ec_switch_matrix.h => keyboards/cipulot/ec_prox/ansi_iso/ec_switch_matrix.h +0 -36
@@ 1,36 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;

ecsm_config_t ecsm_config;

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);

M keyboards/cipulot/ec_prox/ansi_iso/halconf.h => keyboards/cipulot/ec_prox/ansi_iso/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/ec_prox/ansi_iso/info.json => keyboards/cipulot/ec_prox/ansi_iso/info.json +1 -7
@@ 8,19 8,13 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "audio": false,
        "backlight": false,
        "bootmagic": true,
        "command": false,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,
        "nkro": true,
        "rgblight": true
    },
    "mouse_key": {
        "enabled": true
    },
    "processor": "STM32F401",
    "rgblight": {
        "led_count": 22,

M keyboards/cipulot/ec_prox/ansi_iso/keymaps/60_hhkb/keymap.c => keyboards/cipulot/ec_prox/ansi_iso/keymaps/60_hhkb/keymap.c +3 -9
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 23,7 23,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_TAB,   KC_Q,     KC_W,     KC_E,     KC_R,   KC_T,    KC_Y,   KC_U,     KC_I,     KC_O,     KC_P,     KC_LBRC,   KC_RBRC,  KC_DEL,
        KC_LCTL,  KC_A,     KC_S,     KC_D,     KC_F,   KC_G,    KC_H,   KC_J,     KC_K,     KC_L,     KC_SCLN,  KC_QUOT,   KC_ENTER,
        KC_LSFT,  KC_Z,     KC_X,     KC_C,   KC_V,    KC_B,   KC_N,     KC_M,     KC_COMM,  KC_DOT,   KC_SLSH,  KC_RSFT,   MO(1),
                  KC_LGUI,  KC_LALT,                             KC_SPC,                               KC_RALT,  KC_RGUI),
                  KC_LALT,  KC_LGUI,                             KC_SPC,                               KC_RGUI,  KC_RALT),

    [1] = LAYOUT_60_hhkb(
        _______,  KC_F1,    KC_F2,    KC_F3,    KC_F4,    KC_F5,    KC_F6,    KC_F7,      KC_F8,    KC_F9,    KC_F10,   KC_F11,    KC_F12,   KC_INS,  KC_DEL,


@@ 33,13 33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
                  _______,  _______,                                _______,                                  _______,  MO(2)),

    [2] = LAYOUT_60_hhkb(
        RGB_TOG,  RGB_VAD,  RGB_VAI,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  QK_BOOT,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
                  _______,  _______,                                _______,                                _______,  _______),
    [3] = LAYOUT_60_hhkb(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_prox/ansi_iso/keymaps/default/keymap.c => keyboards/cipulot/ec_prox/ansi_iso/keymaps/default/keymap.c +4 -11
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 23,24 23,17 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_TAB,   KC_Q,     KC_W,     KC_E,     KC_R,   KC_T,    KC_Y,   KC_U,     KC_I,     KC_O,     KC_P,     KC_LBRC,   KC_RBRC,  KC_DEL,
        KC_LCTL,  KC_A,     KC_S,     KC_D,     KC_F,   KC_G,    KC_H,   KC_J,     KC_K,     KC_L,     KC_SCLN,  KC_QUOT,   KC_NUHS,  KC_ENTER,
        KC_LSFT,  KC_BSLS,  KC_Z,     KC_X,     KC_C,   KC_V,    KC_B,   KC_N,     KC_M,     KC_COMM,  KC_DOT,   KC_SLSH,   KC_RSFT,  MO(1),
                  KC_LGUI,  KC_LALT,                             KC_SPC,                               KC_RALT,  KC_RGUI),
                  KC_LALT,  KC_LGUI,                             KC_SPC,                               KC_RGUI,  KC_RALT),

    [1] = LAYOUT_all(
        _______,  KC_F1,    KC_F2,    KC_F3,    KC_F4,    KC_F5,    KC_F6,    KC_F7,      KC_F8,    KC_F9,    KC_F10,    KC_F11,    KC_F12,   KC_INS,  KC_DEL,
        KC_CAPS,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    KC_PSCR,  KC_SCRL,  KC_PAUSE,  KC_UP,     _______,  KC_BSPC,
        _______,  KC_VOLD,  KC_VOLU,  KC_MUTE,  _______,  _______,  S(KC_8),  KC_SLSH,    KC_HOME,  KC_PGUP,  KC_LEFT,   KC_RIGHT,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  S(KC_EQL),  KC_MINS,  KC_END,   KC_PGDN,   KC_DOWN,   _______,  _______,
                  _______,  _______,                                _______,                                  _______,   _______),
                  _______,  _______,                                _______,                                  _______,   MO(2)),

    [2] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
                  _______,  _______,                                _______,                                _______,  _______),

    [3] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/config.h => keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/config.h +3 -3
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,5 @@

#pragma once

// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4
// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1

M keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/keymap.c => keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/keymap.c +4 -11
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 23,24 23,17 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_TAB,   KC_Q,     KC_W,     KC_E,     KC_R,   KC_T,    KC_Y,   KC_U,     KC_I,     KC_O,     KC_P,     KC_LBRC,   KC_RBRC,  KC_DEL,
        KC_LCTL,  KC_A,     KC_S,     KC_D,     KC_F,   KC_G,    KC_H,   KC_J,     KC_K,     KC_L,     KC_SCLN,  KC_QUOT,   KC_NUHS,  KC_ENTER,
        KC_LSFT,  KC_BSLS,  KC_Z,     KC_X,     KC_C,   KC_V,    KC_B,   KC_N,     KC_M,     KC_COMM,  KC_DOT,   KC_SLSH,   KC_RSFT,  MO(1),
                  KC_LGUI,  KC_LALT,                             KC_SPC,                               KC_RALT,  KC_RGUI),
                  KC_LALT,  KC_LGUI,                             KC_SPC,                               KC_RGUI,  KC_RALT),

    [1] = LAYOUT_all(
        _______,  KC_F1,    KC_F2,    KC_F3,    KC_F4,    KC_F5,    KC_F6,    KC_F7,      KC_F8,    KC_F9,    KC_F10,    KC_F11,    KC_F12,   KC_INS,  KC_DEL,
        KC_CAPS,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    KC_PSCR,  KC_SCRL,  KC_PAUSE,  KC_UP,     _______,  KC_BSPC,
        _______,  KC_VOLD,  KC_VOLU,  KC_MUTE,  _______,  _______,  S(KC_8),  KC_SLSH,    KC_HOME,  KC_PGUP,  KC_LEFT,   KC_RIGHT,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  S(KC_EQL),  KC_MINS,  KC_END,   KC_PGDN,   KC_DOWN,   _______,  _______,
                  _______,  _______,                                _______,                                  _______,   _______),
                  _______,  _______,                                _______,                                  _______,   MO(2)),

    [2] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
                  _______,  _______,                                _______,                                _______,  _______),

    [3] = LAYOUT_all(
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/rules.mk => keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += via_apc.c

D keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/via_apc.c => keyboards/cipulot/ec_prox/ansi_iso/keymaps/via/via_apc.c +0 -156
@@ 1,156 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

D keyboards/cipulot/ec_prox/ansi_iso/matrix.c => keyboards/cipulot/ec_prox/ansi_iso/matrix.c +0 -44
@@ 1,44 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;

    ecsm_init(&ecsm_config);
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

M keyboards/cipulot/ec_prox/ansi_iso/mcuconf.h => keyboards/cipulot/ec_prox/ansi_iso/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/ec_prox/ansi_iso/post_rules.mk => keyboards/cipulot/ec_prox/ansi_iso/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/ec_prox/ansi_iso/readme.md => keyboards/cipulot/ec_prox/ansi_iso/readme.md +1 -2
@@ 20,8 20,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical Boot0 pins**: Short the Boot0 pins on the back of the PCB while plugging in the keyboard
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available

M keyboards/cipulot/ec_prox/ansi_iso/rules.mk => keyboards/cipulot/ec_prox/ansi_iso/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 2

M keyboards/cipulot/ec_prox/jis/config.h => keyboards/cipulot/ec_prox/jis/config.h +40 -13
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,27 19,54 @@
#define MATRIX_ROWS 5
#define MATRIX_COLS 14

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { A7, B0, A4, A5, A6 }
#define MATRIX_COL_CHANNELS \
    { 2, 1, 0, 3, 5, 7, 4, 6 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 2
#define AMUX_MAX_COLS_COUNT 8

#define AMUX_EN_PINS \
    { B7, B3 }

#define AMUX_SEL_PINS \
    { B4, B5, B6 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN_0 B7
#define APLEX_EN_PIN_1 B3
#define AMUX_COL_CHANNELS_SIZES \
    { 8, 6 }

#define AMUX_0_COL_CHANNELS \
    { 2, 1, 0, 3, 5, 7, 4, 6 }

#define AMUX_1_COL_CHANNELS \
    { 2, 1, 0, 3, 5, 7 }

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS, AMUX_1_COL_CHANNELS

#define DISCHARGE_PIN A2
#define ANALOG_PORT A1

#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 100
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE

#define EECONFIG_KB_DATA_SIZE 150

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 550
#define DEFAULT_RELEASE_LEVEL 500

#define DISCHARGE_TIME 10

D keyboards/cipulot/ec_prox/jis/ec_switch_matrix.c => keyboards/cipulot/ec_prox/jis/ec_switch_matrix.c +0 -183
@@ 1,183 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    //Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN_0);
    writePinLow(APLEX_EN_PIN_0);
    setPinOutput(APLEX_EN_PIN_1);
    writePinLow(APLEX_EN_PIN_1);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    if (channel == 0) {
        writePinHigh(APLEX_EN_PIN_0);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_0);
    } else {
        writePinHigh(APLEX_EN_PIN_1);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_1);
    }

    // Set strobe pins to low state
    writePinLow(row_pins[row]);
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_1);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_0);
    for (int col = 0; col < (sizeof(col_channels) - 2); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
        }
    }
    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

D keyboards/cipulot/ec_prox/jis/ec_switch_matrix.h => keyboards/cipulot/ec_prox/jis/ec_switch_matrix.h +0 -36
@@ 1,36 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;

ecsm_config_t ecsm_config;

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);

M keyboards/cipulot/ec_prox/jis/halconf.h => keyboards/cipulot/ec_prox/jis/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/ec_prox/jis/info.json => keyboards/cipulot/ec_prox/jis/info.json +70 -80
@@ 8,19 8,13 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "audio": false,
        "backlight": false,
        "bootmagic": true,
        "command": false,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,
        "nkro": true,
        "rgblight": true
    },
    "mouse_key": {
        "enabled": true
    },
    "processor": "STM32F401",
    "rgblight": {
        "led_count": 22,


@@ 52,79 46,75 @@
    "layouts": {
        "LAYOUT_jp": {
            "layout": [
                {"matrix": [0, 0], "x": 0, "y": 0},
                {"matrix": [0, 1], "x": 1, "y": 0},
                {"matrix": [0, 2], "x": 2, "y": 0},
                {"matrix": [0, 3], "x": 3, "y": 0},
                {"matrix": [0, 4], "x": 4, "y": 0},
                {"matrix": [0, 5], "x": 5, "y": 0},
                {"matrix": [0, 6], "x": 6, "y": 0},
                {"matrix": [0, 7], "x": 7, "y": 0},
                {"matrix": [0, 8], "x": 8, "y": 0},
                {"matrix": [0, 9], "x": 9, "y": 0},
                {"matrix": [0, 10], "x": 10, "y": 0},
                {"matrix": [0, 11], "x": 11, "y": 0},
                {"matrix": [0, 12], "x": 12, "y": 0},
                {"matrix": [0, 13], "x": 13, "y": 0},
                {"matrix": [1, 13], "x": 14, "y": 0},

                {"matrix": [1, 0], "x": 0, "y": 1, "w": 1.5},
                {"matrix": [1, 1], "x": 1.5, "y": 1},
                {"matrix": [1, 2], "x": 2.5, "y": 1},
                {"matrix": [1, 3], "x": 3.5, "y": 1},
                {"matrix": [1, 4], "x": 4.5, "y": 1},
                {"matrix": [1, 5], "x": 5.5, "y": 1},
                {"matrix": [1, 6], "x": 6.5, "y": 1},
                {"matrix": [1, 7], "x": 7.5, "y": 1},
                {"matrix": [1, 8], "x": 8.5, "y": 1},
                {"matrix": [1, 9], "x": 9.5, "y": 1},
                {"matrix": [1, 10], "x": 10.5, "y": 1},
                {"matrix": [1, 11], "x": 11.5, "y": 1},
                {"matrix": [1, 12], "x": 12.5, "y": 1},

                {"matrix": [2, 0], "x": 0, "y": 2, "w": 1.75},
                {"matrix": [2, 1], "x": 1.75, "y": 2},
                {"matrix": [2, 2], "x": 2.75, "y": 2},
                {"matrix": [2, 3], "x": 3.75, "y": 2},
                {"matrix": [2, 4], "x": 4.75, "y": 2},
                {"matrix": [2, 5], "x": 5.75, "y": 2},
                {"matrix": [2, 6], "x": 6.75, "y": 2},
                {"matrix": [2, 7], "x": 7.75, "y": 2},
                {"matrix": [2, 8], "x": 8.75, "y": 2},
                {"matrix": [2, 9], "x": 9.75, "y": 2},
                {"matrix": [2, 10], "x": 10.75, "y": 2},
                {"matrix": [2, 11], "x": 11.75, "y": 2},
                {"matrix": [2, 12], "x": 12.75, "y": 2},
                {"matrix": [2, 13], "x": 13.75, "y": 1, "w": 1.25, "h": 2},

                {"matrix": [3, 0], "x": 0, "y": 3, "w": 2},
                {"matrix": [3, 1], "x": 2, "y": 3},
                {"matrix": [3, 2], "x": 3, "y": 3},
                {"matrix": [3, 3], "x": 4, "y": 3},
                {"matrix": [3, 4], "x": 5, "y": 3},
                {"matrix": [3, 5], "x": 6, "y": 3},
                {"matrix": [3, 6], "x": 7, "y": 3},
                {"matrix": [3, 7], "x": 8, "y": 3},
                {"matrix": [3, 8], "x": 9, "y": 3},
                {"matrix": [3, 9], "x": 10, "y": 3},
                {"matrix": [3, 10], "x": 11, "y": 3},
                {"matrix": [3, 11], "x": 12, "y": 3},
                {"matrix": [3, 12], "x": 13, "y": 3},
                {"matrix": [3, 13], "x": 14, "y": 3},

                {"matrix": [4, 0], "x": 0, "y": 4},
                {"matrix": [4, 1], "x": 1.25, "y": 4},
                {"matrix": [4, 2], "x": 2.25, "y": 4},
                {"matrix": [4, 3], "x": 3.25, "y": 4},
                {"matrix": [4, 4], "x": 4.25, "y": 4},
                {"matrix": [4, 5], "x": 5.25, "y": 4, "w": 2.5},
                {"matrix": [4, 7], "x": 7.75, "y": 4},
                {"matrix": [4, 8], "x": 8.75, "y": 4},
                {"matrix": [4, 9], "x": 9.75, "y": 4},
                {"matrix": [4, 10], "x": 10.75, "y": 4},
                {"matrix": [4, 11], "x": 12, "y": 4},
                {"matrix": [4, 12], "x": 13, "y": 4},
                {"matrix": [4, 13], "x": 14, "y": 4}
                {"label": "0,0", "matrix": [0, 0], "x": 0, "y": 0},
                {"label": "0,1", "matrix": [0, 1], "x": 1, "y": 0},
                {"label": "0,2", "matrix": [0, 2], "x": 2, "y": 0},
                {"label": "0,3", "matrix": [0, 3], "x": 3, "y": 0},
                {"label": "0,4", "matrix": [0, 4], "x": 4, "y": 0},
                {"label": "0,5", "matrix": [0, 5], "x": 5, "y": 0},
                {"label": "0,6", "matrix": [0, 6], "x": 6, "y": 0},
                {"label": "0,7", "matrix": [0, 7], "x": 7, "y": 0},
                {"label": "0,8", "matrix": [0, 8], "x": 8, "y": 0},
                {"label": "0,9", "matrix": [0, 9], "x": 9, "y": 0},
                {"label": "0,10", "matrix": [0, 10], "x": 10, "y": 0},
                {"label": "0,11", "matrix": [0, 11], "x": 11, "y": 0},
                {"label": "0,12", "matrix": [0, 12], "x": 12, "y": 0},
                {"label": "0,13", "matrix": [0, 13], "x": 13, "y": 0},
                {"label": "1,13", "matrix": [1, 13], "x": 14, "y": 0},
                {"label": "1,0", "matrix": [1, 0], "w": 1.5, "x": 0, "y": 1},
                {"label": "1,1", "matrix": [1, 1], "x": 1.5, "y": 1},
                {"label": "1,2", "matrix": [1, 2], "x": 2.5, "y": 1},
                {"label": "1,3", "matrix": [1, 3], "x": 3.5, "y": 1},
                {"label": "1,4", "matrix": [1, 4], "x": 4.5, "y": 1},
                {"label": "1,5", "matrix": [1, 5], "x": 5.5, "y": 1},
                {"label": "1,6", "matrix": [1, 6], "x": 6.5, "y": 1},
                {"label": "1,7", "matrix": [1, 7], "x": 7.5, "y": 1},
                {"label": "1,8", "matrix": [1, 8], "x": 8.5, "y": 1},
                {"label": "1,9", "matrix": [1, 9], "x": 9.5, "y": 1},
                {"label": "1,10", "matrix": [1, 10], "x": 10.5, "y": 1},
                {"label": "1,11", "matrix": [1, 11], "x": 11.5, "y": 1},
                {"label": "1,12", "matrix": [1, 12], "x": 12.5, "y": 1},
                {"label": "2,13", "matrix": [2, 13], "w": 1.25, "h": 2, "x": 13.75, "y": 1},
                {"label": "2,0", "matrix": [2, 0], "w": 1.75, "x": 0, "y": 2},
                {"label": "2,1", "matrix": [2, 1], "x": 1.75, "y": 2},
                {"label": "2,2", "matrix": [2, 2], "x": 2.75, "y": 2},
                {"label": "2,3", "matrix": [2, 3], "x": 3.75, "y": 2},
                {"label": "2,4", "matrix": [2, 4], "x": 4.75, "y": 2},
                {"label": "2,5", "matrix": [2, 5], "x": 5.75, "y": 2},
                {"label": "2,6", "matrix": [2, 6], "x": 6.75, "y": 2},
                {"label": "2,7", "matrix": [2, 7], "x": 7.75, "y": 2},
                {"label": "2,8", "matrix": [2, 8], "x": 8.75, "y": 2},
                {"label": "2,9", "matrix": [2, 9], "x": 9.75, "y": 2},
                {"label": "2,10", "matrix": [2, 10], "x": 10.75, "y": 2},
                {"label": "2,11", "matrix": [2, 11], "x": 11.75, "y": 2},
                {"label": "2,12", "matrix": [2, 12], "x": 12.75, "y": 2},
                {"label": "3,0", "matrix": [3, 0], "w": 2, "x": 0, "y": 3},
                {"label": "3,1", "matrix": [3, 1], "x": 2, "y": 3},
                {"label": "3,2", "matrix": [3, 2], "x": 3, "y": 3},
                {"label": "3,3", "matrix": [3, 3], "x": 4, "y": 3},
                {"label": "3,4", "matrix": [3, 4], "x": 5, "y": 3},
                {"label": "3,5", "matrix": [3, 5], "x": 6, "y": 3},
                {"label": "3,6", "matrix": [3, 6], "x": 7, "y": 3},
                {"label": "3,7", "matrix": [3, 7], "x": 8, "y": 3},
                {"label": "3,8", "matrix": [3, 8], "x": 9, "y": 3},
                {"label": "3,9", "matrix": [3, 9], "x": 10, "y": 3},
                {"label": "3,10", "matrix": [3, 10], "x": 11, "y": 3},
                {"label": "3,11", "matrix": [3, 11], "x": 12, "y": 3},
                {"label": "3,12", "matrix": [3, 12], "x": 13, "y": 3},
                {"label": "3,13", "matrix": [3, 13], "x": 14, "y": 3},
                {"label": "4,0", "matrix": [4, 0], "x": 0, "y": 4},
                {"label": "4,1", "matrix": [4, 1], "x": 1.25, "y": 4},
                {"label": "4,2", "matrix": [4, 2], "x": 2.25, "y": 4},
                {"label": "4,3", "matrix": [4, 3], "x": 3.25, "y": 4},
                {"label": "4,4", "matrix": [4, 4], "x": 4.25, "y": 4},
                {"label": "4,5", "matrix": [4, 5], "w": 2.5, "x": 5.25, "y": 4},
                {"label": "4,7", "matrix": [4, 7], "x": 7.75, "y": 4},
                {"label": "4,8", "matrix": [4, 8], "x": 8.75, "y": 4},
                {"label": "4,9", "matrix": [4, 9], "x": 9.75, "y": 4},
                {"label": "4,10", "matrix": [4, 10], "x": 10.75, "y": 4},
                {"label": "4,11", "matrix": [4, 11], "x": 12, "y": 4},
                {"label": "4,12", "matrix": [4, 12], "x": 13, "y": 4},
                {"label": "4,13", "matrix": [4, 13], "x": 14, "y": 4}
            ]
        }
    }

M keyboards/cipulot/ec_prox/jis/keymaps/default/keymap.c => keyboards/cipulot/ec_prox/jis/keymaps/default/keymap.c +8 -15
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 21,28 21,21 @@
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
    // clang-format off
    [0] = LAYOUT_jp(
        KC_ESC,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_MINS, KC_EQL,  JP_YEN,  KC_BSPC,
        KC_TAB,  KC_Q,    KC_W,    KC_E,    KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,    KC_O,    KC_P,    KC_LBRC, KC_RBRC,
        KC_LCTL, KC_A,    KC_S,    KC_D,    KC_F,    KC_G,    KC_H,    KC_J,    KC_K,    KC_L,    KC_SCLN, KC_QUOT, KC_BSLS, KC_ENT,
        KC_LSFT, KC_Z,    KC_X,    KC_C,    KC_V,    KC_B,    KC_N,    KC_M,    KC_COMM, KC_DOT,  KC_SLSH, JP_UNDS, KC_UP,   KC_RSFT,
        MO(1),   JP_ZKHK, KC_LGUI, KC_LALT, JP_MHEN,      KC_SPC,      JP_HENK, JP_KANA, KC_RALT, MO(1),   KC_LEFT, KC_DOWN, KC_RGHT),
        KC_ESC,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5, KC_6, KC_7,    KC_8,    KC_9,    KC_0,    KC_MINS, KC_EQL,  JP_YEN, KC_BSPC,
        KC_TAB,  KC_Q,    KC_W,    KC_E,    KC_R,    KC_T, KC_Y, KC_U,    KC_I,    KC_O,    KC_P,    KC_LBRC, KC_RBRC,
        KC_LCTL, KC_A,    KC_S,    KC_D,    KC_F,    KC_G, KC_H, KC_J,    KC_K,    KC_L,    KC_SCLN, KC_QUOT, KC_BSLS, KC_ENT,
        KC_LSFT, KC_Z,    KC_X,    KC_C,    KC_V,    KC_B, KC_N, KC_M,    KC_COMM, KC_DOT,  KC_SLSH, JP_UNDS, KC_UP,   KC_RSFT,
        MO(1),   JP_ZKHK, KC_LGUI, KC_LALT, JP_MHEN,   KC_SPC,   JP_HENK, JP_KANA, KC_RALT, MO(1),   KC_LEFT, KC_DOWN, KC_RGHT),

    [1] = LAYOUT_jp(
        _______, KC_F1,   KC_F2,   KC_F3,   KC_F4,    KC_F5,  KC_F6,   KC_F7,   KC_F8,   KC_F9,   KC_F10,  KC_F11,  KC_F12,  KC_INS,  KC_DEL,
        _______, KC_F1,   KC_F2,   KC_F3,   KC_F4,    KC_F5,  KC_F6,   KC_F7,   KC_F8,   KC_F9,   KC_F10,  KC_F11,  KC_F12,  KC_INS, KC_DEL,
        KC_CAPS, _______, _______, _______, _______, _______, _______, _______, KC_PSCR, KC_SCRL, KC_PAUS, KC_UP,   _______,
        _______, KC_VOLD, KC_VOLU, KC_MUTE, _______, _______, KC_PAST, KC_PSLS, KC_HOME, KC_PGUP, KC_LEFT, KC_RGHT, _______, KC_PENT,
        _______, _______, _______, _______, _______, _______, KC_PPLS, KC_PMNS, KC_END,  KC_PGDN, KC_DOWN, _______, _______, _______,
        _______, _______, _______, _______, _______,      _______,     _______, _______, MO(2),   _______, _______, _______, _______),

    [2] = LAYOUT_jp(
        RGB_TOG, RGB_VAD, RGB_VAI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, QK_BOOT,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______,      _______,     _______, _______, _______, _______, _______, _______, _______),

    [3] = LAYOUT_jp(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/ec_prox/jis/keymaps/via/config.h => keyboards/cipulot/ec_prox/jis/keymaps/via/config.h +3 -3
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,5 @@

#pragma once

// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4
// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1

M keyboards/cipulot/ec_prox/jis/keymaps/via/keymap.c => keyboards/cipulot/ec_prox/jis/keymaps/via/keymap.c +10 -17
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 21,31 21,24 @@
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
    // clang-format off
    [0] = LAYOUT_jp(
        KC_ESC,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_MINS, KC_EQL,  JP_YEN,  KC_BSPC,
        KC_TAB,  KC_Q,    KC_W,    KC_E,    KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,    KC_O,    KC_P,    KC_LBRC, KC_RBRC,
        KC_LCTL, KC_A,    KC_S,    KC_D,    KC_F,    KC_G,    KC_H,    KC_J,    KC_K,    KC_L,    KC_SCLN, KC_QUOT, KC_BSLS, KC_ENT,
        KC_LSFT, KC_Z,    KC_X,    KC_C,    KC_V,    KC_B,    KC_N,    KC_M,    KC_COMM, KC_DOT,  KC_SLSH, JP_UNDS, KC_UP,   KC_RSFT,
        MO(1),   JP_ZKHK, KC_LGUI, KC_LALT, JP_MHEN,      KC_SPC,      JP_HENK, JP_KANA, KC_RALT, MO(1),   KC_LEFT, KC_DOWN, KC_RGHT),
        KC_ESC,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5, KC_6, KC_7,    KC_8,    KC_9,    KC_0,    KC_MINS, KC_EQL,  JP_YEN, KC_BSPC,
        KC_TAB,  KC_Q,    KC_W,    KC_E,    KC_R,    KC_T, KC_Y, KC_U,    KC_I,    KC_O,    KC_P,    KC_LBRC, KC_RBRC, KC_ENT,
        KC_LCTL, KC_A,    KC_S,    KC_D,    KC_F,    KC_G, KC_H, KC_J,    KC_K,    KC_L,    KC_SCLN, KC_QUOT, KC_BSLS,
        KC_LSFT, KC_Z,    KC_X,    KC_C,    KC_V,    KC_B, KC_N, KC_M,    KC_COMM, KC_DOT,  KC_SLSH, JP_UNDS, KC_UP,   KC_RSFT,
        MO(1),   JP_ZKHK, KC_LGUI, KC_LALT, JP_MHEN,   KC_SPC,   JP_HENK, JP_KANA, KC_RALT, MO(1),   KC_LEFT, KC_DOWN, KC_RGHT),

    [1] = LAYOUT_jp(
        _______, KC_F1,   KC_F2,   KC_F3,   KC_F4,    KC_F5,  KC_F6,   KC_F7,   KC_F8,   KC_F9,   KC_F10,  KC_F11,  KC_F12,  KC_INS,  KC_DEL,
        KC_CAPS, _______, _______, _______, _______, _______, _______, _______, KC_PSCR, KC_SCRL, KC_PAUS, KC_UP,   _______,
        _______, KC_VOLD, KC_VOLU, KC_MUTE, _______, _______, KC_PAST, KC_PSLS, KC_HOME, KC_PGUP, KC_LEFT, KC_RGHT, _______, KC_PENT,
        _______, KC_F1,   KC_F2,   KC_F3,   KC_F4,    KC_F5,  KC_F6,   KC_F7,   KC_F8,   KC_F9,   KC_F10,  KC_F11,  KC_F12,  KC_INS, KC_DEL,
        KC_CAPS, _______, _______, _______, _______, _______, _______, _______, KC_PSCR, KC_SCRL, KC_PAUS, KC_UP,   _______, KC_PENT,
        _______, KC_VOLD, KC_VOLU, KC_MUTE, _______, _______, KC_PAST, KC_PSLS, KC_HOME, KC_PGUP, KC_LEFT, KC_RGHT, _______,
        _______, _______, _______, _______, _______, _______, KC_PPLS, KC_PMNS, KC_END,  KC_PGDN, KC_DOWN, _______, _______, _______,
        _______, _______, _______, _______, _______,      _______,     _______, _______, MO(2),   _______, _______, _______, _______),

    [2] = LAYOUT_jp(
        RGB_TOG, RGB_VAD, RGB_VAI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, QK_BOOT,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______,      _______,     _______, _______, _______, _______, _______, _______, _______),

    [3] = LAYOUT_jp(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______,      _______,     _______, _______, _______, _______, _______, _______, _______)
    // clang-format on
};

M keyboards/cipulot/ec_prox/jis/keymaps/via/rules.mk => keyboards/cipulot/ec_prox/jis/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += via_apc.c

D keyboards/cipulot/ec_prox/jis/keymaps/via/via_apc.c => keyboards/cipulot/ec_prox/jis/keymaps/via/via_apc.c +0 -156
@@ 1,156 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

D keyboards/cipulot/ec_prox/jis/matrix.c => keyboards/cipulot/ec_prox/jis/matrix.c +0 -44
@@ 1,44 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;

    ecsm_init(&ecsm_config);
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

M keyboards/cipulot/ec_prox/jis/mcuconf.h => keyboards/cipulot/ec_prox/jis/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/ec_prox/jis/post_rules.mk => keyboards/cipulot/ec_prox/jis/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/ec_prox/jis/readme.md => keyboards/cipulot/ec_prox/jis/readme.md +1 -2
@@ 20,8 20,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical Boot0 pins**: Short the Boot0 pins on the back of the PCB while plugging in the keyboard
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available

M keyboards/cipulot/ec_prox/jis/rules.mk => keyboards/cipulot/ec_prox/jis/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 2

M keyboards/cipulot/ec_theca/config.h => keyboards/cipulot/ec_theca/config.h +39 -13
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,27 19,53 @@
#define MATRIX_ROWS 6
#define MATRIX_COLS 16

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { B4, A14, B3, A15, B6, B5 }
#define MATRIX_COL_CHANNELS \
    { 3, 0, 1, 2, 6, 5, 7, 4 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 2
#define AMUX_MAX_COLS_COUNT 8

#define AMUX_EN_PINS \
    { C13, C14 }

#define AMUX_SEL_PINS \
    { B7, B8, B9 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN_0 C13
#define APLEX_EN_PIN_1 C14
#define AMUX_COL_CHANNELS_SIZES \
    { 8, 8 }

#define AMUX_0_COL_CHANNELS \
    { 3, 0, 1, 2, 6, 5, 7, 4 }

#define AMUX_1_COL_CHANNELS AMUX_0_COL_CHANNELS

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS, AMUX_1_COL_CHANNELS

#define DISCHARGE_PIN B1
#define ANALOG_PORT A3

#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 50
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE
#define DYNAMIC_KEYMAP_LAYER_COUNT 3
#define EECONFIG_KB_DATA_SIZE 202

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 550
#define DEFAULT_RELEASE_LEVEL 500

#define DISCHARGE_TIME 10

D keyboards/cipulot/ec_theca/ec_switch_matrix.c => keyboards/cipulot/ec_theca/ec_switch_matrix.c +0 -191
@@ 1,191 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

static inline void clear_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    // Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN_0);
    writePinLow(APLEX_EN_PIN_0);
    setPinOutput(APLEX_EN_PIN_1);
    writePinLow(APLEX_EN_PIN_1);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    if (channel == 0) {
        writePinHigh(APLEX_EN_PIN_0);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_0);
    } else {
        writePinHigh(APLEX_EN_PIN_1);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_1);
    }

    // Set strobe pins to low state
    //writePinLow(row_pins[row]);
    clear_row();
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_1);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_0);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
        }
    }
    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

D keyboards/cipulot/ec_theca/ec_switch_matrix.h => keyboards/cipulot/ec_theca/ec_switch_matrix.h +0 -36
@@ 1,36 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;

ecsm_config_t ecsm_config;

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);

M keyboards/cipulot/ec_theca/halconf.h => keyboards/cipulot/ec_theca/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/ec_theca/info.json => keyboards/cipulot/ec_theca/info.json +1 -1
@@ 8,7 8,7 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "bootmagic": true,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,

M keyboards/cipulot/ec_theca/keymaps/default/keymap.c => keyboards/cipulot/ec_theca/keymaps/default/keymap.c +2 -2
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 27,7 27,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
    KC_LCTL,  KC_LGUI,  KC_LALT,                  KC_SPC,                            KC_RALT,  KC_RGUI,  MO(1),    KC_RCTL,               KC_LEFT,  KC_DOWN,  KC_RIGHT),

    [1] = LAYOUT_tkl_ansi(
    QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,              _______,  _______,  _______,
    QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,              _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_theca/keymaps/tkl_ansi_tsangan/keymap.c => keyboards/cipulot/ec_theca/keymaps/tkl_ansi_tsangan/keymap.c +2 -2
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 27,7 27,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
    KC_LCTL,  KC_LGUI,  KC_LALT,                  KC_SPC,                                      KC_RALT,  MO(1),    KC_RCTL,               KC_LEFT,  KC_DOWN,  KC_RIGHT),

    [1] = LAYOUT_tkl_ansi_tsangan(
    QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,              _______,  _______,  _______,
    QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,              _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_theca/keymaps/tkl_ansi_tsangan_wkl/keymap.c => keyboards/cipulot/ec_theca/keymaps/tkl_ansi_tsangan_wkl/keymap.c +2 -2
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 27,7 27,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
    KC_LCTL,            KC_LALT,                  KC_SPC,                                      KC_RALT,            KC_RCTL,               KC_LEFT,  KC_DOWN,  KC_RIGHT),

    [1] = LAYOUT_tkl_ansi_wkl(
    QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,              _______,  _______,  _______,
    QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,              _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    _______,  _______,  _______,
    _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_theca/keymaps/via/config.h => keyboards/cipulot/ec_theca/keymaps/via/config.h +3 -3
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,5 @@

#pragma once

// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4
// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1

M keyboards/cipulot/ec_theca/keymaps/via/keymap.c => keyboards/cipulot/ec_theca/keymaps/via/keymap.c +2 -2
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 27,7 27,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_LCTL,  KC_LGUI,  KC_LALT,                  KC_SPC,                            KC_RALT,  MO(1),    MO(1),    KC_RCTL,               KC_LEFT,  KC_DOWN,  KC_RIGHT),

    [1] = LAYOUT_all(
        QK_BOOT,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,              _______,  _______,  _______,
        QK_BOOT,  NK_TOGG,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,              _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,    _______,  _______,  _______,
        _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,  _______,

M keyboards/cipulot/ec_theca/keymaps/via/rules.mk => keyboards/cipulot/ec_theca/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += keyboards/cipulot/common/via_apc.c

D keyboards/cipulot/ec_theca/matrix.c => keyboards/cipulot/ec_theca/matrix.c +0 -44
@@ 1,44 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;

    ecsm_init(&ecsm_config);
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

M keyboards/cipulot/ec_theca/mcuconf.h => keyboards/cipulot/ec_theca/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/ec_theca/post_rules.mk => keyboards/cipulot/ec_theca/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/ec_theca/readme.md => keyboards/cipulot/ec_theca/readme.md +1 -2
@@ 18,8 18,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical reset**: Long short the exposed pads on the top of the PCB
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available

M keyboards/cipulot/ec_theca/rules.mk => keyboards/cipulot/ec_theca/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 3

M keyboards/cipulot/kawayo/config.h => keyboards/cipulot/kawayo/config.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/kawayo/keymaps/default/keymap.c => keyboards/cipulot/kawayo/keymaps/default/keymap.c +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker/keymap.c => keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker/keymap.c +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker_tsangan/keymap.c => keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker_tsangan/keymap.c +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker_tsangan_split_bs/keymap.c => keyboards/cipulot/kawayo/keymaps/default_65_ansi_cb_blocker_tsangan_split_bs/keymap.c +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/kawayo/keymaps/via/keymap.c => keyboards/cipulot/kawayo/keymaps/via/keymap.c +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/rf_r1_8_9xu/config.h => keyboards/cipulot/rf_r1_8_9xu/config.h +39 -13
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 19,27 19,53 @@
#define MATRIX_ROWS 6
#define MATRIX_COLS 16

/* Custom matrix pins and port select array */
#define MATRIX_ROW_PINS \
    { B15, A8, B13, B12, B14, B0 }
#define MATRIX_COL_CHANNELS \
    { 3, 0, 1, 2, 4, 6, 7, 5 }
#define MUX_SEL_PINS \

#define AMUX_COUNT 2
#define AMUX_MAX_COLS_COUNT 8

#define AMUX_EN_PINS \
    { B7, A6 }

#define AMUX_SEL_PINS \
    { B4, B5, B6 }

/* Hardware peripherals pins */
#define APLEX_EN_PIN_0 B7
#define APLEX_EN_PIN_1 A6
#define AMUX_COL_CHANNELS_SIZES \
    { 8, 8 }

#define AMUX_0_COL_CHANNELS \
    { 3, 0, 1, 2, 4, 6, 7, 5 }

#define AMUX_1_COL_CHANNELS AMUX_0_COL_CHANNELS

#define AMUX_COL_CHANNELS AMUX_0_COL_CHANNELS, AMUX_1_COL_CHANNELS

#define DISCHARGE_PIN A4
#define ANALOG_PORT A3

#define DEFAULT_ACTUATION_MODE 0
#define DEFAULT_MODE_0_ACTUATION_LEVEL 550
#define DEFAULT_MODE_0_RELEASE_LEVEL 500
#define DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET DEFAULT_MODE_0_ACTUATION_LEVEL
#define DEFAULT_MODE_1_ACTUATION_OFFSET 70
#define DEFAULT_MODE_1_RELEASE_OFFSET 70
#define DEFAULT_EXTREMUM 1023
#define EXPECTED_NOISE_FLOOR 0
#define NOISE_FLOOR_THRESHOLD 50
#define BOTTOMING_CALIBRATION_THRESHOLD 100
#define DEFAULT_NOISE_FLOOR_SAMPLING_COUNT 30
#define DEFAULT_BOTTOMING_READING 1023
#define DEFAULT_CALIBRATION_STARTER true

#define DISCHARGE_TIME 10

// #define DEBUG_MATRIX_SCAN_RATE
#define DYNAMIC_KEYMAP_LAYER_COUNT 3
#define EECONFIG_KB_DATA_SIZE 202

/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE

#define DEFAULT_ACTUATION_LEVEL 700
#define DEFAULT_RELEASE_LEVEL 650

#define DISCHARGE_TIME 10

D keyboards/cipulot/rf_r1_8_9xu/ec_switch_matrix.c => keyboards/cipulot/rf_r1_8_9xu/ec_switch_matrix.c +0 -183
@@ 1,183 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "analog.h"
#include "atomic_util.h"
#include "print.h"
#include "wait.h"

/* Pin and port array */
const uint32_t row_pins[]     = MATRIX_ROW_PINS;
const uint8_t  col_channels[] = MATRIX_COL_CHANNELS;
const uint32_t mux_sel_pins[] = MUX_SEL_PINS;

static ecsm_config_t config;
static uint16_t      ecsm_sw_value[MATRIX_ROWS][MATRIX_COLS];

static adc_mux adcMux;

static inline void discharge_capacitor(void) {
    writePinLow(DISCHARGE_PIN);
}
static inline void charge_capacitor(uint8_t row) {
    writePinHigh(DISCHARGE_PIN);
    writePinHigh(row_pins[row]);
}

static inline void init_mux_sel(void) {
    for (int idx = 0; idx < 3; idx++) {
        setPinOutput(mux_sel_pins[idx]);
    }
}

static inline void select_mux(uint8_t col) {
    uint8_t ch = col_channels[col];
    writePin(mux_sel_pins[0], ch & 1);
    writePin(mux_sel_pins[1], ch & 2);
    writePin(mux_sel_pins[2], ch & 4);
}

static inline void init_row(void) {
    for (int idx = 0; idx < MATRIX_ROWS; idx++) {
        setPinOutput(row_pins[idx]);
        writePinLow(row_pins[idx]);
    }
}

/* Initialize the peripherals pins */
int ecsm_init(ecsm_config_t const* const ecsm_config) {
    // Initialize config
    config = *ecsm_config;

    palSetLineMode(ANALOG_PORT, PAL_MODE_INPUT_ANALOG);
    adcMux = pinToMux(ANALOG_PORT);

    //Dummy call to make sure that adcStart() has been called in the appropriate state
    adc_read(adcMux);

    // Initialize discharge pin as discharge mode
    writePinLow(DISCHARGE_PIN);
    setPinOutputOpenDrain(DISCHARGE_PIN);

    // Initialize drive lines
    init_row();

    // Initialize multiplexer select pin
    init_mux_sel();

    // Enable AMUX
    setPinOutput(APLEX_EN_PIN_0);
    writePinLow(APLEX_EN_PIN_0);
    setPinOutput(APLEX_EN_PIN_1);
    writePinLow(APLEX_EN_PIN_1);

    return 0;
}

int ecsm_update(ecsm_config_t const* const ecsm_config) {
    // Save config
    config = *ecsm_config;
    return 0;
}

// Read the capacitive sensor value
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col) {
    uint16_t sw_value = 0;

    // Select the multiplexer
    if (channel == 0) {
        writePinHigh(APLEX_EN_PIN_0);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_0);
    } else {
        writePinHigh(APLEX_EN_PIN_1);
        select_mux(col);
        writePinLow(APLEX_EN_PIN_1);
    }

    // Set strobe pins to low state
    writePinLow(row_pins[row]);
    ATOMIC_BLOCK_FORCEON {
        // Set the row pin to high state and have capacitor charge
        charge_capacitor(row);
        // Read the ADC value
        sw_value = adc_read(adcMux);
    }
    // Discharge peak hold capacitor
    discharge_capacitor();
    // Waiting for the ghost capacitor to discharge fully
    wait_us(DISCHARGE_TIME);

    return sw_value;
}

// Update press/release state of key
bool ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value) {
    bool current_state = (*current_row >> col) & 1;

    // Press to release
    if (current_state && sw_value < config.ecsm_actuation_threshold) {
        *current_row &= ~(1 << col);
        return true;
    }

    // Release to press
    if ((!current_state) && sw_value > config.ecsm_release_threshold) {
        *current_row |= (1 << col);
        return true;
    }

    return false;
}

// Scan key values and update matrix state
bool ecsm_matrix_scan(matrix_row_t current_matrix[]) {
    bool updated = false;

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_1);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col] = ecsm_readkey_raw(0, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col, ecsm_sw_value[row][col]);
        }
    }

    // Disable AMUX of channel 1
    writePinHigh(APLEX_EN_PIN_0);
    for (int col = 0; col < sizeof(col_channels); col++) {
        for (int row = 0; row < MATRIX_ROWS; row++) {
            ecsm_sw_value[row][col + 8] = ecsm_readkey_raw(1, row, col);
            updated |= ecsm_update_key(&current_matrix[row], row, col + 8, ecsm_sw_value[row][col + 8]);
        }
    }
    return updated;
}

// Debug print key values
void ecsm_print_matrix(void) {
    for (int row = 0; row < MATRIX_ROWS; row++) {
        for (int col = 0; col < MATRIX_COLS; col++) {
            uprintf("%4d", ecsm_sw_value[row][col]);
            if (col < (MATRIX_COLS - 1)) {
                print(",");
            }
        }
        print("\n");
    }
    print("\n");
}

D keyboards/cipulot/rf_r1_8_9xu/ec_switch_matrix.h => keyboards/cipulot/rf_r1_8_9xu/ec_switch_matrix.h +0 -36
@@ 1,36 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "matrix.h"

typedef struct {
    uint16_t ecsm_actuation_threshold; // threshold for key release
    uint16_t ecsm_release_threshold;   // threshold for key press
} ecsm_config_t;

ecsm_config_t ecsm_config;

int      ecsm_init(ecsm_config_t const* const ecsm_config);
int      ecsm_update(ecsm_config_t const* const ecsm_config);
bool     ecsm_matrix_scan(matrix_row_t current_matrix[]);
uint16_t ecsm_readkey_raw(uint8_t channel, uint8_t row, uint8_t col);
bool     ecsm_update_key(matrix_row_t* current_row, uint8_t row, uint8_t col, uint16_t sw_value);
void     ecsm_print_matrix(void);

M keyboards/cipulot/rf_r1_8_9xu/halconf.h => keyboards/cipulot/rf_r1_8_9xu/halconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

M keyboards/cipulot/rf_r1_8_9xu/info.json => keyboards/cipulot/rf_r1_8_9xu/info.json +1 -7
@@ 8,19 8,13 @@
    },
    "diode_direction": "COL2ROW",
    "features": {
        "audio": false,
        "backlight": false,
        "bootmagic": true,
        "command": false,
        "bootmagic": false,
        "console": true,
        "extrakey": true,
        "mousekey": true,
        "nkro": true,
        "rgblight": true
    },
    "mouse_key": {
        "enabled": true
    },
    "indicators": {
        "caps_lock": "B3",
        "scroll_lock": "A14"

M keyboards/cipulot/rf_r1_8_9xu/keymaps/default/keymap.c => keyboards/cipulot/rf_r1_8_9xu/keymaps/default/keymap.c +2 -18
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 29,23 29,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_LCTL, KC_LGUI, KC_LALT, JP_MHEN,                KC_SPC,                JP_KANA, KC_RALT, KC_RGUI, MO(1),   KC_RCTL,             KC_LEFT, KC_DOWN, KC_RIGHT),

    [1] = LAYOUT_all(
        RGB_TOG, RGB_VAD, RGB_VAI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, QK_BOOT,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______,
        _______, _______, _______, _______,                   _______,                   _______, _______, _______, _______, _______,            _______, _______, _______),

    [2] = LAYOUT_all(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______,
        _______, _______, _______, _______,                   _______,                   _______, _______, _______, _______, _______,            _______, _______, _______),

    [3] = LAYOUT_all(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_ansi_tsangan/keymap.c => keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_ansi_tsangan/keymap.c +2 -18
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 27,23 27,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_LCTL, KC_LGUI, KC_LALT,                      KC_SPC,                                   KC_RALT, MO(1),   KC_RCTL,    KC_LEFT, KC_DOWN, KC_RIGHT),

    [1] = LAYOUT_tkl_ansi_tsangan(
        RGB_TOG, RGB_VAD, RGB_VAI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,            _______, _______, QK_BOOT,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                              _______,
        _______, _______, _______,                            _______,                                     _______, _______, _______,   _______, _______, _______),

    [2] = LAYOUT_tkl_ansi_tsangan(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                              _______,
        _______, _______, _______,                            _______,                                     _______, _______, _______,   _______, _______, _______),

    [3] = LAYOUT_tkl_ansi_tsangan(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_iso_tsangan/keymap.c => keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_iso_tsangan/keymap.c +2 -18
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 27,23 27,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_LCTL, KC_LGUI, KC_LALT,                      KC_SPC,                                 KC_RALT, MO(1),   KC_RCTL,   KC_LEFT, KC_DOWN, KC_RIGHT),

    [1] = LAYOUT_tkl_iso_tsangan(
        RGB_TOG, RGB_VAD, RGB_VAI, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,            _______, _______, QK_BOOT,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,                     _______,
        _______, _______, _______,                            _______,                                      _______, _______, _______,   _______, _______, _______),

    [2] = LAYOUT_tkl_iso_tsangan(
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,                     _______,
        _______, _______, _______,                            _______,                                      _______, _______, _______,   _______, _______, _______),

    [3] = LAYOUT_tkl_iso_tsangan(
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______,  _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_jis/keymap.c => keyboards/cipulot/rf_r1_8_9xu/keymaps/tkl_jis/keymap.c +2 -18
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 29,23 29,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_LCTL, KC_LGUI, KC_LALT, JP_MHEN,               KC_SPC,                 JP_HENK, JP_KANA, KC_RALT, MO(1),   KC_RCTL,             KC_LEFT, KC_DOWN, KC_RIGHT),

    [1] = LAYOUT_tkl_jis(
        RGB_TOG, RGB_VAD, RGB_VAI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, QK_BOOT,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                              _______,
        _______, _______, _______, _______,                   _______,                   _______, _______, _______, _______, _______,            _______, _______, _______),

    [2] = LAYOUT_tkl_jis(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                              _______,
        _______, _______, _______, _______,                   _______,                   _______, _______, _______, _______, _______,            _______, _______, _______),

    [3] = LAYOUT_tkl_jis(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,            _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/rf_r1_8_9xu/keymaps/via/config.h => keyboards/cipulot/rf_r1_8_9xu/keymaps/via/config.h +3 -3
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 16,5 16,5 @@

#pragma once

// This is the size of the EEPROM for the custom VIA-specific data
#define EECONFIG_USER_DATA_SIZE 4
// This is the firmware version for VIA support to avoid conflicts on menu fetching
#define VIA_FIRMWARE_VERSION 1

M keyboards/cipulot/rf_r1_8_9xu/keymaps/via/keymap.c => keyboards/cipulot/rf_r1_8_9xu/keymaps/via/keymap.c +2 -18
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,


@@ 29,23 29,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        KC_LCTL, KC_LGUI, KC_LALT, JP_MHEN,                KC_SPC,                JP_KANA, KC_RALT, KC_RGUI, MO(1),   KC_RCTL,             KC_LEFT, KC_DOWN, KC_RIGHT),

    [1] = LAYOUT_all(
        RGB_TOG, RGB_VAD, RGB_VAI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, QK_BOOT,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______,
        _______, _______, _______, _______,                   _______,                   _______, _______, _______, _______, _______,            _______, _______, _______),

    [2] = LAYOUT_all(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______,
        _______, _______, _______, _______,                   _______,                   _______, _______, _______, _______, _______,            _______, _______, _______),

    [3] = LAYOUT_all(
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        QK_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,                     _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,   _______, _______, _______,
        _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,

M keyboards/cipulot/rf_r1_8_9xu/keymaps/via/rules.mk => keyboards/cipulot/rf_r1_8_9xu/keymaps/via/rules.mk +0 -2
@@ 1,3 1,1 @@
VIA_ENABLE = yes

SRC += via_apc.c

D keyboards/cipulot/rf_r1_8_9xu/keymaps/via/via_apc.c => keyboards/cipulot/rf_r1_8_9xu/keymaps/via/via_apc.c +0 -156
@@ 1,156 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "action.h"
#include "via.h"

void apc_init_thresholds(void);
void apc_set_threshold(bool is_for_actuation);

// Declaring an _apc_config_t struct that will store our data
typedef struct _apc_config_t {
    uint16_t actuation_threshold;
    uint16_t release_threshold;
} apc_config;

// Check if the size of the reserved persistent memory is the same as the size of struct apc_config
_Static_assert(sizeof(apc_config) == EECONFIG_USER_DATA_SIZE, "Mismatch in keyboard EECONFIG stored data");

// Declaring a new variable apc of type apc_config
apc_config apc;

// Declaring enums for VIA config menu
enum via_apc_enums {
    // clang-format off
    id_apc_actuation_threshold = 1,
    id_apc_release_threshold = 2
    // clang-format on
};

// Initializing persistent memory configuration: default values are declared and stored in PMEM
void eeconfig_init_user(void) {
    // Default values
    apc.actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    apc.release_threshold   = DEFAULT_RELEASE_LEVEL;
    // Write default value to EEPROM now
    eeconfig_update_user_datablock(&apc);
}

// On Keyboard startup
void keyboard_post_init_user(void) {
    // Read custom menu variables from memory
    eeconfig_read_user_datablock(&apc);
    apc_init_thresholds();
}

// Handle the data received by the keyboard from the VIA menus
void apc_config_set_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            apc.actuation_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(true);
            break;
        }
        case id_apc_release_threshold: {
            apc.release_threshold = value_data[1] | (value_data[0] << 8);
            apc_set_threshold(false);
            break;
        }
    }
}

// Handle the data sent by the keyboard to the VIA menus
void apc_config_get_value(uint8_t *data) {
    // data = [ value_id, value_data ]
    uint8_t *value_id   = &(data[0]);
    uint8_t *value_data = &(data[1]);

    switch (*value_id) {
        case id_apc_actuation_threshold: {
            value_data[0] = apc.actuation_threshold >> 8;
            value_data[1] = apc.actuation_threshold & 0xFF;
            break;
        }
        case id_apc_release_threshold: {
            value_data[0] = apc.release_threshold >> 8;
            value_data[1] = apc.release_threshold & 0xFF;
            break;
        }
    }
}

// Save the data to persistent memory after changes are made
void apc_config_save(void) {
    eeconfig_update_user_datablock(&apc);
}

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    // data = [ command_id, channel_id, value_id, value_data ]
    uint8_t *command_id        = &(data[0]);
    uint8_t *channel_id        = &(data[1]);
    uint8_t *value_id_and_data = &(data[2]);

    if (*channel_id == id_custom_channel) {
        switch (*command_id) {
            case id_custom_set_value: {
                apc_config_set_value(value_id_and_data);
                break;
            }
            case id_custom_get_value: {
                apc_config_get_value(value_id_and_data);
                break;
            }
            case id_custom_save: {
                apc_config_save();
                break;
            }
            default: {
                // Unhandled message.
                *command_id = id_unhandled;
                break;
            }
        }
        return;
    }

    *command_id = id_unhandled;
}

// Initialize the thresholds
void apc_init_thresholds(void) {
    ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;
    ecsm_config.ecsm_release_threshold   = apc.release_threshold;

    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

// Set the thresholds
void apc_set_threshold(bool is_for_actuation) {
    if (is_for_actuation) {
        ecsm_config.ecsm_actuation_threshold = apc.actuation_threshold;

    } else {
        ecsm_config.ecsm_release_threshold = apc.release_threshold;
    }
    // Update the ecsm_config
    ecsm_update(&ecsm_config);
}

D keyboards/cipulot/rf_r1_8_9xu/matrix.c => keyboards/cipulot/rf_r1_8_9xu/matrix.c +0 -44
@@ 1,44 0,0 @@
/* Copyright 2023 Cipulot
 *
 * 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 "ec_switch_matrix.h"
#include "matrix.h"

/* matrix state(1:on, 0:off) */
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS];     // debounced values

void matrix_init_custom(void) {
    // Default values, overwritten by VIA if enabled later
    ecsm_config.ecsm_actuation_threshold = DEFAULT_ACTUATION_LEVEL;
    ecsm_config.ecsm_release_threshold   = DEFAULT_RELEASE_LEVEL;

    ecsm_init(&ecsm_config);
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool updated = ecsm_matrix_scan(current_matrix);

// RAW matrix values on console
#ifdef CONSOLE_ENABLE
    static int cnt = 0;
    if (cnt++ == 350) {
        cnt = 0;
        ecsm_print_matrix();
    }
#endif
    return updated;
}

M keyboards/cipulot/rf_r1_8_9xu/mcuconf.h => keyboards/cipulot/rf_r1_8_9xu/mcuconf.h +1 -1
@@ 2,7 2,7 @@
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,

A keyboards/cipulot/rf_r1_8_9xu/post_rules.mk => keyboards/cipulot/rf_r1_8_9xu/post_rules.mk +3 -0
@@ 0,0 1,3 @@
ifeq ($(strip $(VIA_ENABLE)), yes)
    SRC += keyboards/cipulot/common/via_ec.c
endif

M keyboards/cipulot/rf_r1_8_9xu/readme.md => keyboards/cipulot/rf_r1_8_9xu/readme.md +1 -2
@@ 20,8 20,7 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Bootloader

Enter the bootloader in 3 ways:
Enter the bootloader in 2 ways:

* **Bootmagic reset**: Hold down the key at (0,0) in the matrix (usually the top left key or Escape) and plug in the keyboard
* **Physical Boot0 pins**: Short the Boot0 pins on the back of the PCB while plugging in the keyboard
* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available

M keyboards/cipulot/rf_r1_8_9xu/rules.mk => keyboards/cipulot/rf_r1_8_9xu/rules.mk +2 -2
@@ 1,4 1,4 @@
CUSTOM_MATRIX = lite
SRC += matrix.c ec_switch_matrix.c

ANALOG_DRIVER_REQUIRED = yes
SRC += keyboards/cipulot/common/matrix.c keyboards/cipulot/common/ec_board.c keyboards/cipulot/common/ec_switch_matrix.c
OPT = 2

Do not follow this link