ソースを参照

Updated Joystick, examples and added sixaxis code

main
Mike S 5年前
コミット
81ce2d607f
7個のファイルの変更752行の追加359行の削除
  1. +12
    -3
      bluetooth.cpp
  2. +55
    -306
      examples/JoystickBT/JoystickBT.ino
  3. +568
    -0
      examples/KeyboardBT/KeyboardBT.ino
  4. +1
    -0
      examples/KeyboardBT/defs.h
  5. +43
    -26
      examples/MouseBT/MouseBT.ino
  6. +72
    -23
      joystick.cpp
  7. +1
    -1
      mouse.cpp

+ 12
- 3
bluetooth.cpp ファイルの表示

@@ -30,8 +30,8 @@
#define print USBHost::print_
#define println USBHost::println_//#define DEBUG_BT

//#define DEBUG_BT
//#define DEBUG_BT_VERBOSE
#define DEBUG_BT
#define DEBUG_BT_VERBOSE

#ifndef DEBUG_BT
#undef DEBUG_BT_VERBOSE
@@ -707,6 +707,15 @@ void BluetoothController::handle_hci_connection_complete() {
// The PS4 requires a connection request to it.
delay(1);
sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, control_dcid_, HID_CTRL_PSM);
delay(1);
uint8_t packet[2];
memset(packet, 0, sizeof(packet));
packet[0] = 0x43;
packet[1] = 0x02; // Report ID
USBHDBGSerial.printf("SixAxis Command Issued!\r\n");
sendL2CapCommand(packet, sizeof(packet), 0x40, 0x00);
}
}

@@ -1076,7 +1085,7 @@ void BluetoothController::sendl2cap_ConnectionRequest(uint16_t handle, uint8_t r
l2capbuf[6] = scid & 0xff; // Source CID
l2capbuf[7] = (scid >> 8) & 0xff;

DBGPrintf("`ConnectionRequest called(");
DBGPrintf("ConnectionRequest called(");
sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
}


+ 55
- 306
examples/JoystickBT/JoystickBT.ino ファイルの表示

@@ -1,8 +1,10 @@

// Simple test of USB Host Mouse/Keyboard
//
// This example is in the public domain

#include "USBHost_t36.h"
//#include "debug_tt.h"

USBHost myusb;
USBHub hub1(myusb);
@@ -16,7 +18,8 @@ JoystickController joystick1(myusb);
//BluetoothController bluet(myusb, true, "0000"); // Version does pairing to device
BluetoothController bluet(myusb); // version assumes it already was paired
int user_axis[64];
uint32_t buttons_prev = 0;
uint8_t buttons_prev = 0;
uint8_t button;
RawHIDController rawhid1(myusb);
RawHIDController rawhid2(myusb, 0xffc90004);

@@ -32,6 +35,7 @@ USBHIDInput *hiddrivers[] = {&joystick1, &rawhid1, &rawhid2};

#define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
const char * hid_driver_names[CNT_DEVICES] = {"Joystick1", "RawHid1", "RawHid2"};

bool hid_driver_active[CNT_DEVICES] = {false, false, false};
bool show_changed_only = false;

@@ -39,17 +43,30 @@ uint8_t joystick_left_trigger_value = 0;
uint8_t joystick_right_trigger_value = 0;
uint64_t joystick_full_notify_mask = (uint64_t)-1;

int psAxis[10];
int psAxis[64];
bool first_joystick_message = true;

void setup()
{
Serial1.begin(115200);
/* Serial4.begin( 1843200 );
debBegin_tt( &Serial4, LED_BUILTIN, 12);
debTraceShow_tt( -1, "", "", "" );
Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
Serial4.println("\n********\n T4 connected Serial1 *******\n");
Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
Serial4.println("\n********\n T4 connected Serial4 *******\n");
*/
Serial1.begin(1843200);
while (!Serial) ; // wait for Arduino Serial Monitor
//debTraceShow_tt( -2, "", "", "" );
//Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
//Serial1.begin( 1843200 );
Serial.println("\n\nUSB Host Testing");
Serial.println(sizeof(USBHub), DEC);
myusb.begin();

delay(2000);

rawhid1.attachReceive(OnReceiveHidData);
rawhid2.attachReceive(OnReceiveHidData);
}
@@ -58,7 +75,7 @@ void setup()
void loop()
{
myusb.Task();
if (Serial.available()) {
int ch = Serial.read(); // get the first char.
while (Serial.read() != -1) ;
@@ -132,27 +149,56 @@ void loop()
if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
}
for (uint8_t i = 0; i<10; i++) {
for (uint8_t i = 0; i<64; i++) {
psAxis[i] = joystick1.getAxis(i);
}

//for (uint8_t i = 0; i < 24; i++) {
// Serial.printf(" %d:%d", i, psAxis[i]);
//}
//Serial.println();
Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[1], psAxis[2], psAxis[3], psAxis[4]);
Serial.printf("L-Trig: %d, R-Trig: %d, Trig-Button: %d \r\n", psAxis[8], psAxis[9], psAxis[6]);
Serial.printf("Buttons: %d, PS: %d\r\n", psAxis[5], psAxis[7]);
Serial.printf("Arrows: %d\r\n", psAxis[0]);
Serial.printf("Battery level percentage: %2f.0 \r\n", (((float) psAxis[12])/255.0f)*100.0f);
Serial.println();
uint8_t ltv;
uint8_t rtv;

ltv = joystick1.getAxis(8);
rtv = joystick1.getAxis(9);
ltv = psAxis[8];
rtv = psAxis[9];

if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
joystick_left_trigger_value = ltv;
joystick_right_trigger_value = rtv;
Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
joystick1.setRumble(ltv, rtv);
}
/* Arrow Buttons (psAxis[0]):
* 0x08 is released,
* 0=N, 1=NE, 2=E, 3=SE, 4=S,
* 5=SW, 6=W, 7=NW)
*/

if (psAxis[5] != buttons_prev) {
uint8_t lr = (psAxis[5] & 1) ? 0xff : 0; //Srq
uint8_t lg = (psAxis[5] & 4) ? 0xff : 0; //Cir
uint8_t lb = (psAxis[5] & 8) ? 0xff : 0; //Tri
//Cross = 2
Serial.print(psAxis[5]); Serial.print(", ");
Serial.print(lr); Serial.print(", ");
Serial.print(lg); Serial.print(", ");
Serial.println(lb);

joystick1.setLEDs(lr, lg, lb);
}
buttons_prev =psAxis[5];

joystick1.joystickDataClear();
}

@@ -171,303 +217,6 @@ void loop()
}
}


/*
void OnPress(int key)
{
Serial.print("key '");
switch (key) {
case KEYD_UP : Serial.print("UP"); break;
case KEYD_DOWN : Serial.print("DN"); break;
case KEYD_LEFT : Serial.print("LEFT"); break;
case KEYD_RIGHT : Serial.print("RIGHT"); break;
case KEYD_INSERT : Serial.print("Ins"); break;
case KEYD_DELETE : Serial.print("Del"); break;
case KEYD_PAGE_UP : Serial.print("PUP"); break;
case KEYD_PAGE_DOWN: Serial.print("PDN"); break;
case KEYD_HOME : Serial.print("HOME"); break;
case KEYD_END : Serial.print("END"); break;
case KEYD_F1 : Serial.print("F1"); break;
case KEYD_F2 : Serial.print("F2"); break;
case KEYD_F3 : Serial.print("F3"); break;
case KEYD_F4 : Serial.print("F4"); break;
case KEYD_F5 : Serial.print("F5"); break;
case KEYD_F6 : Serial.print("F6"); break;
case KEYD_F7 : Serial.print("F7"); break;
case KEYD_F8 : Serial.print("F8"); break;
case KEYD_F9 : Serial.print("F9"); break;
case KEYD_F10 : Serial.print("F10"); break;
case KEYD_F11 : Serial.print("F11"); break;
case KEYD_F12 : Serial.print("F12"); break;
default: Serial.print((char)key); break;
}
Serial.print("' ");
Serial.print(key);
Serial.print(" MOD: ");
if (keyboard1) {
Serial.print(keyboard1.getModifiers(), HEX);
Serial.print(" OEM: ");
Serial.print(keyboard1.getOemKey(), HEX);
Serial.print(" LEDS: ");
Serial.println(keyboard1.LEDS(), HEX);
} else {
Serial.print(keyboard2.getModifiers(), HEX);
Serial.print(" OEM: ");
Serial.print(keyboard2.getOemKey(), HEX);
Serial.print(" LEDS: ");
Serial.println(keyboard2.LEDS(), HEX);
}

//Serial.print("key ");
//Serial.print((char)keyboard1.getKey());
//Serial.print(" ");
//Serial.print((char)keyboard2.getKey());
//Serial.println();
}
void OnHIDExtrasPress(uint32_t top, uint16_t key)
{
Serial.print("HID (");
Serial.print(top, HEX);
Serial.print(") key press:");
Serial.print(key, HEX);
if (top == 0xc0000) {
switch (key) {
case 0x20 : Serial.print(" - +10"); break;
case 0x21 : Serial.print(" - +100"); break;
case 0x22 : Serial.print(" - AM/PM"); break;
case 0x30 : Serial.print(" - Power"); break;
case 0x31 : Serial.print(" - Reset"); break;
case 0x32 : Serial.print(" - Sleep"); break;
case 0x33 : Serial.print(" - Sleep After"); break;
case 0x34 : Serial.print(" - Sleep Mode"); break;
case 0x35 : Serial.print(" - Illumination"); break;
case 0x36 : Serial.print(" - Function Buttons"); break;
case 0x40 : Serial.print(" - Menu"); break;
case 0x41 : Serial.print(" - Menu Pick"); break;
case 0x42 : Serial.print(" - Menu Up"); break;
case 0x43 : Serial.print(" - Menu Down"); break;
case 0x44 : Serial.print(" - Menu Left"); break;
case 0x45 : Serial.print(" - Menu Right"); break;
case 0x46 : Serial.print(" - Menu Escape"); break;
case 0x47 : Serial.print(" - Menu Value Increase"); break;
case 0x48 : Serial.print(" - Menu Value Decrease"); break;
case 0x60 : Serial.print(" - Data On Screen"); break;
case 0x61 : Serial.print(" - Closed Caption"); break;
case 0x62 : Serial.print(" - Closed Caption Select"); break;
case 0x63 : Serial.print(" - VCR/TV"); break;
case 0x64 : Serial.print(" - Broadcast Mode"); break;
case 0x65 : Serial.print(" - Snapshot"); break;
case 0x66 : Serial.print(" - Still"); break;
case 0x80 : Serial.print(" - Selection"); break;
case 0x81 : Serial.print(" - Assign Selection"); break;
case 0x82 : Serial.print(" - Mode Step"); break;
case 0x83 : Serial.print(" - Recall Last"); break;
case 0x84 : Serial.print(" - Enter Channel"); break;
case 0x85 : Serial.print(" - Order Movie"); break;
case 0x86 : Serial.print(" - Channel"); break;
case 0x87 : Serial.print(" - Media Selection"); break;
case 0x88 : Serial.print(" - Media Select Computer"); break;
case 0x89 : Serial.print(" - Media Select TV"); break;
case 0x8A : Serial.print(" - Media Select WWW"); break;
case 0x8B : Serial.print(" - Media Select DVD"); break;
case 0x8C : Serial.print(" - Media Select Telephone"); break;
case 0x8D : Serial.print(" - Media Select Program Guide"); break;
case 0x8E : Serial.print(" - Media Select Video Phone"); break;
case 0x8F : Serial.print(" - Media Select Games"); break;
case 0x90 : Serial.print(" - Media Select Messages"); break;
case 0x91 : Serial.print(" - Media Select CD"); break;
case 0x92 : Serial.print(" - Media Select VCR"); break;
case 0x93 : Serial.print(" - Media Select Tuner"); break;
case 0x94 : Serial.print(" - Quit"); break;
case 0x95 : Serial.print(" - Help"); break;
case 0x96 : Serial.print(" - Media Select Tape"); break;
case 0x97 : Serial.print(" - Media Select Cable"); break;
case 0x98 : Serial.print(" - Media Select Satellite"); break;
case 0x99 : Serial.print(" - Media Select Security"); break;
case 0x9A : Serial.print(" - Media Select Home"); break;
case 0x9B : Serial.print(" - Media Select Call"); break;
case 0x9C : Serial.print(" - Channel Increment"); break;
case 0x9D : Serial.print(" - Channel Decrement"); break;
case 0x9E : Serial.print(" - Media Select SAP"); break;
case 0xA0 : Serial.print(" - VCR Plus"); break;
case 0xA1 : Serial.print(" - Once"); break;
case 0xA2 : Serial.print(" - Daily"); break;
case 0xA3 : Serial.print(" - Weekly"); break;
case 0xA4 : Serial.print(" - Monthly"); break;
case 0xB0 : Serial.print(" - Play"); break;
case 0xB1 : Serial.print(" - Pause"); break;
case 0xB2 : Serial.print(" - Record"); break;
case 0xB3 : Serial.print(" - Fast Forward"); break;
case 0xB4 : Serial.print(" - Rewind"); break;
case 0xB5 : Serial.print(" - Scan Next Track"); break;
case 0xB6 : Serial.print(" - Scan Previous Track"); break;
case 0xB7 : Serial.print(" - Stop"); break;
case 0xB8 : Serial.print(" - Eject"); break;
case 0xB9 : Serial.print(" - Random Play"); break;
case 0xBA : Serial.print(" - Select DisC"); break;
case 0xBB : Serial.print(" - Enter Disc"); break;
case 0xBC : Serial.print(" - Repeat"); break;
case 0xBD : Serial.print(" - Tracking"); break;
case 0xBE : Serial.print(" - Track Normal"); break;
case 0xBF : Serial.print(" - Slow Tracking"); break;
case 0xC0 : Serial.print(" - Frame Forward"); break;
case 0xC1 : Serial.print(" - Frame Back"); break;
case 0xC2 : Serial.print(" - Mark"); break;
case 0xC3 : Serial.print(" - Clear Mark"); break;
case 0xC4 : Serial.print(" - Repeat From Mark"); break;
case 0xC5 : Serial.print(" - Return To Mark"); break;
case 0xC6 : Serial.print(" - Search Mark Forward"); break;
case 0xC7 : Serial.print(" - Search Mark Backwards"); break;
case 0xC8 : Serial.print(" - Counter Reset"); break;
case 0xC9 : Serial.print(" - Show Counter"); break;
case 0xCA : Serial.print(" - Tracking Increment"); break;
case 0xCB : Serial.print(" - Tracking Decrement"); break;
case 0xCD : Serial.print(" - Pause/Continue"); break;
case 0xE0 : Serial.print(" - Volume"); break;
case 0xE1 : Serial.print(" - Balance"); break;
case 0xE2 : Serial.print(" - Mute"); break;
case 0xE3 : Serial.print(" - Bass"); break;
case 0xE4 : Serial.print(" - Treble"); break;
case 0xE5 : Serial.print(" - Bass Boost"); break;
case 0xE6 : Serial.print(" - Surround Mode"); break;
case 0xE7 : Serial.print(" - Loudness"); break;
case 0xE8 : Serial.print(" - MPX"); break;
case 0xE9 : Serial.print(" - Volume Up"); break;
case 0xEA : Serial.print(" - Volume Down"); break;
case 0xF0 : Serial.print(" - Speed Select"); break;
case 0xF1 : Serial.print(" - Playback Speed"); break;
case 0xF2 : Serial.print(" - Standard Play"); break;
case 0xF3 : Serial.print(" - Long Play"); break;
case 0xF4 : Serial.print(" - Extended Play"); break;
case 0xF5 : Serial.print(" - Slow"); break;
case 0x100: Serial.print(" - Fan Enable"); break;
case 0x101: Serial.print(" - Fan Speed"); break;
case 0x102: Serial.print(" - Light"); break;
case 0x103: Serial.print(" - Light Illumination Level"); break;
case 0x104: Serial.print(" - Climate Control Enable"); break;
case 0x105: Serial.print(" - Room Temperature"); break;
case 0x106: Serial.print(" - Security Enable"); break;
case 0x107: Serial.print(" - Fire Alarm"); break;
case 0x108: Serial.print(" - Police Alarm"); break;
case 0x150: Serial.print(" - Balance Right"); break;
case 0x151: Serial.print(" - Balance Left"); break;
case 0x152: Serial.print(" - Bass Increment"); break;
case 0x153: Serial.print(" - Bass Decrement"); break;
case 0x154: Serial.print(" - Treble Increment"); break;
case 0x155: Serial.print(" - Treble Decrement"); break;
case 0x160: Serial.print(" - Speaker System"); break;
case 0x161: Serial.print(" - Channel Left"); break;
case 0x162: Serial.print(" - Channel Right"); break;
case 0x163: Serial.print(" - Channel Center"); break;
case 0x164: Serial.print(" - Channel Front"); break;
case 0x165: Serial.print(" - Channel Center Front"); break;
case 0x166: Serial.print(" - Channel Side"); break;
case 0x167: Serial.print(" - Channel Surround"); break;
case 0x168: Serial.print(" - Channel Low Frequency Enhancement"); break;
case 0x169: Serial.print(" - Channel Top"); break;
case 0x16A: Serial.print(" - Channel Unknown"); break;
case 0x170: Serial.print(" - Sub-channel"); break;
case 0x171: Serial.print(" - Sub-channel Increment"); break;
case 0x172: Serial.print(" - Sub-channel Decrement"); break;
case 0x173: Serial.print(" - Alternate Audio Increment"); break;
case 0x174: Serial.print(" - Alternate Audio Decrement"); break;
case 0x180: Serial.print(" - Application Launch Buttons"); break;
case 0x181: Serial.print(" - AL Launch Button Configuration Tool"); break;
case 0x182: Serial.print(" - AL Programmable Button Configuration"); break;
case 0x183: Serial.print(" - AL Consumer Control Configuration"); break;
case 0x184: Serial.print(" - AL Word Processor"); break;
case 0x185: Serial.print(" - AL Text Editor"); break;
case 0x186: Serial.print(" - AL Spreadsheet"); break;
case 0x187: Serial.print(" - AL Graphics Editor"); break;
case 0x188: Serial.print(" - AL Presentation App"); break;
case 0x189: Serial.print(" - AL Database App"); break;
case 0x18A: Serial.print(" - AL Email Reader"); break;
case 0x18B: Serial.print(" - AL Newsreader"); break;
case 0x18C: Serial.print(" - AL Voicemail"); break;
case 0x18D: Serial.print(" - AL Contacts/Address Book"); break;
case 0x18E: Serial.print(" - AL Calendar/Schedule"); break;
case 0x18F: Serial.print(" - AL Task/Project Manager"); break;
case 0x190: Serial.print(" - AL Log/Journal/Timecard"); break;
case 0x191: Serial.print(" - AL Checkbook/Finance"); break;
case 0x192: Serial.print(" - AL Calculator"); break;
case 0x193: Serial.print(" - AL A/V Capture/Playback"); break;
case 0x194: Serial.print(" - AL Local Machine Browser"); break;
case 0x195: Serial.print(" - AL LAN/WAN Browser"); break;
case 0x196: Serial.print(" - AL Internet Browser"); break;
case 0x197: Serial.print(" - AL Remote Networking/ISP Connect"); break;
case 0x198: Serial.print(" - AL Network Conference"); break;
case 0x199: Serial.print(" - AL Network Chat"); break;
case 0x19A: Serial.print(" - AL Telephony/Dialer"); break;
case 0x19B: Serial.print(" - AL Logon"); break;
case 0x19C: Serial.print(" - AL Logoff"); break;
case 0x19D: Serial.print(" - AL Logon/Logoff"); break;
case 0x19E: Serial.print(" - AL Terminal Lock/Screensaver"); break;
case 0x19F: Serial.print(" - AL Control Panel"); break;
case 0x1A0: Serial.print(" - AL Command Line Processor/Run"); break;
case 0x1A1: Serial.print(" - AL Process/Task Manager"); break;
case 0x1A2: Serial.print(" - AL Select Tast/Application"); break;
case 0x1A3: Serial.print(" - AL Next Task/Application"); break;
case 0x1A4: Serial.print(" - AL Previous Task/Application"); break;
case 0x1A5: Serial.print(" - AL Preemptive Halt Task/Application"); break;
case 0x200: Serial.print(" - Generic GUI Application Controls"); break;
case 0x201: Serial.print(" - AC New"); break;
case 0x202: Serial.print(" - AC Open"); break;
case 0x203: Serial.print(" - AC Close"); break;
case 0x204: Serial.print(" - AC Exit"); break;
case 0x205: Serial.print(" - AC Maximize"); break;
case 0x206: Serial.print(" - AC Minimize"); break;
case 0x207: Serial.print(" - AC Save"); break;
case 0x208: Serial.print(" - AC Print"); break;
case 0x209: Serial.print(" - AC Properties"); break;
case 0x21A: Serial.print(" - AC Undo"); break;
case 0x21B: Serial.print(" - AC Copy"); break;
case 0x21C: Serial.print(" - AC Cut"); break;
case 0x21D: Serial.print(" - AC Paste"); break;
case 0x21E: Serial.print(" - AC Select All"); break;
case 0x21F: Serial.print(" - AC Find"); break;
case 0x220: Serial.print(" - AC Find and Replace"); break;
case 0x221: Serial.print(" - AC Search"); break;
case 0x222: Serial.print(" - AC Go To"); break;
case 0x223: Serial.print(" - AC Home"); break;
case 0x224: Serial.print(" - AC Back"); break;
case 0x225: Serial.print(" - AC Forward"); break;
case 0x226: Serial.print(" - AC Stop"); break;
case 0x227: Serial.print(" - AC Refresh"); break;
case 0x228: Serial.print(" - AC Previous Link"); break;
case 0x229: Serial.print(" - AC Next Link"); break;
case 0x22A: Serial.print(" - AC Bookmarks"); break;
case 0x22B: Serial.print(" - AC History"); break;
case 0x22C: Serial.print(" - AC Subscriptions"); break;
case 0x22D: Serial.print(" - AC Zoom In"); break;
case 0x22E: Serial.print(" - AC Zoom Out"); break;
case 0x22F: Serial.print(" - AC Zoom"); break;
case 0x230: Serial.print(" - AC Full Screen View"); break;
case 0x231: Serial.print(" - AC Normal View"); break;
case 0x232: Serial.print(" - AC View Toggle"); break;
case 0x233: Serial.print(" - AC Scroll Up"); break;
case 0x234: Serial.print(" - AC Scroll Down"); break;
case 0x235: Serial.print(" - AC Scroll"); break;
case 0x236: Serial.print(" - AC Pan Left"); break;
case 0x237: Serial.print(" - AC Pan Right"); break;
case 0x238: Serial.print(" - AC Pan"); break;
case 0x239: Serial.print(" - AC New Window"); break;
case 0x23A: Serial.print(" - AC Tile Horizontally"); break;
case 0x23B: Serial.print(" - AC Tile Vertically"); break;
case 0x23C: Serial.print(" - AC Format"); break;

}
}
Serial.println();
}

void OnHIDExtrasRelease(uint32_t top, uint16_t key)
{
Serial.print("HID (");
Serial.print(top, HEX);
Serial.print(") key release:");
Serial.println(key, HEX);
}
*/
bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
// Called for maybe both HIDS for rawhid basic test. One is for the Teensy
// to output to Serial. while still having Raw Hid...
@@ -501,4 +250,4 @@ bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
}

