diff options
Diffstat (limited to 'lib/usbhost/USB_Host_Shield_2.0/examples')
58 files changed, 6567 insertions, 0 deletions
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/BTHID.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/BTHID.ino new file mode 100644 index 0000000000..919a56468b --- /dev/null +++ b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/BTHID.ino @@ -0,0 +1,55 @@ +/* + Example sketch for the HID Bluetooth library - developed by Kristian Lauszus + For more information visit my blog: http://blog.tkjelectronics.dk/ or + send me an e-mail: kristianl@tkjelectronics.com + */ + +#include <BTHID.h> +#include <usbhub.h> +#include "KeyboardParser.h" +#include "MouseParser.h" + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +//USBHub Hub1(&Usb); // Some dongles have a hub inside +BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so + +/* You can create the instance of the class in two ways */ +// This will start an inquiry and then pair with your device - you only have to do this once +// If you are using a Bluetooth keyboard, then you should type in the password on the keypad and then press enter +BTHID bthid(&Btd, PAIR, "0000"); + +// After that you can simply create the instance like so and then press any button on the device +//BTHID hid(&Btd); + +KbdRptParser keyboardPrs; +MouseRptParser mousePrs; + +void setup() { + Serial.begin(115200); +#if !defined(__MIPSEL__) + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection +#endif + if (Usb.Init() == -1) { + Serial.print(F("\r\nOSC did not start")); + while (1); // Halt + } + + bthid.SetReportParser(KEYBOARD_PARSER_ID, (HIDReportParser*)&keyboardPrs); + bthid.SetReportParser(MOUSE_PARSER_ID, (HIDReportParser*)&mousePrs); + + // If "Boot Protocol Mode" does not work, then try "Report Protocol Mode" + // If that does not work either, then uncomment PRINTREPORT in BTHID.cpp to see the raw report + bthid.setProtocolMode(HID_BOOT_PROTOCOL); // Boot Protocol Mode + //bthid.setProtocolMode(HID_RPT_PROTOCOL); // Report Protocol Mode + + Serial.print(F("\r\nHID Bluetooth Library Started")); +} +void loop() { + Usb.Task(); +} diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/KeyboardParser.h b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/KeyboardParser.h new file mode 100644 index 0000000000..c5394331da --- /dev/null +++ b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/KeyboardParser.h @@ -0,0 +1,105 @@ +#ifndef __kbdrptparser_h_ +#define __kbdrptparser_h_ + +class KbdRptParser : public KeyboardReportParser { + protected: + virtual uint8_t HandleLockingKeys(HID *hid, uint8_t key); + virtual void OnControlKeysChanged(uint8_t before, uint8_t after); + virtual void OnKeyDown(uint8_t mod, uint8_t key); + virtual void OnKeyUp(uint8_t mod, uint8_t key); + virtual void OnKeyPressed(uint8_t key); + + private: + void PrintKey(uint8_t mod, uint8_t key); +}; + +uint8_t KbdRptParser::HandleLockingKeys(HID *hid, uint8_t key) { + uint8_t old_keys = kbdLockingKeys.bLeds; + + switch (key) { + case UHS_HID_BOOT_KEY_NUM_LOCK: + Serial.println(F("Num lock")); + kbdLockingKeys.kbdLeds.bmNumLock = ~kbdLockingKeys.kbdLeds.bmNumLock; + break; + case UHS_HID_BOOT_KEY_CAPS_LOCK: + Serial.println(F("Caps lock")); + kbdLockingKeys.kbdLeds.bmCapsLock = ~kbdLockingKeys.kbdLeds.bmCapsLock; + break; + case UHS_HID_BOOT_KEY_SCROLL_LOCK: + Serial.println(F("Scroll lock")); + kbdLockingKeys.kbdLeds.bmScrollLock = ~kbdLockingKeys.kbdLeds.bmScrollLock; + break; + } + + if (old_keys != kbdLockingKeys.bLeds && hid) { + BTHID *pBTHID = reinterpret_cast<BTHID *> (hid); // A cast the other way around is done in BTHID.cpp + pBTHID->setLeds(kbdLockingKeys.bLeds); // Update the LEDs on the keyboard + } + + return 0; +}; + +void KbdRptParser::PrintKey(uint8_t m, uint8_t key) { + MODIFIERKEYS mod; + *((uint8_t*)&mod) = m; + Serial.print((mod.bmLeftCtrl == 1) ? F("C") : F(" ")); + Serial.print((mod.bmLeftShift == 1) ? F("S") : F(" ")); + Serial.print((mod.bmLeftAlt == 1) ? F("A") : F(" ")); + Serial.print((mod.bmLeftGUI == 1) ? F("G") : F(" ")); + + Serial.print(F(" >")); + PrintHex<uint8_t>(key, 0x80); + Serial.print(F("< ")); + + Serial.print((mod.bmRightCtrl == 1) ? F("C") : F(" ")); + Serial.print((mod.bmRightShift == 1) ? F("S") : F(" ")); + Serial.print((mod.bmRightAlt == 1) ? F("A") : F(" ")); + Serial.println((mod.bmRightGUI == 1) ? F("G") : F(" ")); +}; + +void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) { + Serial.print(F("DN ")); + PrintKey(mod, key); + uint8_t c = OemToAscii(mod, key); + + if (c) + OnKeyPressed(c); +}; + +void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) { + MODIFIERKEYS beforeMod; + *((uint8_t*)&beforeMod) = before; + + MODIFIERKEYS afterMod; + *((uint8_t*)&afterMod) = after; + + if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) + Serial.println(F("LeftCtrl changed")); + if (beforeMod.bmLeftShift != afterMod.bmLeftShift) + Serial.println(F("LeftShift changed")); + if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) + Serial.println(F("LeftAlt changed")); + if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) + Serial.println(F("LeftGUI changed")); + + if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) + Serial.println(F("RightCtrl changed")); + if (beforeMod.bmRightShift != afterMod.bmRightShift) + Serial.println(F("RightShift changed")); + if (beforeMod.bmRightAlt != afterMod.bmRightAlt) + Serial.println(F("RightAlt changed")); + if (beforeMod.bmRightGUI != afterMod.bmRightGUI) + Serial.println(F("RightGUI changed")); +}; + +void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) { + Serial.print(F("UP ")); + PrintKey(mod, key); +}; + +void KbdRptParser::OnKeyPressed(uint8_t key) { + Serial.print(F("ASCII: ")); + Serial.println((char)key); +}; + +#endif diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/MouseParser.h b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/MouseParser.h new file mode 100644 index 0000000000..a9245ded99 --- /dev/null +++ b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/MouseParser.h @@ -0,0 +1,46 @@ +#ifndef __mouserptparser_h__ +#define __mouserptparser_h__ + +class MouseRptParser : public MouseReportParser { + protected: + virtual void OnMouseMove(MOUSEINFO *mi); + virtual void OnLeftButtonUp(MOUSEINFO *mi); + virtual void OnLeftButtonDown(MOUSEINFO *mi); + virtual void OnRightButtonUp(MOUSEINFO *mi); + virtual void OnRightButtonDown(MOUSEINFO *mi); + virtual void OnMiddleButtonUp(MOUSEINFO *mi); + virtual void OnMiddleButtonDown(MOUSEINFO *mi); +}; + +void MouseRptParser::OnMouseMove(MOUSEINFO *mi) { + Serial.print(F("dx=")); + Serial.print(mi->dX, DEC); + Serial.print(F(" dy=")); + Serial.println(mi->dY, DEC); +}; + +void MouseRptParser::OnLeftButtonUp(MOUSEINFO *mi) { + Serial.println(F("L Butt Up")); +}; + +void MouseRptParser::OnLeftButtonDown(MOUSEINFO *mi) { + Serial.println(F("L Butt Dn")); +}; + +void MouseRptParser::OnRightButtonUp(MOUSEINFO *mi) { + Serial.println(F("R Butt Up")); +}; + +void MouseRptParser::OnRightButtonDown(MOUSEINFO *mi) { + Serial.println(F("R Butt Dn")); +}; + +void MouseRptParser::OnMiddleButtonUp(MOUSEINFO *mi) { + Serial.println(F("M Butt Up")); +}; + +void MouseRptParser::OnMiddleButtonDown(MOUSEINFO *mi) { + Serial.println(F("M Butt Dn")); +}; + +#endif diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/PS3BT/PS3BT.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/PS3BT/PS3BT.ino new file mode 100644 index 0000000000..b896734405 --- /dev/null +++ b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/PS3BT/PS3BT.ino @@ -0,0 +1,188 @@ +/* + Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus + For more information visit my blog: http://blog.tkjelectronics.dk/ or + send me an e-mail: kristianl@tkjelectronics.com + */ + +#include <PS3BT.h> +#include <usbhub.h> + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +//USBHub Hub1(&Usb); // Some dongles have a hub inside + +BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so +/* You can create the instance of the class in two ways */ +PS3BT PS3(&Btd); // This will just create the instance +//PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch + +bool printTemperature; +bool printAngle; + +void setup() { + Serial.begin(115200); +#if !defined(__MIPSEL__) + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection +#endif + if (Usb.Init() == -1) { + Serial.print(F("\r\nOSC did not start")); + while (1); //halt + } + Serial.print(F("\r\nPS3 Bluetooth Library Started")); +} +void loop() { + Usb.Task(); + + if (PS3.PS3Connected || PS3.PS3NavigationConnected) { + if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { + Serial.print(F("\r\nLeftHatX: ")); + Serial.print(PS3.getAnalogHat(LeftHatX)); + Serial.print(F("\tLeftHatY: ")); + Serial.print(PS3.getAnalogHat(LeftHatY)); + if (PS3.PS3Connected) { // The Navigation controller only have one joystick + Serial.print(F("\tRightHatX: ")); + Serial.print(PS3.getAnalogHat(RightHatX)); + Serial.print(F("\tRightHatY: ")); + Serial.print(PS3.getAnalogHat(RightHatY)); + } + } + + // Analog button values can be read from almost all buttons + if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) { + Serial.print(F("\r\nL2: ")); + Serial.print(PS3.getAnalogButton(L2)); + if (PS3.PS3Connected) { + Serial.print(F("\tR2: ")); + Serial.print(PS3.getAnalogButton(R2)); + } + } + if (PS3.getButtonClick(PS)) { + Serial.print(F("\r\nPS")); + PS3.disconnect(); + } + else { + if (PS3.getButtonClick(TRIANGLE)) + Serial.print(F("\r\nTraingle")); + if (PS3.getButtonClick(CIRCLE)) + Serial.print(F("\r\nCircle")); + if (PS3.getButtonClick(CROSS)) + Serial.print(F("\r\nCross")); + if (PS3.getButtonClick(SQUARE)) + Serial.print(F("\r\nSquare")); + + if (PS3.getButtonClick(UP)) { + Serial.print(F("\r\nUp")); + if (PS3.PS3Connected) { + PS3.setLedOff(); + PS3.setLedOn(LED4); + } + } + if (PS3.getButtonClick(RIGHT)) { + Serial.print(F("\r\nRight")); + if (PS3.PS3Connected) { + PS3.setLedOff(); + PS3.setLedOn(LED1); + } + } + if (PS3.getButtonClick(DOWN)) { + Serial.print(F("\r\nDown")); + if (PS3.PS3Connected) { + PS3.setLedOff(); + PS3.setLedOn(LED2); + } + } + if (PS3.getButtonClick(LEFT)) { + Serial.print(F("\r\nLeft")); + if (PS3.PS3Connected) { + PS3.setLedOff(); + PS3.setLedOn(LED3); + } + } + + if (PS3.getButtonClick(L1)) + Serial.print(F("\r\nL1")); + if (PS3.getButtonClick(L3)) + Serial.print(F("\r\nL3")); + if (PS3.getButtonClick(R1)) + Serial.print(F("\r\nR1")); + if (PS3.getButtonClick(R3)) + Serial.print(F("\r\nR3")); + + if (PS3.getButtonClick(SELECT)) { + Serial.print(F("\r\nSelect - ")); + PS3.printStatusString(); + } + if (PS3.getButtonClick(START)) { + Serial.print(F("\r\nStart")); + printAngle = !printAngle; + } + } +#if 0 // Set this to 1 in order to see the angle of the controller + if (printAngle) { + Serial.print(F("\r\nPitch: ")); + Serial.print(PS3.getAngle(Pitch)); + Serial.print(F("\tRoll: ")); + Serial.print(PS3.getAngle(Roll)); + } +#endif + } +#if 0 // Set this to 1 in order to enable support for the Playstation Move controller + else if (PS3.PS3MoveConnected) { + if (PS3.getAnalogButton(T)) { + Serial.print(F("\r\nT: ")); + Serial.print(PS3.getAnalogButton(T)); + } + if (PS3.getButtonClick(PS)) { + Serial.print(F("\r\nPS")); + PS3.disconnect(); + } + else { + if (PS3.getButtonClick(SELECT)) { + Serial.print(F("\r\nSelect")); + printTemperature = !printTemperature; + } + if (PS3.getButtonClick(START)) { + Serial.print(F("\r\nStart")); + printAngle = !printAngle; + } + if (PS3.getButtonClick(TRIANGLE)) { + Serial.print(F("\r\nTriangle")); + PS3.moveSetBulb(Red); + } + if (PS3.getButtonClick(CIRCLE)) { + Serial.print(F("\r\nCircle")); + PS3.moveSetBulb(Green); + } + if (PS3.getButtonClick(SQUARE)) { + Serial.print(F("\r\nSquare")); + PS3.moveSetBulb(Blue); + } + if (PS3.getButtonClick(CROSS)) { + Serial.print(F("\r\nCross")); + PS3.moveSetBulb(Yellow); + } + if (PS3.getButtonClick(MOVE)) { + PS3.moveSetBulb(Off); + Serial.print(F("\r\nMove")); + Serial.print(F(" - ")); + PS3.printStatusString(); + } + } + if (printAngle) { + Serial.print(F("\r\nPitch: ")); + Serial.print(PS3.getAngle(Pitch)); + Serial.print(F("\tRoll: ")); + Serial.print(PS3.getAngle(Roll)); + } + else if (printTemperature) { + Serial.print(F("\r\nTemperature: ")); + Serial.print(PS3.getTemperature()); + } + } +#endif +} diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/PS3Multi/PS3Multi.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/PS3Multi/PS3Multi.ino new file mode 100644 index 0000000000..5ebfd7819c --- /dev/null +++ b/lib/usbhost/USB_Host_Shield_2.0/examples/Bluetooth/PS3Multi/PS3Multi.ino @@ -0,0 +1,149 @@ +/* + Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus + This example show how one can use multiple controllers with the library + For more information visit my blog: http://blog.tkjelectronics.dk/ or + send me an e-mail: kristianl@tkjelectronics.com + */ + +#include <PS3BT.h> +#include <usbhub.h> + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +//USBHub Hub1(&Usb); // Some dongles have a hub inside + +BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so +PS3BT *PS3[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM! +const uint8_t length = sizeof(PS3) / sizeof(PS3[0]); // Get the lenght of the array +bool printAngle[length]; +bool oldControllerState[length]; + +void setup() { + for (uint8_t i = 0; i < length; i++) { + PS3[i] = new PS3BT(&Btd); // Create the instances + PS3[i]->attachOnInit(onInit); // onInit() is called upon a new connection - you can call the function whatever you like + } + + Serial.begin(115200); +#if !defined(__MIPSEL__) + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection +#endif + if (Usb.Init() == -1) { + Serial.print(F("\r\nOSC did not start")); + while (1); //halt + } + Serial.print(F("\r\nPS3 Bluetooth Library Started")); +} +void loop() { + Usb.Task(); + + for (uint8_t i = 0; i < length; i++) { + if (PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) { + if (PS3[i]->getAnalogHat(LeftHatX) > 137 || PS3[i]->getAnalogHat(LeftHatX) < 117 || PS3[i]->getAnalogHat(LeftHatY) > 137 || PS3[i]->getAnalogHat(LeftHatY) < 117 || PS3[i]->getAnalogHat(RightHatX) > 137 || PS3[i]->getAnalogHat(RightHatX) < 117 || PS3[i]->getAnalogHat(RightHatY) > 137 || PS3[i]->getAnalogHat(RightHatY) < 117) { + Serial.print(F("\r\nLeftHatX: ")); + Serial.print(PS3[i]->getAnalogHat(LeftHatX)); + Serial.print(F("\tLeftHatY: ")); + Serial.print(PS3[i]->getAnalogHat(LeftHatY)); + if (PS3[i]->PS3Connected) { // The Navigation controller only have one joystick + Serial.print(F("\tRightHatX: ")); + Serial.print(PS3[i]->getAnalogHat(RightHatX)); + Serial.print(F("\tRightHatY: ")); + Serial.print(PS3[i]->getAnalogHat(RightHatY)); + } + } + //Analog button values can be read from almost all buttons + if (PS3[i]->getAnalogButton(L2) || PS3[i]->getAnalogButton(R2)) { + Serial.print(F("\r\nL2: ")); + Serial.print(PS3[i]->getAnalogButton(L2)); + if (PS3[i]->PS3Connected) { + Serial.print(F("\tR2: ")); + Serial.print(PS3[i]->getAnalogButton(R2)); + } + } + if (PS3[i]->getButtonClick(PS)) { + Serial.print(F("\r\nPS")); + PS3[i]->disconnect(); + oldControllerState[i] = false; // Reset value + } + else { + if (PS3[i]->getButtonClick(TRIANGLE)) + Serial.print(F("\r\nTraingle")); + if (PS3[i]->getButtonClick(CIRCLE)) + Serial.print(F("\r\nCircle")); + if (PS3[i]->getButtonClick(CROSS)) + Serial.print(F("\r\nCross")); + if (PS3[i]->getButtonClick(SQUARE)) + Serial.print(F("\r\nSquare")); + + if (PS3[i]->getButtonClick(UP)) { + Serial.print(F("\r\nUp")); + if (PS3[i]->PS3Connected) { + PS3[i]->setLedOff(); + PS3[i]->setLedOn(LED4); + } + } + if (PS3[i]->getButtonClick(RIGHT)) { + Serial.print(F("\r\nRight")); + if (PS3[i]->PS3Connected) { + PS3[i]->setLedOff(); + PS3 |