From 39227f75f7171a85c4a1fbe8cb43218c0df7d4bb Mon Sep 17 00:00:00 2001 From: Drashna Jael're Date: Wed, 16 Jun 2021 16:55:25 -0700 Subject: [PATCH] Move pimoroni trackball to drivers/sensors/ too --- .../sensors}/pimoroni_trackball.c | 55 ++----- .../sensors}/pimoroni_trackball.h | 0 keyboards/draculad/keymaps/pimoroni/keymap.c | 2 +- .../keymaps/pimoroni/pimoroni_trackball.h | 35 ---- keyboards/draculad/keymaps/pimoroni/rules.mk | 2 +- layouts/community/ergodox/drashna/config.h | 1 + users/drashna/drashna.h | 2 +- users/drashna/pimoroni_trackball.c | 151 ------------------ users/drashna/rules.mk | 2 +- 9 files changed, 14 insertions(+), 236 deletions(-) rename {keyboards/draculad/keymaps/pimoroni => drivers/sensors}/pimoroni_trackball.c (72%) rename {users/drashna => drivers/sensors}/pimoroni_trackball.h (100%) delete mode 100644 keyboards/draculad/keymaps/pimoroni/pimoroni_trackball.h delete mode 100644 users/drashna/pimoroni_trackball.c diff --git a/keyboards/draculad/keymaps/pimoroni/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c similarity index 72% rename from keyboards/draculad/keymaps/pimoroni/pimoroni_trackball.c rename to drivers/sensors/pimoroni_trackball.c index c4f4a0441a4f..0437fc483e88 100644 --- a/keyboards/draculad/keymaps/pimoroni/pimoroni_trackball.c +++ b/drivers/sensors/pimoroni_trackball.c @@ -26,8 +26,8 @@ static float precisionSpeed = 1; static uint16_t i2c_timeout_timer; -#ifndef I2C_TIMEOUT -# define I2C_TIMEOUT 100 +#ifndef PIMORONI_I2C_TIMEOUT +# define PIMORONI_I2C_TIMEOUT 100 #endif #ifndef I2C_WAITCHECK # define I2C_WAITCHECK 1000 @@ -38,7 +38,7 @@ static uint16_t i2c_timeout_timer; void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) { uint8_t data[] = {0x00, red, green, blue, white}; - i2c_transmit(TRACKBALL_WRITE, data, sizeof(data), I2C_TIMEOUT); + i2c_transmit(TRACKBALL_WRITE, data, sizeof(data), PIMORONI_I2C_TIMEOUT); } int16_t mouse_offset(uint8_t positive, uint8_t negative, int16_t scale) { @@ -68,59 +68,19 @@ __attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* m } } -bool process_record_kb(uint16_t keycode, keyrecord_t* record) { - if (true) { - xprintf("KL: kc: %u, col: %u, row: %u, pressed: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed); - } - - - if (!process_record_user(keycode, record)) { return false; } - -/* If Mousekeys is disabled, then use handle the mouse button - * keycodes. This makes things simpler, and allows usage of - * the keycodes in a consistent manner. But only do this if - * Mousekeys is not enable, so it's not handled twice. - */ -#ifndef MOUSEKEY_ENABLE - if (IS_MOUSEKEY_BUTTON(keycode)) { - report_mouse_t currentReport = pointing_device_get_report(); - if (record->event.pressed) { - currentReport.buttons |= 1 << (keycode - KC_MS_BTN1); - } else { - currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1)); - } - pointing_device_set_report(currentReport); - pointing_device_send(); - } -#endif - - return true; -} - -void trackball_register_button(bool pressed, enum mouse_buttons button) { - report_mouse_t currentReport = pointing_device_get_report(); - if (pressed) { - currentReport.buttons |= button; - } else { - currentReport.buttons &= ~button; - } - pointing_device_set_report(currentReport); -} - float trackball_get_precision(void) { return precisionSpeed; } void trackball_set_precision(float precision) { precisionSpeed = precision; } bool trackball_is_scrolling(void) { return scrolling; } void trackball_set_scrolling(bool scroll) { scrolling = scroll; } - -__attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x80, 0x00, 0x00, 0x00); } +__attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x00, 0x00, 0x00, 0x00); } void pointing_device_task(void) { static bool debounce; static uint16_t debounce_timer; uint8_t state[5] = {}; if (timer_elapsed(i2c_timeout_timer) > I2C_WAITCHECK) { - if (i2c_readReg(TRACKBALL_WRITE, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) { + if (i2c_readReg(TRACKBALL_READ, 0x04, state, 5, PIMORONI_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) { if (!state[4] && !debounce) { if (scrolling) { #ifdef PIMORONI_TRACKBALL_INVERT_X @@ -159,7 +119,10 @@ void pointing_device_task(void) { if (timer_elapsed(debounce_timer) > MOUSE_DEBOUNCE) debounce = false; report_mouse_t mouse = pointing_device_get_report(); - // trackball_check_click(state[4] & (1 << 7), &mouse); + +#ifdef PIMORONI_TRACKBALL_CLICK + trackball_check_click(state[4] & (1 << 7), &mouse); +#endif #ifndef PIMORONI_TRACKBALL_ROTATE update_member(&mouse.x, &x_offset); diff --git a/users/drashna/pimoroni_trackball.h b/drivers/sensors/pimoroni_trackball.h similarity index 100% rename from users/drashna/pimoroni_trackball.h rename to drivers/sensors/pimoroni_trackball.h diff --git a/keyboards/draculad/keymaps/pimoroni/keymap.c b/keyboards/draculad/keymaps/pimoroni/keymap.c index 87cbe3cd3a58..762ae26c70e6 100644 --- a/keyboards/draculad/keymaps/pimoroni/keymap.c +++ b/keyboards/draculad/keymaps/pimoroni/keymap.c @@ -16,7 +16,7 @@ along with this program. If not, see . */ #include QMK_KEYBOARD_H -#include "pimoroni_trackball.h" +#include "drivers/sensors/pimoroni_trackball.h" #include "pointing_device.h" diff --git a/keyboards/draculad/keymaps/pimoroni/pimoroni_trackball.h b/keyboards/draculad/keymaps/pimoroni/pimoroni_trackball.h deleted file mode 100644 index cfcd5a47a1b1..000000000000 --- a/keyboards/draculad/keymaps/pimoroni/pimoroni_trackball.h +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) - * - * 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 . - */ - -#pragma once - -#include "quantum.h" -#include "pointing_device.h" - -#ifndef TRACKBALL_ADDRESS -# define TRACKBALL_ADDRESS 0x0A -#endif -#define TRACKBALL_WRITE ((TRACKBALL_ADDRESS << 1) | I2C_WRITE) -#define TRACKBALL_READ ((TRACKBALL_ADDRESS << 1) | I2C_READ) - -void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white); -void trackball_check_click(bool pressed, report_mouse_t *mouse); -void trackball_register_button(bool pressed, enum mouse_buttons button); - -float trackball_get_precision(void); -void trackball_set_precision(float precision); -bool trackball_is_scrolling(void); -void trackball_set_scrolling(bool scroll); \ No newline at end of file diff --git a/keyboards/draculad/keymaps/pimoroni/rules.mk b/keyboards/draculad/keymaps/pimoroni/rules.mk index 547a02f26f7e..d8dc92fbfc0d 100644 --- a/keyboards/draculad/keymaps/pimoroni/rules.mk +++ b/keyboards/draculad/keymaps/pimoroni/rules.mk @@ -1,6 +1,6 @@ # only uncomment on the side you have your trackball on POINTING_DEVICE_ENABLE = yes -SRC += pimoroni_trackball.c +SRC += drivers/sensors/pimoroni_trackball.c QUANTUM_LIB_SRC += i2c_master.c OLED_DRIVER_ENABLE = yes MOUSEKEY_ENABLE = no diff --git a/layouts/community/ergodox/drashna/config.h b/layouts/community/ergodox/drashna/config.h index 4ccba8f04b9a..f9daf277fbf6 100644 --- a/layouts/community/ergodox/drashna/config.h +++ b/layouts/community/ergodox/drashna/config.h @@ -47,3 +47,4 @@ #define PIMORONI_TRACKBALL_INVERT_X #define PIMORONI_TRACKBALL_INVERT_Y +#define PIMORONI_TRACKBALL_CLICK diff --git a/users/drashna/drashna.h b/users/drashna/drashna.h index e66f10657450..e37c73bb282b 100644 --- a/users/drashna/drashna.h +++ b/users/drashna/drashna.h @@ -33,7 +33,7 @@ # include "oled_stuff.h" #endif #if defined(PIMORONI_TRACKBALL_ENABLE) -# include "pimoroni_trackball.h" +# include "drivers/sensors/pimoroni_trackball.h" #endif /* Define layer names */ diff --git a/users/drashna/pimoroni_trackball.c b/users/drashna/pimoroni_trackball.c deleted file mode 100644 index a6ca6c9966df..000000000000 --- a/users/drashna/pimoroni_trackball.c +++ /dev/null @@ -1,151 +0,0 @@ -/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) - * - * 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 . - */ - -#include "pimoroni_trackball.h" -#include "i2c_master.h" - -static uint8_t scrolling = 0; -static int16_t x_offset = 0; -static int16_t y_offset = 0; -static int16_t h_offset = 0; -static int16_t v_offset = 0; -static float precisionSpeed = 1; - -#ifndef I2C_TIMEOUT -# define I2C_TIMEOUT 100 -#endif -#ifndef MOUSE_DEBOUNCE -# define MOUSE_DEBOUNCE 5 -#endif - -void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) { - uint8_t data[] = {0x00, red, green, blue, white}; - i2c_transmit(TRACKBALL_ADDRESS << 1, data, sizeof(data), I2C_TIMEOUT); -} - -int16_t mouse_offset(uint8_t positive, uint8_t negative, int16_t scale) { - int16_t offset = (int16_t)positive - (int16_t)negative; - int16_t magnitude = (int16_t)(scale * offset * offset * precisionSpeed); - return offset < 0 ? -magnitude : magnitude; -} - -void update_member(int8_t* member, int16_t* offset) { - if (*offset > 127) { - *member = 127; - *offset -= 127; - } else if (*offset < -127) { - *member = -127; - *offset += 127; - } else { - *member = *offset; - *offset = 0; - } -} - -__attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* mouse) { - if (pressed) { - mouse->buttons |= MOUSE_BTN1; - } else { - mouse->buttons &= ~MOUSE_BTN1; - } -} - -void trackball_register_button(bool pressed, enum mouse_buttons button) { - report_mouse_t currentReport = pointing_device_get_report(); - if (pressed) { - currentReport.buttons |= button; - } else { - currentReport.buttons &= ~button; - } - pointing_device_set_report(currentReport); -} - -float trackball_get_precision(void) { return precisionSpeed; } -void trackball_set_precision(float precision) { precisionSpeed = precision; } -bool trackball_is_scrolling(void) { return scrolling; } -void trackball_set_scrolling(bool scroll) { scrolling = scroll; } - -bool has_report_changed (report_mouse_t first, report_mouse_t second) { - return !( - (!first.buttons && first.buttons == second.buttons) && - (!first.x && first.x == second.x) && - (!first.y && first.y == second.y) && - (!first.h && first.h == second.h) && - (!first.v && first.v == second.v) ); -} - - -__attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x00, 0x00, 0x00, 0x4F); } - -void pointing_device_task(void) { - static bool debounce; - static uint16_t debounce_timer; - uint8_t state[5] = {}; - if (i2c_readReg(TRACKBALL_ADDRESS << 1, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) { - if (!state[4] && !debounce) { - if (scrolling) { -#ifdef PIMORONI_TRACKBALL_INVERT_X - h_offset += mouse_offset(state[2], state[3], 1); -#else - h_offset -= mouse_offset(state[2], state[3], 1); -#endif -#ifdef PIMORONI_TRACKBALL_INVERT_Y - v_offset += mouse_offset(state[1], state[0], 1); -#else - v_offset -= mouse_offset(state[1], state[0], 1); -#endif - } else { -#ifdef PIMORONI_TRACKBALL_INVERT_X - x_offset -= mouse_offset(state[2], state[3], 5); -#else - x_offset += mouse_offset(state[2], state[3], 5); -#endif -#ifdef PIMORONI_TRACKBALL_INVERT_Y - y_offset -= mouse_offset(state[1], state[0], 5); -#else - y_offset += mouse_offset(state[1], state[0], 5); -#endif - } - } else { - if (state[4]) { - debounce = true; - debounce_timer = timer_read(); - } - } - } - - if (timer_elapsed(debounce_timer) > MOUSE_DEBOUNCE) debounce = false; - - report_mouse_t mouse = pointing_device_get_report(); - - trackball_check_click(state[4] & (1 << 7), &mouse); - -#ifndef PIMORONI_TRACKBALL_ROTATE - update_member(&mouse.x, &x_offset); - update_member(&mouse.y, &y_offset); - update_member(&mouse.h, &h_offset); - update_member(&mouse.v, &v_offset); -#else - update_member(&mouse.x, &y_offset); - update_member(&mouse.y, &x_offset); - update_member(&mouse.h, &v_offset); - update_member(&mouse.v, &h_offset); -#endif - pointing_device_set_report(mouse); - if (has_report_changed(mouse, pointing_device_get_report())) { - pointing_device_send(); - } -} diff --git a/users/drashna/rules.mk b/users/drashna/rules.mk index b79051508b44..fa4fb2420dce 100644 --- a/users/drashna/rules.mk +++ b/users/drashna/rules.mk @@ -74,7 +74,7 @@ endif ifeq ($(strip $(PIMORONI_TRACKBALL_ENABLE)), yes) POINTING_DEVICE_ENABLE := yes OPT_DEFS += -DPIMORONI_TRACKBALL_ENABLE - SRC += pimoroni_trackball.c + SRC += drivers/sensors/pimoroni_trackball.c QUANTUM_LIB_SRC += i2c_master.c endif