Skip to content

Instantly share code, notes, and snippets.

@kurosuke
Created March 31, 2025 04:34
Show Gist options
  • Select an option

  • Save kurosuke/2dacf804abefb07f59187bb10d451758 to your computer and use it in GitHub Desktop.

Select an option

Save kurosuke/2dacf804abefb07f59187bb10d451758 to your computer and use it in GitHub Desktop.
SU120 micro keyboard Keymap.c
#include QMK_KEYBOARD_H
// カスタムキーコードを定義
enum custom_keycodes {
SAFE_MO1 = SAFE_RANGE,
SAFE_MO2,
// 必要に応じて追加
};
// キーマップの定義
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT(
KC_BTN1, KC_LEFT_SHIFT, KC_LEFT_CTRL,SAFE_MO1
),
[1] = LAYOUT(
KC_SPC, KC_UP, KC_DOWN, XXXXXXX
)
};
#if defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = {
[0] = { ENCODER_CCW_CW(KC_WH_U, KC_WH_D), ENCODER_CCW_CW(KC_1, KC_2) },
// Encoder 1 Encoder 2(None)
[1] = { ENCODER_CCW_CW(KC_PAGE_DOWN, KC_PAGE_UP), ENCODER_CCW_CW(KC_1, KC_2) }
// Encoder 1 Encoder 2(None)
};
#endif
void keyboard_post_init_user(void) {
// デバッグ機能を無効化してファームウェアサイズを削減
debug_enable=false;
//debug_matrix=false;
//debug_keyboard=false;
//debug_mouse=false;
}
// QMKロゴデータ(32x32px)
static const char PROGMEM qmk_logo[] = {
// 'r', 32x32px
0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, 0xe0, 0x70, 0x38, 0x38, 0x1c, 0x9c, 0x8c, 0xce, 0xce, 0x0e,
0x0e, 0xce, 0xce, 0x8c, 0x9c, 0x1c, 0x38, 0x38, 0x70, 0xe0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0xe0, 0xfc, 0xff, 0x07, 0x03, 0x60, 0x78, 0x7e, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x07, 0x00,
0x00, 0x07, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0x7e, 0x78, 0x60, 0x03, 0x0f, 0xff, 0xfc, 0xe0, 0x00,
0x00, 0x0f, 0x7f, 0xfe, 0xe0, 0x80, 0x0e, 0x3c, 0x7c, 0xfe, 0xfc, 0xf0, 0xe0, 0xc0, 0xc0, 0x00,
0x00, 0xc0, 0xc0, 0xe0, 0xf0, 0xfc, 0xfe, 0x7c, 0x3c, 0x0e, 0x80, 0xe0, 0xff, 0x7f, 0x0f, 0x00,
0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1c, 0x1c, 0x38, 0x31, 0x73, 0x73, 0x63, 0x67, 0x60,
0x60, 0x67, 0x63, 0x73, 0x73, 0x31, 0x38, 0x1c, 0x1c, 0x0f, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00
};
oled_rotation_t oled_init_user(oled_rotation_t rotation) {
return OLED_ROTATION_180;
}
// レイヤー状態を追跡する変数
static uint8_t saved_layer = 0;
static bool needs_update = false; // 追加: 更新フラグ
void oled_display(void)
{
// エンコーダモード表示
// logo表示
uint8_t bytes_per_page = sizeof(qmk_logo)/4;
for (uint8_t page = 0; page < 4; page++) {
oled_set_cursor(0, page);
oled_write_raw_P(qmk_logo + (bytes_per_page*page), bytes_per_page);
}
// QMKロゴ表示
oled_set_cursor(7, 0);
switch (get_highest_layer(layer_state)) {
case 0:
oled_write_P(PSTR("Enc: WHEEL "), false);
break;
case 1:
oled_write_P(PSTR("Enc: PG UP/DN"), false);
break;
}
// キー配置表示
oled_set_cursor(7, 3);
switch (get_highest_layer(layer_state)) {
case 0:
oled_write_P(PSTR("BTN1 SHFT CTRL"), false);
break;
case 1:
oled_write_P(PSTR("SPC ^ v "), false);
break;
}
}
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
case SAFE_MO1:
if (record->event.pressed) {
// キーを押した時:現在のレイヤーを保存して新しいレイヤーに切り替え
saved_layer = get_highest_layer(layer_state);
layer_on(1); // レイヤー1をオン
needs_update = true; // 更新フラグをセット
oled_display();
} else {
// キーを離した時:保存していたレイヤーに戻す
layer_off(1); // レイヤー1をオフ
needs_update = true; // 更新フラグをセット
layer_on(saved_layer);
oled_display();
}
return false; // 他のキーボードにキーコードを送信しない
case SAFE_MO2:
if (record->event.pressed) {
saved_layer = get_highest_layer(layer_state);
layer_on(2);
} else {
layer_off(2);
layer_on(saved_layer);
}
return false;
}
return true; // 他のキーは通常通り処理
}
bool oled_task_user(void) {
if (needs_update) { // フラグがセットされている場合のみ更新
oled_display();
needs_update = false; // フラグをリセット
}
return true; // trueを返して通常の更新も許可
}
// エンコーダーの回転処理を改善するカスタム関数
#ifndef ENCODER_MAP_ENABLE
// ENCODER_MAP_ENABLEが無効な場合のみこの関数が使用される
// 前回の回転方向を記録する変数
static uint8_t prev_encoder_direction[NUM_ENCODERS] = {0};
// 連続した同じ方向の回転をカウントする変数
static uint8_t same_direction_count[NUM_ENCODERS] = {0};
bool encoder_update_user(uint8_t index, bool clockwise) {
// エンコーダーの状態変化を検出したらOLED更新フラグをセット
needs_update = true;
// 現在のレイヤーに基づいて処理
uint8_t layer = get_highest_layer(layer_state);
// 方向の一貫性をチェック - 誤検知防止
uint8_t current_direction = clockwise ? 1 : 2;
// 前回と同じ方向なら、カウントを増やす
if (prev_encoder_direction[index] == current_direction) {
same_direction_count[index]++;
} else {
// 方向が変わった場合、カウントをリセット
same_direction_count[index] = 0;
// 方向が変わったばかりの場合は、誤検知の可能性があるため無視する
// 少なくとも1回は同じ方向に回転した後でないと方向転換と見なさない
if (prev_encoder_direction[index] != 0) {
prev_encoder_direction[index] = current_direction;
return false;
}
}
// 方向を記録
prev_encoder_direction[index] = current_direction;
if (index == 0) { // 最初のエンコーダー
switch (layer) {
case 0:
// レイヤー0: マウスホイール - 通常速度
if (clockwise) {
tap_code(KC_WH_D);
} else {
tap_code(KC_WH_U);
}
break;
case 1:
// レイヤー1: ページスクロール
if (clockwise) {
tap_code(KC_PAGE_DOWN);
} else {
tap_code(KC_PAGE_UP);
}
break;
default:
if (clockwise) {
tap_code(KC_RIGHT);
} else {
tap_code(KC_LEFT);
}
break;
}
} else if (index == 1) { // 2番目のエンコーダー(存在する場合)
if (clockwise) {
tap_code(KC_2);
} else {
tap_code(KC_1);
}
}
return false; // QMKのデフォルト処理をスキップ
}
#else
// ENCODER_MAP_ENABLEが有効な場合のエンコーダー処理
// 前回の回転方向を記録する変数
static uint8_t prev_encoder_direction[NUM_ENCODERS] = {0};
// 連続した同じ方向の回転をカウントする変数
static uint8_t same_direction_count[NUM_ENCODERS] = {0};
// エンコーダーマップの後処理を行う関数
void post_encoder_event(uint8_t index, bool clockwise) {
// エンコーダーの状態変化を検出したらOLED更新フラグをセット
needs_update = true;
}
bool encoder_update_user(uint8_t index, bool clockwise) {
// 方向の一貫性をチェック - 誤検知防止
uint8_t current_direction = clockwise ? 1 : 2;
// 前回と同じ方向なら、カウントを増やす
if (prev_encoder_direction[index] == current_direction) {
same_direction_count[index]++;
} else {
// 方向が変わった場合、カウントをリセット
same_direction_count[index] = 0;
// 方向が変わったばかりの場合は、誤検知の可能性があるため無視する
if (prev_encoder_direction[index] != 0) {
prev_encoder_direction[index] = current_direction;
return false;
}
}
// 方向を記録
prev_encoder_direction[index] = current_direction;
// エンコーダーマップの処理を実行
if (!encoder_update_kb(index, clockwise)) {
return false;
}
// 追加の処理を実行
post_encoder_event(index, clockwise);
return true;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment