From 8efdf711fd88a833acef9c560498acc1a84556a6 Mon Sep 17 00:00:00 2001 From: "Andrew J. Kroll" Date: Tue, 1 Oct 2013 14:50:09 -0400 Subject: [PATCH] Fix example, and reformat --- examples/HID/USBHIDJoystick/Makefile | 30 ++++ .../HID/USBHIDJoystick/USBHIDJoystick.ino | 47 +++--- .../USBHIDJoystick/hidjoystickrptparser.cpp | 135 ++++++++---------- .../HID/USBHIDJoystick/hidjoystickrptparser.h | 52 +++---- .../nbproject/Package-Default.bash | 75 ++++++++++ .../nbproject/configurations.xml | 55 +++++++ .../HID/USBHIDJoystick/nbproject/project.xml | 23 +++ 7 files changed, 277 insertions(+), 140 deletions(-) create mode 100644 examples/HID/USBHIDJoystick/Makefile create mode 100644 examples/HID/USBHIDJoystick/nbproject/Package-Default.bash create mode 100644 examples/HID/USBHIDJoystick/nbproject/configurations.xml create mode 100644 examples/HID/USBHIDJoystick/nbproject/project.xml diff --git a/examples/HID/USBHIDJoystick/Makefile b/examples/HID/USBHIDJoystick/Makefile new file mode 100644 index 00000000..253ecfb5 --- /dev/null +++ b/examples/HID/USBHIDJoystick/Makefile @@ -0,0 +1,30 @@ +# +# 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 uncomment the following two lines +# BOARD = mega2560 +# PROGRAMMER = wiring +# ...and then comment out the following two lines +BOARD = mega +PROGRAMMER = arduino + +# set your Arduino tty port here +PORT = /dev/ttyUSB0 + + +# uncomment the next line to enable debugging +#EXTRA_FLAGS += -D DEBUG_USB_HOST=1 + +# +# Advanced debug on Serial3 +# + +# uncomment the next line to enable debug on Serial3 +#EXTRA_FLAGS += -D USB_HOST_SERIAL=Serial3 + +# The following are the libraries used. +LIB_DIRS = +LIB_DIRS += ../libraries/USB_Host_Shield_2_0 +# And finally, the part that brings everything together for you. +include ../Arduino_Makefile_master/_Makefile.master diff --git a/examples/HID/USBHIDJoystick/USBHIDJoystick.ino b/examples/HID/USBHIDJoystick/USBHIDJoystick.ino index 6ea00924..79497b52 100644 --- a/examples/HID/USBHIDJoystick/USBHIDJoystick.ino +++ b/examples/HID/USBHIDJoystick/USBHIDJoystick.ino @@ -1,46 +1,31 @@ -#include - -#include -#include -#include -#include -#include #include -#include -#include #include #include #include "hidjoystickrptparser.h" -#include -#include -#include -#include -USB Usb; -USBHub Hub(&Usb); -HIDUniversal Hid(&Usb); -JoystickEvents JoyEvents; -JoystickReportParser Joy(&JoyEvents); +USB Usb; +USBHub Hub(&Usb); +HIDUniversal Hid(&Usb); +JoystickEvents JoyEvents; +JoystickReportParser Joy(&JoyEvents); -void setup() -{ - Serial.begin( 115200 ); - Serial.println("Start"); +void setup() { + Serial.begin(115200); + Serial.println("Start"); - if (Usb.Init() == -1) - Serial.println("OSC did not start."); - - delay( 200 ); + if (Usb.Init() == -1) + Serial.println("OSC did not start."); - if (!Hid.SetReportParser(0, &Joy)) - ErrorMessage(PSTR("SetReportParser"), 1 ); + delay(200); + + if (!Hid.SetReportParser(0, &Joy)) + ErrorMessage (PSTR("SetReportParser"), 1); } -void loop() -{ - Usb.Task(); +void loop() { + Usb.Task(); } diff --git a/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp b/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp index ef6444b5..083b95ca 100644 --- a/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp +++ b/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp @@ -1,95 +1,84 @@ #include "hidjoystickrptparser.h" JoystickReportParser::JoystickReportParser(JoystickEvents *evt) : - joyEvents(evt), - oldHat(0xDE), - oldButtons(0) -{ - for (uint8_t i=0; iOnGamePadChanged((const GamePadEventData*)buf); + // Calling Game Pad event handler + if (!match && joyEvents) { + joyEvents->OnGamePadChanged((const GamePadEventData*)buf); - for (uint8_t i=0; iOnHatSwitch(hat); - oldHat = hat; - } + // 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); + 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); + // 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; - } + 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("X: "); - PrintHex(evt->X, 0x80); - Serial.print("\tY: "); - PrintHex(evt->Y, 0x80); - Serial.print("\tZ: "); - PrintHex(evt->Z1, 0x80); - Serial.print("\tZ: "); - PrintHex(evt->Z2, 0x80); - Serial.print("\tRz: "); - PrintHex(evt->Rz, 0x80); - Serial.println(""); +void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) { + Serial.print("X1: "); + PrintHex (evt->X, 0x80); + Serial.print("\tY1: "); + PrintHex (evt->Y, 0x80); + Serial.print("\tX2: "); + PrintHex (evt->Z1, 0x80); + Serial.print("\tY2: "); + PrintHex (evt->Z2, 0x80); + Serial.print("\tRz: "); + PrintHex (evt->Rz, 0x80); + Serial.println(""); } -void JoystickEvents::OnHatSwitch(uint8_t hat) -{ - Serial.print("Hat Switch: "); - PrintHex(hat, 0x80); - Serial.println(""); +void JoystickEvents::OnHatSwitch(uint8_t hat) { + Serial.print("Hat Switch: "); + PrintHex (hat, 0x80); + Serial.println(""); } -void JoystickEvents::OnButtonUp(uint8_t but_id) -{ - Serial.print("Up: "); - Serial.println(but_id, DEC); +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); +void JoystickEvents::OnButtonDn(uint8_t but_id) { + Serial.print("Dn: "); + Serial.println(but_id, DEC); } diff --git a/examples/HID/USBHIDJoystick/hidjoystickrptparser.h b/examples/HID/USBHIDJoystick/hidjoystickrptparser.h index c9aad9d4..028d6c89 100644 --- a/examples/HID/USBHIDJoystick/hidjoystickrptparser.h +++ b/examples/HID/USBHIDJoystick/hidjoystickrptparser.h @@ -1,54 +1,34 @@ #if !defined(__HIDJOYSTICKRPTPARSER_H__) #define __HIDJOYSTICKRPTPARSER_H__ -#include -#include -#include "avrpins.h" -#include "max3421e.h" -#include "usbhost.h" -#include "usb_ch9.h" -#include "Usb.h" +#include +#include -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - -#include "printhex.h" -#include "hexdump.h" -#include "message.h" -#include "confdescparser.h" -#include "hid.h" - -struct GamePadEventData -{ - uint8_t X, Y, Z1, Z2, Rz; +struct GamePadEventData { + uint8_t X, Y, Z1, Z2, Rz; }; -class JoystickEvents -{ +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); + 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; +class JoystickReportParser : public HIDReportParser { + JoystickEvents *joyEvents; - uint8_t oldPad[RPT_GEMEPAD_LEN]; - uint8_t oldHat; - uint16_t oldButtons; + uint8_t oldPad[RPT_GEMEPAD_LEN]; + uint8_t oldHat; + uint16_t oldButtons; public: - JoystickReportParser(JoystickEvents *evt); + JoystickReportParser(JoystickEvents *evt); - virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); + virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); }; #endif // __HIDJOYSTICKRPTPARSER_H__ diff --git a/examples/HID/USBHIDJoystick/nbproject/Package-Default.bash b/examples/HID/USBHIDJoystick/nbproject/Package-Default.bash new file mode 100644 index 00000000..45e0f31e --- /dev/null +++ b/examples/HID/USBHIDJoystick/nbproject/Package-Default.bash @@ -0,0 +1,75 @@ +#!/bin/bash -x + +# +# Generated - do not edit! +# + +# Macros +TOP=`pwd` +CND_PLATFORM=AVR-Linux-x86 +CND_CONF=Default +CND_DISTDIR=dist +CND_BUILDDIR=build +NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging +TMPDIRNAME=tmp-packaging +OUTPUT_PATH=MissingOutputInProject +OUTPUT_BASENAME=MissingOutputInProject +PACKAGE_TOP_DIR=USBHIDJoystick/ + +# Functions +function checkReturnCode +{ + rc=$? + if [ $rc != 0 ] + then + exit $rc + fi +} +function makeDirectory +# $1 directory path +# $2 permission (optional) +{ + mkdir -p "$1" + checkReturnCode + if [ "$2" != "" ] + then + chmod $2 "$1" + checkReturnCode + fi +} +function copyFileToTmpDir +# $1 from-file path +# $2 to-file path +# $3 permission +{ + cp "$1" "$2" + checkReturnCode + if [ "$3" != "" ] + then + chmod $3 "$2" + checkReturnCode + fi +} + +# Setup +cd "${TOP}" +mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package +rm -rf ${NBTMPDIR} +mkdir -p ${NBTMPDIR} + +# Copy files and create directories and links +cd "${TOP}" +makeDirectory "${NBTMPDIR}/USBHIDJoystick" +copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 + + +# Generate tar file +cd "${TOP}" +rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/USBHIDJoystick.tar +cd ${NBTMPDIR} +tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/USBHIDJoystick.tar * +checkReturnCode + +# Cleanup +cd "${TOP}" +rm -rf ${NBTMPDIR} diff --git a/examples/HID/USBHIDJoystick/nbproject/configurations.xml b/examples/HID/USBHIDJoystick/nbproject/configurations.xml new file mode 100644 index 00000000..5bb562ca --- /dev/null +++ b/examples/HID/USBHIDJoystick/nbproject/configurations.xml @@ -0,0 +1,55 @@ + + + + + + CDC.lst + HID.lst + HardwareSerial.lst + IPAddress.lst + Print.lst + Stream.lst + Tone.lst + USBCore.lst + WInterrupts.lst + WMath.lst + WString.lst + wiring.lst + wiring_analog.lst + wiring_digital.lst + wiring_pulse.lst + wiring_shift.lst + + USBHIDJoystick.ino + hidjoystickrptparser.cpp + hidjoystickrptparser.h + + + Makefile + + + ^(nbproject)$ + + . + + Makefile + + + + LOCAL_SOURCES + default + + + + . + ${MAKE} -f Makefile + ${MAKE} -f Makefile clean + + + + + + diff --git a/examples/HID/USBHIDJoystick/nbproject/project.xml b/examples/HID/USBHIDJoystick/nbproject/project.xml new file mode 100644 index 00000000..b912fd91 --- /dev/null +++ b/examples/HID/USBHIDJoystick/nbproject/project.xml @@ -0,0 +1,23 @@ + + + org.netbeans.modules.cnd.makeproject + + + USBHIDJoystick + + cpp,ino + h + UTF-8 + + + . + + + + Default + 0 + + + + +