diff options
| author | Priyadi Iman Nurcahyo <priyadi@priyadi.net> | 2017-02-01 05:07:05 +0700 |
|---|---|---|
| committer | Priyadi Iman Nurcahyo <priyadi@priyadi.net> | 2017-02-01 05:07:05 +0700 |
| commit | d8a9c63c265869822a77ad5c5cb7c8dfa4ff1f6c (patch) | |
| tree | aef00ec84122c366bfc0e70e7d3190976380685b /tmk_core/protocol | |
| parent | ec05f654210a01ef82db2ab54e6065783536b802 (diff) | |
| download | qmk_firmware-d8a9c63c265869822a77ad5c5cb7c8dfa4ff1f6c.tar.gz qmk_firmware-d8a9c63c265869822a77ad5c5cb7c8dfa4ff1f6c.zip | |
Implement runtime selectable output (USB or BT)
Diffstat (limited to 'tmk_core/protocol')
| -rw-r--r-- | tmk_core/protocol/lufa.mk | 1 | ||||
| -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 |
4 files changed, 145 insertions, 69 deletions
diff --git a/tmk_core/protocol/lufa.mk b/tmk_core/protocol/lufa.mk index 151d26cbc..26337cb94 100644 --- a/tmk_core/protocol/lufa.mk +++ b/tmk_core/protocol/lufa.mk | |||
| @@ -15,6 +15,7 @@ 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 | ||
