Fix example, and reformat

This commit is contained in:
Andrew J. Kroll 2013-10-01 14:50:09 -04:00
parent 77538f5d2d
commit 8efdf711fd
7 changed files with 277 additions and 140 deletions

View file

@ -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

View file

@ -1,46 +1,31 @@
#include <avr/pgmspace.h>
#include <avrpins.h>
#include <max3421e.h>
#include <usbhost.h>
#include <usb_ch9.h>
#include <Usb.h>
#include <usbhub.h> #include <usbhub.h>
#include <avr/pgmspace.h>
#include <address.h>
#include <hid.h> #include <hid.h>
#include <hiduniversal.h> #include <hiduniversal.h>
#include "hidjoystickrptparser.h" #include "hidjoystickrptparser.h"
#include <printhex.h>
#include <message.h>
#include <hexdump.h>
#include <parsetools.h>
USB Usb; USB Usb;
USBHub Hub(&Usb); USBHub Hub(&Usb);
HIDUniversal Hid(&Usb); HIDUniversal Hid(&Usb);
JoystickEvents JoyEvents; JoystickEvents JoyEvents;
JoystickReportParser Joy(&JoyEvents); JoystickReportParser Joy(&JoyEvents);
void setup() void setup() {
{ Serial.begin(115200);
Serial.begin( 115200 ); Serial.println("Start");
Serial.println("Start");
if (Usb.Init() == -1) if (Usb.Init() == -1)
Serial.println("OSC did not start."); Serial.println("OSC did not start.");
delay( 200 ); delay(200);
if (!Hid.SetReportParser(0, &Joy)) if (!Hid.SetReportParser(0, &Joy))
ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 ); ErrorMessage<uint8_t > (PSTR("SetReportParser"), 1);
} }
void loop() void loop() {
{ Usb.Task();
Usb.Task();
} }

View file

@ -1,95 +1,84 @@
#include "hidjoystickrptparser.h" #include "hidjoystickrptparser.h"
JoystickReportParser::JoystickReportParser(JoystickEvents *evt) : JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
joyEvents(evt), joyEvents(evt),
oldHat(0xDE), oldHat(0xDE),
oldButtons(0) oldButtons(0) {
{ for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++)
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) oldPad[i] = 0xD;
oldPad[i] = 0xD;
} }
void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
{ bool match = true;
bool match = true;
// Checking if there are changes in report since the method was last called // Checking if there are changes in report since the method was last called
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++)
if (buf[i] != oldPad[i]) if (buf[i] != oldPad[i]) {
{ match = false;
match = false; break;
break; }
}
// Calling Game Pad event handler // Calling Game Pad event handler
if (!match && joyEvents) if (!match && joyEvents) {
{ joyEvents->OnGamePadChanged((const GamePadEventData*)buf);
joyEvents->OnGamePadChanged((const GamePadEventData*)buf);
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i]; for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
} }
uint8_t hat = (buf[5] & 0xF); uint8_t hat = (buf[5] & 0xF);
// Calling Hat Switch event handler // Calling Hat Switch event handler
if (hat != oldHat && joyEvents) if (hat != oldHat && joyEvents) {
{ joyEvents->OnHatSwitch(hat);
joyEvents->OnHatSwitch(hat); oldHat = hat;
oldHat = hat; }
}
uint16_t buttons = (0x0000 | buf[6]); uint16_t buttons = (0x0000 | buf[6]);
buttons <<= 4; buttons <<= 4;
buttons |= (buf[5] >> 4); buttons |= (buf[5] >> 4);
uint16_t changes = (buttons ^ oldButtons); uint16_t changes = (buttons ^ oldButtons);
// Calling Button Event Handler for every button changed // Calling Button Event Handler for every button changed
if (changes) if (changes) {
{ for (uint8_t i = 0; i < 0x0C; i++) {
for (uint8_t i=0; i<0x0C; i++) uint16_t mask = (0x0001 << i);
{
uint16_t mask = (0x0001 << i);
if (((mask & changes) > 0) && joyEvents) if (((mask & changes) > 0) && joyEvents)
if ((buttons & mask) > 0) if ((buttons & mask) > 0)
joyEvents->OnButtonDn(i+1); joyEvents->OnButtonDn(i + 1);
else else
joyEvents->OnButtonUp(i+1); joyEvents->OnButtonUp(i + 1);
} }
oldButtons = buttons; oldButtons = buttons;
} }
} }
void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) {
{ Serial.print("X1: ");
Serial.print("X: "); PrintHex<uint8_t > (evt->X, 0x80);
PrintHex<uint8_t>(evt->X, 0x80); Serial.print("\tY1: ");
Serial.print("\tY: "); PrintHex<uint8_t > (evt->Y, 0x80);
PrintHex<uint8_t>(evt->Y, 0x80); Serial.print("\tX2: ");
Serial.print("\tZ: "); PrintHex<uint8_t > (evt->Z1, 0x80);
PrintHex<uint8_t>(evt->Z1, 0x80); Serial.print("\tY2: ");
Serial.print("\tZ: "); PrintHex<uint8_t > (evt->Z2, 0x80);
PrintHex<uint8_t>(evt->Z2, 0x80); Serial.print("\tRz: ");
Serial.print("\tRz: "); PrintHex<uint8_t > (evt->Rz, 0x80);
PrintHex<uint8_t>(evt->Rz, 0x80); Serial.println("");
Serial.println("");
} }
void JoystickEvents::OnHatSwitch(uint8_t hat) void JoystickEvents::OnHatSwitch(uint8_t hat) {
{ Serial.print("Hat Switch: ");
Serial.print("Hat Switch: "); PrintHex<uint8_t > (hat, 0x80);
PrintHex<uint8_t>(hat, 0x80); Serial.println("");
Serial.println("");
} }
void JoystickEvents::OnButtonUp(uint8_t but_id) void JoystickEvents::OnButtonUp(uint8_t but_id) {
{ Serial.print("Up: ");
Serial.print("Up: "); Serial.println(but_id, DEC);
Serial.println(but_id, DEC);
} }
void JoystickEvents::OnButtonDn(uint8_t but_id) void JoystickEvents::OnButtonDn(uint8_t but_id) {
{ Serial.print("Dn: ");
Serial.print("Dn: "); Serial.println(but_id, DEC);
Serial.println(but_id, DEC);
} }

