From 14946e23e8b1f3061410f5e209366c4bedbccd53 Mon Sep 17 00:00:00 2001 From: takashicompany Date: Sun, 17 Nov 2024 02:17:17 +0900 Subject: [PATCH] joy stick --- .../takashicompany/left_slinger/config.h | 5 ++ .../left_slinger/custom_joystick.c | 49 +++++++++++++++++++ .../left_slinger/custom_joystick.h | 6 +++ .../takashicompany/left_slinger/rules.mk | 4 ++ 4 files changed, 64 insertions(+) create mode 100644 keyboards/takashicompany/left_slinger/config.h create mode 100644 keyboards/takashicompany/left_slinger/custom_joystick.c create mode 100644 keyboards/takashicompany/left_slinger/custom_joystick.h create mode 100644 keyboards/takashicompany/left_slinger/rules.mk diff --git a/keyboards/takashicompany/left_slinger/config.h b/keyboards/takashicompany/left_slinger/config.h new file mode 100644 index 00000000000..e076cbf9e2e --- /dev/null +++ b/keyboards/takashicompany/left_slinger/config.h @@ -0,0 +1,5 @@ +#pragma once + +#define ANALOG_JOYSTICK_X_AXIS_PIN GP28 +#define ANALOG_JOYSTICK_Y_AXIS_PIN GP29 +#define ANALOG_JOYSTICK_READ_INTERVAL 1 \ No newline at end of file diff --git a/keyboards/takashicompany/left_slinger/custom_joystick.c b/keyboards/takashicompany/left_slinger/custom_joystick.c new file mode 100644 index 00000000000..9f6acd6826a --- /dev/null +++ b/keyboards/takashicompany/left_slinger/custom_joystick.c @@ -0,0 +1,49 @@ +#include QMK_KEYBOARD_H +#include "analog.h" +#include "gpio.h" +#include "wait.h" +#include "timer.h" +#include +#include "print.h" +#include "custom_joystick.h" + +int16_t xOrigin, yOrigin; +uint16_t lastCursor = 0; +int16_t joystick_ratio = 100; + +int16_t axisCoordinate_custom(pin_t pin, uint16_t origin) { + int8_t direction; // 符号 + int16_t distanceFromOrigin; // 原点からの距離。負数にはならない + + int16_t position = analogReadPin(pin); // 多分だけどデフォルトだと512が中心に来るようになっている + + if (origin == position) { // 原点と同じなら0とする + return 0; + } else if (origin > position) { // 原点よりマイナス方向の場合の処理 + distanceFromOrigin = origin - position; + direction = -1; + } else { // 原点よりプラス方向の処理 + distanceFromOrigin = position - origin; + direction = 1; + } + + return distanceFromOrigin * direction; +} + +void pointing_device_driver_init(void) { + xOrigin = analogReadPin(ANALOG_JOYSTICK_X_AXIS_PIN); + yOrigin = analogReadPin(ANALOG_JOYSTICK_Y_AXIS_PIN); +} +report_mouse_t pointing_device_driver_get_report(report_mouse_t mouse_report) { + + if (timer_elapsed(lastCursor) > ANALOG_JOYSTICK_READ_INTERVAL) { // 多分、指定のミリ秒経過したかを見て処理を走らせている + lastCursor = timer_read(); + mouse_report.x = axisCoordinate_custom(ANALOG_JOYSTICK_X_AXIS_PIN, xOrigin) / joystick_ratio; + mouse_report.y = axisCoordinate_custom(ANALOG_JOYSTICK_Y_AXIS_PIN, yOrigin) / joystick_ratio; + } + + return mouse_report; +} + +uint16_t pointing_device_driver_get_cpi(void) { return 0; } +void pointing_device_driver_set_cpi(uint16_t cpi) {} \ No newline at end of file diff --git a/keyboards/takashicompany/left_slinger/custom_joystick.h b/keyboards/takashicompany/left_slinger/custom_joystick.h new file mode 100644 index 00000000000..0d7f6b94316 --- /dev/null +++ b/keyboards/takashicompany/left_slinger/custom_joystick.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include + +extern int16_t joystick_ratio; \ No newline at end of file diff --git a/keyboards/takashicompany/left_slinger/rules.mk b/keyboards/takashicompany/left_slinger/rules.mk new file mode 100644 index 00000000000..ea3cc965c5d --- /dev/null +++ b/keyboards/takashicompany/left_slinger/rules.mk @@ -0,0 +1,4 @@ +POINTING_DEVICE_ENABLE = yes +POINTING_DEVICE_DRIVER = custom +ANALOG_DRIVER_REQUIRED = yes +SRC += custom_joystick.c \ No newline at end of file