From 636c5989de2bfd428b723d611f1382eff00dece1 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 2 Oct 2018 23:16:03 -0400 Subject: [PATCH] actually qwiic framework with hooks --- common_features.mk | 2 + drivers/qwiic/joystiic.c | 99 +++++++--------------------------- drivers/qwiic/joystiic.h | 10 +++- drivers/qwiic/qwiic.c | 28 ++++++++++ drivers/qwiic/qwiic.h | 25 +++++++++ drivers/qwiic/qwiic.mk | 13 +++++ keyboards/planck/rev6/rev6.c | 14 +++-- keyboards/planck/rev6/rules.mk | 3 +- tmk_core/common/keyboard.c | 10 ++++ 9 files changed, 120 insertions(+), 84 deletions(-) create mode 100644 drivers/qwiic/qwiic.c create mode 100644 drivers/qwiic/qwiic.h create mode 100644 drivers/qwiic/qwiic.mk diff --git a/common_features.mk b/common_features.mk index 65ff6b5b38..cb36301690 100644 --- a/common_features.mk +++ b/common_features.mk @@ -234,6 +234,8 @@ ifeq ($(strip $(LEADER_ENABLE)), yes) OPT_DEFS += -DLEADER_ENABLE endif +include $(DRIVER_PATH)/qwiic/qwiic.mk + QUANTUM_SRC:= \ $(QUANTUM_DIR)/quantum.c \ $(QUANTUM_DIR)/keymap_common.c \ diff --git a/drivers/qwiic/joystiic.c b/drivers/qwiic/joystiic.c index ac97a22cd7..7f4d550573 100644 --- a/drivers/qwiic/joystiic.c +++ b/drivers/qwiic/joystiic.c @@ -34,14 +34,6 @@ uint8_t joystiic_rx_horizontal[2]; uint8_t joystiic_rx_vertical[2]; uint8_t joystiic_rx_button[1]; -enum { - JOYSTIIC_LEFT, - JOYSTIIC_RIGHT, - JOYSTIIC_UP, - JOYSTIIC_DOWN, - JOYSTIIC_PRESS -}; - bool joystiic_triggered[5] = {0}; void joystiic_init(void) { @@ -54,29 +46,30 @@ void joystiic_update(uint16_t horizontal, uint16_t vertical, bool button) { joystiic_update_user(horizontal, vertical, button); } -void joystiic_update_kb(uint16_t horizontal, uint16_t vertical, bool button) { +__attribute__ ((weak)) +void joystiic_update_kb(uint16_t horizontal, uint16_t vertical, bool button) { } -} - -void joystiic_update_user(uint16_t horizontal, uint16_t vertical, bool button) { - -} +__attribute__ ((weak)) +void joystiic_update_user(uint16_t horizontal, uint16_t vertical, bool button) { } void joystiic_trigger(uint8_t trigger, bool active) { joystiic_trigger_kb(trigger, active); joystiic_trigger_user(trigger, active); } -void joystiic_trigger_kb(uint8_t trigger, bool active) { - switch (trigger) { - case JOYSTIIC_LEFT: active ? register_code(KC_L) : unregister_code(KC_L); break; +__attribute__ ((weak)) +void joystiic_trigger_kb(uint8_t trigger, bool active) { } + +__attribute__ ((weak)) +void joystiic_trigger_user(uint8_t trigger, bool active) { } + +void joystiic_trigger_if_not(uint8_t trigger, bool active) { + if (joystiic_triggered[trigger] != active) { + joystiic_triggered[trigger] = active; + joystiic_trigger(trigger, active); } } -void joystiic_trigger_user(uint8_t trigger, bool active) { - -} - void joystiic_task(void) { // get horizontal axis joystiic_tx[0] = JOYSTIIC_COMMAND_HORIZONTAL; @@ -90,29 +83,8 @@ void joystiic_task(void) { joystiic_horizontal = ((uint16_t)joystiic_rx_horizontal[0] << 8) | joystiic_rx_horizontal[1]; - if (joystiic_horizontal > (JOYSTIIC_CENTER + JOYSTIIC_DEADZONE)) { - if (!joystiic_triggered[JOYSTIIC_LEFT]) { - joystiic_triggered[JOYSTIIC_LEFT] = true; - joystiic_trigger(JOYSTIIC_LEFT, true); - } - } else { - if (joystiic_triggered[JOYSTIIC_LEFT]) { - joystiic_triggered[JOYSTIIC_LEFT] = false; - joystiic_trigger(JOYSTIIC_LEFT, false); - } - } - - if (joystiic_horizontal < (JOYSTIIC_CENTER - JOYSTIIC_DEADZONE)) { - if (!joystiic_triggered[JOYSTIIC_RIGHT]) { - joystiic_triggered[JOYSTIIC_RIGHT] = true; - joystiic_trigger(JOYSTIIC_RIGHT, true); - } - } else { - if (joystiic_triggered[JOYSTIIC_RIGHT]) { - joystiic_triggered[JOYSTIIC_RIGHT] = false; - joystiic_trigger(JOYSTIIC_RIGHT, false); - } - } + joystiic_trigger_if_not(JOYSTIIC_LEFT, joystiic_horizontal > (JOYSTIIC_CENTER + JOYSTIIC_DEADZONE)); + joystiic_trigger_if_not(JOYSTIIC_RIGHT, joystiic_horizontal < (JOYSTIIC_CENTER - JOYSTIIC_DEADZONE)); // get vertical axis joystiic_tx[0] = JOYSTIIC_COMMAND_VERTICAL; @@ -125,29 +97,8 @@ void joystiic_task(void) { joystiic_vertical = ((uint16_t)joystiic_rx_vertical[0] << 8) | joystiic_rx_vertical[1]; - if (joystiic_vertical > (JOYSTIIC_CENTER + JOYSTIIC_DEADZONE)) { - if (!joystiic_triggered[JOYSTIIC_UP]) { - joystiic_triggered[JOYSTIIC_UP] = true; - joystiic_trigger(JOYSTIIC_UP, true); - } - } else { - if (joystiic_triggered[JOYSTIIC_UP]) { - joystiic_triggered[JOYSTIIC_UP] = false; - joystiic_trigger(JOYSTIIC_UP, false); - } - } - - if (joystiic_vertical < (JOYSTIIC_CENTER - JOYSTIIC_DEADZONE)) { - if (!joystiic_triggered[JOYSTIIC_DOWN]) { - joystiic_triggered[JOYSTIIC_DOWN] = true; - joystiic_trigger(JOYSTIIC_DOWN, true); - } - } else { - if (joystiic_triggered[JOYSTIIC_DOWN]) { - joystiic_triggered[JOYSTIIC_DOWN] = false; - joystiic_trigger(JOYSTIIC_DOWN, false); - } - } + joystiic_trigger_if_not(JOYSTIIC_UP, joystiic_vertical > (JOYSTIIC_CENTER + JOYSTIIC_DEADZONE)); + joystiic_trigger_if_not(JOYSTIIC_DOWN, joystiic_vertical < (JOYSTIIC_CENTER - JOYSTIIC_DEADZONE)); // get button press joystiic_tx[0] = JOYSTIIC_COMMAND_BUTTON; @@ -158,19 +109,9 @@ void joystiic_task(void) { printf("error vert\n"); } - joystiic_button = joystiic_rx_button[0]; + joystiic_button = !joystiic_rx_button[0]; - if (joystiic_button) { - if (!joystiic_triggered[JOYSTIIC_PRESS]) { - joystiic_triggered[JOYSTIIC_PRESS] = true; - joystiic_trigger(JOYSTIIC_PRESS, true); - } - } else { - if (joystiic_triggered[JOYSTIIC_PRESS]) { - joystiic_triggered[JOYSTIIC_PRESS] = false; - joystiic_trigger(JOYSTIIC_PRESS, false); - } - } + joystiic_trigger_if_not(JOYSTIIC_PRESS, joystiic_button); joystiic_update(joystiic_horizontal, joystiic_vertical, joystiic_button); diff --git a/drivers/qwiic/joystiic.h b/drivers/qwiic/joystiic.h index 808f1a626e..bb470864c6 100644 --- a/drivers/qwiic/joystiic.h +++ b/drivers/qwiic/joystiic.h @@ -15,7 +15,15 @@ */ #pragma once -#include "i2c_master.h" +#include "qwiic.h" + +enum { + JOYSTIIC_LEFT, + JOYSTIIC_RIGHT, + JOYSTIIC_UP, + JOYSTIIC_DOWN, + JOYSTIIC_PRESS +}; void joystiic_update_kb(uint16_t horizontal, uint16_t vertical, bool button); void joystiic_update_user(uint16_t horizontal, uint16_t vertical, bool button); diff --git a/drivers/qwiic/qwiic.c b/drivers/qwiic/qwiic.c new file mode 100644 index 0000000000..d69f834c66 --- /dev/null +++ b/drivers/qwiic/qwiic.c @@ -0,0 +1,28 @@ +/* Copyright 2018 Jack Humbert + * + * 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 "qwiic.h" + +void qwiic_init(void) { + #ifdef QWIIC_JOYSTIIC_ENABLE + joystiic_init(); + #endif +} + +void qwiic_task(void) { + #ifdef QWIIC_JOYSTIIC_ENABLE + joystiic_task(); + #endif +} diff --git a/drivers/qwiic/qwiic.h b/drivers/qwiic/qwiic.h new file mode 100644 index 0000000000..1a931d3f07 --- /dev/null +++ b/drivers/qwiic/qwiic.h @@ -0,0 +1,25 @@ +/* Copyright 2018 Jack Humbert + * + * 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 "i2c_master.h" + +#ifdef QWIIC_JOYSTIIC_ENABLE + #include "joystiic.h" +#endif + +void qwiic_init(void); +void qwiic_task(void); diff --git a/drivers/qwiic/qwiic.mk b/drivers/qwiic/qwiic.mk new file mode 100644 index 0000000000..4145540dc7 --- /dev/null +++ b/drivers/qwiic/qwiic.mk @@ -0,0 +1,13 @@ +ifneq ($(strip $(QWIIC_ENABLE)),) + COMMON_VPATH += $(DRIVER_PATH)/qwiic + OPT_DEFS += -DQWIIC_ENABLE + SRC += qwiic.c + ifeq ($(filter "i2c_master.c", $(SRC)),) + SRC += i2c_master.c + endif +endif + +ifneq ($(filter JOYSTIIC, $(QWIIC_ENABLE)),) + OPT_DEFS += -DQWIIC_JOYSTIIC_ENABLE + SRC += joystiic.c +endif diff --git a/keyboards/planck/rev6/rev6.c b/keyboards/planck/rev6/rev6.c index e4b5365f99..41cc06a436 100644 --- a/keyboards/planck/rev6/rev6.c +++ b/keyboards/planck/rev6/rev6.c @@ -14,14 +14,22 @@ * along with this program. If not, see . */ #include "rev6.h" -#include "qwiic/joystiic.h" +#include "qwiic.h" void matrix_init_kb(void) { matrix_init_user(); - joystiic_init(); } void matrix_scan_kb(void) { matrix_scan_user(); - joystiic_task(); +} + +void joystiic_trigger_kb(uint8_t trigger, bool active) { + switch (trigger) { + case JOYSTIIC_LEFT: active ? register_code(KC_L) : unregister_code(KC_L); break; + case JOYSTIIC_RIGHT: active ? register_code(KC_R) : unregister_code(KC_R); break; + case JOYSTIIC_UP: active ? register_code(KC_U) : unregister_code(KC_U); break; + case JOYSTIIC_DOWN: active ? register_code(KC_D) : unregister_code(KC_D); break; + case JOYSTIIC_PRESS: active ? register_code(KC_P) : unregister_code(KC_P); break; + } } diff --git a/keyboards/planck/rev6/rules.mk b/keyboards/planck/rev6/rules.mk index c3e9f23afb..0ee32f2ce9 100644 --- a/keyboards/planck/rev6/rules.mk +++ b/keyboards/planck/rev6/rules.mk @@ -1,5 +1,5 @@ # project specific files -SRC = matrix.c qwiic/joystiic.c i2c_master.c +SRC = matrix.c LAYOUTS += ortho_4x12 ## chip/board settings @@ -53,4 +53,5 @@ NKRO_ENABLE = yes # USB Nkey Rollover CUSTOM_MATRIX = yes # Custom matrix file AUDIO_ENABLE = yes RGBLIGHT_ENABLE = no +QWIIC_ENABLE += JOYSTIIC # SERIAL_LINK_ENABLE = yes diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c index 13b3cb4c0c..9b8689694d 100644 --- a/tmk_core/common/keyboard.c +++ b/tmk_core/common/keyboard.c @@ -72,6 +72,9 @@ along with this program. If not, see . #ifdef HD44780_ENABLE # include "hd44780.h" #endif +#ifdef QWIIC_ENABLE +# include "qwiic.h" +#endif #ifdef MATRIX_HAS_GHOST extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; @@ -157,6 +160,9 @@ void keyboard_init(void) { MCUCR |= _BV(JTD); #endif matrix_init(); +#ifdef QWIIC_ENABLE + qwiic_init(); +#endif #ifdef PS2_MOUSE_ENABLE ps2_mouse_init(); #endif @@ -266,6 +272,10 @@ void keyboard_task(void) MATRIX_LOOP_END: +#ifdef QWIIC_ENABLE + qwiic_task(); +#endif + #ifdef MOUSEKEY_ENABLE // mousekey repeat & acceleration mousekey_task();