View file

@ -1,54 +1,34 @@
#if !defined(__HIDJOYSTICKRPTPARSER_H__) #if !defined(__HIDJOYSTICKRPTPARSER_H__)
#define __HIDJOYSTICKRPTPARSER_H__ #define __HIDJOYSTICKRPTPARSER_H__
#include <inttypes.h> #include <Usb.h>
#include <avr/pgmspace.h> #include <hid.h>
#include "avrpins.h"
#include "max3421e.h"
#include "usbhost.h"
#include "usb_ch9.h"
#include "Usb.h"
#if defined(ARDUINO) && ARDUINO >=100 struct GamePadEventData {
#include "Arduino.h" uint8_t X, Y, Z1, Z2, Rz;
#else
#include <WProgram.h>
#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;
}; };
class JoystickEvents class JoystickEvents {
{
public: public:
virtual void OnGamePadChanged(const GamePadEventData *evt); virtual void OnGamePadChanged(const GamePadEventData *evt);
virtual void OnHatSwitch(uint8_t hat); virtual void OnHatSwitch(uint8_t hat);
virtual void OnButtonUp(uint8_t but_id); virtual void OnButtonUp(uint8_t but_id);
virtual void OnButtonDn(uint8_t but_id); virtual void OnButtonDn(uint8_t but_id);
}; };
#define RPT_GEMEPAD_LEN 5 #define RPT_GEMEPAD_LEN 5
class JoystickReportParser : public HIDReportParser class JoystickReportParser : public HIDReportParser {
{ JoystickEvents *joyEvents;
JoystickEvents *joyEvents;
uint8_t oldPad[RPT_GEMEPAD_LEN]; uint8_t oldPad[RPT_GEMEPAD_LEN];
uint8_t oldHat; uint8_t oldHat;
uint16_t oldButtons; uint16_t oldButtons;
public: 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__ #endif // __HIDJOYSTICKRPTPARSER_H__

View file

@ -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}

View file

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<configurationDescriptor version="80">
<logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
<df name="USBHIDJoystick" root=".">
<df name="build">
<in>CDC.lst</in>
<in>HID.lst</in>
<in>HardwareSerial.lst</in>
<in>IPAddress.lst</in>
<in>Print.lst</in>
<in>Stream.lst</in>
<in>Tone.lst</in>
<in>USBCore.lst</in>
<in>WInterrupts.lst</in>
<in>WMath.lst</in>
<in>WString.lst</in>
<in>wiring.lst</in>
<in>wiring_analog.lst</in>
<in>wiring_digital.lst</in>
<in>wiring_pulse.lst</in>
<in>wiring_shift.lst</in>
</df>
<in>USBHIDJoystick.ino</in>
<in>hidjoystickrptparser.cpp</in>
<in>hidjoystickrptparser.h</in>
</df>
<logicalFolder name="ExternalFiles"
displayName="Important Files"
projectFiles="false"
kind="IMPORTANT_FILES_FOLDER">
<itemPath>Makefile</itemPath>
</logicalFolder>
</logicalFolder>
<sourceFolderFilter>^(nbproject)$</sourceFolderFilter>
<sourceRootList>
<Elem>.</Elem>
</sourceRootList>
<projectmakefile>Makefile</projectmakefile>
<confs>
<conf name="Default" type="0">
<toolsSet>
<remote-sources-mode>LOCAL_SOURCES</remote-sources-mode>
<compilerSet>default</compilerSet>
</toolsSet>
<makefileType>
<makeTool>
<buildCommandWorkingDir>.</buildCommandWorkingDir>
<buildCommand>${MAKE} -f Makefile</buildCommand>
<cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
<executablePath></executablePath>
</makeTool>
</makefileType>
</conf>
</confs>
</configurationDescriptor>

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.cnd.makeproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/make-project/1">
<name>USBHIDJoystick</name>
<c-extensions/>
<cpp-extensions>cpp,ino</cpp-extensions>
<header-extensions>h</header-extensions>
<sourceEncoding>UTF-8</sourceEncoding>
<make-dep-projects/>
<sourceRootList>
<sourceRootElem>.</sourceRootElem>
</sourceRootList>
<confList>
<confElem>
<name>Default</name>
<type>0</type>
</confElem>
</confList>
</data>
</configuration>
</project>