return true;
}
}

+ 568
- 0
examples/KeyboardBT/KeyboardBT.ino ファイルの表示

@@ -0,0 +1,568 @@
// Simple test of USB Host Mouse/Keyboard
//
// This example is in the public domain

#include "USBHost_t36.h"

USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
KeyboardController keyboard1(myusb);
//KeyboardController keyboard2(myusb);
USBHIDParser hid1(myusb);
USBHIDParser hid2(myusb);
USBHIDParser hid3(myusb);
USBHIDParser hid4(myusb);
USBHIDParser hid5(myusb);
//MouseController mouse1(myusb);
//JoystickController joystick1(myusb);
BluetoothController bluet(myusb, true, "0000"); // Version does pairing to device
//BluetoothController bluet(myusb); // version assumes it already was paired
int user_axis[64];
uint32_t buttons_prev = 0;
RawHIDController rawhid1(myusb);
RawHIDController rawhid2(myusb, 0xffc90004);

//USBDriver *drivers[] = {&hub1, &hub2,&keyboard1, &keyboard2, &joystick1, &bluet, &hid1, &hid2, &hid3, &hid4, &hid5};
USBDriver *drivers[] = {&hub1, &hub2, &keyboard1, &bluet, &hid1, &hid2, &hid3, &hid4, &hid5};

