summaryrefslogtreecommitdiff
path: root/keyboards/pica40/rev2
diff options
context:
space:
mode:
authorEvgenii Vilkov <zzeneg@gmail.com>2022-12-30 01:53:31 +0100
committerGitHub <noreply@github.com>2022-12-30 11:53:31 +1100
commitced8142847e7c6a1e0e260017131e34e3da1b0ff (patch)
tree35f65396f0119a39f04d96cdb133671cc07a3f5b /keyboards/pica40/rev2
parenta7ff2b8d54e1b56c5c25f4a7b6c8c5d9dedee2ab (diff)
[Keyboard] Add Pica40 (#19220)
Co-authored-by: Drashna Jaelre <drashna@live.com>
Diffstat (limited to 'keyboards/pica40/rev2')
-rw-r--r--keyboards/pica40/rev2/config.h19
-rw-r--r--keyboards/pica40/rev2/info.json53
-rw-r--r--keyboards/pica40/rev2/post_rules.mk8
-rw-r--r--keyboards/pica40/rev2/rev2.c189
-rw-r--r--keyboards/pica40/rev2/rev2.h22
-rw-r--r--keyboards/pica40/rev2/rules.mk2
6 files changed, 293 insertions, 0 deletions
diff --git a/keyboards/pica40/rev2/config.h b/keyboards/pica40/rev2/config.h
new file mode 100644
index 0000000000..1a59bee3dd
--- /dev/null
+++ b/keyboards/pica40/rev2/config.h
@@ -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
diff --git a/keyboards/pica40/rev2/info.json b/keyboards/pica40/rev2/info.json
new file mode 100644
index 0000000000..99540900b9
--- /dev/null
+++ b/keyboards/pica40/rev2/info.json
@@ -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
+ }
+}
diff --git a/keyboards/pica40/rev2/post_rules.mk b/keyboards/pica40/rev2/post_rules.mk
new file mode 100644
index 0000000000..e4dda1925b
--- /dev/null
+++ b/keyboards/pica40/rev2/post_rules.mk
@@ -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
diff --git a/keyboards/pica40/rev2/rev2.c b/keyboards/pica40/rev2/rev2.c
new file mode 100644
index 0000000000..c585ec56d6
--- /dev/null
+++ b/keyboards/pica40/rev2/rev2.c
@@ -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();
+}
diff --git a/keyboards/pica40/rev2/rev2.h b/keyboards/pica40/rev2/rev2.h
new file mode 100644
index 0000000000..473011fbb0
--- /dev/null
+++ b/keyboards/pica40/rev2/rev2.h
@@ -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
diff --git a/keyboards/pica40/rev2/rules.mk b/keyboards/pica40/rev2/rules.mk
new file mode 100644
index 0000000000..8fb51ec82d
--- /dev/null
+++ b/keyboards/pica40/rev2/rules.mk
@@ -0,0 +1,2 @@
+SERIAL_DRIVER = vendor
+WS2812_DRIVER = vendor