diff options
| author | Jack Humbert <jack.humb@gmail.com> | 2017-02-07 13:12:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-02-07 13:12:29 -0500 |
| commit | 0c2b6951a6ad80649798c0eca36a9999ebae0b13 (patch) | |
| tree | fb8c24479e7978848a1c2a7ff39f117cb3383464 | |
| parent | de659486f52db6492be6ca03ce5450690f5d891d (diff) | |
| parent | e7c4f621f14b60bde68c01ae076cac49cac9927e (diff) | |
| download | qmk_firmware-0c2b6951a6ad80649798c0eca36a9999ebae0b13.tar.gz qmk_firmware-0c2b6951a6ad80649798c0eca36a9999ebae0b13.zip | |
Merge pull request #1057 from priyadi/selectable_output
Implement runtime selectable output (USB or BT)
| -rw-r--r-- | quantum/quantum.c | 33 | ||||
| -rw-r--r-- | quantum/quantum.h | 1 | ||||
| -rw-r--r-- | quantum/quantum_keycodes.h | 10 | ||||
| -rw-r--r-- | tmk_core/protocol/lufa.mk | 3 | ||||
| -rw-r--r-- | tmk_core/protocol/lufa/lufa.c | 116 | ||||
| -rw-r--r-- | tmk_core/protocol/lufa/outputselect.c | 56 | ||||
| -rw-r--r-- | tmk_core/protocol/lufa/outputselect.h | 41 |
7 files changed, 189 insertions, 71 deletions
diff --git a/quantum/quantum.c b/quantum/quantum.c index b83ae433e..d3905decf 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c | |||
| @@ -1,4 +1,7 @@ | |||
| 1 | #include "quantum.h" | 1 | #include "quantum.h" |
| 2 | #ifdef PROTOCOL_LUFA | ||
| 3 | #include "outputselect.h" | ||
| 4 | #endif | ||
| 2 | 5 | ||
| 3 | #ifndef TAPPING_TERM | 6 | #ifndef TAPPING_TERM |
| 4 | #define TAPPING_TERM 200 | 7 | #define TAPPING_TERM 200 |
| @@ -243,6 +246,36 @@ bool process_record_quantum(keyrecord_t *record) { | |||
| 243 | return false; | 246 | return false; |
| 244 | break; | 247 | break; |
| 245 | #endif | 248 | #endif |
| 249 | #ifdef PROTOCOL_LUFA | ||
| 250 | case OUT_AUTO: | ||
| 251 | if (record->event.pressed) { | ||
| 252 | set_output(OUTPUT_AUTO); | ||
| 253 | } | ||
| 254 | return false; | ||
| 255 | break; | ||
| 256 | case OUT_USB: | ||
| 257 | if (record->event.pressed) { | ||
| 258 | set_output(OUTPUT_USB); | ||
| 259 | } | ||
| 260 | return false; | ||
| 261 | break; | ||
| 262 | #ifdef BLUETOOTH_ENABLE | ||
| 263 | case OUT_BT: | ||
| 264 | if (record->event.pressed) { | ||
| 265 | set_output(OUTPUT_BLUETOOTH); | ||
| 266 | } | ||
| 267 | return false; | ||
| 268 | break; | ||
| 269 | #endif | ||
| 270 | #ifdef ADAFRUIT_BLE_ENABLE | ||
| 271 | case OUT_BLE: | ||
| 272 | if (record->event.pressed) { | ||
| 273 | set_output(OUTPUT_ADAFRUIT_BLE); | ||
| 274 | } | ||
| 275 | return false; | ||
| 276 | break; | ||
| 277 | #endif | ||
| 278 | #endif | ||
| 246 | case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO: | 279 | case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO: |
| 247 | if (record->event.pressed) { | 280 | if (record->event.pressed) { |
| 248 | // MAGIC actions (BOOTMAGIC without the boot) | 281 | // MAGIC actions (BOOTMAGIC without the boot) |
diff --git a/quantum/quantum.h b/quantum/quantum.h index 8614c053a..18f072189 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h | |||
| @@ -15,7 +15,6 @@ | |||
| 15 | #ifdef RGBLIGHT_ENABLE | 15 | #ifdef RGBLIGHT_ENABLE |
| 16 | #include "rgblight.h" | 16 | #include "rgblight.h" |
| 17 | #endif | 17 | #endif |
| 18 | |||
| 19 | #include "action_layer.h" | 18 | #include "action_layer.h" |
| 20 | #include "eeconfig.h" | 19 | #include "eeconfig.h" |
| 21 | #include <stddef.h> | 20 | #include <stddef.h> |
diff --git a/quantum/quantum_keycodes.h b/quantum/quantum_keycodes.h index 91324be35..a786bd322 100644 --- a/quantum/quantum_keycodes.h +++ b/quantum/quantum_keycodes.h | |||
| @@ -141,6 +141,16 @@ enum quantum_keycodes { | |||
| 141 | PRINT_ON, | 141 | PRINT_ON, |
| 142 | PRINT_OFF, | 142 | PRINT_OFF, |
| 143 | 143 | ||
| 144 | // output selection | ||
| 145 | OUT_AUTO, | ||
| 146 | OUT_USB, | ||
| 147 | #ifdef BLUETOOTH_ENABLE | ||
| 148 | OUT_BT, | ||
| 149 | #endif | ||
| 150 | #ifdef ADAFRUIT_BLE_ENABLE | ||
| 151 | OUT_BLE, | ||
| 152 | #endif | ||
| 153 | |||
| 144 | // always leave at the end | 154 | // always leave at the end |
| 145 | SAFE_RANGE | 155 | SAFE_RANGE |
| 146 | }; | 156 | }; |
diff --git a/tmk_core/protocol/lufa.mk b/tmk_core/protocol/lufa.mk index 151d26cbc..de0cc795f 100644 --- a/tmk_core/protocol/lufa.mk +++ b/tmk_core/protocol/lufa.mk | |||
| @@ -8,13 +8,14 @@ LUFA_PATH ?= $(LUFA_DIR)/LUFA-git | |||
| 8 | ifneq (, $(wildcard $(TMK_PATH)/$(LUFA_PATH)/LUFA/Build/lufa_sources.mk)) | 8 | ifneq (, $(wildcard $(TMK_PATH)/$(LUFA_PATH)/LUFA/Build/lufa_sources.mk)) |
| 9 | # New build system from 20120730 | 9 | # New build system from 20120730 |
| 10 | LUFA_ROOT_PATH = $(LUFA_PATH)/LUFA | 10 | LUFA_ROOT_PATH = $(LUFA_PATH)/LUFA |
| 11 | include $(TMK_PATH)/$(LUFA_PATH)/LUFA/Build/lufa_sources.mk | 11 | include $(TMK_PATH)/$(LUFA_PATH)/LUFA/Build/lufa_sources.mk |
| 12 | else | 12 | else |
| 13 | include $(TMK_PATH)/$(LUFA_PATH)/LUFA/makefile | 13 | include $(TMK_PATH)/$(LUFA_PATH)/LUFA/makefile |
| 14 | endif | 14 | endif |
| 15 | 15 | ||
| 16 | LUFA_SRC = lufa.c \ | 16 | LUFA_SRC = lufa.c \ |
| 17 | descriptor.c \ | 17 | descriptor.c \ |
| 18 | outputselect.c \ | ||
| 18 | $(LUFA_SRC_USB) | 19 | $(LUFA_SRC_USB) |
| 19 | 20 | ||
| 20 | ifeq ($(strip $(MIDI_ENABLE)), yes) | 21 | ifeq ($(strip $(MIDI_ENABLE)), yes) |
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 6dd5959dc..ba49284c9 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c | |||
| @@ -53,6 +53,7 @@ | |||
| 53 | #include "lufa.h" | 53 | #include "lufa.h" |
| 54 | #include "quantum.h" | 54 | #include "quantum.h" |
| 55 | #include <util/atomic.h> | 55 | #include <util/atomic.h> |
| 56 | #include "outputselect.h" | ||
| 56 | 57 | ||
| 57 | #ifdef NKRO_ENABLE | 58 | #ifdef NKRO_ENABLE |
| 58 | #include "keycode_config.h" | 59 | #include "keycode_config.h" |
| @@ -589,59 +590,33 @@ void EVENT_USB_Device_ControlRequest(void) | |||
| 589 | 590 | ||
| 590 | /******************************************************************************* | 591 | /******************************************************************************* |
| 591 | * Host driver | 592 | * Host driver |
| 592 | p | ||
| 593 | ******************************************************************************/ | 593 | ******************************************************************************/ |
| 594 | static uint8_t keyboard_leds(void) | 594 | static uint8_t keyboard_leds(void) |
| 595 | { | 595 | { |
| 596 | return keyboard_led_stats; | 596 | return keyboard_led_stats; |
| 597 | } | 597 | } |
| 598 | 598 | ||
| 599 | #define SendToUSB 1 | ||
| 600 | #define SendToBT 2 | ||
| 601 | #define SendToBLE 4 | ||
| 602 | |||
| 603 | static inline uint8_t where_to_send(void) { | ||
| 604 | #ifdef ADAFRUIT_BLE_ENABLE | ||
| 605 | #if 0 | ||
| 606 | if (adafruit_ble_is_connected()) { | ||
| 607 | // For testing, send to BLE as a priority | ||
| 608 | return SendToBLE; | ||
| 609 | } | ||
| 610 | #endif | ||
| 611 | |||
| 612 | // This is the real policy | ||
| 613 | if (USB_DeviceState != DEVICE_STATE_Configured) { | ||
| 614 | if (adafruit_ble_is_connected()) { | ||
| 615 | return SendToBLE; | ||
| 616 | } | ||
| 617 | } | ||
| 618 | #endif | ||
| 619 | return ((USB_DeviceState == DEVICE_STATE_Configured) ? SendToUSB : 0) | ||
| 620 | #ifdef BLUETOOTH_ENABLE | ||
| 621 | || SendToBT | ||
| 622 | #endif | ||
| 623 | ; | ||
| 624 | } | ||
| 625 | |||
| 626 | static void send_keyboard(report_keyboard_t *report) | 599 | static void send_keyboard(report_keyboard_t *report) |
| 627 | { | 600 | { |
| 601 | uint8_t timeout = 255; | ||
| 602 | uint8_t where = where_to_send(); | ||
| 603 | |||
| 628 | #ifdef BLUETOOTH_ENABLE | 604 | #ifdef BLUETOOTH_ENABLE |
| 629 | bluefruit_serial_send(0xFD); | 605 | if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { |
| 630 | for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) { | 606 | bluefruit_serial_send(0xFD); |
| 631 | bluefruit_serial_send(report->raw[i]); | 607 | for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) { |
| 608 | bluefruit_serial_send(report->raw[i]); | ||
| 609 | } | ||
| 632 | } | 610 | } |
| 633 | #endif | 611 | #endif |
| 634 | 612 | ||
| 635 | uint8_t timeout = 255; | ||
| 636 | uint8_t where = where_to_send(); | ||
| 637 | |||
| 638 | #ifdef ADAFRUIT_BLE_ENABLE | 613 | #ifdef ADAFRUIT_BLE_ENABLE |
| 639 | if (where & SendToBLE) { | 614 | if (where == OUTPUT_ADAFRUIT_BLE) { |
| 640 | adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); | 615 | adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); |
| 641 | } | 616 | } |
| 642 | #endif | 617 | #endif |
| 643 | 618 | ||
| 644 | if (!(where & SendToUSB)) { | 619 | if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { |
| 645 | return; | 620 | return; |
| 646 | } | 621 | } |
| 647 | 622 | ||
| @@ -681,30 +656,31 @@ static void send_keyboard(report_keyboard_t *report) | |||
| 681 | static void send_mouse(report_mouse_t *report) | 656 | static void send_mouse(report_mouse_t *report) |
| 682 | { | 657 | { |
| 683 | #ifdef MOUSE_ENABLE | 658 | #ifdef MOUSE_ENABLE |
| 659 | uint8_t timeout = 255; | ||
| 660 | uint8_t where = where_to_send(); | ||
| 684 | 661 | ||
| 685 | #ifdef BLUETOOTH_ENABLE | 662 | #ifdef BLUETOOTH_ENABLE |
| 686 | bluefruit_serial_send(0xFD); | 663 | if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { |
| 687 | bluefruit_serial_send(0x00); | 664 | bluefruit_serial_send(0xFD); |
| 688 | bluefruit_serial_send(0x03); | 665 | bluefruit_serial_send(0x00); |
| 689 | bluefruit_serial_send(report->buttons); | 666 | bluefruit_serial_send(0x03); |
| 690 | bluefruit_serial_send(report->x); | 667 | bluefruit_serial_send(report->buttons); |
| 691 | bluefruit_serial_send(report->y); | 668 | bluefruit_serial_send(report->x); |
| 692 | bluefruit_serial_send(report->v); // should try sending the wheel v here | 669 | bluefruit_serial_send(report->y); |
| 693 | bluefruit_serial_send(report->h); // should try sending the wheel h here | 670 | bluefruit_serial_send(report->v); // should try sending the wheel v here |
| 694 | bluefruit_serial_send(0x00); | 671 | bluefruit_serial_send(report->h); // should try sending the wheel h here |
| 672 | bluefruit_serial_send(0x00); | ||
| 673 | } | ||
| 695 | #endif | 674 | #endif |
| 696 | 675 | ||
| 697 | uint8_t timeout = 255; | ||
| 698 | |||
| 699 | uint8_t where = where_to_send(); | ||
| 700 | |||
| 701 | #ifdef ADAFRUIT_BLE_ENABLE | 676 | #ifdef ADAFRUIT_BLE_ENABLE |
| 702 | if (where & SendToBLE) { | 677 | if (where == OUTPUT_ADAFRUIT_BLE) { |
| 703 | // FIXME: mouse buttons | 678 | // FIXME: mouse buttons |
| 704 | adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h); | 679 | adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h); |
| 705 | } | 680 | } |
| 706 | #endif | 681 | #endif |
| 707 | if (!(where & SendToUSB)) { | 682 | |
| 683 | if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { | ||
| 708 | return; | 684 | return; |
| 709 | } | 685 | } |
| 710 | 686 | ||
| @@ -746,32 +722,34 @@ static void send_system(uint16_t data) | |||
| 746 | 722 | ||
| 747 | static void send_consumer(uint16_t data) | 723 | static void send_consumer(uint16_t data) |
| 748 | { | 724 | { |
| 725 | uint8_t timeout = 255; | ||
| 726 | uint8_t where = where_to_send(); | ||
| 749 | 727 | ||
| 750 | #ifdef BLUETOOTH_ENABLE | 728 | #ifdef BLUETOOTH_ENABLE |
| 751 | static uint16_t last_data = 0; | 729 | if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { |
| 752 | if (data == last_data) return; | 730 | static uint16_t last_data = 0; |
| 753 | last_data = data; | 731 | if (data == last_data) return; |
| 754 | uint16_t bitmap = CONSUMER2BLUEFRUIT(data); | 732 | last_data = data; |
| 755 | bluefruit_serial_send(0xFD); | 733 | uint16_t bitmap = CONSUMER2BLUEFRUIT(data); |
| 756 | bluefruit_serial_send(0x00); | 734 | bluefruit_serial_send(0xFD); |
| 757 | bluefruit_serial_send(0x02); | 735 | bluefruit_serial_send(0x00); |
| 758 | bluefruit_serial_send((bitmap>>8)&0xFF); | 736 | bluefruit_serial_send(0x02); |
| 759 | bluefruit_serial_send(bitmap&0xFF); | 737 | bluefruit_serial_send((bitmap>>8)&0xFF); |
| 760 | bluefruit_serial_send(0x00); | 738 | bluefruit_serial_send(bitmap&0xFF); |
| 761 | bluefruit_serial_send(0x00); | 739 | bluefruit_serial_send(0x00); |
| 762 | bluefruit_serial_send(0x00); | 740 | bluefruit_serial_send(0x00); |
| 763 | bluefruit_serial_send(0x00); | 741 | bluefruit_serial_send(0x00); |
| 742 | bluefruit_serial_send(0x00); | ||
| 743 | } | ||
| 764 | #endif | 744 | #endif |
| 765 | 745 | ||
| 766 | uint8_t timeout = 255; | ||
| 767 | uint8_t where = where_to_send(); | ||
| 768 | |||
| 769 | #ifdef ADAFRUIT_BLE_ENABLE | 746 | #ifdef ADAFRUIT_BLE_ENABLE |
| 770 | if (where & SendToBLE) { | 747 | if (where == OUTPUT_ADAFRUIT_BLE) { |
| 771 | adafruit_ble_send_consumer_key(data, 0); | 748 | adafruit_ble_send_consumer_key(data, 0); |
| 772 | } | 749 | } |
| 773 | #endif | 750 | #endif |
| 774 | if (!(where & SendToUSB)) { | 751 | |
| 752 | if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { | ||
| 775 | return; | 753 | return; |
| 776 | } | 754 | } |
| 777 | 755 | ||
diff --git a/tmk_core/protocol/lufa/outputselect.c b/tmk_core/protocol/lufa/outputselect.c new file mode 100644 index 000000000..5d2457bff --- /dev/null +++ b/tmk_core/protocol/lufa/outputselect.c | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2017 Priyadi Iman Nurcahyo | ||
| 3 | This program is free software: you can redistribute it and/or modify | ||
| 4 | it under the terms of the GNU General Public License as published by | ||
| 5 | the Free Software Foundation, either version 2 of the License, or | ||
| 6 | (at your option) any later version. | ||
| 7 | This program is distributed in the hope that it will be useful, | ||
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 10 | GNU General Public License for more details. | ||
| 11 | You should have received a copy of the GNU General Public License | ||
| 12 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include "lufa.h" | ||
| 16 | #include "outputselect.h" | ||
| 17 | #ifdef ADAFRUIT_BLE_ENABLE | ||
| 18 | #include "adafruit_ble.h" | ||
| 19 | #endif | ||
| 20 | |||
| 21 | uint8_t desired_output = OUTPUT_DEFAULT; | ||
| 22 | |||
| 23 | void set_output(uint8_t output) { | ||
| 24 | set_output_user(output); | ||
| 25 | desired_output = output; | ||
| 26 | } | ||
| 27 | |||
| 28 | __attribute__((weak)) | ||
| 29 | void set_output_user(uint8_t output) { | ||
| 30 | } | ||
| 31 | |||
| 32 | uint8_t auto_detect_output(void) { | ||
| 33 | if (USB_DeviceState == DEVICE_STATE_Configured) { | ||
| 34 | return OUTPUT_USB; | ||
| 35 | } | ||
| 36 | |||
| 37 | #ifdef ADAFRUIT_BLE_ENABLE | ||
| 38 | if (adafruit_ble_is_connected()) { | ||
| 39 | return OUTPUT_ADAFRUIT_BLE; | ||
| 40 | } | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #ifdef BLUETOOTH_ENABLE | ||
| 44 | return OUTPUT_BLUETOOTH; // should check if BT is connected here | ||
| 45 | #endif | ||
| 46 | |||
| 47 | return OUTPUT_NONE; | ||
| 48 | } | ||
| 49 | |||
| 50 | uint8_t where_to_send(void) { | ||
| 51 | if (desired_output == OUTPUT_AUTO) { | ||
| 52 | return auto_detect_output(); | ||
| 53 | } | ||
| 54 | return desired_output; | ||
| 55 | } | ||
| 56 | |||
diff --git a/tmk_core/protocol/lufa/outputselect.h b/tmk_core/protocol/lufa/outputselect.h new file mode 100644 index 000000000..79b4dd35d --- /dev/null +++ b/tmk_core/protocol/lufa/outputselect.h | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2017 Priyadi Iman Nurcahyo | ||
| 3 | This program is free software: you can redistribute it and/or modify | ||
| 4 | it under the terms of the GNU General Public License as published by | ||
| 5 | the Free Software Foundation, either version 2 of the License, or | ||
| 6 | (at your option) any later version. | ||
| 7 | This program is distributed in the hope that it will be useful, | ||
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 10 | GNU General Public License for more details. | ||
| 11 | You should have received a copy of the GNU General Public License | ||
| 12 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 13 | */ | ||
| 14 | |||
| 15 | enum outputs { | ||
| 16 | OUTPUT_AUTO, | ||
| 17 | |||
| 18 | OUTPUT_NONE, | ||
| 19 | OUTPUT_USB, | ||
| 20 | OUTPUT_BLUETOOTH, | ||
| 21 | OUTPUT_ADAFRUIT_BLE, | ||
| 22 | |||
| 23 | // backward compatibility | ||
| 24 | OUTPUT_USB_AND_BT | ||
| 25 | }; | ||
| 26 | |||
| 27 | /** | ||
| 28 | * backward compatibility for BLUETOOTH_ENABLE, send to BT and USB by default | ||
| 29 | */ | ||
| 30 | #ifndef OUTPUT_DEFAULT | ||
| 31 | #ifdef BLUETOOTH_ENABLE | ||
| 32 | #define OUTPUT_DEFAULT OUTPUT_USB_AND_BT | ||
| 33 | #else | ||
| 34 | #define OUTPUT_DEFAULT OUTPUT_AUTO | ||
| 35 | #endif | ||
| 36 | #endif | ||
| 37 | |||
| 38 | void set_output(uint8_t output); | ||
| 39 | void set_output_user(uint8_t output); | ||
| 40 | uint8_t auto_detect_output(void); | ||
| 41 | uint8_t where_to_send(void); \ No newline at end of file | ||