#define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
//const char * driver_names[CNT_DEVICES] = {"Hub1","Hub2", "KB1", "KB2", "JOY1D", "Bluet", "HID1" , "HID2", "HID3", "HID4", "HID5"};
const char * driver_names[CNT_DEVICES] = {"Hub1","Hub2", "KB1", "Bluet", "HID1" , "HID2", "HID3", "HID4", "HID5"};

bool driver_active[CNT_DEVICES] = {false, false, false, false};

// Lets also look at HID Input devices
//USBHIDInput *hiddrivers[] = {&mouse1, &joystick1, &rawhid1, &rawhid2};
USBHIDInput *hiddrivers[] = {&rawhid1, &rawhid2};

#define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
//const char * hid_driver_names[CNT_DEVICES] = {"Mouse1","Joystick1", "RawHid1", "RawHid2"};
const char * hid_driver_names[CNT_DEVICES] = {"RawHid1", "RawHid2"};

bool hid_driver_active[CNT_DEVICES] = {false, false, false};
bool show_changed_only = false;

uint8_t joystick_left_trigger_value = 0;
uint8_t joystick_right_trigger_value = 0;
uint64_t joystick_full_notify_mask = (uint64_t)-1;

void setup()
{
while (!Serial) ; // wait for Arduino Serial Monitor
Serial.println("\n\nUSB Host Testing");
Serial.println(sizeof(USBHub), DEC);
myusb.begin();
keyboard1.attachPress(OnPress);
//keyboard2.attachPress(OnPress);
keyboard1.attachExtrasPress(OnHIDExtrasPress);
keyboard1.attachExtrasRelease(OnHIDExtrasRelease);
//keyboard2.attachExtrasPress(OnHIDExtrasPress);
//keyboard2.attachExtrasRelease(OnHIDExtrasRelease);

rawhid1.attachReceive(OnReceiveHidData);
rawhid2.attachReceive(OnReceiveHidData);
}


void loop()
{
myusb.Task();
/*
if (Serial.available()) {
int ch = Serial.read(); // get the first char.
while (Serial.read() != -1) ;
if ((ch == 'b') || (ch == 'B')) {
Serial.println("Only notify on Basic Axis changes");
joystick1.axisChangeNotifyMask(0x3ff);
} else if ((ch == 'f') || (ch == 'F')) {
Serial.println("Only notify on Full Axis changes");
joystick1.axisChangeNotifyMask(joystick_full_notify_mask);

} else {
if (show_changed_only) {
show_changed_only = false;
Serial.println("\n*** Show All fields mode ***");
} else {
show_changed_only = true;
Serial.println("\n*** Show only changed fields mode ***");
}
}
}
*/
for (uint8_t i = 0; i < CNT_DEVICES; i++) {
if (*drivers[i] != driver_active[i]) {
if (driver_active[i]) {
Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
driver_active[i] = false;
} else {
Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
driver_active[i] = true;

const uint8_t *psz = drivers[i]->manufacturer();
if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
psz = drivers[i]->product();
if (psz && *psz) Serial.printf(" product: %s\n", psz);
psz = drivers[i]->serialNumber();
if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
}
}
}

for (uint8_t i = 0; i < CNT_HIDDEVICES; i++) {
if (*hiddrivers[i] != hid_driver_active[i]) {
if (hid_driver_active[i]) {
Serial.printf("*** HID Device %s - disconnected ***\n", hid_driver_names[i]);
hid_driver_active[i] = false;
} else {
Serial.printf("*** HID Device %s %x:%x - connected ***\n", hid_driver_names[i], hiddrivers[i]->idVendor(), hiddrivers[i]->idProduct());
hid_driver_active[i] = true;

const uint8_t *psz = hiddrivers[i]->manufacturer();
if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
psz = hiddrivers[i]->product();
if (psz && *psz) Serial.printf(" product: %s\n", psz);
psz = hiddrivers[i]->serialNumber();
if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
}
}
}


/*
if(mouse1.available()) {
Serial.print("Mouse: buttons = ");
Serial.print(mouse1.getButtons());
Serial.print(", mouseX = ");
Serial.print(mouse1.getMouseX());
Serial.print(", mouseY = ");
Serial.print(mouse1.getMouseY());
Serial.print(", wheel = ");
Serial.print(mouse1.getWheel());
Serial.print(", wheelH = ");
Serial.print(mouse1.getWheelH());
Serial.println();
mouse1.mouseDataClear();
}

if (joystick1.available()) {
uint64_t axis_mask = joystick1.axisMask();
uint64_t axis_changed_mask = joystick1.axisChangedMask();
Serial.print("Joystick: buttons = ");
uint32_t buttons = joystick1.getButtons();
Serial.print(buttons, HEX);
//Serial.printf(" AMasks: %x %x:%x", axis_mask, (uint32_t)(user_axis_mask >> 32), (uint32_t)(user_axis_mask & 0xffffffff));
//Serial.printf(" M: %lx %lx", axis_mask, joystick1.axisChangedMask());
if (show_changed_only) {
for (uint8_t i = 0; axis_changed_mask != 0; i++, axis_changed_mask >>= 1) {
if (axis_changed_mask & 1) {
Serial.printf(" %d:%d", i, joystick1.getAxis(i));
}
}

} else {
for (uint8_t i = 0; axis_mask != 0; i++, axis_mask >>= 1) {
if (axis_mask & 1) {
Serial.printf(" %d:%d", i, joystick1.getAxis(i));
}
}
}
uint8_t ltv;
uint8_t rtv;
switch (joystick1.joystickType) {
default:
break;
case JoystickController::PS4:
ltv = joystick1.getAxis(3);
rtv = joystick1.getAxis(4);
if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
joystick_left_trigger_value = ltv;
joystick_right_trigger_value = rtv;
joystick1.setRumble(ltv, rtv);
}
break;

case JoystickController::PS3:
ltv = joystick1.getAxis(18);
rtv = joystick1.getAxis(19);
if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
joystick_left_trigger_value = ltv;
joystick_right_trigger_value = rtv;
joystick1.setRumble(ltv, rtv, 50);
}
break;

case JoystickController::XBOXONE:
case JoystickController::XBOX360:
ltv = joystick1.getAxis(4);
rtv = joystick1.getAxis(5);
if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
joystick_left_trigger_value = ltv;
joystick_right_trigger_value = rtv;
joystick1.setRumble(ltv, rtv);
Serial.printf(" Set Rumble %d %d", ltv, rtv);
}
break;
}
if (buttons != buttons_prev) {
if (joystick1.joystickType == JoystickController::PS3) {
joystick1.setLEDs((buttons>>12) & 0xf); // try to get to TRI/CIR/X/SQuare
} else {
uint8_t lr = (buttons & 1)? 0xff : 0;
uint8_t lg = (buttons & 2)? 0xff : 0;
uint8_t lb = (buttons & 4)? 0xff : 0;
joystick1.setLEDs(lr, lg, lb);
}
buttons_prev = buttons;
}

Serial.println();
joystick1.joystickDataClear();
}
*/
// See if we have some RAW data
if (rawhid1) {
int ch;
uint8_t buffer[64];
uint8_t count_chars = 0;
memset(buffer, 0, sizeof(buffer));
if (Serial.available()) {
while (((ch = Serial.read()) != -1) && (count_chars < sizeof(buffer))) {
buffer[count_chars++] = ch;
}
rawhid1.sendPacket(buffer);
}
}
}



