diff options
author | Jack Humbert <jack.humb@gmail.com> | 2015-10-27 14:52:18 -0400 |
---|---|---|
committer | Jack Humbert <jack.humb@gmail.com> | 2015-10-27 14:52:18 -0400 |
commit | d311fd8d1a0af3b767d26b520d399bf0db22f058 (patch) | |
tree | b9de5704143ac205b4e26222f9d7551e9b161924 /tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples | |
parent | a766918d5c48204375f4c207b30bbbf1389df14f (diff) | |
parent | 6f3141965e88c4ee0ebf7c18a243e4c2d9c5021b (diff) |
Merge pull request #34 from jackhumbert/tmk-master
merging from tmk, new_project script and template
Diffstat (limited to 'tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples')
58 files changed, 6567 insertions, 0 deletions
diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/BTHID.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/BTHID.ino new file mode 100644 index 0000000000..919a56468b --- /dev/null +++ b/tmk_core/protocol/usb_hid/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/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/KeyboardParser.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/KeyboardParser.h new file mode 100644 index 0000000000..c5394331da --- /dev/null +++ b/tmk_core/protocol/usb_hid/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/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/MouseParser.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/BTHID/MouseParser.h new file mode 100644 index 0000000000..a9245ded99 --- /dev/null +++ b/tmk_core/protocol/usb_hid/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/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3BT/PS3BT.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3BT/PS3BT.ino new file mode 100644 index 0000000000..b896734405 --- /dev/null +++ b/tmk_core/protocol/usb_hid/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/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3Multi/PS3Multi.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3Multi/PS3Multi.ino new file mode 100644 index 0000000000..5ebfd7819c --- /dev/null +++ b/tmk_core/protocol/usb_hid/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[i]->setLedOn(LED1); + } + } + if (PS3[i]->getButtonClick(DOWN)) { + Serial.print(F("\r\nDown")); + if (PS3[i]->PS3Connected) { + PS3[i]->setLedOff(); + PS3[i]->setLedOn(LED2); + } + } + if (PS3[i]->getButtonClick(LEFT)) { + Serial.print(F("\r\nLeft")); + if (PS3[i]->PS3Connected) { + PS3[i]->setLedOff(); + PS3[i]->setLedOn(LED3); + } + } + + if (PS3[i]->getButtonClick(L1)) + Serial.print(F("\r\nL1")); + if (PS3[i]->getButtonClick(L3)) + Serial.print(F("\r\nL3")); + if (PS3[i]->getButtonClick(R1)) + Serial.print(F("\r\nR1")); + if (PS3[i]->getButtonClick(R3)) + Serial.print(F("\r\nR3")); + + if (PS3[i]->getButtonClick(SELECT)) { + Serial.print(F("\r\nSelect - ")); + PS3[i]->printStatusString(); + } + if (PS3[i]->getButtonClick(START)) { + Serial.print(F("\r\nStart")); + printAngle[i] = !printAngle[i]; + } + } + if (printAngle[i]) { + Serial.print(F("\r\nPitch: ")); + Serial.print(PS3[i]->getAngle(Pitch)); + Serial.print(F("\tRoll: ")); + Serial.print(PS3[i]->getAngle(Roll)); + } + } + /* I have removed the PS3 Move code as an Uno will run out of RAM if it's included */ + //else if(PS3[i]->PS3MoveConnected) { + } +} + +void onInit() { + for (uint8_t i = 0; i < length; i++) { + if ((PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) && !oldControllerState[i]) { + oldControllerState[i] = true; // Used to check which is the new controller + PS3[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h" + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3SPP/PS3SPP.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3SPP/PS3SPP.ino new file mode 100644 index 0000000000..8f234cbd8d --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS3SPP/PS3SPP.ino @@ -0,0 +1,162 @@ +/* + Example sketch for the 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 + + This example show how one can combine all the difference Bluetooth services in one single code. + Note: + You will need a Arduino Mega 1280/2560 to run this sketch, + as a normal Arduino (Uno, Duemilanove etc.) doesn't have enough SRAM and FLASH + */ + +#include <PS3BT.h> +#include <SPP.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 instances of the bluetooth services in two ways */ +SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "0000" +//SPP SerialBTBT(&Btd,"Lauszus's Arduino","0000"); // You can also set the name and pin like so +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 firstMessage = true; +String output = ""; // We will store the data in this string + +void setup() { + Serial.begin(115200); // This wil lprint the debugging from the libraries +#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\nBluetooth Library Started")); + output.reserve(200); // Reserve 200 bytes for the output string +} +void loop() { + Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well + + if (SerialBT.connected) { + if (firstMessage) { + firstMessage = false; + SerialBT.println(F("Hello from Arduino")); // Send welcome message + } + if (Serial.available()) + SerialBT.write(Serial.read()); + if (SerialBT.available()) + Serial.write(SerialBT.read()); + } + else + firstMessage = true; + + if (PS3.PS3Connected || PS3.PS3NavigationConnected) { + output = ""; // Reset output string + 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) { + output += "LeftHatX: "; + output += PS3.getAnalogHat(LeftHatX); + output += "\tLeftHatY: "; + output += PS3.getAnalogHat(LeftHatY); + if (PS3.PS3Connected) { // The Navigation controller only have one joystick + output += "\tRightHatX: "; + output += PS3.getAnalogHat(RightHatX); + output += "\tRightHatY: "; + output += PS3.getAnalogHat(RightHatY); + } + } + //Analog button values can be read from almost all buttons + if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) { + if (output != "") + output += "\r\n"; + output += "L2: "; + output += PS3.getAnalogButton(L2); + if (PS3.PS3Connected) { + output += "\tR2: "; + output += PS3.getAnalogButton(R2); + } + } + if (output != "") { + Serial.println(output); + if (SerialBT.connected) + SerialBT.println(output); + output = ""; // Reset output string + } + if (PS3.getButtonClick(PS)) { + output += " - PS"; + PS3.disconnect(); + } + else { + if (PS3.getButtonClick(TRIANGLE)) + output += " - Traingle"; + if (PS3.getButtonClick(CIRCLE)) + output += " - Circle"; + if (PS3.getButtonClick(CROSS)) + output += " - Cross"; + if (PS3.getButtonClick(SQUARE)) + output += " - Square"; + + if (PS3.getButtonClick(UP)) { + output += " - Up"; + if (PS3.PS3Connected) { + PS3.setLedOff(); + PS3.setLedOn(LED4); + } + } + if (PS3.getButtonClick(RIGHT)) { + output += " - Right"; + if (PS3.PS3Connected) { + PS3.setLedOff(); + PS3.setLedOn(LED1); + } + } + if (PS3.getButtonClick(DOWN)) { + output += " - Down"; + if (PS3.PS3Connected) { + PS3.setLedOff(); + PS3.setLedOn(LED2); + } + } + if (PS3.getButtonClick(LEFT)) { + output += " - Left"; + if (PS3.PS3Connected) { + PS3.setLedOff(); + PS3.setLedOn(LED3); + } + } + + if (PS3.getButtonClick(L1)) + output += " - L1"; + if (PS3.getButtonClick(L3)) + output += " - L3"; + if (PS3.getButtonClick(R1)) + output += " - R1"; + if (PS3.getButtonClick(R3)) + output += " - R3"; + + if (PS3.getButtonClick(SELECT)) { + output += " - Select"; + } + if (PS3.getButtonClick(START)) + output += " - Start"; + + if (output != "") { + String string = "PS3 Controller" + output; + Serial.println(string); + if (SerialBT.connected) + SerialBT.println(string); + } + } + delay(10); + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS4BT/PS4BT.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS4BT/PS4BT.ino new file mode 100644 index 0000000000..c3ba696bd1 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/PS4BT/PS4BT.ino @@ -0,0 +1,146 @@ +/* + Example sketch for the PS4 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 <PS4BT.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 PS4BT class in two ways */ +// This will start an inquiry and then pair with the PS4 controller - you only have to do this once +// You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in paring mode +PS4BT PS4(&Btd, PAIR); + +// After that you can simply create the instance like so and then press the PS button on the device +//PS4BT PS4(&Btd); + +bool printAngle, printTouch; +uint8_t oldL2Value, oldR2Value; + +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\nPS4 Bluetooth Library Started")); +} +void loop() { + Usb.Task(); + + if (PS4.connected()) { + if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) < 117 || PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117 || PS4.getAnalogHat(RightHatX) > 137 || PS4.getAnalogHat(RightHatX) < 117 || PS4.getAnalogHat(RightHatY) > 137 || PS4.getAnalogHat(RightHatY) < 117) { + Serial.print(F("\r\nLeftHatX: ")); + Serial.print(PS4.getAnalogHat(LeftHatX)); + Serial.print(F("\tLeftHatY: ")); + Serial.print(PS4.getAnalogHat(LeftHatY)); + Serial.print(F("\tRightHatX: ")); + Serial.print(PS4.getAnalogHat(RightHatX)); + Serial.print(F("\tRightHatY: ")); + Serial.print(PS4.getAnalogHat(RightHatY)); + } + + if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller + Serial.print(F("\r\nL2: ")); + Serial.print(PS4.getAnalogButton(L2)); + Serial.print(F("\tR2: ")); + Serial.print(PS4.getAnalogButton(R2)); + } + if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different + PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2)); + oldL2Value = PS4.getAnalogButton(L2); + oldR2Value = PS4.getAnalogButton(R2); + + if (PS4.getButtonClick(PS)) { + Serial.print(F("\r\nPS")); + PS4.disconnect(); + } + else { + if (PS4.getButtonClick(TRIANGLE)) { + Serial.print(F("\r\nTraingle")); + PS4.setRumbleOn(RumbleLow); + } + if (PS4.getButtonClick(CIRCLE)) { + Serial.print(F("\r\nCircle")); + PS4.setRumbleOn(RumbleHigh); + } + if (PS4.getButtonClick(CROSS)) { + Serial.print(F("\r\nCross")); + PS4.setLedFlash(10, 10); // Set it to blink rapidly + } + if (PS4.getButtonClick(SQUARE)) { + Serial.print(F("\r\nSquare")); + PS4.setLedFlash(0, 0); // Turn off blinking + } + + if (PS4.getButtonClick(UP)) { + Serial.print(F("\r\nUp")); + PS4.setLed(Red); + } if (PS4.getButtonClick(RIGHT)) { + Serial.print(F("\r\nRight")); + PS4.setLed(Blue); + } if (PS4.getButtonClick(DOWN)) { + Serial.print(F("\r\nDown")); + PS4.setLed(Yellow); + } if (PS4.getButtonClick(LEFT)) { + Serial.print(F("\r\nLeft")); + PS4.setLed(Green); + } + + if (PS4.getButtonClick(L1)) + Serial.print(F("\r\nL1")); + if (PS4.getButtonClick(L3)) + Serial.print(F("\r\nL3")); + if (PS4.getButtonClick(R1)) + Serial.print(F("\r\nR1")); + if (PS4.getButtonClick(R3)) + Serial.print(F("\r\nR3")); + + if (PS4.getButtonClick(SHARE)) + Serial.print(F("\r\nShare")); + if (PS4.getButtonClick(OPTIONS)) { + Serial.print(F("\r\nOptions")); + printAngle = !printAngle; + } + if (PS4.getButtonClick(TOUCHPAD)) { + Serial.print(F("\r\nTouchpad")); + printTouch = !printTouch; + } + + if (printAngle) { // Print angle calculated using the accelerometer only + Serial.print(F("\r\nPitch: ")); + Serial.print(PS4.getAngle(Pitch)); + Serial.print(F("\tRoll: ")); + Serial.print(PS4.getAngle(Roll)); + } + + if (printTouch) { // Print the x, y coordinates of the touchpad + if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad + Serial.print(F("\r\n")); + for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers + if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad + Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": ")); + Serial.print(PS4.getX(i)); + Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": ")); + Serial.print(PS4.getY(i)); + Serial.print(F("\t")); + } + } + } + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/SPP/SPP.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/SPP/SPP.ino new file mode 100644 index 0000000000..8fb9c4eca2 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/SPP/SPP.ino @@ -0,0 +1,52 @@ +/* + Example sketch for the RFCOMM/SPP 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 <SPP.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 */ +SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "0000" +//SPP SerialBT(&Btd, "Lauszus's Arduino", "1234"); // You can also set the name and pin like so + +bool firstMessage = true; + +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\nSPP Bluetooth Library Started")); +} +void loop() { + Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well + + if (SerialBT.connected) { + if (firstMessage) { + firstMessage = false; + SerialBT.println(F("Hello from Arduino")); // Send welcome message + } + if (Serial.available()) + SerialBT.write(Serial.read()); + if (SerialBT.available()) + Serial.write(SerialBT.read()); + } + else + firstMessage = true; +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/SPPMulti/SPPMulti.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/SPPMulti/SPPMulti.ino new file mode 100644 index 0000000000..df521d8e17 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/SPPMulti/SPPMulti.ino @@ -0,0 +1,67 @@ +/* + Example sketch for the RFCOMM/SPP 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 <SPP.h> +#include <usbhub.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#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 + +const uint8_t length = 2; // Set the number of instances here +SPP *SerialBT[length]; // We will use this pointer to store the instances, you can easily make it larger if you like, but it will use a lot of RAM! + +bool firstMessage[length] = { true }; // Set all to true + +void setup() { + for (uint8_t i = 0; i < length; i++) + SerialBT[i] = new SPP(&Btd); // This will set the name to the default: "Arduino" and the pin to "0000" for all connections + + 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\nSPP Bluetooth Library Started")); +} + +void loop() { + Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well + + for (uint8_t i = 0; i < length; i++) { + if (SerialBT[i]->connected) { + if (firstMessage[i]) { + firstMessage[i] = false; + SerialBT[i]->println(F("Hello from Arduino")); // Send welcome message + } + if (SerialBT[i]->available()) + Serial.write(SerialBT[i]->read()); + } + else + firstMessage[i] = true; + } + + // Set the connection you want to send to using the first character + // For instance "0Hello World" would send "Hello World" to connection 0 + if (Serial.available()) { + delay(10); // Wait for the rest of the data to arrive + uint8_t id = Serial.read() - '0'; // Convert from ASCII + if (id < length && SerialBT[id]->connected) { // Make sure that the id is valid and make sure that a device is actually connected + while (Serial.available()) // Check if data is available + SerialBT[id]->write(Serial.read()); // Send the data + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/Wii/Wii.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/Wii/Wii.ino new file mode 100644 index 0000000000..b193568163 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/Wii/Wii.ino @@ -0,0 +1,118 @@ +/* + Example sketch for the Wiimote 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 <Wii.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 */ +WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once +//WII Wii(&Btd); // After that you can simply create the instance like so and then press any button on the Wiimote + +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\nWiimote Bluetooth Library Started")); +} +void loop() { + Usb.Task(); + if (Wii.wiimoteConnected) { + if (Wii.getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down + Serial.print(F("\r\nHOME")); + Wii.disconnect(); + } + else { + if (Wii.getButtonClick(LEFT)) { + Wii.setLedOff(); + Wii.setLedOn(LED1); + Serial.print(F("\r\nLeft")); + } + if (Wii.getButtonClick(RIGHT)) { + Wii.setLedOff(); + Wii.setLedOn(LED3); + Serial.print(F("\r\nRight")); + } + if (Wii.getButtonClick(DOWN)) { + Wii.setLedOff(); + Wii.setLedOn(LED4); + Serial.print(F("\r\nDown")); + } + if (Wii.getButtonClick(UP)) { + Wii.setLedOff(); + Wii.setLedOn(LED2); + Serial.print(F("\r\nUp")); + } + + if (Wii.getButtonClick(PLUS)) + Serial.print(F("\r\nPlus")); + if (Wii.getButtonClick(MINUS)) + Serial.print(F("\r\nMinus")); + + if (Wii.getButtonClick(ONE)) + Serial.print(F("\r\nOne")); + if (Wii.getButtonClick(TWO)) + Serial.print(F("\r\nTwo")); + + if (Wii.getButtonClick(A)) { + printAngle = !printAngle; + Serial.print(F("\r\nA")); + } + if (Wii.getButtonClick(B)) { + Wii.setRumbleToggle(); + Serial.print(F("\r\nB")); + } + } +#if 0 // Set this to 1 in order to see the angle of the controllers + if (printAngle) { + Serial.print(F("\r\nPitch: ")); + Serial.print(Wii.getPitch()); + Serial.print(F("\tRoll: ")); + Serial.print(Wii.getRoll()); + if (Wii.motionPlusConnected) { + Serial.print(F("\tYaw: ")); + Serial.print(Wii.getYaw()); + } + if (Wii.nunchuckConnected) { + Serial.print(F("\tNunchuck Pitch: ")); + Serial.print(Wii.getNunchuckPitch()); + Serial.print(F("\tNunchuck Roll: ")); + Serial.print(Wii.getNunchuckRoll()); + } + } +#endif + } +#if 0 // Set this to 1 if you are using a Nunchuck controller + if (Wii.nunchuckConnected) { + if (Wii.getButtonClick(Z)) + Serial.print(F("\r\nZ")); + if (Wii.getButtonClick(C)) + Serial.print(F("\r\nC")); + if (Wii.getAnalogHat(HatX) > 137 || Wii.getAnalogHat(HatX) < 117 || Wii.getAnalogHat(HatY) > 137 || Wii.getAnalogHat(HatY) < 117) { + Serial.print(F("\r\nHatX: ")); + Serial.print(Wii.getAnalogHat(HatX)); + Serial.print(F("\tHatY: ")); + Serial.print(Wii.getAnalogHat(HatY)); + } + } +#endif +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiBalanceBoard/WiiBalanceBoard.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiBalanceBoard/WiiBalanceBoard.ino new file mode 100644 index 0000000000..18c5b411ef --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiBalanceBoard/WiiBalanceBoard.ino @@ -0,0 +1,51 @@ +/* + Example sketch for the Wii Balance Board 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 <Wii.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 */ +WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wii Balance Board - you only have to do this once +//WII Wii(&Btd); // After that you can simply create the instance like so and then press the power button on the Wii Balance Board + +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\nWii Balance Board Bluetooth Library Started")); +} +void loop() { + Usb.Task(); + if (Wii.wiiBalanceBoardConnected) { + Serial.print(F("\r\nWeight: ")); + for (uint8_t i = 0; i < 4; i++) { + Serial.print(Wii.getWeight((BalanceBoardEnum)i)); + Serial.print(F("\t")); + } + Serial.print(F("Total Weight: ")); + Serial.print(Wii.getTotalWeight()); + if (Wii.getButtonClick(A)) { + Serial.print(F("\r\nA")); + //Wii.setLedToggle(LED1); // The Wii Balance Board has one LED as well + Wii.disconnect(); + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiIRCamera/WiiIRCamera.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiIRCamera/WiiIRCamera.ino new file mode 100644 index 0000000000..573b8bd48c --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiIRCamera/WiiIRCamera.ino @@ -0,0 +1,133 @@ +/* +Example sketch for the Wii libary showing the IR camera functionality. This example +is for the Bluetooth Wii library developed for the USB shield from Circuits@Home + +Created by Allan Glover and Kristian Lauszus. +Contact Kristian: http://blog.tkjelectronics.dk/ or send an email at kristianl@tkjelectronics.com. +Contact Allan at adglover9.81@gmail.com + +To test the Wiimote IR camera, you will need access to an IR source. Sunlight will work but is not ideal. +The simpleist solution is to use the Wii sensor bar, i.e. emitter bar, supplied by the Wii system. +Otherwise, wire up a IR LED yourself. +*/ + +#include <Wii.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 + +#ifndef WIICAMERA // Used to check if WIICAMERA is defined +#error "Please set ENABLE_WII_IR_CAMERA to 1 in settings.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 */ +WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once +//WII Wii(&Btd); // After the Wiimote pairs once with the line of code above, you can simply create the instance like so and re upload and then press any button on the Wiimote + +bool printAngle; +uint8_t printObjects; + +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\nWiimote Bluetooth Library Started")); +} + +void loop() { + Usb.Task(); + if (Wii.wiimoteConnected) { + if (Wii.getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down + Serial.print(F("\r\nHOME")); + Wii.disconnect(); + } + else { + if (Wii.getButtonClick(ONE)) + Wii.IRinitialize(); // Run the initialisation sequence + if (Wii.getButtonClick(MINUS) || Wii.getButtonClick(PLUS)) { + if (!Wii.isIRCameraEnabled()) + Serial.print(F("\r\nEnable IR camera first")); + else { + if (Wii.getButtonPress(MINUS)) { // getButtonClick will only return true once + if (printObjects > 0) + printObjects--; + } + else { + if (printObjects < 4) + printObjects++; + } + Serial.print(F("\r\nTracking ")); + Serial.print(printObjects); + Serial.print(F(" objects")); + } + } + if (Wii.getButtonClick(A)) { + printAngle = !printAngle; + Serial.print(F("\r\nA")); + } + if (Wii.getButtonClick(B)) { + Serial.print(F("\r\nBattery level: ")); + Serial.print(Wii.getBatteryLevel()); // You can get the battery level as well + } + } + if (printObjects > 0) { + if (Wii.getIRx1() != 0x3FF || Wii.getIRy1() != 0x3FF || Wii.getIRs1() != 0) { // Only print if the IR camera is actually seeing something + Serial.print(F("\r\nx1: ")); + Serial.print(Wii.getIRx1()); + Serial.print(F("\ty1: ")); + Serial.print(Wii.getIRy1()); + Serial.print(F("\ts1:")); + Serial.print(Wii.getIRs1()); + } + if (printObjects > 1) { + if (Wii.getIRx2() != 0x3FF || Wii.getIRy2() != 0x3FF || Wii.getIRs2() != 0) { + Serial.print(F("\r\nx2: ")); + Serial.print(Wii.getIRx2()); + Serial.print(F("\ty2: ")); + Serial.print(Wii.getIRy2()); + Serial.print(F("\ts2:")); + Serial.print(Wii.getIRs2()); + } + if (printObjects > 2) { + if (Wii.getIRx3() != 0x3FF || Wii.getIRy3() != 0x3FF || Wii.getIRs3() != 0) { + Serial.print(F("\r\nx3: ")); + Serial.print(Wii.getIRx3()); + Serial.print(F("\ty3: ")); + Serial.print(Wii.getIRy3()); + Serial.print(F("\ts3:")); + Serial.print(Wii.getIRs3()); + } + if (printObjects > 3) { + if (Wii.getIRx4() != 0x3FF || Wii.getIRy4() != 0x3FF || Wii.getIRs4() != 0) { + Serial.print(F("\r\nx4: ")); + Serial.print(Wii.getIRx4()); + Serial.print(F("\ty4: ")); + Serial.print(Wii.getIRy4()); + Serial.print(F("\ts4:")); + Serial.print(Wii.getIRs4()); + } + } + } + } + } + if (printAngle) { // There is no extension bytes available, so the MotionPlus or Nunchuck can't be read + Serial.print(F("\r\nPitch: ")); + Serial.print(Wii.getPitch()); + Serial.print(F("\tRoll: ")); + Serial.print(Wii.getRoll()); + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino new file mode 100644 index 0000000000..07c6f13d2b --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiMulti/WiiMulti.ino @@ -0,0 +1,132 @@ +/* + Example sketch for the Wiimote 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 <Wii.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 +WII *Wii[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(Wii) / sizeof(Wii[0]); // Get the lenght of the array +bool printAngle[length]; +bool oldControllerState[length]; + +void setup() { + for (uint8_t i = 0; i < length; i++) { + Wii[i] = new WII(&Btd); // You will have to pair each controller with the dongle before you can define the instances like so, just add PAIR as the second argument + Wii[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\nWiimote Bluetooth Library Started")); +} +void loop() { + Usb.Task(); + + for (uint8_t i = 0; i < length; i++) { + if (Wii[i]->wiimoteConnected) { + if (Wii[i]->getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down + Serial.print(F("\r\nHOME")); + Wii[i]->disconnect(); + oldControllerState[i] = false; // Reset value + } + else { + if (Wii[i]->getButtonClick(LEFT)) { + Wii[i]->setLedOff(); + Wii[i]->setLedOn(LED1); + Serial.print(F("\r\nLeft")); + } + if (Wii[i]->getButtonClick(RIGHT)) { + Wii[i]->setLedOff(); + Wii[i]->setLedOn(LED3); + Serial.print(F("\r\nRight")); + } + if (Wii[i]->getButtonClick(DOWN)) { + Wii[i]->setLedOff(); + Wii[i]->setLedOn(LED4); + Serial.print(F("\r\nDown")); + } + if (Wii[i]->getButtonClick(UP)) { + Wii[i]->setLedOff(); + Wii[i]->setLedOn(LED2); + Serial.print(F("\r\nUp")); + } + + if (Wii[i]->getButtonClick(PLUS)) + Serial.print(F("\r\nPlus")); + if (Wii[i]->getButtonClick(MINUS)) + Serial.print(F("\r\nMinus")); + + if (Wii[i]->getButtonClick(ONE)) + Serial.print(F("\r\nOne")); + if (Wii[i]->getButtonClick(TWO)) + Serial.print(F("\r\nTwo")); + + if (Wii[i]->getButtonClick(A)) { + printAngle[i] = !printAngle[i]; + Serial.print(F("\r\nA")); + } + if (Wii[i]->getButtonClick(B)) { + Wii[i]->setRumbleToggle(); + Serial.print(F("\r\nB")); + } + } + if (printAngle[i]) { + Serial.print(F("\r\nPitch: ")); + Serial.print(Wii[i]->getPitch()); + Serial.print(F("\tRoll: ")); + Serial.print(Wii[i]->getRoll()); + if (Wii[i]->motionPlusConnected) { + Serial.print(F("\tYaw: ")); + Serial.print(Wii[i]->getYaw()); + } + if (Wii[i]->nunchuckConnected) { + Serial.print(F("\tNunchuck Pitch: ")); + Serial.print(Wii[i]->getNunchuckPitch()); + Serial.print(F("\tNunchuck Roll: ")); + Serial.print(Wii[i]->getNunchuckRoll()); + } + } + } + if (Wii[i]->nunchuckConnected) { + if (Wii[i]->getButtonClick(Z)) + Serial.print(F("\r\nZ")); + if (Wii[i]->getButtonClick(C)) + Serial.print(F("\r\nC")); + if (Wii[i]->getAnalogHat(HatX) > 137 || Wii[i]->getAnalogHat(HatX) < 117 || Wii[i]->getAnalogHat(HatY) > 137 || Wii[i]->getAnalogHat(HatY) < 117) { + Serial.print(F("\r\nHatX: ")); + Serial.print(Wii[i]->getAnalogHat(HatX)); + Serial.print(F("\tHatY: ")); + Serial.print(Wii[i]->getAnalogHat(HatY)); + } + } + } +} + +void onInit() { + for (uint8_t i = 0; i < length; i++) { + if (Wii[i]->wiimoteConnected && !oldControllerState[i]) { + oldControllerState[i] = true; // Used to check which is the new controller + Wii[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h" + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiUProController/WiiUProController.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiUProController/WiiUProController.ino new file mode 100644 index 0000000000..ab35a27479 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/WiiUProController/WiiUProController.ino @@ -0,0 +1,104 @@ +/* + Example sketch for the Wiimote 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 <Wii.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 */ +WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once +//WII Wii(&Btd); // After that you can simply create the instance like so and then press any button on the Wiimote + +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\nWiimote Bluetooth Library Started")); +} +void loop() { + Usb.Task(); + if (Wii.wiiUProControllerConnected) { + if (Wii.getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down + Serial.print(F("\r\nHome")); + Wii.disconnect(); + } + else { + if (Wii.getButtonClick(LEFT)) { + Wii.setLedOff(); + Wii.setLedOn(LED1); + Serial.print(F("\r\nLeft")); + } + if (Wii.getButtonClick(RIGHT)) { + Wii.setLedOff(); + Wii.setLedOn(LED3); + Serial.print(F("\r\nRight")); + } + if (Wii.getButtonClick(DOWN)) { + Wii.setLedOff(); + Wii.setLedOn(LED4); + Serial.print(F("\r\nDown")); + } + if (Wii.getButtonClick(UP)) { + Wii.setLedOff(); + Wii.setLedOn(LED2); + Serial.print(F("\r\nUp")); + } + + if (Wii.getButtonClick(PLUS)) + Serial.print(F("\r\nPlus")); + if (Wii.getButtonClick(MINUS)) + Serial.print(F("\r\nMinus")); + + if (Wii.getButtonClick(A)) + Serial.print(F("\r\nA")); + if (Wii.getButtonClick(B)) { + Wii.setRumbleToggle(); + Serial.print(F("\r\nB")); + } + if (Wii.getButtonClick(X)) + Serial.print(F("\r\nX")); + if (Wii.getButtonClick(Y)) + Serial.print(F("\r\nY")); + + if (Wii.getButtonClick(L)) + Serial.print(F("\r\nL")); + if (Wii.getButtonClick(R)) + Serial.print(F("\r\nR")); + if (Wii.getButtonClick(ZL)) + Serial.print(F("\r\nZL")); + if (Wii.getButtonClick(ZR)) + Serial.print(F("\r\nZR")); + if (Wii.getButtonClick(L3)) + Serial.print(F("\r\nL3")); + if (Wii.getButtonClick(R3)) + Serial.print(F("\r\nR3")); + } + if (Wii.getAnalogHat(LeftHatX) > 2200 || Wii.getAnalogHat(LeftHatX) < 1800 || Wii.getAnalogHat(LeftHatY) > 2200 || Wii.getAnalogHat(LeftHatY) < 1800 || Wii.getAnalogHat(RightHatX) > 2200 || Wii.getAnalogHat(RightHatX) < 1800 || Wii.getAnalogHat(RightHatY) > 2200 || Wii.getAnalogHat(RightHatY) < 1800) { + Serial.print(F("\r\nLeftHatX: ")); + Serial.print(Wii.getAnalogHat(LeftHatX)); + Serial.print(F("\tLeftHatY: ")); + Serial.print(Wii.getAnalogHat(LeftHatY)); + Serial.print(F("\tRightHatX: ")); + Serial.print(Wii.getAnalogHat(RightHatX)); + Serial.print(F("\tRightHatY: ")); + Serial.print(Wii.getAnalogHat(RightHatY)); + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino new file mode 100644 index 0000000000..48b33abfd2 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino @@ -0,0 +1,129 @@ +#include <hidboot.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 + +class KbdRptParser : public KeyboardReportParser +{ + void PrintKey(uint8_t mod, uint8_t key); + + protected: + void OnControlKeysChanged(uint8_t before, uint8_t after); + + void OnKeyDown (uint8_t mod, uint8_t key); + void OnKeyUp (uint8_t mod, uint8_t key); + void OnKeyPressed(uint8_t key); +}; + +void KbdRptParser::PrintKey(uint8_t m, uint8_t key) +{ + MODIFIERKEYS mod; + *((uint8_t*)&mod) = m; + Serial.print((mod.bmLeftCtrl == 1) ? "C" : " "); + Serial.print((mod.bmLeftShift == 1) ? "S" : " "); + Serial.print((mod.bmLeftAlt == 1) ? "A" : " "); + Serial.print((mod.bmLeftGUI == 1) ? "G" : " "); + + Serial.print(" >"); + PrintHex<uint8_t>(key, 0x80); + Serial.print("< "); + + Serial.print((mod.bmRightCtrl == 1) ? "C" : " "); + Serial.print((mod.bmRightShift == 1) ? "S" : " "); + Serial.print((mod.bmRightAlt == 1) ? "A" : " "); + Serial.println((mod.bmRightGUI == 1) ? "G" : " "); +}; + +void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) +{ + Serial.print("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("LeftCtrl changed"); + } + if (beforeMod.bmLeftShift != afterMod.bmLeftShift) { + Serial.println("LeftShift changed"); + } + if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) { + Serial.println("LeftAlt changed"); + } + if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) { + Serial.println("LeftGUI changed"); + } + + if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) { + Serial.println("RightCtrl changed"); + } + if (beforeMod.bmRightShift != afterMod.bmRightShift) { + Serial.println("RightShift changed"); + } + if (beforeMod.bmRightAlt != afterMod.bmRightAlt) { + Serial.println("RightAlt changed"); + } + if (beforeMod.bmRightGUI != afterMod.bmRightGUI) { + Serial.println("RightGUI changed"); + } + +} + +void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) +{ + Serial.print("UP "); + PrintKey(mod, key); +} + +void KbdRptParser::OnKeyPressed(uint8_t key) +{ + Serial.print("ASCII: "); + Serial.println((char)key); +}; + +USB Usb; +//USBHub Hub(&Usb); +HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb); + +uint32_t next_time; + +KbdRptParser Prs; + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + next_time = millis() + 5000; + + HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs); +} + +void loop() +{ + Usb.Task(); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino new file mode 100644 index 0000000000..5fc8c96fc9 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino @@ -0,0 +1,178 @@ +#include <hidboot.h> +#include <usbhub.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +class MouseRptParser : public MouseReportParser +{ + protected: + void OnMouseMove(MOUSEINFO *mi); + void OnLeftButtonUp(MOUSEINFO *mi); + void OnLeftButtonDown(MOUSEINFO *mi); + void OnRightButtonUp(MOUSEINFO *mi); + void OnRightButtonDown(MOUSEINFO *mi); + void OnMiddleButtonUp(MOUSEINFO *mi); + void OnMiddleButtonDown(MOUSEINFO *mi); +}; +void MouseRptParser::OnMouseMove(MOUSEINFO *mi) +{ + Serial.print("dx="); + Serial.print(mi->dX, DEC); + Serial.print(" dy="); + Serial.println(mi->dY, DEC); +}; +void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi) +{ + Serial.println("L Butt Up"); +}; +void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi) +{ + Serial.println("L Butt Dn"); +}; +void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi) +{ + Serial.println("R Butt Up"); +}; +void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi) +{ + Serial.println("R Butt Dn"); +}; +void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi) +{ + Serial.println("M Butt Up"); +}; +void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) +{ + Serial.println("M Butt Dn"); +}; + +class KbdRptParser : public KeyboardReportParser +{ + void PrintKey(uint8_t mod, uint8_t key); + + protected: + void OnControlKeysChanged(uint8_t before, uint8_t after); + void OnKeyDown (uint8_t mod, uint8_t key); + void OnKeyUp (uint8_t mod, uint8_t key); + void OnKeyPressed(uint8_t key); +}; + +void KbdRptParser::PrintKey(uint8_t m, uint8_t key) +{ + MODIFIERKEYS mod; + *((uint8_t*)&mod) = m; + Serial.print((mod.bmLeftCtrl == 1) ? "C" : " "); + Serial.print((mod.bmLeftShift == 1) ? "S" : " "); + Serial.print((mod.bmLeftAlt == 1) ? "A" : " "); + Serial.print((mod.bmLeftGUI == 1) ? "G" : " "); + + Serial.print(" >"); + PrintHex<uint8_t>(key, 0x80); + Serial.print("< "); + + Serial.print((mod.bmRightCtrl == 1) ? "C" : " "); + Serial.print((mod.bmRightShift == 1) ? "S" : " "); + Serial.print((mod.bmRightAlt == 1) ? "A" : " "); + Serial.println((mod.bmRightGUI == 1) ? "G" : " "); +}; + +void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) +{ + Serial.print("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("LeftCtrl changed"); + } + if (beforeMod.bmLeftShift != afterMod.bmLeftShift) { + Serial.println("LeftShift changed"); + } + if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) { + Serial.println("LeftAlt changed"); + } + if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) { + Serial.println("LeftGUI changed"); + } + + if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) { + Serial.println("RightCtrl changed"); + } + if (beforeMod.bmRightShift != afterMod.bmRightShift) { + Serial.println("RightShift changed"); + } + if (beforeMod.bmRightAlt != afterMod.bmRightAlt) { + Serial.println("RightAlt changed"); + } + if (beforeMod.bmRightGUI != afterMod.bmRightGUI) { + Serial.println("RightGUI changed"); + } + +} + +void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) +{ + Serial.print("UP "); + PrintKey(mod, key); +} + +void KbdRptParser::OnKeyPressed(uint8_t key) +{ + Serial.print("ASCII: "); + Serial.println((char)key); +}; + +USB Usb; +USBHub Hub(&Usb); + +HIDBoot < HID_PROTOCOL_KEYBOARD | HID_PROTOCOL_MOUSE > HidComposite(&Usb); +HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb); +HIDBoot<HID_PROTOCOL_MOUSE> HidMouse(&Usb); + +//uint32_t next_time; + +KbdRptParser KbdPrs; +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + //next_time = millis() + 5000; + + HidComposite.SetReportParser(0, (HIDReportParser*)&KbdPrs); + HidComposite.SetReportParser(1, (HIDReportParser*)&MousePrs); + HidKeyboard.SetReportParser(0, (HIDReportParser*)&KbdPrs); + HidMouse.SetReportParser(0, (HIDReportParser*)&MousePrs); +} + +void loop() +{ + Usb.Task(); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootMouse/USBHIDBootMouse.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootMouse/USBHIDBootMouse.ino new file mode 100644 index 0000000000..53102512bd --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootMouse/USBHIDBootMouse.ino @@ -0,0 +1,83 @@ +#include <hidboot.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 + +class MouseRptParser : public MouseReportParser +{ +protected: + void OnMouseMove (MOUSEINFO *mi); + void OnLeftButtonUp (MOUSEINFO *mi); + void OnLeftButtonDown (MOUSEINFO *mi); + void OnRightButtonUp (MOUSEINFO *mi); + void OnRightButtonDown (MOUSEINFO *mi); + void OnMiddleButtonUp (MOUSEINFO *mi); + void OnMiddleButtonDown (MOUSEINFO *mi); +}; +void MouseRptParser::OnMouseMove(MOUSEINFO *mi) +{ + Serial.print("dx="); + Serial.print(mi->dX, DEC); + Serial.print(" dy="); + Serial.println(mi->dY, DEC); +}; +void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi) +{ + Serial.println("L Butt Up"); +}; +void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi) +{ + Serial.println("L Butt Dn"); +}; +void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi) +{ + Serial.println("R Butt Up"); +}; +void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi) +{ + Serial.println("R Butt Dn"); +}; +void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi) +{ + Serial.println("M Butt Up"); +}; +void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) +{ + Serial.println("M Butt Dn"); +}; + +USB Usb; +USBHub Hub(&Usb); +HIDBoot<HID_PROTOCOL_MOUSE> HidMouse(&Usb); + +uint32_t next_time; + +MouseRptParser Prs; + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + next_time = millis() + 5000; + + HidMouse.SetReportParser(0,(HIDReportParser*)&Prs); +} + +void loop() +{ + Usb.Task(); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/USBHIDJoystick.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/USBHIDJoystick.ino new file mode 100644 index 0000000000..956441d67a --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/USBHIDJoystick.ino @@ -0,0 +1,38 @@ +#include <hid.h> +#include <hiduniversal.h> +#include <usbhub.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +#include "hidjoystickrptparser.h" + +USB Usb; +USBHub Hub(&Usb); +HIDUniversal Hid(&Usb); +JoystickEvents JoyEvents; +JoystickReportParser Joy(&JoyEvents); + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay(200); + + if (!Hid.SetReportParser(0, &Joy)) + ErrorMessage<uint8_t > (PSTR("SetReportParser"), 1); +} + +void loop() { + Usb.Task(); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp new file mode 100644 index 0000000000..083b95cac5 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp @@ -0,0 +1,84 @@ +#include "hidjoystickrptparser.h" + +JoystickReportParser::JoystickReportParser(JoystickEvents *evt) : +joyEvents(evt), +oldHat(0xDE), +oldButtons(0) { + for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) + oldPad[i] = 0xD; +} + +void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) { + bool match = true; + + // Checking if there are changes in report since the method was last called + for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) + if (buf[i] != oldPad[i]) { + match = false; + break; + } + + // Calling Game Pad event handler + if (!match && joyEvents) { + joyEvents->OnGamePadChanged((const GamePadEventData*)buf); + + for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i]; + } + + uint8_t hat = (buf[5] & 0xF); + + // Calling Hat Switch event handler + if (hat != oldHat && joyEvents) { + joyEvents->OnHatSwitch(hat); + oldHat = hat; + } + + uint16_t buttons = (0x0000 | buf[6]); + buttons <<= 4; + buttons |= (buf[5] >> 4); + uint16_t changes = (buttons ^ oldButtons); + + // Calling Button Event Handler for every button changed + if (changes) { + for (uint8_t i = 0; i < 0x0C; i++) { + uint16_t mask = (0x0001 << i); + + if (((mask & changes) > 0) && joyEvents) + if ((buttons & mask) > 0) + joyEvents->OnButtonDn(i + 1); + else + joyEvents->OnButtonUp(i + 1); + } + oldButtons = buttons; + } +} + +void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) { + Serial.print("X1: "); + PrintHex<uint8_t > (evt->X, 0x80); + Serial.print("\tY1: "); + PrintHex<uint8_t > (evt->Y, 0x80); + Serial.print("\tX2: "); + PrintHex<uint8_t > (evt->Z1, 0x80); + Serial.print("\tY2: "); + PrintHex<uint8_t > (evt->Z2, 0x80); + Serial.print("\tRz: "); + PrintHex<uint8_t > (evt->Rz, 0x80); + Serial.println(""); +} + +void JoystickEvents::OnHatSwitch(uint8_t hat) { + Serial.print("Hat Switch: "); + PrintHex<uint8_t > (hat, 0x80); + Serial.println(""); +} + +void JoystickEvents::OnButtonUp(uint8_t but_id) { + Serial.print("Up: "); + Serial.println(but_id, DEC); +} + +void JoystickEvents::OnButtonDn(uint8_t but_id) { + Serial.print("Dn: "); + Serial.println(but_id, DEC); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.h new file mode 100644 index 0000000000..733b8f8da8 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.h @@ -0,0 +1,33 @@ +#if !defined(__HIDJOYSTICKRPTPARSER_H__) +#define __HIDJOYSTICKRPTPARSER_H__ + +#include <hid.h> + +struct GamePadEventData { + uint8_t X, Y, Z1, Z2, Rz; +}; + +class JoystickEvents { +public: + virtual void OnGamePadChanged(const GamePadEventData *evt); + virtual void OnHatSwitch(uint8_t hat); + virtual void OnButtonUp(uint8_t but_id); + virtual void OnButtonDn(uint8_t but_id); +}; + +#define RPT_GEMEPAD_LEN 5 + +class JoystickReportParser : public HIDReportParser { + JoystickEvents *joyEvents; + + uint8_t oldPad[RPT_GEMEPAD_LEN]; + uint8_t oldHat; + uint16_t oldButtons; + +public: + JoystickReportParser(JoystickEvents *evt); + + virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); +}; + +#endif // __HIDJOYSTICKRPTPARSER_H__ diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHID_desc/USBHID_desc.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHID_desc/USBHID_desc.ino new file mode 100644 index 0000000000..85cfc19a2e --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHID_desc/USBHID_desc.ino @@ -0,0 +1,77 @@ +#include <hid.h> +#include <hiduniversal.h> +#include <hidescriptorparser.h> +#include <usbhub.h> +#include "pgmstrings.h" + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +class HIDUniversal2 : public HIDUniversal +{ +public: + HIDUniversal2(USB *usb) : HIDUniversal(usb) {}; + +protected: + uint8_t OnInitSuccessful(); +}; + +uint8_t HIDUniversal2::OnInitSuccessful() +{ + uint8_t rcode; + + HexDumper<USBReadParser, uint16_t, uint16_t> Hex; + ReportDescParser Rpt; + + if ((rcode = GetReportDescr(0, &Hex))) + goto FailGetReportDescr1; + + if ((rcode = GetReportDescr(0, &Rpt))) + goto FailGetReportDescr2; + + return 0; + +FailGetReportDescr1: + USBTRACE("GetReportDescr1:"); + goto Fail; + +FailGetReportDescr2: + USBTRACE("GetReportDescr2:"); + goto Fail; + +Fail: + Serial.println(rcode, HEX); + Release(); + return rcode; +} + +USB Usb; +//USBHub Hub(&Usb); +HIDUniversal2 Hid(&Usb); +UniversalReportParser Uni; + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + if (!Hid.SetReportParser(0, &Uni)) + ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 ); +} + +void loop() +{ + Usb.Task(); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHID_desc/pgmstrings.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHID_desc/pgmstrings.h new file mode 100644 index 0000000000..bdb0077ecc --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHID_desc/pgmstrings.h @@ -0,0 +1,52 @@ +#if !defined(__PGMSTRINGS_H__) +#define __PGMSTRINGS_H__ + +#define LOBYTE(x) ((char*)(&(x)))[0] +#define HIBYTE(x) ((char*)(&(x)))[1] +#define BUFSIZE 256 //buffer size + + +/* Print strings in Program Memory */ +const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t"; +const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: "; +const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t"; +const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t"; +const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t"; +const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t"; +const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t"; +const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t"; +const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t"; +const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t"; +const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t"; +const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t"; +const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t"; +const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t"; +const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t"; +const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t"; +const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes"; +const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:"; +const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t"; +const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t"; +const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t"; +const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t"; +const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t"; +const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:"; +const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t"; +const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t"; +const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t"; +const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t"; +const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t"; +const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t"; +const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t"; +const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:"; +const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t"; +const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t"; +const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t"; +const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:"; +const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t"; +const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t"; +const char Unk_Contents_str[] PROGMEM ="\r\nContents:\t"; + +#endif // __PGMSTRINGS_H__
\ No newline at end of file diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp.ino new file mode 100644 index 0000000000..837d7f5a70 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp.ino @@ -0,0 +1,42 @@ +/* Simplified Logitech Extreme 3D Pro Joystick Report Parser */ + +#include <hid.h> +#include <hiduniversal.h> +#include <usbhub.h> + +#include "le3dp_rptparser.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 Hub(&Usb); +HIDUniversal Hid(&Usb); +JoystickEvents JoyEvents; +JoystickReportParser Joy(&JoyEvents); + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + if (!Hid.SetReportParser(0, &Joy)) + ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 ); +} + +void loop() +{ + Usb.Task(); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.cpp b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.cpp new file mode 100644 index 0000000000..baece13b2c --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.cpp @@ -0,0 +1,43 @@ +#include "le3dp_rptparser.h" + +JoystickReportParser::JoystickReportParser(JoystickEvents *evt) : + joyEvents(evt) +{} + +void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) +{ + bool match = true; + + // Checking if there are changes in report since the method was last called + for (uint8_t i=0; i<RPT_GAMEPAD_LEN; i++) { + if( buf[i] != oldPad[i] ) { + match = false; + break; + } + } + // Calling Game Pad event handler + if (!match && joyEvents) { + joyEvents->OnGamePadChanged((const GamePadEventData*)buf); + + for (uint8_t i=0; i<RPT_GAMEPAD_LEN; i++) oldPad[i] = buf[i]; + } +} + +void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) +{ + Serial.print("X: "); + PrintHex<uint16_t>(evt->x, 0x80); + Serial.print(" Y: "); + PrintHex<uint16_t>(evt->y, 0x80); + Serial.print(" Hat Switch: "); + PrintHex<uint8_t>(evt->hat, 0x80); + Serial.print(" Twist: "); + PrintHex<uint8_t>(evt->twist, 0x80); + Serial.print(" Slider: "); + PrintHex<uint8_t>(evt->slider, 0x80); + Serial.print(" Buttons A: "); + PrintHex<uint8_t>(evt->buttons_a, 0x80); + Serial.print(" Buttons B: "); + PrintHex<uint8_t>(evt->buttons_b, 0x80); + Serial.println(""); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.h new file mode 100644 index 0000000000..2400364e65 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.h @@ -0,0 +1,42 @@ +#if !defined(__HIDJOYSTICKRPTPARSER_H__) +#define __HIDJOYSTICKRPTPARSER_H__ + +#include <hid.h> + +struct GamePadEventData +{ + union { //axes and hut switch + uint32_t axes; + struct { + uint32_t x : 10; + uint32_t y : 10; + uint32_t hat : 4; + uint32_t twist : 8; + }; + }; + uint8_t buttons_a; + uint8_t slider; + uint8_t buttons_b; +}; + +class JoystickEvents +{ +public: + virtual void OnGamePadChanged(const GamePadEventData *evt); +}; + +#define RPT_GAMEPAD_LEN sizeof(GamePadEventData)/sizeof(uint8_t) + +class JoystickReportParser : public HIDReportParser +{ + JoystickEvents *joyEvents; + + uint8_t oldPad[RPT_GAMEPAD_LEN]; + +public: + JoystickReportParser(JoystickEvents *evt); + + virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); +}; + +#endif // __HIDJOYSTICKRPTPARSER_H__ diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale.ino new file mode 100644 index 0000000000..f26ff964da --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale.ino @@ -0,0 +1,51 @@ +/* Digital Scale Output. Written for Stamps.com Model 510 */ +/* 5lb Digital Scale; any HID scale with Usage page 0x8d should work */ + +#include <hid.h> +#include <hiduniversal.h> +#include <usbhub.h> + +#include "scale_rptparser.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 Hub(&Usb); +HIDUniversal Hid(&Usb); +Max_LCD LCD(&Usb); +ScaleEvents ScaleEvents(&LCD); +ScaleReportParser Scale(&ScaleEvents); + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + // set up the LCD's number of rows and columns: + LCD.begin(16, 2); + LCD.clear(); + LCD.home(); + LCD.setCursor(0,0); + LCD.write('R'); + + delay( 200 ); + + if (!Hid.SetReportParser(0, &Scale)) + ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 ); +} + +void loop() +{ + Usb.Task(); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.cpp b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.cpp new file mode 100644 index 0000000000..01ed980cfb --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.cpp @@ -0,0 +1,150 @@ +/* Parser for standard HID scale (usage page 0x8d) data input report (ID 3) */ +#include "scale_rptparser.h" + +const char* UNITS[13] = { + "units", // unknown unit + "mg", // milligram + "g", // gram + "kg", // kilogram + "cd", // carat + "taels", // lian + "gr", // grain + "dwt", // pennyweight + "tonnes", // metric tons + "tons", // avoir ton + "ozt", // troy ounce + "oz", // ounce + "lbs" // pound +}; + +ScaleReportParser::ScaleReportParser(ScaleEvents *evt) : + scaleEvents(evt) +{} + +void ScaleReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) +{ + bool match = true; + + // Checking if there are changes in report since the method was last called + for (uint8_t i=0; i<RPT_SCALE_LEN; i++) { + if( buf[i] != oldScale[i] ) { + match = false; + break; + } + } + // Calling Game Pad event handler + if (!match && scaleEvents) { + scaleEvents->OnScaleChanged((const ScaleEventData*)buf); + + for (uint8_t i=0; i<RPT_SCALE_LEN; i++) oldScale[i] = buf[i]; + } +} + +ScaleEvents::ScaleEvents( Max_LCD* pLCD ) : + + pLcd( pLCD ) + +{} + +void ScaleEvents::LcdPrint( const char* str ) +{ + + while( *str ) { + + pLcd->write( *str++ ); + + } +} + +void ScaleEvents::OnScaleChanged(const ScaleEventData *evt) +{ + + pLcd->clear(); + pLcd->home(); + pLcd->setCursor(0,0); + + if( evt->reportID != 3 ) { + + const char inv_report[]="Invalid report!"; + + Serial.println(inv_report); + LcdPrint(inv_report); + + return; + + }//if( evt->reportID != 3... + + switch( evt->status ) { + + case REPORT_FAULT: + Serial.println(F("Report fault")); + break; + + case ZEROED: + Serial.println(F("Scale zero set")); + break; + + case WEIGHING: { + + const char progress[] = "Weighing..."; + Serial.println(progress); + LcdPrint(progress); + break; + } + + case WEIGHT_VALID: { + + char buf[10]; + double weight = evt->weight * pow( 10, evt->exp ); + + + + Serial.print(F("Weight: ")); + Serial.print( weight ); + Serial.print(F(" ")); + Serial.println( UNITS[ evt->unit ]); + + LcdPrint("Weight: "); + dtostrf( weight, 4, 2, buf ); + LcdPrint( buf ); + LcdPrint( UNITS[ evt->unit ]); + + break; + + }//case WEIGHT_VALID... + + case WEIGHT_NEGATIVE: { + + const char negweight[] = "Negative weight"; + Serial.println(negweight); + LcdPrint(negweight); + break; + } + + case OVERWEIGHT: { + + const char overweight[] = "Max.weight reached"; + Serial.println(overweight); + LcdPrint( overweight ); + break; + } + + case CALIBRATE_ME: + + Serial.println(F("Scale calibration required")); + break; + + case ZERO_ME: + + Serial.println(F("Scale zeroing required")); + break; + + default: + + Serial.print(F("Undefined status code: ")); + Serial.println( evt->status ); + break; + + }//switch( evt->status... + +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.h new file mode 100644 index 0000000000..57fbb033bf --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.h @@ -0,0 +1,55 @@ +#if !defined(__SCALERPTPARSER_H__) +#define __SCALERPTPARSER_H__ + +#include <max_LCD.h> +#include <hid.h> + +/* Scale status constants */ +#define REPORT_FAULT 0x01 +#define ZEROED 0x02 +#define WEIGHING 0x03 +#define WEIGHT_VALID 0x04 +#define WEIGHT_NEGATIVE 0x05 +#define OVERWEIGHT 0x06 +#define CALIBRATE_ME 0x07 +#define ZERO_ME 0x08 + +/* input data report */ +struct ScaleEventData +{ + uint8_t reportID; //must be 3 + uint8_t status; + uint8_t unit; + int8_t exp; //scale factor for the weight + uint16_t weight; // +}; + +class ScaleEvents +{ + + Max_LCD* pLcd; + + void LcdPrint( const char* str ); + +public: + + ScaleEvents( Max_LCD* pLCD ); + + virtual void OnScaleChanged(const ScaleEventData *evt); +}; + +#define RPT_SCALE_LEN sizeof(ScaleEventData)/sizeof(uint8_t) + +class ScaleReportParser : public HIDReportParser +{ + ScaleEvents *scaleEvents; + + uint8_t oldScale[RPT_SCALE_LEN]; + +public: + ScaleReportParser(ScaleEvents *evt); + + virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); +}; + +#endif // __SCALERPTPARSER_H__ diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PS3USB/PS3USB.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PS3USB/PS3USB.ino new file mode 100644 index 0000000000..a53dcfbe61 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PS3USB/PS3USB.ino @@ -0,0 +1,148 @@ +/* + Example sketch for the PS3 USB 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 <PS3USB.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; +/* You can create the instance of the class in two ways */ +PS3USB PS3(&Usb); // This will just create the instance +//PS3USB PS3(&Usb,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 printAngle; +uint8_t state = 0; + +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 USB 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.PS3NavigationConnected) { + Serial.print(F("\tR2: ")); + Serial.print(PS3.getAnalogButton(R2)); + } + } + if (PS3.getButtonClick(PS)) + Serial.print(F("\r\nPS")); + + 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")); + PS3.setLedOff(); + PS3.setLedOn(LED4); + } + if (PS3.getButtonClick(RIGHT)) { + Serial.print(F("\r\nRight")); + PS3.setLedOff(); + PS3.setLedOn(LED1); + } + if (PS3.getButtonClick(DOWN)) { + Serial.print(F("\r\nDown")); + PS3.setLedOff(); + PS3.setLedOn(LED2); + } + if (PS3.getButtonClick(LEFT)) { + Serial.print(F("\r\nLeft")); + 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 (printAngle) { + Serial.print(F("\r\nPitch: ")); + Serial.print(PS3.getAngle(Pitch)); + Serial.print(F("\tRoll: ")); + Serial.print(PS3.getAngle(Roll)); + } + } + else if (PS3.PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB + if (state == 0) { + PS3.moveSetRumble(0); + PS3.moveSetBulb(Off); + } else if (state == 1) { + PS3.moveSetRumble(75); + PS3.moveSetBulb(Red); + } else if (state == 2) { + PS3.moveSetRumble(125); + PS3.moveSetBulb(Green); + } else if (state == 3) { + PS3.moveSetRumble(150); + PS3.moveSetBulb(Blue); + } else if (state == 4) { + PS3.moveSetRumble(175); + PS3.moveSetBulb(Yellow); + } else if (state == 5) { + PS3.moveSetRumble(200); + PS3.moveSetBulb(Lightblue); + } else if (state == 6) { + PS3.moveSetRumble(225); + PS3.moveSetBulb(Purble); + } else if (state == 7) { + PS3.moveSetRumble(250); + PS3.moveSetBulb(White); + } + + state++; + if (state > 7) + state = 0; + delay(1000); + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PS4USB/PS4USB.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PS4USB/PS4USB.ino new file mode 100644 index 0000000000..d0d76790ec --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PS4USB/PS4USB.ino @@ -0,0 +1,133 @@ +/* + Example sketch for the PS4 USB 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 <PS4USB.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; +PS4USB PS4(&Usb); + +bool printAngle, printTouch; +uint8_t oldL2Value, oldR2Value; + +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\nPS4 USB Library Started")); +} + +void loop() { + Usb.Task(); + + if (PS4.connected()) { + if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) < 117 || PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117 || PS4.getAnalogHat(RightHatX) > 137 || PS4.getAnalogHat(RightHatX) < 117 || PS4.getAnalogHat(RightHatY) > 137 || PS4.getAnalogHat(RightHatY) < 117) { + Serial.print(F("\r\nLeftHatX: ")); + Serial.print(PS4.getAnalogHat(LeftHatX)); + Serial.print(F("\tLeftHatY: ")); + Serial.print(PS4.getAnalogHat(LeftHatY)); + Serial.print(F("\tRightHatX: ")); + Serial.print(PS4.getAnalogHat(RightHatX)); + Serial.print(F("\tRightHatY: ")); + Serial.print(PS4.getAnalogHat(RightHatY)); + } + + if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller + Serial.print(F("\r\nL2: ")); + Serial.print(PS4.getAnalogButton(L2)); + Serial.print(F("\tR2: ")); + Serial.print(PS4.getAnalogButton(R2)); + } + if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different + PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2)); + oldL2Value = PS4.getAnalogButton(L2); + oldR2Value = PS4.getAnalogButton(R2); + + if (PS4.getButtonClick(PS)) + Serial.print(F("\r\nPS")); + if (PS4.getButtonClick(TRIANGLE)) { + Serial.print(F("\r\nTraingle")); + PS4.setRumbleOn(RumbleLow); + } + if (PS4.getButtonClick(CIRCLE)) { + Serial.print(F("\r\nCircle")); + PS4.setRumbleOn(RumbleHigh); + } + if (PS4.getButtonClick(CROSS)) { + Serial.print(F("\r\nCross")); + PS4.setLedFlash(10, 10); // Set it to blink rapidly + } + if (PS4.getButtonClick(SQUARE)) { + Serial.print(F("\r\nSquare")); + PS4.setLedFlash(0, 0); // Turn off blinking + } + + if (PS4.getButtonClick(UP)) { + Serial.print(F("\r\nUp")); + PS4.setLed(Red); + } if (PS4.getButtonClick(RIGHT)) { + Serial.print(F("\r\nRight")); + PS4.setLed(Blue); + } if (PS4.getButtonClick(DOWN)) { + Serial.print(F("\r\nDown")); + PS4.setLed(Yellow); + } if (PS4.getButtonClick(LEFT)) { + Serial.print(F("\r\nLeft")); + PS4.setLed(Green); + } + + if (PS4.getButtonClick(L1)) + Serial.print(F("\r\nL1")); + if (PS4.getButtonClick(L3)) + Serial.print(F("\r\nL3")); + if (PS4.getButtonClick(R1)) + Serial.print(F("\r\nR1")); + if (PS4.getButtonClick(R3)) + Serial.print(F("\r\nR3")); + + if (PS4.getButtonClick(SHARE)) + Serial.print(F("\r\nShare")); + if (PS4.getButtonClick(OPTIONS)) { + Serial.print(F("\r\nOptions")); + printAngle = !printAngle; + } + if (PS4.getButtonClick(TOUCHPAD)) { + Serial.print(F("\r\nTouchpad")); + printTouch = !printTouch; + } + + if (printAngle) { // Print angle calculated using the accelerometer only + Serial.print(F("\r\nPitch: ")); + Serial.print(PS4.getAngle(Pitch)); + Serial.print(F("\tRoll: ")); + Serial.print(PS4.getAngle(Roll)); + } + + if (printTouch) { // Print the x, y coordinates of the touchpad + if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad + Serial.print(F("\r\n")); + for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers + if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad + Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": ")); + Serial.print(PS4.getX(i)); + Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": ")); + Serial.print(PS4.getY(i)); + Serial.print(F("\t")); + } + } + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PSBuzz/PSBuzz.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PSBuzz/PSBuzz.ino new file mode 100644 index 0000000000..6ee462c1eb --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/PSBuzz/PSBuzz.ino @@ -0,0 +1,49 @@ +/* + Example sketch for the Playstation Buzz 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 <PSBuzz.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; +PSBuzz Buzz(&Usb); + +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.println(F("\r\nPS Buzz Library Started")); +} + +void loop() { + Usb.Task(); + + if (Buzz.connected()) { + for (uint8_t i = 0; i < 4; i++) { + if (Buzz.getButtonClick(RED, i)) { + Buzz.setLedToggle(i); // Toggle the LED + Serial.println(F("RED")); + } + if (Buzz.getButtonClick(YELLOW, i)) + Serial.println(F("YELLOW")); + if (Buzz.getButtonClick(GREEN, i)) + Serial.println(F("GREEN")); + if (Buzz.getButtonClick(ORANGE, i)) + Serial.println(F("ORANGE")); + if (Buzz.getButtonClick(BLUE, i)) + Serial.println(F("BLUE")); + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/USB_desc/USB_desc.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/USB_desc/USB_desc.ino new file mode 100644 index 0000000000..acfe57d374 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/USB_desc/USB_desc.ino @@ -0,0 +1,349 @@ +#include <usbhub.h> + +#include "pgmstrings.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); +//USBHub Hub2(&Usb); +//USBHub Hub3(&Usb); +//USBHub Hub4(&Usb); +//USBHub Hub5(&Usb); +//USBHub Hub6(&Usb); +//USBHub Hub7(&Usb); + +uint32_t next_time; + +void PrintAllAddresses(UsbDevice *pdev) +{ + UsbDeviceAddress adr; + adr.devAddress = pdev->address.devAddress; + Serial.print("\r\nAddr:"); + Serial.print(adr.devAddress, HEX); + Serial.print("("); + Serial.print(adr.bmHub, HEX); + Serial.print("."); + Serial.print(adr.bmParent, HEX); + Serial.print("."); + Serial.print(adr.bmAddress, HEX); + Serial.println(")"); +} + +void PrintAddress(uint8_t addr) +{ + UsbDeviceAddress adr; + adr.devAddress = addr; + Serial.print("\r\nADDR:\t"); + Serial.println(adr.devAddress, HEX); + Serial.print("DEV:\t"); + Serial.println(adr.bmAddress, HEX); + Serial.print("PRNT:\t"); + Serial.println(adr.bmParent, HEX); + Serial.print("HUB:\t"); + Serial.println(adr.bmHub, HEX); +} + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + next_time = millis() + 10000; +} + +byte getdevdescr( byte addr, byte &num_conf ); + +void PrintDescriptors(uint8_t addr) +{ + uint8_t rcode = 0; + byte num_conf = 0; + + rcode = getdevdescr( (byte)addr, num_conf ); + if ( rcode ) + { + printProgStr(Gen_Error_str); + print_hex( rcode, 8 ); + } + Serial.print("\r\n"); + + for (int i = 0; i < num_conf; i++) + { + rcode = getconfdescr( addr, i ); // get configuration descriptor + if ( rcode ) + { + printProgStr(Gen_Error_str); + print_hex(rcode, 8); + } + Serial.println("\r\n"); + } +} + +void PrintAllDescriptors(UsbDevice *pdev) +{ + Serial.println("\r\n"); + print_hex(pdev->address.devAddress, 8); + Serial.println("\r\n--"); + PrintDescriptors( pdev->address.devAddress ); +} + +void loop() +{ + Usb.Task(); + + if ( Usb.getUsbTaskState() == USB_STATE_RUNNING ) + { + //if (millis() >= next_time) + { + Usb.ForEachUsbDevice(&PrintAllDescriptors); + Usb.ForEachUsbDevice(&PrintAllAddresses); + + while ( 1 ); //stop + } + } +} + +byte getdevdescr( byte addr, byte &num_conf ) +{ + USB_DEVICE_DESCRIPTOR buf; + byte rcode; + rcode = Usb.getDevDescr( addr, 0, 0x12, ( uint8_t *)&buf ); + if ( rcode ) { + return ( rcode ); + } + printProgStr(Dev_Header_str); + printProgStr(Dev_Length_str); + print_hex( buf.bLength, 8 ); + printProgStr(Dev_Type_str); + print_hex( buf.bDescriptorType, 8 ); + printProgStr(Dev_Version_str); + print_hex( buf.bcdUSB, 16 ); + printProgStr(Dev_Class_str); + print_hex( buf.bDeviceClass, 8 ); + printProgStr(Dev_Subclass_str); + print_hex( buf.bDeviceSubClass, 8 ); + printProgStr(Dev_Protocol_str); + print_hex( buf.bDeviceProtocol, 8 ); + printProgStr(Dev_Pktsize_str); + print_hex( buf.bMaxPacketSize0, 8 ); + printProgStr(Dev_Vendor_str); + print_hex( buf.idVendor, 16 ); + printProgStr(Dev_Product_str); + print_hex( buf.idProduct, 16 ); + printProgStr(Dev_Revision_str); + print_hex( buf.bcdDevice, 16 ); + printProgStr(Dev_Mfg_str); + print_hex( buf.iManufacturer, 8 ); + printProgStr(Dev_Prod_str); + print_hex( buf.iProduct, 8 ); + printProgStr(Dev_Serial_str); + print_hex( buf.iSerialNumber, 8 ); + printProgStr(Dev_Nconf_str); + print_hex( buf.bNumConfigurations, 8 ); + num_conf = buf.bNumConfigurations; + return ( 0 ); +} + +void printhubdescr(uint8_t *descrptr, uint8_t addr) +{ + HubDescriptor *pHub = (HubDescriptor*) descrptr; + uint8_t len = *((uint8_t*)descrptr); + + printProgStr(PSTR("\r\n\r\nHub Descriptor:\r\n")); + printProgStr(PSTR("bDescLength:\t\t")); + Serial.println(pHub->bDescLength, HEX); + + printProgStr(PSTR("bDescriptorType:\t")); + Serial.println(pHub->bDescriptorType, HEX); + + printProgStr(PSTR("bNbrPorts:\t\t")); + Serial.println(pHub->bNbrPorts, HEX); + + printProgStr(PSTR("LogPwrSwitchMode:\t")); + Serial.println(pHub->LogPwrSwitchMode, BIN); + + printProgStr(PSTR("CompoundDevice:\t\t")); + Serial.println(pHub->CompoundDevice, BIN); + + printProgStr(PSTR("OverCurrentProtectMode:\t")); + Serial.println(pHub->OverCurrentProtectMode, BIN); + + printProgStr(PSTR("TTThinkTime:\t\t")); + Serial.println(pHub->TTThinkTime, BIN); + + printProgStr(PSTR("PortIndicatorsSupported:")); + Serial.println(pHub->PortIndicatorsSupported, BIN); + + printProgStr(PSTR("Reserved:\t\t")); + Serial.println(pHub->Reserved, HEX); + + printProgStr(PSTR("bPwrOn2PwrGood:\t\t")); + Serial.println(pHub->bPwrOn2PwrGood, HEX); + + printProgStr(PSTR("bHubContrCurrent:\t")); + Serial.println(pHub->bHubContrCurrent, HEX); + + for (uint8_t i = 7; i < len; i++) + print_hex(descrptr[i], 8); + + //for (uint8_t i=1; i<=pHub->bNbrPorts; i++) + // PrintHubPortStatus(&Usb, addr, i, 1); +} + +byte getconfdescr( byte addr, byte conf ) +{ + uint8_t buf[ BUFSIZE ]; + uint8_t* buf_ptr = buf; + byte rcode; + byte descr_length; + byte descr_type; + unsigned int total_length; + rcode = Usb.getConfDescr( addr, 0, 4, conf, buf ); //get total length + LOBYTE( total_length ) = buf[ 2 ]; + HIBYTE( total_length ) = buf[ 3 ]; + if ( total_length > 256 ) { //check if total length is larger than buffer + printProgStr(Conf_Trunc_str); + total_length = 256; + } + rcode = Usb.getConfDescr( addr, 0, total_length, conf, buf ); //get the whole descriptor + while ( buf_ptr < buf + total_length ) { //parsing descriptors + descr_length = *( buf_ptr ); + descr_type = *( buf_ptr + 1 ); + switch ( descr_type ) { + case ( USB_DESCRIPTOR_CONFIGURATION ): + printconfdescr( buf_ptr ); + break; + case ( USB_DESCRIPTOR_INTERFACE ): + printintfdescr( buf_ptr ); + break; + case ( USB_DESCRIPTOR_ENDPOINT ): + printepdescr( buf_ptr ); + break; + case 0x29: + printhubdescr( buf_ptr, addr ); + break; + default: + printunkdescr( buf_ptr ); + break; + }//switch( descr_type + buf_ptr = ( buf_ptr + descr_length ); //advance buffer pointer + }//while( buf_ptr <=... + return ( rcode ); +} +/* prints hex numbers with leading zeroes */ +// copyright, Peter H Anderson, Baltimore, MD, Nov, '07 +// source: http://www.phanderson.com/arduino/arduino_display.html +void print_hex(int v, int num_places) +{ + int mask = 0, n, num_nibbles, digit; + + for (n = 1; n <= num_places; n++) { + mask = (mask << 1) | 0x0001; + } + v = v & mask; // truncate v to specified number of places + + num_nibbles = num_places / 4; + if ((num_places % 4) != 0) { + ++num_nibbles; + } + do { + digit = ((v >> (num_nibbles - 1) * 4)) & 0x0f; + Serial.print(digit, HEX); + } + while (--num_nibbles); +} +/* function to print configuration descriptor */ +void printconfdescr( uint8_t* descr_ptr ) +{ + USB_CONFIGURATION_DESCRIPTOR* conf_ptr = ( USB_CONFIGURATION_DESCRIPTOR* )descr_ptr; + printProgStr(Conf_Header_str); + printProgStr(Conf_Totlen_str); + print_hex( conf_ptr->wTotalLength, 16 ); + printProgStr(Conf_Nint_str); + print_hex( conf_ptr->bNumInterfaces, 8 ); + printProgStr(Conf_Value_str); + print_hex( conf_ptr->bConfigurationValue, 8 ); + printProgStr(Conf_String_str); + print_hex( conf_ptr->iConfiguration, 8 ); + printProgStr(Conf_Attr_str); + print_hex( conf_ptr->bmAttributes, 8 ); + printProgStr(Conf_Pwr_str); + print_hex( conf_ptr->bMaxPower, 8 ); + return; +} +/* function to print interface descriptor */ +void printintfdescr( uint8_t* descr_ptr ) +{ + USB_INTERFACE_DESCRIPTOR* intf_ptr = ( USB_INTERFACE_DESCRIPTOR* )descr_ptr; + printProgStr(Int_Header_str); + printProgStr(Int_Number_str); + print_hex( intf_ptr->bInterfaceNumber, 8 ); + printProgStr(Int_Alt_str); + print_hex( intf_ptr->bAlternateSetting, 8 ); + printProgStr(Int_Endpoints_str); + print_hex( intf_ptr->bNumEndpoints, 8 ); + printProgStr(Int_Class_str); + print_hex( intf_ptr->bInterfaceClass, 8 ); + printProgStr(Int_Subclass_str); + print_hex( intf_ptr->bInterfaceSubClass, 8 ); + printProgStr(Int_Protocol_str); + print_hex( intf_ptr->bInterfaceProtocol, 8 ); + printProgStr(Int_String_str); + print_hex( intf_ptr->iInterface, 8 ); + return; +} +/* function to print endpoint descriptor */ +void printepdescr( uint8_t* descr_ptr ) +{ + USB_ENDPOINT_DESCRIPTOR* ep_ptr = ( USB_ENDPOINT_DESCRIPTOR* )descr_ptr; + printProgStr(End_Header_str); + printProgStr(End_Address_str); + print_hex( ep_ptr->bEndpointAddress, 8 ); + printProgStr(End_Attr_str); + print_hex( ep_ptr->bmAttributes, 8 ); + printProgStr(End_Pktsize_str); + print_hex( ep_ptr->wMaxPacketSize, 16 ); + printProgStr(End_Interval_str); + print_hex( ep_ptr->bInterval, 8 ); + + return; +} +/*function to print unknown descriptor */ +void printunkdescr( uint8_t* descr_ptr ) +{ + byte length = *descr_ptr; + byte i; + printProgStr(Unk_Header_str); + printProgStr(Unk_Length_str); + print_hex( *descr_ptr, 8 ); + printProgStr(Unk_Type_str); + print_hex( *(descr_ptr + 1 ), 8 ); + printProgStr(Unk_Contents_str); + descr_ptr += 2; + for ( i = 0; i < length; i++ ) { + print_hex( *descr_ptr, 8 ); + descr_ptr++; + } +} + + +/* Print a string from Program Memory directly to save RAM */ +void printProgStr(const char* str) +{ + char c; + if (!str) return; + while ((c = pgm_read_byte(str++))) + Serial.print(c); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/USB_desc/pgmstrings.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/USB_desc/pgmstrings.h new file mode 100644 index 0000000000..bdb0077ecc --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/USB_desc/pgmstrings.h @@ -0,0 +1,52 @@ +#if !defined(__PGMSTRINGS_H__) +#define __PGMSTRINGS_H__ + +#define LOBYTE(x) ((char*)(&(x)))[0] +#define HIBYTE(x) ((char*)(&(x)))[1] +#define BUFSIZE 256 //buffer size + + +/* Print strings in Program Memory */ +const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t"; +const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: "; +const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t"; +const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t"; +const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t"; +const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t"; +const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t"; +const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t"; +const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t"; +const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t"; +const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t"; +const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t"; +const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t"; +const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t"; +const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t"; +const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t"; +const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes"; +const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:"; +const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t"; +const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t"; +const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t"; +const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t"; +const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t"; +const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:"; +const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t"; +const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t"; +const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t"; +const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t"; +const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t"; +const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t"; +const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t"; +const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:"; +const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t"; +const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t"; +const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t"; +const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:"; +const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t"; +const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t"; +const char Unk_Contents_str[] PROGMEM ="\r\nContents:\t"; + +#endif // __PGMSTRINGS_H__
\ No newline at end of file diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXOLD/XBOXOLD.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXOLD/XBOXOLD.ino new file mode 100644 index 0000000000..64a3ed6120 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXOLD/XBOXOLD.ino @@ -0,0 +1,110 @@ +/* + Example sketch for the original Xbox 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 <XBOXOLD.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); // The controller has a built in hub, so this instance is needed +XBOXOLD Xbox(&Usb); + +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\nXBOX Library Started")); +} +void loop() { + Usb.Task(); + if (Xbox.XboxConnected) { + if (Xbox.getButtonPress(BLACK) || Xbox.getButtonPress(WHITE)) { + Serial.print("BLACK: "); + Serial.print(Xbox.getButtonPress(BLACK)); + Serial.print("\tWHITE: "); + Serial.println(Xbox.getButtonPress(WHITE)); + Xbox.setRumbleOn(Xbox.getButtonPress(BLACK), Xbox.getButtonPress(WHITE)); + } else + Xbox.setRumbleOn(0, 0); + + if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500 || Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500 || Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500 || Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) { + if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500) { + Serial.print(F("LeftHatX: ")); + Serial.print(Xbox.getAnalogHat(LeftHatX)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500) { + Serial.print(F("LeftHatY: ")); + Serial.print(Xbox.getAnalogHat(LeftHatY)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500) { + Serial.print(F("RightHatX: ")); + Serial.print(Xbox.getAnalogHat(RightHatX)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) { + Serial.print(F("RightHatY: ")); + Serial.print(Xbox.getAnalogHat(RightHatY)); + } + Serial.println(); + } + + if (Xbox.getButtonClick(UP)) + Serial.println(F("Up")); + if (Xbox.getButtonClick(DOWN)) + Serial.println(F("Down")); + if (Xbox.getButtonClick(LEFT)) + Serial.println(F("Left")); + if (Xbox.getButtonClick(RIGHT)) + Serial.println(F("Right")); + + if (Xbox.getButtonClick(START)) + Serial.println(F("Start")); + if (Xbox.getButtonClick(BACK)) + Serial.println(F("Back")); + if (Xbox.getButtonClick(L3)) + Serial.println(F("L3")); + if (Xbox.getButtonClick(R3)) + Serial.println(F("R3")); + + if (Xbox.getButtonPress(A)) { + Serial.print(F("A: ")); + Serial.println(Xbox.getButtonPress(A)); + } + if (Xbox.getButtonPress(B)) { + Serial.print(F("B: ")); + Serial.println(Xbox.getButtonPress(B)); + } + if (Xbox.getButtonPress(X)) { + Serial.print(F("X: ")); + Serial.println(Xbox.getButtonPress(X)); + } + if (Xbox.getButtonPress(Y)) { + Serial.print(F("Y: ")); + Serial.println(Xbox.getButtonPress(Y)); + } + if (Xbox.getButtonPress(L1)) { + Serial.print(F("L1: ")); + Serial.println(Xbox.getButtonPress(L1)); + } + if (Xbox.getButtonPress(R1)) { + Serial.print(F("R1: ")); + Serial.println(Xbox.getButtonPress(R1)); + } + } + delay(1); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXONE/XBOXONE.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXONE/XBOXONE.ino new file mode 100644 index 0000000000..9526f53d19 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXONE/XBOXONE.ino @@ -0,0 +1,106 @@ +/* + Example sketch for the Xbox ONE USB library - by guruthree, based on work by + Kristian Lauszus. + */ + +#include <XBOXONE.h> +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#endif + +USB Usb; +XBOXONE Xbox(&Usb); + +void setup() { + Serial.begin(115200); + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection + if (Usb.Init() == -1) { + Serial.print(F("\r\nOSC did not start")); + while (1); //halt + } + Serial.print(F("\r\nXBOX USB Library Started")); +} +void loop() { + Usb.Task(); + if (Xbox.XboxOneConnected) { + if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500 || Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500 || Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500 || Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) { + if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500) { + Serial.print(F("LeftHatX: ")); + Serial.print(Xbox.getAnalogHat(LeftHatX)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500) { + Serial.print(F("LeftHatY: ")); + Serial.print(Xbox.getAnalogHat(LeftHatY)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500) { + Serial.print(F("RightHatX: ")); + Serial.print(Xbox.getAnalogHat(RightHatX)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) { + Serial.print(F("RightHatY: ")); + Serial.print(Xbox.getAnalogHat(RightHatY)); + } + Serial.println(); + } + + if (Xbox.getButtonPress(L2) > 0 || Xbox.getButtonPress(R2) > 0) { + if (Xbox.getButtonPress(L2) > 0) { + Serial.print(F("L2: ")); + Serial.print(Xbox.getButtonPress(L2)); + Serial.print("\t"); + } + if (Xbox.getButtonPress(R2) > 0) { + Serial.print(F("R2: ")); + Serial.print(Xbox.getButtonPress(R2)); + Serial.print("\t"); + } + Serial.println(); + } + + if (Xbox.getButtonClick(UP)) + Serial.println(F("Up")); + if (Xbox.getButtonClick(DOWN)) + Serial.println(F("Down")); + if (Xbox.getButtonClick(LEFT)) + Serial.println(F("Left")); + if (Xbox.getButtonClick(RIGHT)) + Serial.println(F("Right")); + + if (Xbox.getButtonClick(START)) + Serial.println(F("Start")); + if (Xbox.getButtonClick(BACK)) + Serial.println(F("Back")); + if (Xbox.getButtonClick(XBOX)) + Serial.println(F("Xbox")); + if (Xbox.getButtonClick(SYNC)) + Serial.println(F("Sync")); + + if (Xbox.getButtonClick(L1)) + Serial.println(F("L1")); + if (Xbox.getButtonClick(R1)) + Serial.println(F("R1")); + if (Xbox.getButtonClick(L2)) + Serial.println(F("L2")); + if (Xbox.getButtonClick(R2)) + Serial.println(F("R2")); + if (Xbox.getButtonClick(L3)) + Serial.println(F("L3")); + if (Xbox.getButtonClick(R3)) + Serial.println(F("R3")); + + + if (Xbox.getButtonClick(A)) + Serial.println(F("A")); + if (Xbox.getButtonClick(B)) + Serial.println(F("B")); + if (Xbox.getButtonClick(X)) + Serial.println(F("X")); + if (Xbox.getButtonClick(Y)) + Serial.println(F("Y")); + } + delay(1); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXRECV/XBOXRECV.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXRECV/XBOXRECV.ino new file mode 100644 index 0000000000..491b287e44 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXRECV/XBOXRECV.ino @@ -0,0 +1,122 @@ +/* + Example sketch for the Xbox Wireless Reciver library - developed by Kristian Lauszus + It supports up to four controllers wirelessly + For more information see the blog post: http://blog.tkjelectronics.dk/2012/12/xbox-360-receiver-added-to-the-usb-host-library/ or + send me an e-mail: kristianl@tkjelectronics.com + */ + +#include <XBOXRECV.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; +XBOXRECV Xbox(&Usb); + +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\nXbox Wireless Receiver Library Started")); +} +void loop() { + Usb.Task(); + if (Xbox.XboxReceiverConnected) { + for (uint8_t i = 0; i < 4; i++) { + if (Xbox.Xbox360Connected[i]) { + if (Xbox.getButtonPress(L2, i) || Xbox.getButtonPress(R2, i)) { + Serial.print("L2: "); + Serial.print(Xbox.getButtonPress(L2, i)); + Serial.print("\tR2: "); + Serial.println(Xbox.getButtonPress(R2, i)); + Xbox.setRumbleOn(Xbox.getButtonPress(L2, i), Xbox.getButtonPress(R2, i), i); + } + + if (Xbox.getAnalogHat(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500 || Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500 || Xbox.getAnalogHat(RightHatX, i) > 7500 || Xbox.getAnalogHat(RightHatX, i) < -7500 || Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500) { + if (Xbox.getAnalogHat(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500) { + Serial.print(F("LeftHatX: ")); + Serial.print(Xbox.getAnalogHat(LeftHatX, i)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500) { + Serial.print(F("LeftHatY: ")); + Serial.print(Xbox.getAnalogHat(LeftHatY, i)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatX, i) > 7500 || Xbox.getAnalogHat(RightHatX, i) < -7500) { + Serial.print(F("RightHatX: ")); + Serial.print(Xbox.getAnalogHat(RightHatX, i)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500) { + Serial.print(F("RightHatY: ")); + Serial.print(Xbox.getAnalogHat(RightHatY, i)); + } + Serial.println(); + } + + if (Xbox.getButtonClick(UP, i)) { + Xbox.setLedOn(LED1, i); + Serial.println(F("Up")); + } + if (Xbox.getButtonClick(DOWN, i)) { + Xbox.setLedOn(LED4, i); + Serial.println(F("Down")); + } + if (Xbox.getButtonClick(LEFT, i)) { + Xbox.setLedOn(LED3, i); + Serial.println(F("Left")); + } + if (Xbox.getButtonClick(RIGHT, i)) { + Xbox.setLedOn(LED2, i); + Serial.println(F("Right")); + } + + if (Xbox.getButtonClick(START, i)) { + Xbox.setLedMode(ALTERNATING, i); + Serial.println(F("Start")); + } + if (Xbox.getButtonClick(BACK, i)) { + Xbox.setLedBlink(ALL, i); + Serial.println(F("Back")); + } + if (Xbox.getButtonClick(L3, i)) + Serial.println(F("L3")); + if (Xbox.getButtonClick(R3, i)) + Serial.println(F("R3")); + + if (Xbox.getButtonClick(L1, i)) + Serial.println(F("L1")); + if (Xbox.getButtonClick(R1, i)) + Serial.println(F("R1")); + if (Xbox.getButtonClick(XBOX, i)) { + Xbox.setLedMode(ROTATING, i); + Serial.print(F("Xbox (Battery: ")); + Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 + Serial.println(F(")")); + } + if (Xbox.getButtonClick(SYNC, i)) { + Serial.println(F("Sync")); + Xbox.disconnect(i); + } + + if (Xbox.getButtonClick(A, i)) + Serial.println(F("A")); + if (Xbox.getButtonClick(B, i)) + Serial.println(F("B")); + if (Xbox.getButtonClick(X, i)) + Serial.println(F("X")); + if (Xbox.getButtonClick(Y, i)) + Serial.println(F("Y")); + } + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXUSB/XBOXUSB.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXUSB/XBOXUSB.ino new file mode 100644 index 0000000000..8a5691c6e1 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Xbox/XBOXUSB/XBOXUSB.ino @@ -0,0 +1,113 @@ +/* + Example sketch for the Xbox 360 USB 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 <XBOXUSB.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; +XBOXUSB Xbox(&Usb); + +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\nXBOX USB Library Started")); +} +void loop() { + Usb.Task(); + if (Xbox.Xbox360Connected) { + if (Xbox.getButtonPress(L2) || Xbox.getButtonPress(R2)) { + Serial.print("L2: "); + Serial.print(Xbox.getButtonPress(L2)); + Serial.print("\tR2: "); + Serial.println(Xbox.getButtonPress(R2)); + Xbox.setRumbleOn(Xbox.getButtonPress(L2), Xbox.getButtonPress(R2)); + } else + Xbox.setRumbleOn(0, 0); + + if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500 || Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500 || Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500 || Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) { + if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500) { + Serial.print(F("LeftHatX: ")); + Serial.print(Xbox.getAnalogHat(LeftHatX)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500) { + Serial.print(F("LeftHatY: ")); + Serial.print(Xbox.getAnalogHat(LeftHatY)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500) { + Serial.print(F("RightHatX: ")); + Serial.print(Xbox.getAnalogHat(RightHatX)); + Serial.print("\t"); + } + if (Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) { + Serial.print(F("RightHatY: ")); + Serial.print(Xbox.getAnalogHat(RightHatY)); + } + Serial.println(); + } + + if (Xbox.getButtonClick(UP)) { + Xbox.setLedOn(LED1); + Serial.println(F("Up")); + } + if (Xbox.getButtonClick(DOWN)) { + Xbox.setLedOn(LED4); + Serial.println(F("Down")); + } + if (Xbox.getButtonClick(LEFT)) { + Xbox.setLedOn(LED3); + Serial.println(F("Left")); + } + if (Xbox.getButtonClick(RIGHT)) { + Xbox.setLedOn(LED2); + Serial.println(F("Right")); + } + + if (Xbox.getButtonClick(START)) { + Xbox.setLedMode(ALTERNATING); + Serial.println(F("Start")); + } + if (Xbox.getButtonClick(BACK)) { + Xbox.setLedBlink(ALL); + Serial.println(F("Back")); + } + if (Xbox.getButtonClick(L3)) + Serial.println(F("L3")); + if (Xbox.getButtonClick(R3)) + Serial.println(F("R3")); + + if (Xbox.getButtonClick(L1)) + Serial.println(F("L1")); + if (Xbox.getButtonClick(R1)) + Serial.println(F("R1")); + if (Xbox.getButtonClick(XBOX)) { + Xbox.setLedMode(ROTATING); + Serial.println(F("Xbox")); + } + + if (Xbox.getButtonClick(A)) + Serial.println(F("A")); + if (Xbox.getButtonClick(B)) + Serial.println(F("B")); + if (Xbox.getButtonClick(X)) + Serial.println(F("X")); + if (Xbox.getButtonClick(Y)) + Serial.println(F("Y")); + } + delay(1); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/acm/acm_terminal/acm_terminal.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/acm/acm_terminal/acm_terminal.ino new file mode 100644 index 0000000000..f509cda890 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/acm/acm_terminal/acm_terminal.ino @@ -0,0 +1,100 @@ +#include <cdcacm.h> +#include <usbhub.h> + +#include "pgmstrings.h" + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +class ACMAsyncOper : public CDCAsyncOper +{ +public: + uint8_t OnInit(ACM *pacm); +}; + +uint8_t ACMAsyncOper::OnInit(ACM *pacm) +{ + uint8_t rcode; + // Set DTR = 1 RTS=1 + rcode = pacm->SetControlLineState(3); + + if (rcode) + { + ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode); + return rcode; + } + + LINE_CODING lc; + lc.dwDTERate = 115200; + lc.bCharFormat = 0; + lc.bParityType = 0; + lc.bDataBits = 8; + + rcode = pacm->SetLineCoding(&lc); + + if (rcode) + ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode); + + return rcode; +} + +USB Usb; +//USBHub Hub(&Usb); +ACMAsyncOper AsyncOper; +ACM Acm(&Usb, &AsyncOper); + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSCOKIRQ failed to assert"); + + delay( 200 ); +} + +void loop() +{ + Usb.Task(); + + if( Acm.isReady()) { + uint8_t rcode; + + /* reading the keyboard */ + if(Serial.available()) { + uint8_t data= Serial.read(); + /* sending to the phone */ + rcode = Acm.SndData(1, &data); + if (rcode) + ErrorMessage<uint8_t>(PSTR("SndData"), rcode); + }//if(Serial.available()... + + delay(50); + + /* reading the phone */ + /* buffer size must be greater or equal to max.packet size */ + /* it it set to 64 (largest possible max.packet size) here, can be tuned down + for particular endpoint */ + uint8_t buf[64]; + uint16_t rcvd = 64; + rcode = Acm.RcvData(&rcvd, buf); + if (rcode && rcode != hrNAK) + ErrorMessage<uint8_t>(PSTR("Ret"), rcode); + + if( rcvd ) { //more than zero bytes received + for(uint16_t i=0; i < rcvd; i++ ) { + Serial.print((char)buf[i]); //printing on the screen + } + } + delay(10); + }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING.. +} + + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/acm/acm_terminal/pgmstrings.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/acm/acm_terminal/pgmstrings.h new file mode 100644 index 0000000000..bdb0077ecc --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/acm/acm_terminal/pgmstrings.h @@ -0,0 +1,52 @@ +#if !defined(__PGMSTRINGS_H__) +#define __PGMSTRINGS_H__ + +#define LOBYTE(x) ((char*)(&(x)))[0] +#define HIBYTE(x) ((char*)(&(x)))[1] +#define BUFSIZE 256 //buffer size + + +/* Print strings in Program Memory */ +const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t"; +const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: "; +const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t"; +const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t"; +const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t"; +const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t"; +const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t"; +const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t"; +const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t"; +const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t"; +const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t"; +const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t"; +const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t"; +const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t"; +const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t"; +const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t"; +const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes"; +const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:"; +const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t"; +const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t"; +const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t"; +const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t"; +const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t"; +const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:"; +const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t"; +const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t"; +const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t"; +const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t"; +const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t"; +const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t"; +const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t"; +const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:"; +const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t"; +const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t"; +const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t"; +const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:"; +const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t"; +const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t"; +const char Unk_Contents_str[] PROGMEM ="\r\nContents:\t"; + +#endif // __PGMSTRINGS_H__
\ No newline at end of file diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/ArduinoBlinkLED/ArduinoBlinkLED.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/ArduinoBlinkLED/ArduinoBlinkLED.ino new file mode 100644 index 0000000000..d59b9bb3dc --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/ArduinoBlinkLED/ArduinoBlinkLED.ino @@ -0,0 +1,89 @@ +// The source for the Android application can be found at the following link: https://github.com/Lauszus/ArduinoBlinkLED +// The code for the Android application is heavily based on this guide: http://allaboutee.com/2011/12/31/arduino-adk-board-blink-an-led-with-your-phone-code-and-explanation/ by Miguel +#include <adk.h> + +// +// CAUTION! WARNING! ATTENTION! VORSICHT! ADVARSEL! ¡CUIDADO! ВНИМАНИЕ! +// +// Pin 13 is occupied by the SCK pin on various Arduino boards, +// including Uno, Duemilanove, etc., so use a different pin for those boards. +// +// CAUTION! WARNING! ATTENTION! VORSICHT! ADVARSEL! ¡CUIDADO! ВНИМАНИЕ! +// +#if defined(LED_BUILTIN) +#define LED LED_BUILTIN // Use built in LED +#else +#define LED 9 // Set to something here that makes sense for your board. +#endif + + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +ADK adk(&Usb, "TKJElectronics", // Manufacturer Name + "ArduinoBlinkLED", // Model Name + "Example sketch for the USB Host Shield", // Description (user-visible string) + "1.0", // Version + "http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk", // URL (web page to visit if no installed apps support the accessory) + "123456789"); // Serial Number (optional) + +uint32_t timer; +bool connected; + +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("\r\nOSCOKIRQ failed to assert"); + while (1); // halt + } + pinMode(LED, OUTPUT); + Serial.print("\r\nArduino Blink LED Started"); +} + +void loop() { + Usb.Task(); + + if (adk.isReady()) { + if (!connected) { + connected = true; + Serial.print(F("\r\nConnected to accessory")); + } + + uint8_t msg[1]; + uint16_t len = sizeof(msg); + uint8_t rcode = adk.RcvData(&len, msg); + if (rcode && rcode != hrNAK) { + Serial.print(F("\r\nData rcv: ")); + Serial.print(rcode, HEX); + } else if (len > 0) { + Serial.print(F("\r\nData Packet: ")); + Serial.print(msg[0]); + digitalWrite(LED, msg[0] ? HIGH : LOW); + } + + if (millis() - timer >= 1000) { // Send data every 1s + timer = millis(); + rcode = adk.SndData(sizeof(timer), (uint8_t*)&timer); + if (rcode && rcode != hrNAK) { + Serial.print(F("\r\nData send: ")); + Serial.print(rcode, HEX); + } else if (rcode != hrNAK) { + Serial.print(F("\r\nTimer: ")); + Serial.print(timer); + } + } + } else { + if (connected) { + connected = false; + Serial.print(F("\r\nDisconnected from accessory")); + digitalWrite(LED, LOW); + } + } +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/adk_barcode/adk_barcode.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/adk_barcode/adk_barcode.ino new file mode 100644 index 0000000000..a308ff0f83 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/adk_barcode/adk_barcode.ino @@ -0,0 +1,91 @@ +/**/ +/* A sketch demonstrating data exchange between two USB devices - a HID barcode scanner and ADK-compatible Android phone */ +/**/ +#include <adk.h> +#include <hidboot.h> +#include <usbhub.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +USBHub Hub1(&Usb); +USBHub Hub2(&Usb); +HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard(&Usb); + +ADK adk(&Usb,"Circuits@Home, ltd.", + "USB Host Shield", + "Arduino Terminal for Android", + "1.0", + "http://www.circuitsathome.com", + "0000000000000001"); + + +class KbdRptParser : public KeyboardReportParser +{ + +protected: + void OnKeyDown (uint8_t mod, uint8_t key); + void OnKeyPressed(uint8_t key); +}; + +void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) +{ + uint8_t c = OemToAscii(mod, key); + + if (c) + OnKeyPressed(c); +} + +/* what to do when symbol arrives */ +void KbdRptParser::OnKeyPressed(uint8_t key) +{ +const char* new_line = "\n"; +uint8_t rcode; +uint8_t keylcl; + + if( adk.isReady() == false ) { + return; + } + + keylcl = key; + + if( keylcl == 0x13 ) { + rcode = adk.SndData( strlen( new_line ), (uint8_t *)new_line ); + } + else { + rcode = adk.SndData( 1, &keylcl ); + } + + Serial.print((char) keylcl ); + Serial.print(" : "); + Serial.println( keylcl, HEX ); +}; + +KbdRptParser Prs; + +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 + Serial.println("\r\nADK demo start"); + + if (Usb.Init() == -1) { + Serial.println("OSCOKIRQ failed to assert"); + while(1); //halt + }//if (Usb.Init() == -1... + + Keyboard.SetReportParser(0, (HIDReportParser*)&Prs); + + delay( 200 ); +} + +void loop() +{ + Usb.Task(); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/demokit_20/demokit_20.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/demokit_20/demokit_20.ino new file mode 100644 index 0000000000..f65adf57bb --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/demokit_20/demokit_20.ino @@ -0,0 +1,103 @@ +#include <adk.h> +#include <usbhub.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +USBHub hub0(&Usb); +USBHub hub1(&Usb); +ADK adk(&Usb,"Google, Inc.", + "DemoKit", + "DemoKit Arduino Board", + "1.0", + "http://www.android.com", + "0000000012345678"); +uint8_t b, b1; + + +#define LED1_RED 3 +#define BUTTON1 2 + +void init_buttons() +{ + pinMode(BUTTON1, INPUT); + + // enable the internal pullups + digitalWrite(BUTTON1, HIGH); +} + +void init_leds() +{ + digitalWrite(LED1_RED, 0); + + pinMode(LED1_RED, OUTPUT); +} + +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 + Serial.println("\r\nADK demo start"); + + if (Usb.Init() == -1) { + Serial.println("OSCOKIRQ failed to assert"); + while(1); //halt + }//if (Usb.Init() == -1... + + + init_leds(); + init_buttons(); + b1 = digitalRead(BUTTON1); +} + +void loop() +{ + uint8_t rcode; + uint8_t msg[3] = { 0x00 }; + Usb.Task(); + + if( adk.isReady() == false ) { + analogWrite(LED1_RED, 255); + return; + } + uint16_t len = sizeof(msg); + + rcode = adk.RcvData(&len, msg); + if( rcode ) { + USBTRACE2("Data rcv. :", rcode ); + } + if(len > 0) { + USBTRACE("\r\nData Packet."); + // assumes only one command per packet + if (msg[0] == 0x2) { + switch( msg[1] ) { + case 0: + analogWrite(LED1_RED, 255 - msg[2]); + break; + }//switch( msg[1]... + }//if (msg[0] == 0x2... + }//if( len > 0... + + msg[0] = 0x1; + + b = digitalRead(BUTTON1); + if (b != b1) { + USBTRACE("\r\nButton state changed"); + msg[1] = 0; + msg[2] = b ? 0 : 1; + rcode = adk.SndData( 3, msg ); + if( rcode ) { + USBTRACE2("Button send: ", rcode ); + } + b1 = b; + }//if (b != b1... + + + delay( 10 ); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/term_test/term_test.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/term_test/term_test.ino new file mode 100644 index 0000000000..db681c3b50 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/term_test/term_test.ino @@ -0,0 +1,65 @@ +#include <adk.h> +#include <usbhub.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +//USBHub Hub(&Usb); + +ADK adk(&Usb,"Circuits@Home, ltd.", + "USB Host Shield", + "Arduino Terminal for Android", + "1.0", + "http://www.circuitsathome.com", + "0000000000000001"); + +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 + Serial.println("\r\nADK demo start"); + + if (Usb.Init() == -1) { + Serial.println("OSCOKIRQ failed to assert"); + while(1); //halt + }//if (Usb.Init() == -1... +} + +void loop() +{ + uint8_t rcode; + uint8_t msg[64] = { 0x00 }; + const char* recv = "Received: "; + + Usb.Task(); + + if( adk.isReady() == false ) { + return; + } + uint16_t len = 64; + + rcode = adk.RcvData(&len, msg); + if( rcode & ( rcode != hrNAK )) { + USBTRACE2("Data rcv. :", rcode ); + } + if(len > 0) { + USBTRACE("\r\nData Packet."); + + for( uint8_t i = 0; i < len; i++ ) { + Serial.print((char)msg[i]); + } + /* sending back what was received */ + rcode = adk.SndData( strlen( recv ), (uint8_t *)recv ); + rcode = adk.SndData( strlen(( char * )msg ), msg ); + + }//if( len > 0 )... + + delay( 1000 ); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/term_time/term_time.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/term_time/term_time.ino new file mode 100644 index 0000000000..a3f1dbc8cb --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/term_time/term_time.ino @@ -0,0 +1,50 @@ +#include <adk.h> +#include <usbhub.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; + +ADK adk(&Usb,"Circuits@Home, ltd.", + "USB Host Shield", + "Arduino Terminal for Android", + "1.0", + "http://www.circuitsathome.com", + "0000000000000001"); + +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 + Serial.println("\r\nADK demo start"); + + if (Usb.Init() == -1) { + Serial.println("OSCOKIRQ failed to assert"); + while(1); //halt + }//if (Usb.Init() == -1... +} + +void loop() +{ + uint8_t buf[ 12 ] = { 0 }; //buffer to convert unsigned long to ASCII + const char* sec_ela = " seconds elapsed\r"; + uint8_t rcode; + + Usb.Task(); + if( adk.isReady() == false ) { + return; + } + + ultoa( millis()/1000, (char *)buf, 10 ); + + rcode = adk.SndData( strlen((char *)buf), buf ); + rcode = adk.SndData( strlen( sec_ela), (uint8_t *)sec_ela ); + + delay( 1000 ); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/board_qc/board_qc.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/board_qc/board_qc.ino new file mode 100644 index 0000000000..573c3ce083 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/board_qc/board_qc.ino @@ -0,0 +1,259 @@ +/* USB Host Shield 2.0 board quality control routine */ +/* To see the output set your terminal speed to 115200 */ +/* for GPIO test to pass you need to connect GPIN0 to GPOUT7, GPIN1 to GPOUT6, etc. */ +/* otherwise press any key after getting GPIO error to complete the test */ +/**/ +#include <usbhub.h> + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <../../../../hardware/pic32/libraries/SPI/SPI.h> // Hack to use the SPI library +#include <SPI.h> // Hack to use the SPI library +#endif + +/* variables */ +uint8_t rcode; +uint8_t usbstate; +uint8_t laststate; +//uint8_t buf[sizeof(USB_DEVICE_DESCRIPTOR)]; +USB_DEVICE_DESCRIPTOR buf; + +/* objects */ +USB Usb; +//USBHub hub(&Usb); + +void setup() { + laststate = 0; + 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 + E_Notify(PSTR("\r\nCircuits At Home 2011"), 0x80); + E_Notify(PSTR("\r\nUSB Host Shield Quality Control Routine"), 0x80); + /* SPI quick test - check revision register */ + E_Notify(PSTR("\r\nReading REVISION register... Die revision "), 0x80); + Usb.Init(); // Initializes SPI, we don't care about the return value here + { + uint8_t tmpbyte = Usb.regRd(rREVISION); + switch(tmpbyte) { + case( 0x01): //rev.01 + E_Notify(PSTR("01"), 0x80); + break; + case( 0x12): //rev.02 + E_Notify(PSTR("02"), 0x80); + break; + case( 0x13): //rev.03 + E_Notify(PSTR("03"), 0x80); + break; + default: + E_Notify(PSTR("invalid. Value returned: "), 0x80); + print_hex(tmpbyte, 8); + halt55(); + break; + }//switch( tmpbyte... + }//check revision register + /* SPI long test */ + { + E_Notify(PSTR("\r\nSPI long test. Transfers 1MB of data. Each dot is 64K"), 0x80); + uint8_t sample_wr = 0; + uint8_t sample_rd = 0; + uint8_t gpinpol_copy = Usb.regRd(rGPINPOL); + for(uint8_t i = 0; i < 16; i++) { + for(uint16_t j = 0; j < 65535; j++) { + Usb.regWr(rGPINPOL, sample_wr); + sample_rd = Usb.regRd(rGPINPOL); + if(sample_rd != sample_wr) { + E_Notify(PSTR("\r\nTest failed. "), 0x80); + E_Notify(PSTR("Value written: "), 0x80); + print_hex(sample_wr, 8); + E_Notify(PSTR(" read: "), 0x80); + print_hex(sample_rd, 8); + halt55(); + }//if( sample_rd != sample_wr.. + sample_wr++; + }//for( uint16_t j... + E_Notify(PSTR("."), 0x80); + }//for( uint8_t i... + Usb.regWr(rGPINPOL, gpinpol_copy); + E_Notify(PSTR(" SPI long test passed"), 0x80); + }//SPI long test + /* GPIO test */ + /* in order to simplify board layout, GPIN pins on text fixture are connected to GPOUT */ + /* in reverse order, i.e, GPIN0 is connected to GPOUT7, GPIN1 to GPOUT6, etc. */ + { + uint8_t tmpbyte; + E_Notify(PSTR("\r\nGPIO test. Connect GPIN0 to GPOUT7, GPIN1 to GPOUT6, and so on"), 0x80); + for(uint8_t sample_gpio = 0; sample_gpio < 255; sample_gpio++) { + Usb.gpioWr(sample_gpio); + tmpbyte = Usb.gpioRd(); + /* bit reversing code copied vetbatim from http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */ + tmpbyte = ((tmpbyte * 0x0802LU & 0x22110LU) | (tmpbyte * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16; + if(sample_gpio != tmpbyte) { + E_Notify(PSTR("\r\nTest failed. Value written: "), 0x80); + print_hex(sample_gpio, 8); + E_Notify(PSTR(" Value read: "), 0x80); + print_hex(tmpbyte, 8); + E_Notify(PSTR(" "), 0x80); + press_any_key(); + break; + }//if( sample_gpio != tmpbyte... + }//for( uint8_t sample_gpio... + E_Notify(PSTR("\r\nGPIO test passed."), 0x80); + }//GPIO test + /* PLL test. Stops/starts MAX3421E oscillator several times */ + { + E_Notify(PSTR("\r\nPLL test. 100 chip resets will be performed"), 0x80); + /* check current state of the oscillator */ + if(!(Usb.regRd(rUSBIRQ) & bmOSCOKIRQ)) { //wrong state - should be on + E_Notify(PSTR("\r\nCurrent oscillator state unexpected."), 0x80); + press_any_key(); + } + /* Restart oscillator */ + E_Notify(PSTR("\r\nResetting oscillator\r\n"), 0x80); + for(uint16_t i = 0; i < 100; i++) { + E_Notify(PSTR("\rReset number "), 0x80); + Serial.print(i, DEC); + Usb.regWr(rUSBCTL, bmCHIPRES); //reset + if(Usb.regRd(rUSBIRQ) & bmOSCOKIRQ) { //wrong state - should be off + E_Notify(PSTR("\r\nCurrent oscillator state unexpected."), 0x80); + halt55(); + } + Usb.regWr(rUSBCTL, 0x00); //release from reset + uint16_t j = 0; + for(j = 1; j < 65535; j++) { //tracking off to on time + if(Usb.regRd(rUSBIRQ) & bmOSCOKIRQ) { + E_Notify(PSTR(" Time to stabilize - "), 0x80); + Serial.print(j, DEC); + E_Notify(PSTR(" cycles\r\n"), 0x80); + break; + } + }//for( uint16_t j = 0; j < 65535; j++ + if(j == 0) { + E_Notify(PSTR("PLL failed to stabilize"), 0x80); + press_any_key(); + } + }//for( uint8_t i = 0; i < 255; i++ + + }//PLL test + /* initializing USB stack */ + if(Usb.Init() == -1) { + E_Notify(PSTR("\r\nOSCOKIRQ failed to assert"), 0x80); + halt55(); + } + E_Notify(PSTR("\r\nChecking USB device communication.\r\n"), 0x80); +} + +void loop() { + delay(200); + Usb.Task(); + usbstate = Usb.getUsbTaskState(); + if(usbstate != laststate) { + laststate = usbstate; + /**/ + switch(usbstate) { + case( USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE): + E_Notify(PSTR("\r\nWaiting for device..."), 0x80); + break; + case( USB_ATTACHED_SUBSTATE_RESET_DEVICE): + E_Notify(PSTR("\r\nDevice connected. Resetting..."), 0x80); + break; + case( USB_ATTACHED_SUBSTATE_WAIT_SOF): + E_Notify(PSTR("\r\nReset complete. Waiting for the first SOF..."), 0x80); + break; + case( USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE): + E_Notify(PSTR("\r\nSOF generation started. Enumerating device..."), 0x80); + break; + case( USB_STATE_ADDRESSING): + E_Notify(PSTR("\r\nSetting device address..."), 0x80); + break; + case( USB_STATE_RUNNING): + E_Notify(PSTR("\r\nGetting device descriptor"), 0x80); + rcode = Usb.getDevDescr(1, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*) & buf); + + if(rcode) { + E_Notify(PSTR("\r\nError reading device descriptor. Error code "), 0x80); + print_hex(rcode, 8); + } else { + /**/ + E_Notify(PSTR("\r\nDescriptor Length:\t"), 0x80); + print_hex(buf.bLength, 8); + E_Notify(PSTR("\r\nDescriptor type:\t"), 0x80); + print_hex(buf.bDescriptorType, 8); + E_Notify(PSTR("\r\nUSB version:\t\t"), 0x80); + print_hex(buf.bcdUSB, 16); + E_Notify(PSTR("\r\nDevice class:\t\t"), 0x80); + print_hex(buf.bDeviceClass, 8); + E_Notify(PSTR("\r\nDevice Subclass:\t"), 0x80); + print_hex(buf.bDeviceSubClass, 8); + E_Notify(PSTR("\r\nDevice Protocol:\t"), 0x80); + print_hex(buf.bDeviceProtocol, 8); + E_Notify(PSTR("\r\nMax.packet size:\t"), 0x80); + print_hex(buf.bMaxPacketSize0, 8); + E_Notify(PSTR("\r\nVendor ID:\t\t"), 0x80); + print_hex(buf.idVendor, 16); + E_Notify(PSTR("\r\nProduct ID:\t\t"), 0x80); + print_hex(buf.idProduct, 16); + E_Notify(PSTR("\r\nRevision ID:\t\t"), 0x80); + print_hex(buf.bcdDevice, 16); + E_Notify(PSTR("\r\nMfg.string index:\t"), 0x80); + print_hex(buf.iManufacturer, 8); + E_Notify(PSTR("\r\nProd.string index:\t"), 0x80); + print_hex(buf.iProduct, 8); + E_Notify(PSTR("\r\nSerial number index:\t"), 0x80); + print_hex(buf.iSerialNumber, 8); + E_Notify(PSTR("\r\nNumber of conf.:\t"), 0x80); + print_hex(buf.bNumConfigurations, 8); + /**/ + E_Notify(PSTR("\r\n\nAll tests passed. Press RESET to restart test"), 0x80); + while(1); + } + break; + case( USB_STATE_ERROR): + E_Notify(PSTR("\r\nUSB state machine reached error state"), 0x80); + break; + + default: + break; + }//switch( usbstate... + } +}//loop()... + +/* constantly transmits 0x55 via SPI to aid probing */ +void halt55() { + + E_Notify(PSTR("\r\nUnrecoverable error - test halted!!"), 0x80); + E_Notify(PSTR("\r\n0x55 pattern is transmitted via SPI"), 0x80); + E_Notify(PSTR("\r\nPress RESET to restart test"), 0x80); + + while(1) { + Usb.regWr(0x55, 0x55); + } +} + +/* prints hex numbers with leading zeroes */ +void print_hex(int v, int num_places) { + int mask = 0, n, num_nibbles, digit; + + for(n = 1; n <= num_places; n++) { + mask = (mask << 1) | 0x0001; + } + v = v & mask; // truncate v to specified number of places + + num_nibbles = num_places / 4; + if((num_places % 4) != 0) { + ++num_nibbles; + } + do { + digit = ((v >> (num_nibbles - 1) * 4)) & 0x0f; + Serial.print(digit, HEX); + } while(--num_nibbles); +} + +/* prints "Press any key" and returns when key is pressed */ +void press_any_key() { + E_Notify(PSTR("\r\nPress any key to continue..."), 0x80); + while(Serial.available() <= 0); //wait for input + Serial.read(); //empty input buffer + return; +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/cdc_XR21B1411/XR_terminal/XR_terminal.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/cdc_XR21B1411/XR_terminal/XR_terminal.ino new file mode 100644 index 0000000000..0173a08b50 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/cdc_XR21B1411/XR_terminal/XR_terminal.ino @@ -0,0 +1,83 @@ +#include <cdc_XR21B1411.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +class ACMAsyncOper : public CDCAsyncOper +{ +public: + uint8_t OnInit(ACM *pacm); +}; + +uint8_t ACMAsyncOper::OnInit(ACM *pacm) +{ + uint8_t rcode; + // Set DTR = 1 RTS=1 + rcode = pacm->SetControlLineState(3); + + if (rcode) + { + ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode); + return rcode; + } + + LINE_CODING lc; + lc.dwDTERate = 115200; + lc.bCharFormat = 0; + lc.bParityType = 0; + lc.bDataBits = 8; + + rcode = pacm->SetLineCoding(&lc); + + if (rcode) + ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode); + + return rcode; +} + +USB Usb; +ACMAsyncOper AsyncOper; +XR21B1411 Acm(&Usb, &AsyncOper); + +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 + Serial.println("\r\n\r\nStart"); + + if (Usb.Init() == -1) Serial.println("OSCOKIRQ failed to assert"); +} + +void loop() { + Usb.Task(); + if( Acm.isReady()) { + uint8_t rcode; + uint8_t buf[1]; + uint16_t rcvd = 1; + + /* read keyboard */ + if(Serial.available()) { + uint8_t data = Serial.read(); + /* send */ + rcode = Acm.SndData(1, &data); + if (rcode) + ErrorMessage<uint8_t>(PSTR("SndData"), rcode); + } + + /* read XR serial */ + rcode = Acm.RcvData(&rcvd, buf); + if (rcode && rcode != hrNAK) + ErrorMessage<uint8_t>(PSTR("Ret"), rcode); + + if( rcvd ) { //more than zero bytes received + for(uint16_t i=0; i < rcvd; i++ ) { + Serial.print((char)buf[i]); + } + } + } +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/USBFTDILoopback.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/USBFTDILoopback.ino new file mode 100644 index 0000000000..5be7adc2f3 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/USBFTDILoopback.ino @@ -0,0 +1,98 @@ +#include <cdcftdi.h> +#include <usbhub.h> + +#include "pgmstrings.h" + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +class FTDIAsync : public FTDIAsyncOper +{ +public: + uint8_t OnInit(FTDI *pftdi); +}; + +uint8_t FTDIAsync::OnInit(FTDI *pftdi) +{ + uint8_t rcode = 0; + + rcode = pftdi->SetBaudRate(115200); + + if (rcode) + { + ErrorMessage<uint8_t>(PSTR("SetBaudRate"), rcode); + return rcode; + } + rcode = pftdi->SetFlowControl(FTDI_SIO_DISABLE_FLOW_CTRL); + + if (rcode) + ErrorMessage<uint8_t>(PSTR("SetFlowControl"), rcode); + + return rcode; +} + +USB Usb; +//USBHub Hub(&Usb); +FTDIAsync FtdiAsync; +FTDI Ftdi(&Usb, &FtdiAsync); + +uint32_t next_time; + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + next_time = millis() + 5000; +} + +void loop() +{ + Usb.Task(); + + if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) + { + uint8_t rcode; + char strbuf[] = "DEADBEEF"; + //char strbuf[] = "The quick brown fox jumps over the lazy dog"; + //char strbuf[] = "This string contains 61 character to demonstrate FTDI buffers"; //add one symbol to it to see some garbage + Serial.print("."); + + rcode = Ftdi.SndData(strlen(strbuf), (uint8_t*)strbuf); + + if (rcode) + ErrorMessage<uint8_t>(PSTR("SndData"), rcode); + + delay(50); + + uint8_t buf[64]; + + for (uint8_t i=0; i<64; i++) + buf[i] = 0; + + uint16_t rcvd = 64; + rcode = Ftdi.RcvData(&rcvd, buf); + + if (rcode && rcode != hrNAK) + ErrorMessage<uint8_t>(PSTR("Ret"), rcode); + + // The device reserves the first two bytes of data + // to contain the current values of the modem and line status registers. + if (rcvd > 2) + Serial.print((char*)(buf+2)); + + delay(10); + } +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/pgmstrings.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/pgmstrings.h new file mode 100644 index 0000000000..bdb0077ecc --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/pgmstrings.h @@ -0,0 +1,52 @@ +#if !defined(__PGMSTRINGS_H__) +#define __PGMSTRINGS_H__ + +#define LOBYTE(x) ((char*)(&(x)))[0] +#define HIBYTE(x) ((char*)(&(x)))[1] +#define BUFSIZE 256 //buffer size + + +/* Print strings in Program Memory */ +const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t"; +const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: "; +const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t"; +const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t"; +const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t"; +const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t"; +const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t"; +const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t"; +const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t"; +const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t"; +const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t"; +const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t"; +const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t"; +const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t"; +const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t"; +const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t"; +const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes"; +const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:"; +const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t"; +const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t"; +const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t"; +const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t"; +const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t"; +const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:"; +const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t"; +const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t"; +const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t"; +const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t"; +const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t"; +const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t"; +const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t"; +const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:"; +const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t"; +const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t"; +const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t"; +const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:"; +const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t"; +const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t"; +const char Unk_Contents_str[] PROGMEM ="\r\nContents:\t"; + +#endif // __PGMSTRINGS_H__
\ No newline at end of file diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/hub_demo/hub_demo.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/hub_demo/hub_demo.ino new file mode 100644 index 0000000000..d8b2d4bb72 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/hub_demo/hub_demo.ino @@ -0,0 +1,345 @@ +#include <usbhub.h> +#include "pgmstrings.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); +USBHub Hub2(&Usb); +USBHub Hub3(&Usb); +USBHub Hub4(&Usb); + +uint32_t next_time; + +void PrintAllAddresses(UsbDevice *pdev) +{ + UsbDeviceAddress adr; + adr.devAddress = pdev->address.devAddress; + Serial.print("\r\nAddr:"); + Serial.print(adr.devAddress, HEX); + Serial.print("("); + Serial.print(adr.bmHub, HEX); + Serial.print("."); + Serial.print(adr.bmParent, HEX); + Serial.print("."); + Serial.print(adr.bmAddress, HEX); + Serial.println(")"); +} + +void PrintAddress(uint8_t addr) +{ + UsbDeviceAddress adr; + adr.devAddress = addr; + Serial.print("\r\nADDR:\t"); + Serial.println(adr.devAddress, HEX); + Serial.print("DEV:\t"); + Serial.println(adr.bmAddress, HEX); + Serial.print("PRNT:\t"); + Serial.println(adr.bmParent, HEX); + Serial.print("HUB:\t"); + Serial.println(adr.bmHub, HEX); +} + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + next_time = millis() + 10000; +} + +byte getdevdescr( byte addr, byte &num_conf ); + +void PrintDescriptors(uint8_t addr) +{ + uint8_t rcode = 0; + byte num_conf = 0; + + rcode = getdevdescr( (byte)addr, num_conf ); + if ( rcode ) + { + printProgStr(Gen_Error_str); + print_hex( rcode, 8 ); + } + Serial.print("\r\n"); + + for (int i = 0; i < num_conf; i++) + { + rcode = getconfdescr( addr, i ); // get configuration descriptor + if ( rcode ) + { + printProgStr(Gen_Error_str); + print_hex(rcode, 8); + } + Serial.println("\r\n"); + } +} + +void PrintAllDescriptors(UsbDevice *pdev) +{ + Serial.println("\r\n"); + print_hex(pdev->address.devAddress, 8); + Serial.println("\r\n--"); + PrintDescriptors( pdev->address.devAddress ); +} + +void loop() +{ + Usb.Task(); + + if ( Usb.getUsbTaskState() == USB_STATE_RUNNING ) + { + if ((millis() - next_time) >= 0L) + { + Usb.ForEachUsbDevice(&PrintAllDescriptors); + Usb.ForEachUsbDevice(&PrintAllAddresses); + + while ( 1 ); //stop + } + } +} + +byte getdevdescr( byte addr, byte &num_conf ) +{ + USB_DEVICE_DESCRIPTOR buf; + byte rcode; + rcode = Usb.getDevDescr( addr, 0, 0x12, ( uint8_t *)&buf ); + if ( rcode ) { + return ( rcode ); + } + printProgStr(Dev_Header_str); + printProgStr(Dev_Length_str); + print_hex( buf.bLength, 8 ); + printProgStr(Dev_Type_str); + print_hex( buf.bDescriptorType, 8 ); + printProgStr(Dev_Version_str); + print_hex( buf.bcdUSB, 16 ); + printProgStr(Dev_Class_str); + print_hex( buf.bDeviceClass, 8 ); + printProgStr(Dev_Subclass_str); + print_hex( buf.bDeviceSubClass, 8 ); + printProgStr(Dev_Protocol_str); + print_hex( buf.bDeviceProtocol, 8 ); + printProgStr(Dev_Pktsize_str); + print_hex( buf.bMaxPacketSize0, 8 ); + printProgStr(Dev_Vendor_str); + print_hex( buf.idVendor, 16 ); + printProgStr(Dev_Product_str); + print_hex( buf.idProduct, 16 ); + printProgStr(Dev_Revision_str); + print_hex( buf.bcdDevice, 16 ); + printProgStr(Dev_Mfg_str); + print_hex( buf.iManufacturer, 8 ); + printProgStr(Dev_Prod_str); + print_hex( buf.iProduct, 8 ); + printProgStr(Dev_Serial_str); + print_hex( buf.iSerialNumber, 8 ); + printProgStr(Dev_Nconf_str); + print_hex( buf.bNumConfigurations, 8 ); + num_conf = buf.bNumConfigurations; + return ( 0 ); +} + +void printhubdescr(uint8_t *descrptr, uint8_t addr) +{ + HubDescriptor *pHub = (HubDescriptor*) descrptr; + uint8_t len = *((uint8_t*)descrptr); + + printProgStr(PSTR("\r\n\r\nHub Descriptor:\r\n")); + printProgStr(PSTR("bDescLength:\t\t")); + Serial.println(pHub->bDescLength, HEX); + + printProgStr(PSTR("bDescriptorType:\t")); + Serial.println(pHub->bDescriptorType, HEX); + + printProgStr(PSTR("bNbrPorts:\t\t")); + Serial.println(pHub->bNbrPorts, HEX); + + printProgStr(PSTR("LogPwrSwitchMode:\t")); + Serial.println(pHub->LogPwrSwitchMode, BIN); + + printProgStr(PSTR("CompoundDevice:\t\t")); + Serial.println(pHub->CompoundDevice, BIN); + + printProgStr(PSTR("OverCurrentProtectMode:\t")); + Serial.println(pHub->OverCurrentProtectMode, BIN); + + printProgStr(PSTR("TTThinkTime:\t\t")); + Serial.println(pHub->TTThinkTime, BIN); + + printProgStr(PSTR("PortIndicatorsSupported:")); + Serial.println(pHub->PortIndicatorsSupported, BIN); + + printProgStr(PSTR("Reserved:\t\t")); + Serial.println(pHub->Reserved, HEX); + + printProgStr(PSTR("bPwrOn2PwrGood:\t\t")); + Serial.println(pHub->bPwrOn2PwrGood, HEX); + + printProgStr(PSTR("bHubContrCurrent:\t")); + Serial.println(pHub->bHubContrCurrent, HEX); + + for (uint8_t i = 7; i < len; i++) + print_hex(descrptr[i], 8); + + //for (uint8_t i=1; i<=pHub->bNbrPorts; i++) + // PrintHubPortStatus(&Usb, addr, i, 1); +} + +byte getconfdescr( byte addr, byte conf ) +{ + uint8_t buf[ BUFSIZE ]; + uint8_t* buf_ptr = buf; + byte rcode; + byte descr_length; + byte descr_type; + unsigned int total_length; + rcode = Usb.getConfDescr( addr, 0, 4, conf, buf ); //get total length + LOBYTE( total_length ) = buf[ 2 ]; + HIBYTE( total_length ) = buf[ 3 ]; + if ( total_length > 256 ) { //check if total length is larger than buffer + printProgStr(Conf_Trunc_str); + total_length = 256; + } + rcode = Usb.getConfDescr( addr, 0, total_length, conf, buf ); //get the whole descriptor + while ( buf_ptr < buf + total_length ) { //parsing descriptors + descr_length = *( buf_ptr ); + descr_type = *( buf_ptr + 1 ); + switch ( descr_type ) { + case ( USB_DESCRIPTOR_CONFIGURATION ): + printconfdescr( buf_ptr ); + break; + case ( USB_DESCRIPTOR_INTERFACE ): + printintfdescr( buf_ptr ); + break; + case ( USB_DESCRIPTOR_ENDPOINT ): + printepdescr( buf_ptr ); + break; + case 0x29: + printhubdescr( buf_ptr, addr ); + break; + default: + printunkdescr( buf_ptr ); + break; + }//switch( descr_type + buf_ptr = ( buf_ptr + descr_length ); //advance buffer pointer + }//while( buf_ptr <=... + return ( rcode ); +} +/* prints hex numbers with leading zeroes */ +// copyright, Peter H Anderson, Baltimore, MD, Nov, '07 +// source: http://www.phanderson.com/arduino/arduino_display.html +void print_hex(int v, int num_places) +{ + int mask = 0, n, num_nibbles, digit; + + for (n = 1; n <= num_places; n++) { + mask = (mask << 1) | 0x0001; + } + v = v & mask; // truncate v to specified number of places + + num_nibbles = num_places / 4; + if ((num_places % 4) != 0) { + ++num_nibbles; + } + do { + digit = ((v >> (num_nibbles - 1) * 4)) & 0x0f; + Serial.print(digit, HEX); + } + while (--num_nibbles); +} +/* function to print configuration descriptor */ +void printconfdescr( uint8_t* descr_ptr ) +{ + USB_CONFIGURATION_DESCRIPTOR* conf_ptr = ( USB_CONFIGURATION_DESCRIPTOR* )descr_ptr; + printProgStr(Conf_Header_str); + printProgStr(Conf_Totlen_str); + print_hex( conf_ptr->wTotalLength, 16 ); + printProgStr(Conf_Nint_str); + print_hex( conf_ptr->bNumInterfaces, 8 ); + printProgStr(Conf_Value_str); + print_hex( conf_ptr->bConfigurationValue, 8 ); + printProgStr(Conf_String_str); + print_hex( conf_ptr->iConfiguration, 8 ); + printProgStr(Conf_Attr_str); + print_hex( conf_ptr->bmAttributes, 8 ); + printProgStr(Conf_Pwr_str); + print_hex( conf_ptr->bMaxPower, 8 ); + return; +} +/* function to print interface descriptor */ +void printintfdescr( uint8_t* descr_ptr ) +{ + USB_INTERFACE_DESCRIPTOR* intf_ptr = ( USB_INTERFACE_DESCRIPTOR* )descr_ptr; + printProgStr(Int_Header_str); + printProgStr(Int_Number_str); + print_hex( intf_ptr->bInterfaceNumber, 8 ); + printProgStr(Int_Alt_str); + print_hex( intf_ptr->bAlternateSetting, 8 ); + printProgStr(Int_Endpoints_str); + print_hex( intf_ptr->bNumEndpoints, 8 ); + printProgStr(Int_Class_str); + print_hex( intf_ptr->bInterfaceClass, 8 ); + printProgStr(Int_Subclass_str); + print_hex( intf_ptr->bInterfaceSubClass, 8 ); + printProgStr(Int_Protocol_str); + print_hex( intf_ptr->bInterfaceProtocol, 8 ); + printProgStr(Int_String_str); + print_hex( intf_ptr->iInterface, 8 ); + return; +} +/* function to print endpoint descriptor */ +void printepdescr( uint8_t* descr_ptr ) +{ + USB_ENDPOINT_DESCRIPTOR* ep_ptr = ( USB_ENDPOINT_DESCRIPTOR* )descr_ptr; + printProgStr(End_Header_str); + printProgStr(End_Address_str); + print_hex( ep_ptr->bEndpointAddress, 8 ); + printProgStr(End_Attr_str); + print_hex( ep_ptr->bmAttributes, 8 ); + printProgStr(End_Pktsize_str); + print_hex( ep_ptr->wMaxPacketSize, 16 ); + printProgStr(End_Interval_str); + print_hex( ep_ptr->bInterval, 8 ); + + return; +} +/*function to print unknown descriptor */ +void printunkdescr( uint8_t* descr_ptr ) +{ + byte length = *descr_ptr; + byte i; + printProgStr(Unk_Header_str); + printProgStr(Unk_Length_str); + print_hex( *descr_ptr, 8 ); + printProgStr(Unk_Type_str); + print_hex( *(descr_ptr + 1 ), 8 ); + printProgStr(Unk_Contents_str); + descr_ptr += 2; + for ( i = 0; i < length; i++ ) { + print_hex( *descr_ptr, 8 ); + descr_ptr++; + } +} + + +/* Print a string from Program Memory directly to save RAM */ +void printProgStr(const char* str) +{ + char c; + if (!str) return; + while ((c = pgm_read_byte(str++))) + Serial.print(c); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/hub_demo/pgmstrings.h b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/hub_demo/pgmstrings.h new file mode 100644 index 0000000000..bdb0077ecc --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/hub_demo/pgmstrings.h @@ -0,0 +1,52 @@ +#if !defined(__PGMSTRINGS_H__) +#define __PGMSTRINGS_H__ + +#define LOBYTE(x) ((char*)(&(x)))[0] +#define HIBYTE(x) ((char*)(&(x)))[1] +#define BUFSIZE 256 //buffer size + + +/* Print strings in Program Memory */ +const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t"; +const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: "; +const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t"; +const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t"; +const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t"; +const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t"; +const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t"; +const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t"; +const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t"; +const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t"; +const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t"; +const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t"; +const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t"; +const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t"; +const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t"; +const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t"; +const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes"; +const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:"; +const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t"; +const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t"; +const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t"; +const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t"; +const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t"; +const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:"; +const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t"; +const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t"; +const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t"; +const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t"; +const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t"; +const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t"; +const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t"; +const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:"; +const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t"; +const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t"; +const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t"; +const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:"; +const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t"; +const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t"; +const char Unk_Contents_str[] PROGMEM ="\r\nContents:\t"; + +#endif // __PGMSTRINGS_H__
\ No newline at end of file diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/max_LCD/max_LCD.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/max_LCD/max_LCD.ino new file mode 100644 index 0000000000..6603ab90db --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/max_LCD/max_LCD.ino @@ -0,0 +1,29 @@ +// Just a copy of the HelloWorld example bundled with the LiquidCrystal library in the Arduino IDE + +// HD44780 compatible LCD display via MAX3421E GPOUT support header +// pinout: D[4-7] -> GPOUT[4-7], RS-> GPOUT[2], E ->GPOUT[3] + +#include <max_LCD.h> + +// Satisfy IDE, which only needs to see the include statment in the ino. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +USB Usb; +Max_LCD lcd(&Usb); + +void setup() { + // Set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the LCD. + lcd.print("Hello, World!"); +} + +void loop() { + // Set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0): + lcd.setCursor(0, 1); + // Print the number of seconds since reset: + lcd.print(millis() / 1000); +} diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.ino new file mode 100644 index 0000000000..7c4c9f6cbe --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.ino @@ -0,0 +1,101 @@ +/* Arduino terminal for PL2303 USB to serial converter and DealeXtreme GPRS modem. */ +/* USB support */ +#include <usbhub.h> +/* CDC support */ +#include <cdcacm.h> +#include <cdcprolific.h> + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +class PLAsyncOper : public CDCAsyncOper +{ +public: + uint8_t OnInit(ACM *pacm); +}; + +uint8_t PLAsyncOper::OnInit(ACM *pacm) +{ + uint8_t rcode; + + // Set DTR = 1 + rcode = pacm->SetControlLineState(1); + + if (rcode) + { + ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode); + return rcode; + } + + LINE_CODING lc; + //lc.dwDTERate = 9600; + lc.dwDTERate = 115200; + lc.bCharFormat = 0; + lc.bParityType = 0; + lc.bDataBits = 8; + + rcode = pacm->SetLineCoding(&lc); + + if (rcode) + ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode); + + return rcode; +} +USB Usb; +//USBHub Hub(&Usb); +PLAsyncOper AsyncOper; +PL2303 Pl(&Usb, &AsyncOper); + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSCOKIRQ failed to assert"); + + delay( 200 ); +} + +void loop() +{ + Usb.Task(); + + if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) + { + uint8_t rcode; + + /* reading the keyboard */ + if(Serial.available()) { + uint8_t data= Serial.read(); + + /* sending to the phone */ + rcode = Pl.SndData(1, &data); + if (rcode) + ErrorMessage<uint8_t>(PSTR("SndData"), rcode); + }//if(Serial.available()... + + /* reading the converter */ + /* buffer size must be greater or equal to max.packet size */ + /* it it set to 64 (largest possible max.packet size) here, can be tuned down + for particular endpoint */ + uint8_t buf[64]; + uint16_t rcvd = 64; + rcode = Pl.RcvData(&rcvd, buf); + if (rcode && rcode != hrNAK) + ErrorMessage<uint8_t>(PSTR("Ret"), rcode); + + if( rcvd ) { //more than zero bytes received + for(uint16_t i=0; i < rcvd; i++ ) { + Serial.print((char)buf[i]); //printing on the screen + } + }//if( rcvd ... + }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING.. +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_gps/pl2303_gps.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_gps/pl2303_gps.ino new file mode 100644 index 0000000000..e8c8a02230 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_gps/pl2303_gps.ino @@ -0,0 +1,88 @@ +/* USB Host to PL2303-based USB GPS unit interface */ +/* Navibee GM720 receiver - Sirf Star III */ +/* USB support */ +#include <usbhub.h> +/* CDC support */ +#include <cdcacm.h> +#include <cdcprolific.h> + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +class PLAsyncOper : public CDCAsyncOper { +public: + uint8_t OnInit(ACM *pacm); +}; + +uint8_t PLAsyncOper::OnInit(ACM *pacm) { + uint8_t rcode; + + // Set DTR = 1 + rcode = pacm->SetControlLineState(1); + + if(rcode) { + ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode); + return rcode; + } + + LINE_CODING lc; + lc.dwDTERate = 4800; //default serial speed of GPS unit + lc.bCharFormat = 0; + lc.bParityType = 0; + lc.bDataBits = 8; + + rcode = pacm->SetLineCoding(&lc); + + if(rcode) + ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode); + + return rcode; +} + +USB Usb; +USBHub Hub(&Usb); +PLAsyncOper AsyncOper; +PL2303 Pl(&Usb, &AsyncOper); +uint32_t read_delay; +#define READ_DELAY 100 + +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 + Serial.println("Start"); + + if(Usb.Init() == -1) + Serial.println("OSCOKIRQ failed to assert"); + + delay(200); +} + +void loop() { + uint8_t rcode; + uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint + uint16_t rcvd = 64; + + Usb.Task(); + + if(Pl.isReady()) { + /* reading the GPS */ + if((long)(millis() - read_delay) >= 0L) { + read_delay += READ_DELAY; + rcode = Pl.RcvData(&rcvd, buf); + if(rcode && rcode != hrNAK) + ErrorMessage<uint8_t>(PSTR("Ret"), rcode); + if(rcvd) { //more than zero bytes received + for(uint16_t i = 0; i < rcvd; i++) { + Serial.print((char)buf[i]); //printing on the screen + }//for( uint16_t i=0; i < rcvd; i++... + }//if( rcvd + }//if( read_delay > millis()... + }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING.. +} + + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_tinygps/pl2303_tinygps.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_tinygps/pl2303_tinygps.ino new file mode 100644 index 0000000000..d527eabe00 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_tinygps/pl2303_tinygps.ino @@ -0,0 +1,217 @@ +/* USB Host to PL2303-based USB GPS unit interface */ +/* Navibee GM720 receiver - Sirf Star III */ +/* Mikal Hart's TinyGPS library */ +/* test_with_gps_device library example modified for PL2302 access */ + +/* USB support */ +#include <usbhub.h> + +/* CDC support */ +#include <cdcacm.h> +#include <cdcprolific.h> + +#include <TinyGPS.h> + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +/* This sample code demonstrates the normal use of a TinyGPS object. + Modified to be used with USB Host Shield Library r2.0 + and USB Host Shield 2.0 +*/ + +class PLAsyncOper : public CDCAsyncOper +{ +public: + uint8_t OnInit(ACM *pacm); +}; + +uint8_t PLAsyncOper::OnInit(ACM *pacm) +{ + uint8_t rcode; + + // Set DTR = 1 + rcode = pacm->SetControlLineState(1); + + if (rcode) { + ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode); + return rcode; + } + + LINE_CODING lc; + lc.dwDTERate = 4800; //default serial speed of GPS unit + lc.bCharFormat = 0; + lc.bParityType = 0; + lc.bDataBits = 8; + + rcode = pacm->SetLineCoding(&lc); + + if (rcode) { + ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode); + } + + return rcode; +} + +USB Usb; +//USBHub Hub(&Usb); +PLAsyncOper AsyncOper; +PL2303 Pl(&Usb, &AsyncOper); +TinyGPS gps; + +void gpsdump(TinyGPS &gps); +bool feedgps(); +void printFloat(double f, int digits = 2); + +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 + + Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version()); + Serial.println("by Mikal Hart"); + Serial.println(); + Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS)); + Serial.println(); + /* USB Initialization */ + if (Usb.Init() == -1) { + Serial.println("OSCOKIRQ failed to assert"); + } + + delay( 200 ); +} + +void loop() +{ + Usb.Task(); + + if( Pl.isReady()) { + + bool newdata = false; + unsigned long start = millis(); + + // Every 5 seconds we print an update + while (millis() - start < 5000) { + if( feedgps()) { + newdata = true; + } + }//while (millis()... + + if (newdata) { + Serial.println("Acquired Data"); + Serial.println("-------------"); + gpsdump(gps); + Serial.println("-------------"); + Serial.println(); + }//if( newdata... + }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING... +} + +void printFloat(double number, int digits) +{ + // Handle negative numbers + if (number < 0.0) + { + Serial.print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i<digits; ++i) + rounding /= 10.0; + + number += rounding; + + // Extract the integer part of the number and print it + unsigned long int_part = (unsigned long)number; + double remainder = number - (double)int_part; + Serial.print(int_part); + + // Print the decimal point, but only if there are digits beyond + if (digits > 0) + Serial.print("."); + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + int toPrint = int(remainder); + Serial.print(toPrint); + remainder -= toPrint; + } +} + +void gpsdump(TinyGPS &gps) +{ + long lat, lon; + float flat, flon; + unsigned long age, date, time, chars; + int year; + byte month, day, hour, minute, second, hundredths; + unsigned short sentences, failed; + + gps.get_position(&lat, &lon, &age); + Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon); + Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); + + feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors + + gps.f_get_position(&flat, &flon, &age); + Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5); + Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); + + feedgps(); + + gps.get_datetime(&date, &time, &age); + Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print(" Time(hhmmsscc): "); Serial.print(time); + Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); + + feedgps(); + + gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age); + Serial.print("Date: "); Serial.print(static_cast<int>(month)); Serial.print("/"); Serial.print(static_cast<int>(day)); Serial.print("/"); Serial.print(year); + Serial.print(" Time: "); Serial.print(static_cast<int>(hour)); Serial.print(":"); Serial.print(static_cast<int>(minute)); Serial.print(":"); Serial.print(static_cast<int>(second)); Serial.print("."); Serial.print(static_cast<int>(hundredths)); + Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); + + feedgps(); + + Serial.print("Alt(cm): "); Serial.print(gps.altitude()); Serial.print(" Course(10^-2 deg): "); Serial.print(gps.course()); Serial.print(" Speed(10^-2 knots): "); Serial.println(gps.speed()); + Serial.print("Alt(float): "); printFloat(gps.f_altitude()); Serial.print(" Course(float): "); printFloat(gps.f_course()); Serial.println(); + Serial.print("Speed(knots): "); printFloat(gps.f_speed_knots()); Serial.print(" (mph): "); printFloat(gps.f_speed_mph()); + Serial.print(" (mps): "); printFloat(gps.f_speed_mps()); Serial.print(" (kmph): "); printFloat(gps.f_speed_kmph()); Serial.println(); + + feedgps(); + + gps.stats(&chars, &sentences, &failed); + Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed); +} + +bool feedgps() +{ + uint8_t rcode; + uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint + uint16_t rcvd = 64; + { + /* reading the GPS */ + rcode = Pl.RcvData(&rcvd, buf); + if (rcode && rcode != hrNAK) + ErrorMessage<uint8_t>(PSTR("Ret"), rcode); + rcode = false; + if( rcvd ) { //more than zero bytes received + for( uint16_t i=0; i < rcvd; i++ ) { + if( gps.encode((char)buf[i])) { //feed a character to gps object + rcode = true; + }//if( gps.encode(buf[i]... + }//for( uint16_t i=0; i < rcvd; i++... + }//if( rcvd... + } + return( rcode ); +} + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.ino new file mode 100644 index 0000000000..67b7dab603 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.ino @@ -0,0 +1,117 @@ +/* Arduino terminal for PL2303 USB to serial converter and XBee radio. */ +/* Inserts linefeed after carriage return in data sent to and received from Xbee */ +/* USB support */ +#include <usbhub.h> +/* CDC support */ +#include <cdcacm.h> +#include <cdcprolific.h> + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include <spi4teensy3.h> +#include <SPI.h> +#endif + +class PLAsyncOper : public CDCAsyncOper +{ +public: + uint8_t OnInit(ACM *pacm); +}; + +uint8_t PLAsyncOper::OnInit(ACM *pacm) +{ + uint8_t rcode; + + // Set DTR = 1 + rcode = pacm->SetControlLineState(1); + + if (rcode) + { + ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode); + return rcode; + } + + LINE_CODING lc; + lc.dwDTERate = 115200; + lc.bCharFormat = 0; + lc.bParityType = 0; + lc.bDataBits = 8; + + rcode = pacm->SetLineCoding(&lc); + + if (rcode) + ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode); + + return rcode; +} +USB Usb; +//USBHub Hub(&Usb); +PLAsyncOper AsyncOper; +PL2303 Pl(&Usb, &AsyncOper); + +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 + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSCOKIRQ failed to assert"); + + delay( 200 ); +} + +void loop() +{ + Usb.Task(); + + if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) + { + uint8_t rcode; + + /* reading the keyboard */ + if(Serial.available()) { + uint8_t data= Serial.read(); + + if ( data == '\r' ) { + Serial.print("\r\n"); //insert linefeed + } + else { + Serial.print( data ); //echo back to the screen + } + + /* sending to the phone */ + rcode = Pl.SndData(1, &data); + if (rcode) + ErrorMessage<uint8_t>(PSTR("SndData"), rcode); + }//if(Serial.available()... + + delay(50); + + /* reading the converter */ + /* buffer size must be greater or equal to max.packet size */ + /* it it set to 64 (largest possible max.packet size) here, can be tuned down + for particular endpoint */ + uint8_t buf[64]; + uint16_t rcvd = 64; + rcode = Pl.RcvData(&rcvd, buf); + if (rcode && rcode != hrNAK) + ErrorMessage<uint8_t>(PSTR("Ret"), rcode); + + if( rcvd ) { //more than zero bytes received + for(uint16_t i=0; i < rcvd; i++ ) { + if( buf[i] =='\r' ) { + Serial.print("\r\n"); //insert linefeed + } + else { + Serial.print((char)buf[i]); //printing on the screen + } + } + } + delay(10); + }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING.. +} + + diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/Makefile b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/Makefile new file mode 100644 index 0000000000..8a12ddc047 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/Makefile @@ -0,0 +1,64 @@ +# +# These are set for a mega 1280 + quadram plus my serial patch. +# If you lack quadram, or want to disable LFN, just change _FS_TINY=1 _USE_LFN=0 +# +# If your board is a mega 2560 comment out the following two lines +BOARD = mega + +BOARD_SUB = mega.menu.cpu.atmega1280 +PROGRAMMER = arduino + +# ...and then uncomment out the following two lines +#BOARD_SUB = mega.menu.cpu.atmega2560 +#PROGRAMMER = wiring + +#BOARD = teensypp2 +#BOARD = teensy3 +#BOARD = teensy31 + +# set your Arduino tty port here +PORT = /dev/ttyUSB0 + +EXTRA_FLAGS = -D _USE_LFN=3 + +# change to 0 if you have quadram to take advantage of caching FAT +EXTRA_FLAGS += -D _FS_TINY=1 + + +EXTRA_FLAGS += -D _MAX_SS=512 + + +# Don't worry if you don't have external RAM, xmem2 detects this situation. +# You *WILL* be wanting to get some kind of external ram on your mega in order to +# do anything that is intense. +EXTRA_FLAGS += -D EXT_RAM_STACK=1 +EXTRA_FLAGS += -D EXT_RAM_HEAP=1 + + +# These are no longer needed for the demo to work. +# In the event you need more ram, uncomment these 3 lines. +#EXTRA_FLAGS += -D DISABLE_SERIAL1 +#EXTRA_FLAGS += -D DISABLE_SERIAL2 +#EXTRA_FLAGS += -D DISABLE_SERIAL3 + +# +# Advanced debug on Serial3 +# + +# uncomment the next two to enable debug on Serial3 +EXTRA_FLAGS += -D USB_HOST_SERIAL=Serial3 +#EXTRA_FLAGS += -D DEBUG_USB_HOST + +# The following are the libraries used. +LIB_DIRS += ../../ +LIB_DIRS += ../testusbhostFAT/xmem2 +LIB_DIRS += ../testusbhostFAT/generic_storage +LIB_DIRS += ../testusbhostFAT/RTClib +LIB_DIRS += $(ARD_HOME)/libraries/Wire +LIB_DIRS += $(ARD_HOME)/libraries/Wire/utility +LIB_DIRS += $(ARD_HOME)/hardware/arduino/$(BUILD_ARCH)/libraries/Wire +LIB_DIRS += $(ARD_HOME)/hardware/arduino/$(BUILD_ARCH)/libraries/Wire/utility +LIB_DIRS += $(ARD_HOME)/hardware/arduino/$(BUILD_ARCH)/libraries/SPI + +# And finally, the part that brings everything together for you. +include Arduino_Makefile_master/_Makefile.master diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/README.md b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/README.md new file mode 100644 index 0000000000..d8b4296b1a --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/README.md @@ -0,0 +1,29 @@ +This small sketch tests the USB host shield mass storage library. + +__Note:__ This will not run a Arduino Uno due to the limited ram available in the ATmega328p. + +The Arduino Mega (ATmega1280) and the Arduino Mega 2560 (ATmega2560) are confirmed to work with this test code. + +To compile this example you will need the following libraries as well: + +* [xmem2](https://github.com/xxxajk/xmem2) +* [generic_storage FATfs](https://github.com/xxxajk/generic_storage) +* [RTClib](https://github.com/xxxajk/RTClib) + +The following shield is recommended for larger projects: <http://ruggedcircuits.com/html/quadram.html>. + +You may use the bundled [Makefile](Makefile) to compile the code instead of the Arduino IDE if you have problems or want a smaller binary. The master makefile is bundled as a submodule, but can also be downloaded manually at the following link: <https://github.com/xxxajk/Arduino_Makefile_master>. + +To download the USB Host library and all the needed libraries for this test. + +Run the following command in a terminal application: + +``` +git clone --recursive https://github.com/felis/USB_Host_Shield_2.0 +``` + +If you want to update all the submodules run: + +``` +git submodule foreach --recursive git pull origin master +``` diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/testusbhostFAT.ino b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/testusbhostFAT.ino new file mode 100644 index 0000000000..e8b9cd3592 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/testusbhostFAT/testusbhostFAT.ino @@ -0,0 +1,736 @@ +/* + * Mega + USB storage + optional DS1307 + optional expansion RAM + funky status LED, + * Includes interactive debug level setting, and supports hot-plug. + * + * IMPORTANT! PLEASE USE Arduino 1.0.5 or better! + * Older versions HAVE MAJOR BUGS AND WILL NOT WORK AT ALL! + * Use of gcc-avr and lib-c that is newer than the Arduino version is even better. + * If you experience random crashes, use make. + * The options that the IDE use can generate bad code and cause the AVR to crash. + * + * This sketch requires the following libraries: + * https://github.com/felis/USB_Host_Shield_2.0 Install as 'USB_Host_Shield_2_0' + * https://github.com/xxxajk/xmem2 Install as 'xmem', provides memory services. + * https://github.com/xxxajk/generic_storage provides access to FAT file system. + * https://github.com/xxxajk/RTClib provides access to DS1307, or fake clock. + * + * Optional, to use the Makefile (Recommended! See above!): + * https://github.com/xxxajk/Arduino_Makefile_master + * + */ + +///////////////////////////////////////////////////////////// +// Please Note: // +// This section is for info with the Arduino IDE ONLY. // +// Unfortunately due to short sightedness of the Arduino // +// code team, that you must set the following in the // +// respective libraries. // +// Changing them here will have _NO_ effect! // +///////////////////////////////////////////////////////////// + +// Uncomment to enable debugging +//#define DEBUG_USB_HOST +// This is where stderr/USB debugging goes to +//#define USB_HOST_SERIAL Serial3 + +// If you have external memory, setting this to 0 enables FAT table caches. +// The 0 setting is recommended only if you have external memory. +//#define _FS_TINY 1 + +//#define _USE_LFN 3 +//#define _MAX_SS 512 + + +///////////////////////////////////////////////////////////// +// End of Arduino IDE specific information // +///////////////////////////////////////////////////////////// + +// You can set this to 0 if you are not using a USB hub. +// It will save a little bit of flash and RAM. +// Set to 1 if you want to use a hub. +#define WANT_HUB_TEST 1 + +// this is for XMEM2 +#define EXT_RAM_STACK 1 +#define EXT_RAM_HEAP 1 +#define LOAD_XMEM + +#if defined(CORE_TEENSY) && !defined(_AVR_) +#include <xmem.h> +#include <spi4teensy3.h> +#endif + +#if defined(__AVR__) +#include <xmem.h> +#include <SPI.h> +#elif defined(ARDUINO_ARCH_SAM) +#include <SPI.h> +#endif + +#if WANT_HUB_TEST +#include <usbhub.h> +#endif +#include <Wire.h> +#define LOAD_RTCLIB +#include <RTClib.h> +#include <masstorage.h> +#include <Storage.h> +#include <PCpartition/PCPartition.h> +#include <avr/interrupt.h> +#include <FAT/FAT.h> +#include <stdio.h> +#if defined(__AVR__) +static FILE tty_stdio; +static FILE tty_stderr; +volatile uint32_t LEDnext_time; // fade timeout +volatile uint32_t HEAPnext_time; // when to print out next heap report +volatile int brightness = 0; // how bright the LED is +volatile int fadeAmount = 80; // how many points to fade the LED by +#endif + +USB Usb; + +volatile uint8_t current_state = 1; +volatile uint8_t last_state = 0; +volatile bool fatready = false; +volatile bool partsready = false; +volatile bool notified = false; +volatile bool runtest = false; +volatile bool usbon = false; +volatile uint32_t usbon_time; +volatile bool change = false; +volatile bool reportlvl = false; +int cpart = 0; +PCPartition *PT; + +#if WANT_HUB_TEST +#define MAX_HUBS 1 +USBHub *Hubs[MAX_HUBS]; +#endif + +static PFAT *Fats[_VOLUMES]; +static part_t parts[_VOLUMES]; +static storage_t sto[_VOLUMES]; + +/*make sure this is a power of two. */ +#define mbxs 128 +static uint8_t My_Buff_x[mbxs]; /* File read buffer */ + +#if defined(__AVR__) + +#define prescale1 ((1 << WGM12) | (1 << CS10)) +#define prescale8 ((1 << WGM12) | (1 << CS11)) +#define prescale64 ((1 << WGM12) | (1 << CS10) | (1 << CS11)) +#define prescale256 ((1 << WGM12) | (1 << CS12)) +#define prescale1024 ((1 << WGM12) | (1 << CS12) | (1 << CS10)) + +extern "C" { + extern unsigned int freeHeap(); +} +static int tty_stderr_putc(char c, FILE *t) { + USB_HOST_SERIAL.write(c); + return 0; +} + +static int __attribute__((unused)) tty_stderr_flush(FILE *t) { + USB_HOST_SERIAL.flush(); + return 0; +} + +static int tty_std_putc(char c, FILE *t) { + Serial.write(c); + return 0; +} + +static int tty_std_getc(FILE *t) { + while(!Serial.available()); + return Serial.read(); +} + +static int __attribute__((unused)) tty_std_flush(FILE *t) { + Serial.flush(); + return 0; +} + +#else +// Supposedly the DUE has stdio already pointing to serial... +#if !defined(ARDUINO_ARCH_SAM) +// But newlib needs this... +extern "C" { + int _write(int fd, const char *ptr, int len) { + int j; + for(j = 0; j < len; j++) { + if(fd == 1) + Serial.write(*ptr++); + else if(fd == 2) + USB_HOST_SERIAL.write(*ptr++); + } + return len; + } + + int _read(int fd, char *ptr, int len) { + if(len > 0 && fd == 0) { + while(!Serial.available()); + *ptr = Serial.read(); + return 1; + } + return 0; + } + +#include <sys/stat.h> + + int _fstat(int fd, struct stat *st) { + memset(st, 0, sizeof (*st)); + st->st_mode = S_IFCHR; + st->st_blksize = 1024; + return 0; + } + + int _isatty(int fd) { + return (fd < 3) ? 1 : 0; + } +} +#endif // !defined(ARDUINO_ARCH_SAM) +#endif + +void setup() { + bool serr = false; + for(int i = 0; i < _VOLUMES; i++) { + Fats[i] = NULL; + sto[i].private_data = new pvt_t; + ((pvt_t *)sto[i].private_data)->B = 255; // impossible + } + // Set this to higher values to enable more debug information + // minimum 0x00, maximum 0xff + UsbDEBUGlvl = 0x81; + +#if !defined(CORE_TEENSY) && defined(__AVR__) + // make LED pin as an output: + pinMode(LED_BUILTIN, OUTPUT); + pinMode(2, OUTPUT); + // Ensure TX is off + _SFR_BYTE(UCSR0B) &= ~_BV(TXEN0); + // Initialize 'debug' serial port + USB_HOST_SERIAL.begin(115200); + // Do not start primary Serial port if already started. + if(bit_is_clear(UCSR0B, TXEN0)) { + Serial.begin(115200); + serr = true; + } + + + // Blink LED + delay(500); + analogWrite(LED_BUILTIN, 255); + delay(500); + analogWrite(LED_BUILTIN, 0); + delay(500); +#else + while(!Serial); + Serial.begin(115200); // On the Teensy 3.x we get a delay at least! +#endif +#if defined(__AVR__) + // Set up stdio/stderr + tty_stdio.put = tty_std_putc; + tty_stdio.get = tty_std_getc; + tty_stdio.flags = _FDEV_SETUP_RW; + tty_stdio.udata = 0; + + tty_stderr.put = tty_stderr_putc; + tty_stderr.get = NULL; + tty_stderr.flags = _FDEV_SETUP_WRITE; + tty_stderr.udata = 0; + + stdout = &tty_stdio; + stdin = &tty_stdio; + stderr = &tty_stderr; +#endif + printf_P(PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n")); + printf_P(PSTR("Current UsbDEBUGlvl %02x\r\n"), UsbDEBUGlvl); + printf_P(PSTR("'+' and '-' increase/decrease by 0x01\r\n")); + printf_P(PSTR("'.' and ',' increase/decrease by 0x10\r\n")); + printf_P(PSTR("'t' will run a 10MB write/read test and print out the time it took.\r\n")); + printf_P(PSTR("'e' will toggle vbus off for a few moments.\r\n\r\n")); + printf_P(PSTR("Long filename support: " +#if _USE_LFN + "Enabled" +#else + "Disabled" +#endif + "\r\n")); + if(serr) { + fprintf_P(stderr, PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n")); + fprintf_P(stderr, PSTR("Current UsbDEBUGlvl %02x\r\n"), UsbDEBUGlvl); + fprintf_P(stderr, PSTR("Long filename support: " +#if _USE_LFN + "Enabled" +#else + "Disabled" +#endif + "\r\n")); + } + +#if !defined(CORE_TEENSY) && defined(__AVR__) + analogWrite(LED_BUILTIN, 255); + delay(500); + analogWrite(LED_BUILTIN, 0); + delay(500); + analogWrite(LED_BUILTIN, 255); + delay(500); + analogWrite(LED_BUILTIN, 0); + delay(500); + analogWrite(LED_BUILTIN, 255); + delay(500); + analogWrite(LED_BUILTIN, 0); + delay(500); + + LEDnext_time = millis() + 1; +#if EXT_RAM + printf_P(PSTR("Total EXT RAM banks %i\r\n"), xmem::getTotalBanks()); +#endif + printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); + printf_P(PSTR("SP %x\r\n"), (uint8_t *)(SP)); +#endif + + // Even though I'm not going to actually be deleting, + // I want to be able to have slightly more control. + // Besides, it is easier to initialize stuff... +#if WANT_HUB_TEST + for(int i = 0; i < MAX_HUBS; i++) { + Hubs[i] = new USBHub(&Usb); +#if defined(__AVR__) + printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); +#endif + } +#endif + // Initialize generic storage. This must be done before USB starts. + Init_Generic_Storage(); + + while(Usb.Init(1000) == -1) { + printf_P(PSTR("No USB HOST Shield?\r\n")); + Notify(PSTR("OSC did not start."), 0x40); + } + +#if !defined(CORE_TEENSY) && defined(__AVR__) + cli(); + TCCR3A = 0; + TCCR3B = 0; + // (0.01/(1/((16 *(10^6)) / 8))) - 1 = 19999 + OCR3A = 19999; + TCCR3B |= prescale8; + TIMSK3 |= (1 << OCIE1A); + sei(); + + HEAPnext_time = millis() + 10000; +#endif +#if defined(__AVR__) + HEAPnext_time = millis() + 10000; +#endif +} + +void serialEvent() { + // Adjust UsbDEBUGlvl level on-the-fly. + // + to increase, - to decrease, * to display current level. + // . to increase by 16, , to decrease by 16 + // e to flick VBUS + // * to report debug level + if(Serial.available()) { + int inByte = Serial.read(); + switch(inByte) { + case '+': + if(UsbDEBUGlvl < 0xff) UsbDEBUGlvl++; + reportlvl = true; + break; + case '-': + if(UsbDEBUGlvl > 0x00) UsbDEBUGlvl--; + reportlvl = true; + break; + case '.': + if(UsbDEBUGlvl < 0xf0) UsbDEBUGlvl += 16; + reportlvl = true; + break; + case ',': + if(UsbDEBUGlvl > 0x0f) UsbDEBUGlvl -= 16; + reportlvl = true; + break; + case '*': + reportlvl = true; + break; + case 't': + runtest = true; + break; + case 'e': + change = true; + usbon = false; + break; + } + } +} + +#if !defined(CORE_TEENSY) && defined(__AVR__) +// ALL teensy versions LACK PWM ON LED + +ISR(TIMER3_COMPA_vect) { + if((long)(millis() - LEDnext_time) >= 0L) { + LEDnext_time = millis() + 30; + + // set the brightness of LED + analogWrite(LED_BUILTIN, brightness); + + // change the brightness for next time through the loop: + brightness = brightness + fadeAmount; + + // reverse the direction of the fading at the ends of the fade: + if(brightness <= 0) { + brightness = 0; + fadeAmount = -fadeAmount; + } + if(brightness >= 255) { + brightness = 255; + fadeAmount = -fadeAmount; + } + } +} +#endif + +bool isfat(uint8_t t) { + return (t == 0x01 || t == 0x04 || t == 0x06 || t == 0x0b || t == 0x0c || t == 0x0e || t == 0x1); +} + +void die(FRESULT rc) { + printf_P(PSTR("Failed with rc=%u.\r\n"), rc); + //for (;;); +} + +void loop() { + FIL My_File_Object_x; /* File object */ + +#if defined(__AVR__) + // Print a heap status report about every 10 seconds. + if((long)(millis() - HEAPnext_time) >= 0L) { + if(UsbDEBUGlvl > 0x50) { + printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); + } + HEAPnext_time = millis() + 10000; + } + TCCR3B = 0; +#endif +#if defined(CORE_TEENSY) + // Teensy suffers here, oh well... + serialEvent(); +#endif + // Horrid! This sort of thing really belongs in an ISR, not here! + // We also will be needing to test each hub port, we don't do this yet! + if(!change && !usbon && (long)(millis() - usbon_time) >= 0L) { + change = true; + usbon = true; + } + + if(change) { + change = false; + if(usbon) { + Usb.vbusPower(vbus_on); + printf_P(PSTR("VBUS on\r\n")); + } else { + Usb.vbusPower(vbus_off); + usbon_time = millis() + 2000; + } + } + Usb.Task(); + current_state = Usb.getUsbTaskState(); + if(current_state != last_state) { + if(UsbDEBUGlvl > 0x50) + printf_P(PSTR("USB state = %x\r\n"), current_state); +#if !defined(CORE_TEENSY) && defined(__AVR__) + if(current_state == USB_STATE_RUNNING) { + fadeAmount = 30; + } +#endif + if(current_state == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) { +#if !defined(CORE_TEENSY) && defined(__AVR__) + fadeAmount = 80; +#endif + partsready = false; + for(int i = 0; i < cpart; i++) { + if(Fats[i] != NULL) + delete Fats[i]; + Fats[i] = NULL; + } + fatready = false; + notified = false; + cpart = 0; + } + last_state = current_state; + } + + // only do any of this if usb is on + if(usbon) { + if(partsready && !fatready) { + if(cpart > 0) fatready = true; + } + // This is horrible, and needs to be moved elsewhere! + for(int B = 0; B < MAX_USB_MS_DRIVERS; B++) { + if((!partsready) && (UHS_USB_BulkOnly[B]->GetAddress())) { + + // Build a list. + int ML = UHS_USB_BulkOnly[B]->GetbMaxLUN(); + //printf("MAXLUN = %i\r\n", ML); + ML++; + for(int i = 0; i < ML; i++) { + if(UHS_USB_BulkOnly[B]->LUNIsGood(i)) { + partsready = true; + ((pvt_t *)(sto[i].private_data))->lun = i; + ((pvt_t *)(sto[i].private_data))->B = B; + sto[i].Reads = *UHS_USB_BulkOnly_Read; + sto[i].Writes = *UHS_USB_BulkOnly_Write; + sto[i].Status = *UHS_USB_BulkOnly_Status; + sto[i].Initialize = *UHS_USB_BulkOnly_Initialize; + sto[i].Commit = *UHS_USB_BulkOnly_Commit; + sto[i].TotalSectors = UHS_USB_BulkOnly[B]->GetCapacity(i); + sto[i].SectorSize = UHS_USB_BulkOnly[B]->GetSectorSize(i); + printf_P(PSTR("LUN:\t\t%u\r\n"), i); + printf_P(PSTR("Total Sectors:\t%08lx\t%lu\r\n"), sto[i].TotalSectors, sto[i].TotalSectors); + printf_P(PSTR("Sector Size:\t%04x\t\t%u\r\n"), sto[i].SectorSize, sto[i].SectorSize); + // get the partition data... + PT = new PCPartition; + + if(!PT->Init(&sto[i])) { + part_t *apart; + for(int j = 0; j < 4; j++) { + apart = PT->GetPart(j); + if(apart != NULL && apart->type != 0x00) { + memcpy(&(parts[cpart]), apart, sizeof (part_t)); + printf_P(PSTR("Partition %u type %#02x\r\n"), j, parts[cpart].type); + // for now + if(isfat(parts[cpart].type)) { + Fats[cpart] = new PFAT(&sto[i], cpart, parts[cpart].firstSector); + //int r = Fats[cpart]->Good(); + if(Fats[cpart]->MountStatus()) { + delete Fats[cpart]; + Fats[cpart] = NULL; + } else cpart++; + } + } + } + } else { + // try superblock + Fats[cpart] = new PFAT(&sto[i], cpart, 0); + //int r = Fats[cpart]->Good(); + if(Fats[cpart]->MountStatus()) { + //printf_P(PSTR("Superblock error %x\r\n"), r); + delete Fats[cpart]; + Fats[cpart] = NULL; + } else cpart++; + + } + delete PT; + } else { + sto[i].Writes = NULL; + sto[i].Reads = NULL; + sto[i].Initialize = NULL; + sto[i].TotalSectors = 0UL; + sto[i].SectorSize = 0; + } + } + + } + } + + if(fatready) { + if(Fats[0] != NULL) { + struct Pvt * p; + p = ((struct Pvt *)(Fats[0]->storage->private_data)); + if(!UHS_USB_BulkOnly[p->B]->LUNIsGood(p->lun)) { + // media change +#if !defined(CORE_TEENSY) && defined(__AVR__) + fadeAmount = 80; +#endif + partsready = false; + for(int i = 0; i < cpart; i++) { + if(Fats[i] != NULL) + delete Fats[i]; + Fats[cpart] = NULL; + } + fatready = false; + notified = false; + cpart = 0; + } + + } + } + if(fatready) { + FRESULT rc; /* Result code */ + UINT bw, br, i; + if(!notified) { +#if !defined(CORE_TEENSY) && defined(__AVR__) + fadeAmount = 5; +#endif + notified = true; + FATFS *fs = NULL; + for(int zz = 0; zz < _VOLUMES; zz++) { + if(Fats[zz]->volmap == 0) fs = Fats[zz]->ffs; + } + printf_P(PSTR("\r\nOpen an existing file (message.txt).\r\n")); + rc = f_open(&My_File_Object_x, "0:/MESSAGE.TXT", FA_READ); + if(rc) printf_P(PSTR("Error %i, message.txt not found.\r\n"), rc); + else { + printf_P(PSTR("\r\nType the file content.\r\n")); + for(;;) { + rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &br); /* Read a chunk of file */ + if(rc || !br) break; /* Error or end of file */ + for(i = 0; i < br; i++) { + /* Type the data */ + if(My_Buff_x[i] == '\n') + Serial.write('\r'); + if(My_Buff_x[i] != '\r') + Serial.write(My_Buff_x[i]); + Serial.flush(); + } + } + if(rc) { + f_close(&My_File_Object_x); + goto out; + } + + printf_P(PSTR("\r\nClose the file.\r\n")); + rc = f_close(&My_File_Object_x); + if(rc) goto out; + } + printf_P(PSTR("\r\nCreate a new file (hello.txt).\r\n")); + rc = f_open(&My_File_Object_x, "0:/Hello.TxT", FA_WRITE | FA_CREATE_ALWAYS); + if(rc) { + die(rc); + goto outdir; + } + printf_P(PSTR("\r\nWrite a text data. (Hello world!)\r\n")); + rc = f_write(&My_File_Object_x, "Hello world!\r\n", 14, &bw); + if(rc) { + goto out; + } + printf_P(PSTR("%u bytes written.\r\n"), bw); + + printf_P(PSTR("\r\nClose the file.\r\n")); + rc = f_close(&My_File_Object_x); + if(rc) { + die(rc); + goto out; + } +outdir:{ +#if _USE_LFN + char lfn[_MAX_LFN + 1]; + FILINFO My_File_Info_Object_x; /* File information object */ + My_File_Info_Object_x.lfname = lfn; +#endif + DIR My_Dir_Object_x; /* Directory object */ + printf_P(PSTR("\r\nOpen root directory.\r\n")); + rc = f_opendir(&My_Dir_Object_x, "0:/"); + if(rc) { + die(rc); + goto out; + } + + printf_P(PSTR("\r\nDirectory listing...\r\n")); +#if defined(__AVR__) + printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); +#endif + for(;;) { +#if _USE_LFN + My_File_Info_Object_x.lfsize = _MAX_LFN; +#endif + + rc = f_readdir(&My_Dir_Object_x, &My_File_Info_Object_x); /* Read a directory item */ + if(rc || !My_File_Info_Object_x.fname[0]) break; /* Error or end of dir */ + + if(My_File_Info_Object_x.fattrib & AM_DIR) { + Serial.write('d'); + } else { + Serial.write('-'); + } + Serial.write('r'); + + if(My_File_Info_Object_x.fattrib & AM_RDO) { + Serial.write('-'); + } else { + Serial.write('w'); + } + if(My_File_Info_Object_x.fattrib & AM_HID) { + Serial.write('h'); + } else { + Serial.write('-'); + } + + if(My_File_Info_Object_x.fattrib & AM_SYS) { + Serial.write('s'); + } else { + Serial.write('-'); + } + + if(My_File_Info_Object_x.fattrib & AM_ARC) { + Serial.write('a'); + } else { + Serial.write('-'); + } + +#if _USE_LFN + if(*My_File_Info_Object_x.lfname) + printf_P(PSTR(" %8lu %s (%s)\r\n"), My_File_Info_Object_x.fsize, My_File_Info_Object_x.fname, My_File_Info_Object_x.lfname); + else +#endif + printf_P(PSTR(" %8lu %s\r\n"), My_File_Info_Object_x.fsize, &(My_File_Info_Object_x.fname[0])); + } + } +out: + if(rc) die(rc); + + DISK_IOCTL(fs->drv, CTRL_COMMIT, 0); + printf_P(PSTR("\r\nTest completed.\r\n")); + + } + + if(runtest) { + ULONG ii, wt, rt, start, end; + FATFS *fs = NULL; + for(int zz = 0; zz < _VOLUMES; zz++) { + if(Fats[zz]->volmap == 0) fs = Fats[zz]->ffs; + } + runtest = false; + f_unlink("0:/10MB.bin"); + printf_P(PSTR("\r\nCreate a new 10MB test file (10MB.bin).\r\n")); + rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_WRITE | FA_CREATE_ALWAYS); + if(rc) goto failed; + for(bw = 0; bw < mbxs; bw++) My_Buff_x[bw] = bw & 0xff; + fflush(stdout); + start = millis(); + while(start == millis()); + for(ii = 10485760LU / mbxs; ii > 0LU; ii--) { + rc = f_write(&My_File_Object_x, My_Buff_x, mbxs, &bw); + if(rc || !bw) goto failed; + } + rc = f_close(&My_File_Object_x); + if(rc) goto failed; + end = millis(); + wt = (end - start) - 1; + printf_P(PSTR("Time to write 10485760 bytes: %lu ms (%lu sec) \r\n"), wt, (500 + wt) / 1000UL); + rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_READ); + fflush(stdout); + start = millis(); + while(start == millis()); + if(rc) goto failed; + for(;;) { + rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &bw); /* Read a chunk of file */ + if(rc || !bw) break; /* Error or end of file */ + } + end = millis(); + if(rc) goto failed; + rc = f_close(&My_File_Object_x); + if(rc) goto failed; + rt = (end - start) - 1; + printf_P(PSTR("Time to read 10485760 bytes: %lu ms (%lu sec)\r\nDelete test file\r\n"), rt, (500 + rt) / 1000UL); +failed: + if(rc) die(rc); + DISK_IOCTL(fs->drv, CTRL_COMMIT, 0); + printf_P(PSTR("10MB timing test finished.\r\n")); + } + } + } +} |