[Keyboard] Add Pica40 (#19220)

Co-authored-by: Drashna Jaelre <drashna@live.com>
This commit is contained in:
Evgenii Vilkov
2022-12-30 01:53:31 +01:00
committed by GitHub
parent a7ff2b8d54
commit ced8142847
18 changed files with 775 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
// Copyright 2022 zzeneg (@zzeneg)
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#define SERIAL_USART_TX_PIN GP0
#define RP2040_BOOTLOADER_DOUBLE_TAP_RESET
#define RP2040_BOOTLOADER_DOUBLE_TAP_RESET_LED GP17
#define RP2040_BOOTLOADER_DOUBLE_TAP_RESET_TIMEOUT 500U
#ifdef RGBLIGHT_ENABLE
# define RGBLIGHT_DISABLE_KEYCODES // disable keycodes for RGB Light controls, only status LED is supported
# define PICA40_RGBLIGHT_TIMEOUT 5 // turn RGB off after N minutes
#endif
#ifdef ENCODER_ENABLE
# define SPLIT_TRANSACTION_IDS_KB ENCODER_SYNC
#endif

View File

@@ -0,0 +1,53 @@
{
"processor": "RP2040",
"bootloader": "rp2040",
"diode_direction": "COL2ROW",
"matrix_pins": {
"cols": ["GP26", "GP27", "GP28", "GP29", "GP6"],
"rows": ["GP3", "GP4", "GP2", "GP1"]
},
"indicators": {
"num_lock": "GP17",
"caps_lock": "GP16",
"scroll_lock": "GP25",
"on_state": 0
},
"features": {
"bootmagic": true,
"command": false,
"console": false,
"mousekey": true,
"extrakey": true,
"encoder": true,
"rgblight": true,
"nkro": true
},
"rgblight": {
"led_count": 1,
"pin": "GP12",
"split": true,
"layers": {
"enabled": true,
"max": 3
}
},
"split": {
"enabled": true,
"encoder": {
"right": {
"rotary": []
}
}
},
"encoder": {
"rotary": [{ "pin_a": "GP7", "pin_b": "GP7" }]
},
"usb": {
"device_version": "1.0.0",
"pid": "0x0842",
"vid": "0xFEED"
},
"build": {
"lto": true
}
}

View File

@@ -0,0 +1,8 @@
# if ENCODER_ENABLE is set, add defines but avoid adding encoder.c as it's replaced by custom code in rev2.c
ifeq ($(strip $(ENCODER_ENABLE)), yes)
ENCODER_ENABLE := no
OPT_DEFS += -DENCODER_ENABLE
ifeq ($(strip $(ENCODER_MAP_ENABLE)), yes)
OPT_DEFS += -DENCODER_MAP_ENABLE
endif
endif

View File

@@ -0,0 +1,189 @@
// Copyright 2022 zzeneg (@zzeneg)
// SPDX-License-Identifier: GPL-2.0-or-later
#include "rev2.h"
#ifdef ENCODER_ENABLE // code based on encoder.c
static const pin_t encoders_pad_a[] = ENCODERS_PAD_A;
static const pin_t encoders_pad_b[] = ENCODERS_PAD_B;
static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
static uint8_t encoder_state = 3;
static int8_t encoder_pulses = 0;
static uint8_t encoder_value = 0;
typedef struct encoder_sync_data {
int value;
} encoder_sync_data;
// custom handler that returns encoder B pin status from slave side
void encoder_sync_slave_handler(uint8_t in_buflen, const void *in_data, uint8_t out_buflen, void *out_data) {
encoder_sync_data *data = (encoder_sync_data *)out_data;
data->value = readPin(encoders_pad_b[0]);
}
__attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) {
return true;
}
bool encoder_update_kb(uint8_t index, bool clockwise) {
if (!encoder_update_user(index, clockwise)) return false;
tap_code(clockwise ? KC_VOLU : KC_VOLD);
return false;
}
#ifdef ENCODER_MAP_ENABLE
static void encoder_exec_mapping(uint8_t index, bool clockwise) {
action_exec(clockwise ? ENCODER_CW_EVENT(index, true) : ENCODER_CCW_EVENT(index, true));
wait_ms(ENCODER_MAP_KEY_DELAY);
action_exec(clockwise ? ENCODER_CW_EVENT(index, false) : ENCODER_CCW_EVENT(index, false));
wait_ms(ENCODER_MAP_KEY_DELAY);
}
#endif // ENCODER_MAP_ENABLE
void encoder_init(void) {
setPinInputHigh(encoders_pad_a[0]);
setPinInputHigh(encoders_pad_b[0]);
wait_us(100);
transaction_register_rpc(ENCODER_SYNC, encoder_sync_slave_handler);
}
bool encoder_read(void) {
// ignore if running on slave side
if (!is_keyboard_master()) return false;
bool changed = false;
encoder_sync_data data = {0};
// request pin B status from slave side
if (transaction_rpc_recv(ENCODER_SYNC, sizeof(data), &data)) {
uint8_t new_status = (readPin(encoders_pad_a[0]) << 0) | (data.value << 1);
if ((encoder_state & 0x3) != new_status) {
encoder_state <<= 2;
encoder_state |= new_status;
encoder_pulses += encoder_LUT[encoder_state & 0xF];
if (encoder_pulses >= ENCODER_RESOLUTION) {
encoder_value++;
changed = true;
#ifdef ENCODER_MAP_ENABLE
encoder_exec_mapping(0, false);
#else // ENCODER_MAP_ENABLE
encoder_update_kb(0, false);
#endif // ENCODER_MAP_ENABLE
}
if (encoder_pulses <= -ENCODER_RESOLUTION) {
encoder_value--;
changed = true;
#ifdef ENCODER_MAP_ENABLE
encoder_exec_mapping(0, true);
#else // ENCODER_MAP_ENABLE
encoder_update_kb(0, true);
#endif // ENCODER_MAP_ENABLE
}
encoder_pulses %= ENCODER_RESOLUTION;
}
}
return changed;
}
// do not use standard split encoder transactions
void encoder_state_raw(uint8_t *slave_state) {}
void encoder_update_raw(uint8_t *slave_state) {}
#endif // ENCODER_ENABLE
#ifdef PICA40_RGBLIGHT_TIMEOUT
uint16_t check_rgblight_timer = 0;
uint16_t idle_timer = 0;
int8_t counter = 0;
bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
if (record->event.pressed && timer_elapsed(idle_timer) > 1000) {
idle_timer = timer_read();
counter = 0;
if (!rgblight_is_enabled()) {
rgblight_enable_noeeprom();
}
}
return process_record_user(keycode, record);
}
#endif // PICA40_RGBLIGHT_TIMEOUT
#if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
uint16_t check_layer_timer = 0;
bool is_layer_active = false;
bool should_set_rgblight = false;
#endif // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
void keyboard_post_init_kb(void) {
setPinOutput(PICA40_RGB_POWER_PIN);
#ifdef PICA40_RGBLIGHT_TIMEOUT
idle_timer = timer_read();
check_rgblight_timer = timer_read();
rgblight_enable_noeeprom();
#endif // RGBLIGHT_ENABLE
#if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
check_layer_timer = timer_read();
#endif // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
keyboard_post_init_user();
}
void housekeeping_task_kb(void) {
#ifdef PICA40_RGBLIGHT_TIMEOUT
if (is_keyboard_master()) {
if (timer_elapsed(check_rgblight_timer) > 1000) {
check_rgblight_timer = timer_read();
if (rgblight_is_enabled() && timer_elapsed(idle_timer) > 10000) {
idle_timer = timer_read();
counter++;
}
if (rgblight_is_enabled() && counter > PICA40_RGBLIGHT_TIMEOUT * 6) {
counter = 0;
rgblight_disable_noeeprom();
}
}
}
#endif // PICA40_RGBLIGHT_TIMEOUT
#if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
if (timer_elapsed(check_layer_timer) > 100) {
check_layer_timer = timer_read();
if (should_set_rgblight) {
// set in the next housekeeping cycle after setting pin to avoid issues
rgblight_set();
should_set_rgblight = false;
}
bool current_is_layer_active = false;
for (uint8_t i = 0; i < RGBLIGHT_MAX_LAYERS; i++) {
current_is_layer_active = current_is_layer_active || rgblight_get_layer_state(i);
}
if (is_layer_active != current_is_layer_active) {
is_layer_active = current_is_layer_active;
should_set_rgblight = true;
if (is_layer_active) {
writePinHigh(PICA40_RGB_POWER_PIN);
} else {
writePinLow(PICA40_RGB_POWER_PIN);
}
}
}
#endif // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
housekeeping_task_user();
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 zzeneg (@zzeneg)
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "quantum.h"
#include "gpio.h"
// RGB LED support for XIAO RP2040
#define PICA40_RGB_POWER_PIN GP11
// enable custom encoder functionality for Pica40
#ifdef ENCODER_ENABLE
# include "encoder.h"
# include "transactions.h"
# ifndef ENCODER_MAP_KEY_DELAY
# define ENCODER_MAP_KEY_DELAY 2
# endif
# ifndef ENCODER_RESOLUTION
# define ENCODER_RESOLUTION 4
# endif
#endif

View File

@@ -0,0 +1,2 @@
SERIAL_DRIVER = vendor
WS2812_DRIVER = vendor