void OnPress(int key)
{
Serial.print("key '");
switch (key) {
case KEYD_UP : Serial.print("UP"); break;
case KEYD_DOWN : Serial.print("DN"); break;
case KEYD_LEFT : Serial.print("LEFT"); break;
case KEYD_RIGHT : Serial.print("RIGHT"); break;
case KEYD_INSERT : Serial.print("Ins"); break;
case KEYD_DELETE : Serial.print("Del"); break;
case KEYD_PAGE_UP : Serial.print("PUP"); break;
case KEYD_PAGE_DOWN: Serial.print("PDN"); break;
case KEYD_HOME : Serial.print("HOME"); break;
case KEYD_END : Serial.print("END"); break;
case KEYD_F1 : Serial.print("F1"); break;
case KEYD_F2 : Serial.print("F2"); break;
case KEYD_F3 : Serial.print("F3"); break;
case KEYD_F4 : Serial.print("F4"); break;
case KEYD_F5 : Serial.print("F5"); break;
case KEYD_F6 : Serial.print("F6"); break;
case KEYD_F7 : Serial.print("F7"); break;
case KEYD_F8 : Serial.print("F8"); break;
case KEYD_F9 : Serial.print("F9"); break;
case KEYD_F10 : Serial.print("F10"); break;
case KEYD_F11 : Serial.print("F11"); break;
case KEYD_F12 : Serial.print("F12"); break;
default: Serial.print((char)key); break;
}
Serial.print("' ");
Serial.print(key);
Serial.print(" MOD: ");
if (keyboard1) {
Serial.print(keyboard1.getModifiers(), HEX);
Serial.print(" OEM: ");
Serial.print(keyboard1.getOemKey(), HEX);
Serial.print(" LEDS: ");
Serial.println(keyboard1.LEDS(), HEX);
//} else {
//Serial.print(keyboard2.getModifiers(), HEX);
//Serial.print(" OEM: ");
//Serial.print(keyboard2.getOemKey(), HEX);
//Serial.print(" LEDS: ");
//Serial.println(keyboard2.LEDS(), HEX);
}

//Serial.print("key ");
//Serial.print((char)keyboard1.getKey());
//Serial.print(" ");
//Serial.print((char)keyboard2.getKey());
//Serial.println();
}
void OnHIDExtrasPress(uint32_t top, uint16_t key)
{
Serial.print("HID (");
Serial.print(top, HEX);
Serial.print(") key press:");
Serial.print(key, HEX);
if (top == 0xc0000) {
switch (key) {
case 0x20 : Serial.print(" - +10"); break;
case 0x21 : Serial.print(" - +100"); break;
case 0x22 : Serial.print(" - AM/PM"); break;
case 0x30 : Serial.print(" - Power"); break;
case 0x31 : Serial.print(" - Reset"); break;
case 0x32 : Serial.print(" - Sleep"); break;
case 0x33 : Serial.print(" - Sleep After"); break;
case 0x34 : Serial.print(" - Sleep Mode"); break;
case 0x35 : Serial.print(" - Illumination"); break;
case 0x36 : Serial.print(" - Function Buttons"); break;
case 0x40 : Serial.print(" - Menu"); break;
case 0x41 : Serial.print(" - Menu Pick"); break;
case 0x42 : Serial.print(" - Menu Up"); break;
case 0x43 : Serial.print(" - Menu Down"); break;
case 0x44 : Serial.print(" - Menu Left"); break;
case 0x45 : Serial.print(" - Menu Right"); break;
case 0x46 : Serial.print(" - Menu Escape"); break;
case 0x47 : Serial.print(" - Menu Value Increase"); break;
case 0x48 : Serial.print(" - Menu Value Decrease"); break;
case 0x60 : Serial.print(" - Data On Screen"); break;
case 0x61 : Serial.print(" - Closed Caption"); break;
case 0x62 : Serial.print(" - Closed Caption Select"); break;
case 0x63 : Serial.print(" - VCR/TV"); break;
case 0x64 : Serial.print(" - Broadcast Mode"); break;
case 0x65 : Serial.print(" - Snapshot"); break;
case 0x66 : Serial.print(" - Still"); break;
case 0x80 : Serial.print(" - Selection"); break;
case 0x81 : Serial.print(" - Assign Selection"); break;
case 0x82 : Serial.print(" - Mode Step"); break;
case 0x83 : Serial.print(" - Recall Last"); break;
case 0x84 : Serial.print(" - Enter Channel"); break;
case 0x85 : Serial.print(" - Order Movie"); break;
case 0x86 : Serial.print(" - Channel"); break;
case 0x87 : Serial.print(" - Media Selection"); break;
case 0x88 : Serial.print(" - Media Select Computer"); break;
case 0x89 : Serial.print(" - Media Select TV"); break;
case 0x8A : Serial.print(" - Media Select WWW"); break;
case 0x8B : Serial.print(" - Media Select DVD"); break;
case 0x8C : Serial.print(" - Media Select Telephone"); break;
case 0x8D : Serial.print(" - Media Select Program Guide"); break;
case 0x8E : Serial.print(" - Media Select Video Phone"); break;
case 0x8F : Serial.print(" - Media Select Games"); break;
case 0x90 : Serial.print(" - Media Select Messages"); break;
case 0x91 : Serial.print(" - Media Select CD"); break;
case 0x92 : Serial.print(" - Media Select VCR"); break;
case 0x93 : Serial.print(" - Media Select Tuner"); break;
case 0x94 : Serial.print(" - Quit"); break;
case 0x95 : Serial.print(" - Help"); break;
case 0x96 : Serial.print(" - Media Select Tape"); break;
case 0x97 : Serial.print(" - Media Select Cable"); break;
case 0x98 : Serial.print(" - Media Select Satellite"); break;
case 0x99 : Serial.print(" - Media Select Security"); break;
case 0x9A : Serial.print(" - Media Select Home"); break;
case 0x9B : Serial.print(" - Media Select Call"); break;
case 0x9C : Serial.print(" - Channel Increment"); break;
case 0x9D : Serial.print(" - Channel Decrement"); break;
case 0x9E : Serial.print(" - Media Select SAP"); break;
case 0xA0 : Serial.print(" - VCR Plus"); break;
case 0xA1 : Serial.print(" - Once"); break;
case 0xA2 : Serial.print(" - Daily"); break;
case 0xA3 : Serial.print(" - Weekly"); break;
case 0xA4 : Serial.print(" - Monthly"); break;
case 0xB0 : Serial.print(" - Play"); break;
case 0xB1 : Serial.print(" - Pause"); break;
case 0xB2 : Serial.print(" - Record"); break;
case 0xB3 : Serial.print(" - Fast Forward"); break;
case 0xB4 : Serial.print(" - Rewind"); break;
case 0xB5 : Serial.print(" - Scan Next Track"); break;
case 0xB6 : Serial.print(" - Scan Previous Track"); break;
case 0xB7 : Serial.print(" - Stop"); break;
case 0xB8 : Serial.print(" - Eject"); break;
case 0xB9 : Serial.print(" - Random Play"); break;
case 0xBA : Serial.print(" - Select DisC"); break;
case 0xBB : Serial.print(" - Enter Disc"); break;
case 0xBC : Serial.print(" - Repeat"); break;
case 0xBD : Serial.print(" - Tracking"); break;
case 0xBE : Serial.print(" - Track Normal"); break;
case 0xBF : Serial.print(" - Slow Tracking"); break;
case 0xC0 : Serial.print(" - Frame Forward"); break;
case 0xC1 : Serial.print(" - Frame Back"); break;
case 0xC2 : Serial.print(" - Mark"); break;
case 0xC3 : Serial.print(" - Clear Mark"); break;
case 0xC4 : Serial.print(" - Repeat From Mark"); break;
case 0xC5 : Serial.print(" - Return To Mark"); break;
case 0xC6 : Serial.print(" - Search Mark Forward"); break;
case 0xC7 : Serial.print(" - Search Mark Backwards"); break;
case 0xC8 : Serial.print(" - Counter Reset"); break;
case 0xC9 : Serial.print(" - Show Counter"); break;
case 0xCA : Serial.print(" - Tracking Increment"); break;
case 0xCB : Serial.print(" - Tracking Decrement"); break;
case 0xCD : Serial.print(" - Pause/Continue"); break;
case 0xE0 : Serial.print(" - Volume"); break;
case 0xE1 : Serial.print(" - Balance"); break;
case 0xE2 : Serial.print(" - Mute"); break;
case 0xE3 : Serial.print(" - Bass"); break;
case 0xE4 : Serial.print(" - Treble"); break;
case 0xE5 : Serial.print(" - Bass Boost"); break;
case 0xE6 : Serial.print(" - Surround Mode"); break;
case 0xE7 : Serial.print(" - Loudness"); break;
case 0xE8 : Serial.print(" - MPX"); break;
case 0xE9 : Serial.print(" - Volume Up"); break;
case 0xEA : Serial.print(" - Volume Down"); break;
case 0xF0 : Serial.print(" - Speed Select"); break;
case 0xF1 : Serial.print(" - Playback Speed"); break;
case 0xF2 : Serial.print(" - Standard Play"); break;
case 0xF3 : Serial.print(" - Long Play"); break;
case 0xF4 : Serial.print(" - Extended Play"); break;
case 0xF5 : Serial.print(" - Slow"); break;
case 0x100: Serial.print(" - Fan Enable"); break;
case 0x101: Serial.print(" - Fan Speed"); break;
case 0x102: Serial.print(" - Light"); break;
case 0x103: Serial.print(" - Light Illumination Level"); break;
case 0x104: Serial.print(" - Climate Control Enable"); break;
case 0x105: Serial.print(" - Room Temperature"); break;
case 0x106: Serial.print(" - Security Enable"); break;
case 0x107: Serial.print(" - Fire Alarm"); break;
case 0x108: Serial.print(" - Police Alarm"); break;
case 0x150: Serial.print(" - Balance Right"); break;
case 0x151: Serial.print(" - Balance Left"); break;
case 0x152: Serial.print(" - Bass Increment"); break;
case 0x153: Serial.print(" - Bass Decrement"); break;
case 0x154: Serial.print(" - Treble Increment"); break;
case 0x155: Serial.print(" - Treble Decrement"); break;
case 0x160: Serial.print(" - Speaker System"); break;
case 0x161: Serial.print(" - Channel Left"); break;
case 0x162: Serial.print(" - Channel Right"); break;
case 0x163: Serial.print(" - Channel Center"); break;
case 0x164: Serial.print(" - Channel Front"); break;
case 0x165: Serial.print(" - Channel Center Front"); break;
case 0x166: Serial.print(" - Channel Side"); break;
case 0x167: Serial.print(" - Channel Surround"); break;
case 0x168: Serial.print(" - Channel Low Frequency Enhancement"); break;
case 0x169: Serial.print(" - Channel Top"); break;
case 0x16A: Serial.print(" - Channel Unknown"); break;
case 0x170: Serial.print(" - Sub-channel"); break;
case 0x171: Serial.print(" - Sub-channel Increment"); break;
case 0x172: Serial.print(" - Sub-channel Decrement"); break;
case 0x173: Serial.print(" - Alternate Audio Increment"); break;
case 0x174: Serial.print(" - Alternate Audio Decrement"); break;
case 0x180: Serial.print(" - Application Launch Buttons"); break;
case 0x181: Serial.print(" - AL Launch Button Configuration Tool"); break;
case 0x182: Serial.print(" - AL Programmable Button Configuration"); break;
case 0x183: Serial.print(" - AL Consumer Control Configuration"); break;
case 0x184: Serial.print(" - AL Word Processor"); break;
case 0x185: Serial.print(" - AL Text Editor"); break;
case 0x186: Serial.print(" - AL Spreadsheet"); break;
case 0x187: Serial.print(" - AL Graphics Editor"); break;
case 0x188: Serial.print(" - AL Presentation App"); break;
case 0x189: Serial.print(" - AL Database App"); break;
case 0x18A: Serial.print(" - AL Email Reader"); break;
case 0x18B: Serial.print(" - AL Newsreader"); break;
case 0x18C: Serial.print(" - AL Voicemail"); break;
case 0x18D: Serial.print(" - AL Contacts/Address Book"); break;
case 0x18E: Serial.print(" - AL Calendar/Schedule"); break;
case 0x18F: Serial.print(" - AL Task/Project Manager"); break;
case 0x190: Serial.print(" - AL Log/Journal/Timecard"); break;
case 0x191: Serial.print(" - AL Checkbook/Finance"); break;
case 0x192: Serial.print(" - AL Calculator"); break;
case 0x193: Serial.print(" - AL A/V Capture/Playback"); break;
case 0x194: Serial.print(" - AL Local Machine Browser"); break;
case 0x195: Serial.print(" - AL LAN/WAN Browser"); break;
case 0x196: Serial.print(" - AL Internet Browser"); break;
case 0x197: Serial.print(" - AL Remote Networking/ISP Connect"); break;
case 0x198: Serial.print(" - AL Network Conference"); break;
case 0x199: Serial.print(" - AL Network Chat"); break;
case 0x19A: Serial.print(" - AL Telephony/Dialer"); break;
case 0x19B: Serial.print(" - AL Logon"); break;
case 0x19C: Serial.print(" - AL Logoff"); break;
case 0x19D: Serial.print(" - AL Logon/Logoff"); break;
case 0x19E: Serial.print(" - AL Terminal Lock/Screensaver"); break;
case 0x19F: Serial.print(" - AL Control Panel"); break;
case 0x1A0: Serial.print(" - AL Command Line Processor/Run"); break;
case 0x1A1: Serial.print(" - AL Process/Task Manager"); break;
case 0x1A2: Serial.print(" - AL Select Tast/Application"); break;
case 0x1A3: Serial.print(" - AL Next Task/Application"); break;
case 0x1A4: Serial.print(" - AL Previous Task/Application"); break;
case 0x1A5: Serial.print(" - AL Preemptive Halt Task/Application"); break;
case 0x200: Serial.print(" - Generic GUI Application Controls"); break;
case 0x201: Serial.print(" - AC New"); break;
case 0x202: Serial.print(" - AC Open"); break;
case 0x203: Serial.print(" - AC Close"); break;
case 0x204: Serial.print(" - AC Exit"); break;
case 0x205: Serial.print(" - AC Maximize"); break;
case 0x206: Serial.print(" - AC Minimize"); break;
case 0x207: Serial.print(" - AC Save"); break;
case 0x208: Serial.print(" - AC Print"); break;
case 0x209: Serial.print(" - AC Properties"); break;
case 0x21A: Serial.print(" - AC Undo"); break;
case 0x21B: Serial.print(" - AC Copy"); break;
case 0x21C: Serial.print(" - AC Cut"); break;
case 0x21D: Serial.print(" - AC Paste"); break;
case 0x21E: Serial.print(" - AC Select All"); break;
case 0x21F: Serial.print(" - AC Find"); break;
case 0x220: Serial.print(" - AC Find and Replace"); break;
case 0x221: Serial.print(" - AC Search"); break;
case 0x222: Serial.print(" - AC Go To"); break;
case 0x223: Serial.print(" - AC Home"); break;
case 0x224: Serial.print(" - AC Back"); break;
case 0x225: Serial.print(" - AC Forward"); break;
case 0x226: Serial.print(" - AC Stop"); break;
case 0x227: Serial.print(" - AC Refresh"); break;
case 0x228: Serial.print(" - AC Previous Link"); break;
case 0x229: Serial.print(" - AC Next Link"); break;
case 0x22A: Serial.print(" - AC Bookmarks"); break;
case 0x22B: Serial.print(" - AC History"); break;
case 0x22C: Serial.print(" - AC Subscriptions"); break;
case 0x22D: Serial.print(" - AC Zoom In"); break;
case 0x22E: Serial.print(" - AC Zoom Out"); break;
case 0x22F: Serial.print(" - AC Zoom"); break;
case 0x230: Serial.print(" - AC Full Screen View"); break;
case 0x231: Serial.print(" - AC Normal View"); break;
case 0x232: Serial.print(" - AC View Toggle"); break;
case 0x233: Serial.print(" - AC Scroll Up"); break;
case 0x234: Serial.print(" - AC Scroll Down"); break;
case 0x235: Serial.print(" - AC Scroll"); break;
case 0x236: Serial.print(" - AC Pan Left"); break;
case 0x237: Serial.print(" - AC Pan Right"); break;
case 0x238: Serial.print(" - AC Pan"); break;
case 0x239: Serial.print(" - AC New Window"); break;
case 0x23A: Serial.print(" - AC Tile Horizontally"); break;
case 0x23B: Serial.print(" - AC Tile Vertically"); break;
case 0x23C: Serial.print(" - AC Format"); break;

}
}
Serial.println();
}

void OnHIDExtrasRelease(uint32_t top, uint16_t key)
{
Serial.print("HID (");
Serial.print(top, HEX);
Serial.print(") key release:");
Serial.println(key, HEX);
}

bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
// Called for maybe both HIDS for rawhid basic test. One is for the Teensy
// to output to Serial. while still having Raw Hid...
if (usage == 0xffc90004) {
// Lets trim off trailing null characters.
while ((len > 0) && (data[len-1] == 0)) {
len--;
}
if (len) {
Serial.print("RawHid Serial: ");
Serial.write(data, len);
}
} else {
Serial.print("RawHID data: ");
Serial.println(usage, HEX);
while (len) {
uint8_t cb = (len > 16)? 16 : len;
const uint8_t *p = data;
uint8_t i;
for (i = 0; i < cb; i++) {
Serial.printf("%02x ", *p++);
}
Serial.print(": ");
for (i = 0; i < cb; i++) {
Serial.write(((*data >= ' ')&&(*data <= '~'))? *data : '.');
data++;
}
len -= cb;
Serial.println();
}
}

return true;
}

+ 1
- 0
examples/KeyboardBT/defs.h ファイルの表示

@@ -0,0 +1 @@


+ 43
- 26
examples/MouseBT/MouseBT.ino ファイルの表示

@@ -16,8 +16,8 @@ USBHIDParser hid4(myusb);
USBHIDParser hid5(myusb);
MouseController mouse1(myusb);
JoystickController joystick1(myusb);
//BluetoothController bluet(myusb, true, "0000"); // Version does pairing to device
BluetoothController bluet(myusb); // version assumes it already was paired
BluetoothController bluet(myusb, true, "0000"); // Version does pairing to device
//BluetoothController bluet(myusb); // version assumes it already was paired
int user_axis[64];
uint32_t buttons_prev = 0;
RawHIDController rawhid1(myusb);
@@ -31,13 +31,13 @@ bool driver_active[CNT_DEVICES] = {false, false, false, false};
// Lets also look at HID Input devices
USBHIDInput *hiddrivers[] = {&mouse1, &joystick1, &rawhid1, &rawhid2};
#define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
const char * hid_driver_names[CNT_DEVICES] = {"Mouse1","Joystick1", "RawHid1", "RawHid2"};
const char * hid_driver_names[CNT_DEVICES] = {"Mouse1", "Joystick1", "RawHid1", "RawHid2"};
bool hid_driver_active[CNT_DEVICES] = {false, false};
bool show_changed_only = false;
bool show_changed_only = false;

uint8_t joystick_left_trigger_value = 0;
uint8_t joystick_right_trigger_value = 0;
uint64_t joystick_full_notify_mask = (uint64_t)-1;
uint64_t joystick_full_notify_mask = (uint64_t) - 1;

void setup()
{
@@ -52,6 +52,11 @@ void setup()
keyboard2.attachExtrasPress(OnHIDExtrasPress);
keyboard2.attachExtrasRelease(OnHIDExtrasRelease);

// The below forceBootProtocol will force which ever
// next keyboard that attaches to this device to be in boot protocol
// Only try this if you run into keyboard with issues. If this is a combined
// device like wireless mouse and keyboard this can cause mouse problems.
//keyboard1.forceBootProtocol();
rawhid1.attachReceive(OnReceiveHidData);
rawhid2.attachReceive(OnReceiveHidData);
}
@@ -62,8 +67,8 @@ void loop()
myusb.Task();

if (Serial.available()) {
int ch = Serial.read(); // get the first char.
while (Serial.read() != -1) ;
int ch = Serial.read(); // get the first char.
while (Serial.read() != -1) ;
if ((ch == 'b') || (ch == 'B')) {
Serial.println("Only notify on Basic Axis changes");
joystick1.axisChangeNotifyMask(0x3ff);
@@ -80,7 +85,7 @@ void loop()
Serial.println("\n*** Show only changed fields mode ***");
}
}
}
}

for (uint8_t i = 0; i < CNT_DEVICES; i++) {
if (*drivers[i] != driver_active[i]) {
@@ -97,6 +102,19 @@ void loop()
if (psz && *psz) Serial.printf(" product: %s\n", psz);
psz = drivers[i]->serialNumber();
if (psz && *psz) Serial.printf(" Serial: %s\n", psz);

// Note: with some keyboards there is an issue that they don't output in boot protocol mode
// and may not work. The above code can try to force the keyboard into boot mode, but there
// are issues with doing this blindly with combo devices like wireless keyboard/mouse, which
// may cause the mouse to not work. Note: the above id is in the builtin list of
// vendor IDs that are already forced
if (drivers[i] == &keyboard1) {
if (keyboard1.idVendor() == 0x04D9) {
Serial.println("Gigabyte vendor: force boot protocol");
// Gigabyte keyboard
keyboard1.forceBootProtocol();
}
}
}
}
}
@@ -122,7 +140,7 @@ void loop()



if(mouse1.available()) {
if (mouse1.available()) {
Serial.print("Mouse: buttons = ");
Serial.print(mouse1.getButtons());
Serial.print(", mouseX = ");
@@ -136,7 +154,6 @@ void loop()
Serial.println();
mouse1.mouseDataClear();
}

if (joystick1.available()) {
uint64_t axis_mask = joystick1.axisMask();
uint64_t axis_changed_mask = joystick1.axisChangedMask();
@@ -171,7 +188,7 @@ void loop()
joystick_left_trigger_value = ltv;
joystick_right_trigger_value = rtv;
joystick1.setRumble(ltv, rtv);
}
}
break;

case JoystickController::PS3:
@@ -181,11 +198,11 @@ void loop()
joystick_left_trigger_value = ltv;
joystick_right_trigger_value = rtv;
joystick1.setRumble(ltv, rtv, 50);
}
}
break;

case JoystickController::XBOXONE:
case JoystickController::XBOX360:
case JoystickController::XBOXONE:
case JoystickController::XBOX360:
ltv = joystick1.getAxis(4);
rtv = joystick1.getAxis(5);
if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
@@ -193,16 +210,16 @@ void loop()
joystick_right_trigger_value = rtv;
joystick1.setRumble(ltv, rtv);
Serial.printf(" Set Rumble %d %d", ltv, rtv);
}
}
break;
}
if (buttons != buttons_prev) {
if (joystick1.joystickType == JoystickController::PS3) {
joystick1.setLEDs((buttons>>12) & 0xf); // try to get to TRI/CIR/X/SQuare
joystick1.setLEDs((buttons >> 12) & 0xf); // try to get to TRI/CIR/X/SQuare
} else {
uint8_t lr = (buttons & 1)? 0xff : 0;
uint8_t lg = (buttons & 2)? 0xff : 0;
uint8_t lb = (buttons & 4)? 0xff : 0;
uint8_t lr = (buttons & 1) ? 0xff : 0;
uint8_t lg = (buttons & 2) ? 0xff : 0;
uint8_t lb = (buttons & 4) ? 0xff : 0;
joystick1.setLEDs(lr, lg, lb);
}
buttons_prev = buttons;
@@ -216,7 +233,7 @@ void loop()
if (rawhid1) {
int ch;
uint8_t buffer[64];
uint8_t count_chars = 0;
uint8_t count_chars = 0;
memset(buffer, 0, sizeof(buffer));
if (Serial.available()) {
while (((ch = Serial.read()) != -1) && (count_chars < sizeof(buffer))) {
@@ -280,7 +297,7 @@ void OnPress(int key)
//Serial.print((char)keyboard2.getKey());
//Serial.println();
}
void OnHIDExtrasPress(uint32_t top, uint16_t key)
void OnHIDExtrasPress(uint32_t top, uint16_t key)
{
Serial.print("HID (");
Serial.print(top, HEX);
@@ -516,7 +533,7 @@ void OnHIDExtrasPress(uint32_t top, uint16_t key)
Serial.println();
}

void OnHIDExtrasRelease(uint32_t top, uint16_t key)
void OnHIDExtrasRelease(uint32_t top, uint16_t key)
{
Serial.print("HID (");
Serial.print(top, HEX);
@@ -526,10 +543,10 @@ void OnHIDExtrasRelease(uint32_t top, uint16_t key)

bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
// Called for maybe both HIDS for rawhid basic test. One is for the Teensy
// to output to Serial. while still having Raw Hid...
// to output to Serial. while still having Raw Hid...
if (usage == 0xffc90004) {
// Lets trim off trailing null characters.
while ((len > 0) && (data[len-1] == 0)) {
while ((len > 0) && (data[len - 1] == 0)) {
len--;
}
if (len) {
@@ -540,7 +557,7 @@ bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
Serial.print("RawHID data: ");
Serial.println(usage, HEX);
while (len) {
uint8_t cb = (len > 16)? 16 : len;
uint8_t cb = (len > 16) ? 16 : len;
const uint8_t *p = data;
uint8_t i;
for (i = 0; i < cb; i++) {
@@ -548,7 +565,7 @@ bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
}
Serial.print(": ");
for (i = 0; i < cb; i++) {
Serial.write(((*data >= ' ')&&(*data <= '~'))? *data : '.');
Serial.write(((*data >= ' ') && (*data <= '~')) ? *data : '.');
data++;
}
len -= cb;

+ 72
- 23
joystick.cpp ファイルの表示

@@ -233,9 +233,9 @@ bool JoystickController::transmitPS4UserFeedbackMsg() {
packet[10] = leds_[1];
packet[11] = leds_[2];

// 9, 10 flash ON, OFF times in 100ths of sedond? 2.5 seconds = 255
// 12, 13 flash ON, OFF times in 100ths of sedond? 2.5 seconds = 255
USBHDBGSerial.printf("Joystick update Rumble/LEDs\n");
btdriver_->sendL2CapCommand(packet, sizeof(packet), 0x41, 0x00);
btdriver_->sendL2CapCommand(packet, sizeof(packet), 0x40, 0x00);

return true;
}
@@ -687,27 +687,76 @@ bool JoystickController::process_bluetooth_HID_data(const uint8_t *data, uint16_
// LX LY RX RY BT BT PS LT RT
//USBHDBGSerial.printf("JoystickController::process_bluetooth_HID_data\n");
// May have to look at this one with other controllers...
if (data[0] != 1) return false;
//print(" Joystick Data: ");
//print_hexbytes(data, length);
//USBHDBGSerial.printf(" Joystick Data: ");
uint64_t mask = 0x1;
axis_mask_ = 0;
axis_changed_mask_ = 0;
for (uint16_t i = 0; i < length; i++ ) {
axis_mask_ |= mask;
if(data[i] != axis[i]) {
axis_changed_mask_ |= mask;
axis[i] = data[i];
}
mask <<= 1; // shift down the mask.
//USBHDBGSerial.printf("%02x ", axis[i]);
if (data[0] == 1) {
//print(" Joystick Data: ");
//print_hexbytes(data, length);
USBHDBGSerial.printf(" Joystick Data: ");
uint64_t mask = 0x1;
axis_mask_ = 0;
axis_changed_mask_ = 0;
for (uint16_t i = 0; i < length; i++ ) {
axis_mask_ |= mask;
if(data[i] != axis[i]) {
axis_changed_mask_ |= mask;
axis[i] = data[i];
}
mask <<= 1; // shift down the mask.
USBHDBGSerial.printf("%02x ", axis[i]);
}
USBHDBGSerial.printf("\n");
joystickEvent = true;
connected_ = true;
return true;
} else if(data[0] == 0x11){
USBHDBGSerial.printf(" Joystick Data: ");
uint64_t mask = 0x1;
axis_mask_ = 0;
axis_changed_mask_ = 0;
//This moves data to be equivalent to what we see for
//data[0] = 0x01
uint8_t tmp_data[length-2];
for (uint16_t i = 0; i < (length-2); i++ ) {
tmp_data[i] = data[i+2];
}
/*
* [1] LX, [2] = LY, [3] = RX, [4] = RY
* [5] combo, tri, cir, x, sqr, D-PAD (4bits, 0-3
* [6] R3,L3, opt, share, R2, L2, R1, L1
* [7] Counter (bit7-2), T-PAD, PS
* [8] Left Trigger, [9] Right Trigger
* [10-11] Timestamp
* [12] Battery (0 to 0xff)
* [13-14] acceleration x
* [15-16] acceleration y
* [17-18] acceleration z
* [19-20] gyro x
* [21-22] gyro y
* [23-24] gyro z
* [25-29] unknown
* [30] 0x00,phone,mic, usb, battery level (4bits)
* rest is trackpad? to do implement?
*/
//PS Bit
tmp_data[7] = (tmp_data[7] >> 0) & 1;
//set arrow buttons to axis[0]
tmp_data[0] = tmp_data[5] & ((1 << 4) - 1);
//set buttons for last 4bits in the axis[5]
tmp_data[5] = tmp_data[5] >> 4;
for (uint16_t i = 0; i < (length-2); i++ ) {
if(tmp_data[i] != axis[i]) {
axis_changed_mask_ |= mask;
axis[i] = tmp_data[i];
}
mask <<= 1; // shift down the mask.
USBHDBGSerial.printf("%02x ", axis[i]);
}
USBHDBGSerial.printf("\n");
joystickEvent = true;
connected_ = true;
}
//USBHDBGSerial.printf("\n");
joystickEvent = true;
connected_ = true;
return true;
return false;
}

void JoystickController::remoteNameComplete(const uint8_t *remoteName)
@@ -718,7 +767,7 @@ void JoystickController::remoteNameComplete(const uint8_t *remoteName)
if (strncmp((const char *)remoteName, "Wireless Controller", 19) == 0) {
USBHDBGSerial.printf(" JoystickController::remoteNameComplete %s - set to PS4\n", remoteName);
needs_connect_to_device = true; // We need to force this.
joystickType = PS4;
joystickType = PS4;
}

}

+ 1
- 1
mouse.cpp ファイルの表示

@@ -160,4 +160,4 @@ bool MouseController::process_bluetooth_HID_data(const uint8_t *data, uint16_t l
void MouseController::release_bluetooth()
{
//btdevice = nullptr;
}
}

読み込み中…
キャンセル
保存