From be026c4dfbf2160247297f1e1e365fe58640b25f Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Wed, 29 May 2013 10:29:32 -0700 Subject: [PATCH 01/12] Add Teensy 3.0 pins to avrpins.h --- avrpins.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/avrpins.h b/avrpins.h index e2a023bc..e8816c05 100644 --- a/avrpins.h +++ b/avrpins.h @@ -20,6 +20,8 @@ e-mail : support@circuitsathome.com #ifndef _avrpins_h_ #define _avrpins_h_ +#if defined(__AVR__) + #if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__) /* Uncomment the following if you have Arduino Mega ADK board with MAX3421e built-in */ //#define BOARD_MEGA_ADK @@ -738,4 +740,78 @@ public: #define P31 Pa7 #endif // Sanguino -#endif //_avrpins_h_ \ No newline at end of file +#endif // __AVR__ + +#if defined(__arm__) + +// pointers are 32 bits on ARM +#define pgm_read_pointer(p) pgm_read_dword(p) + +#include "core_pins.h" +#include "avr_emulation.h" + +#define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000) +#define GPIO_BITBAND_PTR(reg, bit) ((uint8_t *)GPIO_BITBAND_ADDR((reg), (bit))) + +#define MAKE_PIN(className, baseReg, pinNum, configReg) \ +class className { \ +public: \ + static void Set() { \ + *GPIO_BITBAND_PTR(baseReg, pinNum) = 1; \ + } \ + static void Clear() { \ + *GPIO_BITBAND_PTR(baseReg, pinNum) = 0; \ + } \ + static void SetDirRead() { \ + configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \ + *(GPIO_BITBAND_PTR(baseReg, pinNum) + 640) = 0; \ + } \ + static void SetDirWrite() { \ + configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \ + *(GPIO_BITBAND_PTR(baseReg, pinNum) + 640) = 1; \ + } \ + static uint8_t IsSet() { \ + return *(GPIO_BITBAND_PTR(baseReg, pinNum) + 512); \ + } \ +} + +MAKE_PIN(P0, CORE_PIN0_PORTREG, CORE_PIN0_BIT, CORE_PIN0_CONFIG); +MAKE_PIN(P1, CORE_PIN1_PORTREG, CORE_PIN1_BIT, CORE_PIN1_CONFIG); +MAKE_PIN(P2, CORE_PIN2_PORTREG, CORE_PIN2_BIT, CORE_PIN2_CONFIG); +MAKE_PIN(P3, CORE_PIN3_PORTREG, CORE_PIN3_BIT, CORE_PIN3_CONFIG); +MAKE_PIN(P4, CORE_PIN4_PORTREG, CORE_PIN4_BIT, CORE_PIN4_CONFIG); +MAKE_PIN(P5, CORE_PIN5_PORTREG, CORE_PIN5_BIT, CORE_PIN5_CONFIG); +MAKE_PIN(P6, CORE_PIN6_PORTREG, CORE_PIN6_BIT, CORE_PIN6_CONFIG); +MAKE_PIN(P7, CORE_PIN7_PORTREG, CORE_PIN7_BIT, CORE_PIN7_CONFIG); +MAKE_PIN(P8, CORE_PIN8_PORTREG, CORE_PIN8_BIT, CORE_PIN8_CONFIG); +MAKE_PIN(P9, CORE_PIN9_PORTREG, CORE_PIN9_BIT, CORE_PIN9_CONFIG); +MAKE_PIN(P10, CORE_PIN10_PORTREG, CORE_PIN10_BIT, CORE_PIN10_CONFIG); +MAKE_PIN(P11, CORE_PIN11_PORTREG, CORE_PIN11_BIT, CORE_PIN11_CONFIG); +MAKE_PIN(P12, CORE_PIN12_PORTREG, CORE_PIN12_BIT, CORE_PIN12_CONFIG); +MAKE_PIN(P13, CORE_PIN13_PORTREG, CORE_PIN13_BIT, CORE_PIN13_CONFIG); +MAKE_PIN(P14, CORE_PIN14_PORTREG, CORE_PIN14_BIT, CORE_PIN14_CONFIG); +MAKE_PIN(P15, CORE_PIN15_PORTREG, CORE_PIN15_BIT, CORE_PIN15_CONFIG); +MAKE_PIN(P16, CORE_PIN16_PORTREG, CORE_PIN16_BIT, CORE_PIN16_CONFIG); +MAKE_PIN(P17, CORE_PIN17_PORTREG, CORE_PIN17_BIT, CORE_PIN17_CONFIG); +MAKE_PIN(P18, CORE_PIN18_PORTREG, CORE_PIN18_BIT, CORE_PIN18_CONFIG); +MAKE_PIN(P19, CORE_PIN19_PORTREG, CORE_PIN19_BIT, CORE_PIN19_CONFIG); +MAKE_PIN(P20, CORE_PIN20_PORTREG, CORE_PIN20_BIT, CORE_PIN20_CONFIG); +MAKE_PIN(P21, CORE_PIN21_PORTREG, CORE_PIN21_BIT, CORE_PIN21_CONFIG); +MAKE_PIN(P22, CORE_PIN22_PORTREG, CORE_PIN22_BIT, CORE_PIN22_CONFIG); +MAKE_PIN(P23, CORE_PIN23_PORTREG, CORE_PIN23_BIT, CORE_PIN23_CONFIG); +MAKE_PIN(P24, CORE_PIN24_PORTREG, CORE_PIN24_BIT, CORE_PIN24_CONFIG); +MAKE_PIN(P25, CORE_PIN25_PORTREG, CORE_PIN25_BIT, CORE_PIN25_CONFIG); +MAKE_PIN(P26, CORE_PIN26_PORTREG, CORE_PIN26_BIT, CORE_PIN26_CONFIG); +MAKE_PIN(P27, CORE_PIN27_PORTREG, CORE_PIN27_BIT, CORE_PIN27_CONFIG); +MAKE_PIN(P28, CORE_PIN28_PORTREG, CORE_PIN28_BIT, CORE_PIN28_CONFIG); +MAKE_PIN(P29, CORE_PIN29_PORTREG, CORE_PIN29_BIT, CORE_PIN29_CONFIG); +MAKE_PIN(P30, CORE_PIN30_PORTREG, CORE_PIN30_BIT, CORE_PIN30_CONFIG); +MAKE_PIN(P31, CORE_PIN31_PORTREG, CORE_PIN31_BIT, CORE_PIN31_CONFIG); +MAKE_PIN(P32, CORE_PIN32_PORTREG, CORE_PIN32_BIT, CORE_PIN32_CONFIG); +MAKE_PIN(P33, CORE_PIN33_PORTREG, CORE_PIN33_BIT, CORE_PIN33_CONFIG); + +#undef MAKE_PIN + +#endif // __arm__ + +#endif //_avrpins_h_ From fca5a4125fdccbd49877e138121856df6f6c5db6 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Wed, 29 May 2013 10:32:51 -0700 Subject: [PATCH 02/12] Add Teensy 3.0 SPI to usbhost.h --- usbhost.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/usbhost.h b/usbhost.h index 04a2947f..ddfc41c9 100644 --- a/usbhost.h +++ b/usbhost.h @@ -51,6 +51,9 @@ typedef SPi< Pb5, Pb3, Pb4, Pb2 > spi; #if defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi; #endif +#if defined(__MK20DX128__) +typedef SPi< P13, P11, P12, P10 > spi; +#endif template< typename SS, typename INTR > class MAX3421e /* : public spi */ { static uint8_t vbusState; From c9815144af59b2fec6fe825201d40b839e32de63 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Wed, 29 May 2013 10:40:45 -0700 Subject: [PATCH 03/12] Include system headers before library headers (fix DEBUG issue with newlib) --- Usb.cpp | 10 +++++----- Usb.h | 12 ++++++------ adk.h | 14 +++++++------- cdcacm.h | 14 +++++++------- cdcprolific.h | 14 +++++++------- hid.h | 14 +++++++------- hidboot.h | 14 +++++++------- hidescriptorparser.h | 14 +++++++------- masstorage.h | 12 ++++++------ message.cpp | 6 ++++++ parsetools.h | 12 ++++++------ usbhub.h | 12 ++++++------ 12 files changed, 77 insertions(+), 71 deletions(-) diff --git a/Usb.cpp b/Usb.cpp index ec273f43..c73760e2 100644 --- a/Usb.cpp +++ b/Usb.cpp @@ -16,17 +16,17 @@ e-mail : support@circuitsathome.com */ /* USB functions */ -#include "avrpins.h" -#include "max3421e.h" -#include "usbhost.h" -#include "Usb.h" - #if defined(ARDUINO) && ARDUINO >=100 #include "Arduino.h" #else #include #endif +#include "avrpins.h" +#include "max3421e.h" +#include "usbhost.h" +#include "Usb.h" + static uint8_t usb_error = 0; static uint8_t usb_task_state; diff --git a/Usb.h b/Usb.h index 9992aa31..152543fd 100644 --- a/Usb.h +++ b/Usb.h @@ -18,6 +18,12 @@ e-mail : support@circuitsathome.com #ifndef _usb_h_ #define _usb_h_ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + //#define BOARD_BLACK_WIDDOW #define USB_METHODS_INLINE @@ -31,12 +37,6 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "address.h" - -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif #include "message.h" /* shield pins. First parameter - SS pin, second parameter - INT pin */ diff --git a/adk.h b/adk.h index 4ea0ed63..8ad4543a 100644 --- a/adk.h +++ b/adk.h @@ -20,6 +20,12 @@ e-mail : support@circuitsathome.com #if !defined(_ADK_H_) #define _ADK_H_ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #include #include #include "avrpins.h" @@ -28,12 +34,6 @@ e-mail : support@circuitsathome.com #include "usb_ch9.h" #include "Usb.h" -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - #include "printhex.h" #include "hexdump.h" #include "message.h" @@ -146,4 +146,4 @@ inline uint8_t ADK::switchAcc(void) { return( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_SEND, ADK_ACCSTART, 0, 0, 0, 0, 0, NULL, NULL)); } -#endif // _ADK_H_ \ No newline at end of file +#endif // _ADK_H_ diff --git a/cdcacm.h b/cdcacm.h index 469d9383..41b9b23f 100644 --- a/cdcacm.h +++ b/cdcacm.h @@ -17,6 +17,12 @@ e-mail : support@circuitsathome.com #if !defined(__CDCACM_H__) #define __CDCACM_H__ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #include #include #include "avrpins.h" @@ -25,12 +31,6 @@ e-mail : support@circuitsathome.com #include "usb_ch9.h" #include "Usb.h" -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - #include "printhex.h" #include "hexdump.h" #include "message.h" @@ -208,4 +208,4 @@ public: virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); }; -#endif // __CDCACM_H__ \ No newline at end of file +#endif // __CDCACM_H__ diff --git a/cdcprolific.h b/cdcprolific.h index 9d7a31ce..cc1304f0 100644 --- a/cdcprolific.h +++ b/cdcprolific.h @@ -17,6 +17,12 @@ e-mail : support@circuitsathome.com #if !defined(__CDCPROLIFIC_H__) #define __CDCPROLIFIC_H__ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #include #include #include "avrpins.h" @@ -25,12 +31,6 @@ e-mail : support@circuitsathome.com #include "usb_ch9.h" #include "Usb.h" -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - #include "printhex.h" #include "hexdump.h" #include "message.h" @@ -150,4 +150,4 @@ public: //virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); }; -#endif // __CDCPROLIFIC_H__ \ No newline at end of file +#endif // __CDCPROLIFIC_H__ diff --git a/hid.h b/hid.h index f894a64d..307a6ad5 100644 --- a/hid.h +++ b/hid.h @@ -17,6 +17,12 @@ e-mail : support@circuitsathome.com #if !defined(__HID_H__) #define __HID_H__ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #include #include #include "avrpins.h" @@ -25,12 +31,6 @@ e-mail : support@circuitsathome.com #include "usb_ch9.h" #include "Usb.h" -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - #include "printhex.h" #include "hexdump.h" #include "message.h" @@ -198,4 +198,4 @@ public: uint8_t SetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr); }; -#endif // __HID_H__ \ No newline at end of file +#endif // __HID_H__ diff --git a/hidboot.h b/hidboot.h index bfcdebdb..ca1fb534 100644 --- a/hidboot.h +++ b/hidboot.h @@ -17,6 +17,12 @@ e-mail : support@circuitsathome.com #if !defined(__HIDBOOT_H__) #define __HIDBOOT_H__ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #include #include #include "avrpins.h" @@ -26,12 +32,6 @@ e-mail : support@circuitsathome.com #include "Usb.h" #include "hid.h" -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - #include "printhex.h" #include "hexdump.h" #include "message.h" @@ -494,4 +494,4 @@ uint8_t HIDBoot::Poll() { } -#endif // __HIDBOOTMOUSE_H__ \ No newline at end of file +#endif // __HIDBOOTMOUSE_H__ diff --git a/hidescriptorparser.h b/hidescriptorparser.h index 1c4a1900..e491d0cc 100644 --- a/hidescriptorparser.h +++ b/hidescriptorparser.h @@ -17,6 +17,12 @@ e-mail : support@circuitsathome.com #if !defined(__HIDDESCRIPTORPARSER_H__) #define __HIDDESCRIPTORPARSER_H__ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #include #include #include "avrpins.h" @@ -25,12 +31,6 @@ e-mail : support@circuitsathome.com #include "usb_ch9.h" #include "Usb.h" -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - #include "printhex.h" #include "hexdump.h" #include "message.h" @@ -188,4 +188,4 @@ public: virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); }; -#endif // __HIDDESCRIPTORPARSER_H__ \ No newline at end of file +#endif // __HIDDESCRIPTORPARSER_H__ diff --git a/masstorage.h b/masstorage.h index 82a8abf0..81855faf 100644 --- a/masstorage.h +++ b/masstorage.h @@ -1,6 +1,12 @@ #if !defined(__MASSTORAGE_H__) #define __MASSTORAGE_H__ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #define DEBUG #include @@ -11,12 +17,6 @@ #include "usb_ch9.h" #include "Usb.h" -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - #include #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) diff --git a/message.cpp b/message.cpp index ecbc0d40..7aab07e8 100644 --- a/message.cpp +++ b/message.cpp @@ -15,6 +15,12 @@ Web : http://www.circuitsathome.com e-mail : support@circuitsathome.com */ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #define DEBUG #include "message.h" // 0x80 is the default (i.e. trace) to turn off set this global to something lower. diff --git a/parsetools.h b/parsetools.h index 59402df2..c51b9862 100644 --- a/parsetools.h +++ b/parsetools.h @@ -17,18 +17,18 @@ e-mail : support@circuitsathome.com #if !defined(__PARSETOOLS_H__) #define __PARSETOOLS_H__ -#include -#include -#include "printhex.h" -#include "hexdump.h" -#include "message.h" - #if defined(ARDUINO) && ARDUINO >=100 #include "Arduino.h" #else #include #endif +#include +#include +#include "printhex.h" +#include "hexdump.h" +#include "message.h" + struct MultiValueBuffer { uint8_t valueSize; void *pValue; diff --git a/usbhub.h b/usbhub.h index 0150c4b3..b2426693 100644 --- a/usbhub.h +++ b/usbhub.h @@ -17,6 +17,12 @@ e-mail : support@circuitsathome.com #if !defined(__USBHUB_H__) #define __USBHUB_H__ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif + #include #include #include "avrpins.h" @@ -26,12 +32,6 @@ e-mail : support@circuitsathome.com #include "Usb.h" -#if defined(ARDUINO) && ARDUINO >=100 -#include "Arduino.h" -#else -#include -#endif - #define USB_DESCRIPTOR_HUB 0x09 // Hub descriptor type // Hub Requests From 8c89442590edbc99725955bc5283a46185532b29 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Wed, 29 May 2013 10:42:21 -0700 Subject: [PATCH 04/12] Comment out assert() - TODO: fix assert on Teensy3 --- Usb.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Usb.cpp b/Usb.cpp index c73760e2..a777ed8d 100644 --- a/Usb.cpp +++ b/Usb.cpp @@ -241,7 +241,7 @@ uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, ui } pktsize = regRd(rRCVBC); //number of received bytes //printf("Got %i bytes ", pktsize); - assert(pktsize <= nbytes); + //assert(pktsize <= nbytes); int16_t mem_left = (int16_t)nbytes - *((int16_t*)nbytesptr); From ae3e6ca74b8e46eedd9c1834d1cdb09eea390bb0 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Wed, 29 May 2013 10:46:53 -0700 Subject: [PATCH 05/12] Add const to char* (fixes warnings) --- XBOXRECV.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 4087519f..3706b1ea 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -302,7 +302,7 @@ void XBOXRECV::readReport(uint8_t controller) { #endif if (Xbox360Connected[controller]) { #ifdef DEBUG - char* str = 0; + const char* str = 0; switch (readBuf[1]) { case 0x80: str = PSTR(" as controller\r\n"); break; From d0c362b66f93e5ab019e01fbb00657af08970929 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Wed, 29 May 2013 10:53:36 -0700 Subject: [PATCH 06/12] Add pgm_read_pointer for 16 vs 32 bit pointers --- avrpins.h | 3 ++ hidescriptorparser.cpp | 104 ++++++++++++++++++++--------------------- 2 files changed, 55 insertions(+), 52 deletions(-) diff --git a/avrpins.h b/avrpins.h index e8816c05..d37531de 100644 --- a/avrpins.h +++ b/avrpins.h @@ -22,6 +22,9 @@ e-mail : support@circuitsathome.com #if defined(__AVR__) +// pointers are 16 bits on AVR +#define pgm_read_pointer(p) pgm_read_word(p) + #if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__) /* Uncomment the following if you have Arduino Mega ADK board with MAX3421e built-in */ //#define BOARD_MEGA_ADK diff --git a/hidescriptorparser.cpp b/hidescriptorparser.cpp index a0226f2f..36c102ad 100644 --- a/hidescriptorparser.cpp +++ b/hidescriptorparser.cpp @@ -1222,7 +1222,7 @@ void ReportDescParserBase::SetUsagePage(uint16_t page) { //else if (page > 0x83 && page < 0x8c) // Notify(pstrUsagePagePower); //else if (page > 0x8b && page < 0x92) - // Notify((char*)pgm_read_word(&usagePageTitles1[page - 0x8c])); + // Notify((char*)pgm_read_pointer(&usagePageTitles1[page - 0x8c])); //else if (page > 0xfeff && page <= 0xffff) // Notify(pstrUsagePageVendorDefined); else @@ -1240,13 +1240,13 @@ void ReportDescParserBase::PrintUsagePage(uint16_t page) { Notify(pstrSpace, 0x80); if (page > 0x00 && page < 0x11) - Notify((char*)pgm_read_word(&usagePageTitles0[page - 1]), 0x80); + Notify((char*)pgm_read_pointer(&usagePageTitles0[page - 1]), 0x80); else if (page > 0x7f && page < 0x84) Notify(pstrUsagePageMonitor, 0x80); else if (page > 0x83 && page < 0x8c) Notify(pstrUsagePagePower, 0x80); else if (page > 0x8b && page < 0x92) - Notify((char*)pgm_read_word(&usagePageTitles1[page - 0x8c]), 0x80); + Notify((char*)pgm_read_pointer(&usagePageTitles1[page - 0x8c]), 0x80); else if (page > 0xfeff && page <= 0xffff) Notify(pstrUsagePageVendorDefined, 0x80); else @@ -1283,15 +1283,15 @@ void ReportDescParserBase::PrintGenericDesktopPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x0a) - Notify((char*)pgm_read_word(&genDesktopTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&genDesktopTitles0[usage - 1]), 0x80); else if (usage > 0x2f && usage < 0x49) - Notify((char*)pgm_read_word(&genDesktopTitles1[usage - 0x30]), 0x80); + Notify((char*)pgm_read_pointer(&genDesktopTitles1[usage - 0x30]), 0x80); else if (usage > 0x7f && usage < 0x94) - Notify((char*)pgm_read_word(&genDesktopTitles2[usage - 0x80]), 0x80); + Notify((char*)pgm_read_pointer(&genDesktopTitles2[usage - 0x80]), 0x80); else if (usage > 0x9f && usage < 0xa9) - Notify((char*)pgm_read_word(&genDesktopTitles3[usage - 0xa0]), 0x80); + Notify((char*)pgm_read_pointer(&genDesktopTitles3[usage - 0xa0]), 0x80); else if (usage > 0xaf && usage < 0xb8) - Notify((char*)pgm_read_word(&genDesktopTitles4[usage - 0xb0]), 0x80); + Notify((char*)pgm_read_pointer(&genDesktopTitles4[usage - 0xb0]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1300,11 +1300,11 @@ void ReportDescParserBase::PrintSimulationControlsPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x0d) - Notify((char*)pgm_read_word(&simuTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&simuTitles0[usage - 1]), 0x80); else if (usage > 0x1f && usage < 0x26) - Notify((char*)pgm_read_word(&simuTitles1[usage - 0x20]), 0x80); + Notify((char*)pgm_read_pointer(&simuTitles1[usage - 0x20]), 0x80); else if (usage > 0xaf && usage < 0xd1) - Notify((char*)pgm_read_word(&simuTitles2[usage - 0xb0]), 0x80); + Notify((char*)pgm_read_pointer(&simuTitles2[usage - 0xb0]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1313,9 +1313,9 @@ void ReportDescParserBase::PrintVRControlsPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x0b) - Notify((char*)pgm_read_word(&vrTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&vrTitles0[usage - 1]), 0x80); else if (usage > 0x1f && usage < 0x22) - Notify((char*)pgm_read_word(&vrTitles1[usage - 0x20]), 0x80); + Notify((char*)pgm_read_pointer(&vrTitles1[usage - 0x20]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1324,11 +1324,11 @@ void ReportDescParserBase::PrintSportsControlsPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x05) - Notify((char*)pgm_read_word(&sportsCtrlTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&sportsCtrlTitles0[usage - 1]), 0x80); else if (usage > 0x2f && usage < 0x3a) - Notify((char*)pgm_read_word(&sportsCtrlTitles1[usage - 0x30]), 0x80); + Notify((char*)pgm_read_pointer(&sportsCtrlTitles1[usage - 0x30]), 0x80); else if (usage > 0x4f && usage < 0x64) - Notify((char*)pgm_read_word(&sportsCtrlTitles2[usage - 0x50]), 0x80); + Notify((char*)pgm_read_pointer(&sportsCtrlTitles2[usage - 0x50]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1337,9 +1337,9 @@ void ReportDescParserBase::PrintGameControlsPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x04) - Notify((char*)pgm_read_word(&gameTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&gameTitles0[usage - 1]), 0x80); else if (usage > 0x1f && usage < 0x3a) - Notify((char*)pgm_read_word(&gameTitles1[usage - 0x20]), 0x80); + Notify((char*)pgm_read_pointer(&gameTitles1[usage - 0x20]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1348,7 +1348,7 @@ void ReportDescParserBase::PrintGenericDeviceControlsPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x1f && usage < 0x27) - Notify((char*)pgm_read_word(&genDevCtrlTitles[usage - 0x20]), 0x80); + Notify((char*)pgm_read_pointer(&genDevCtrlTitles[usage - 0x20]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1357,7 +1357,7 @@ void ReportDescParserBase::PrintLEDPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x4e) - Notify((char*)pgm_read_word(&ledTitles[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&ledTitles[usage - 1]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1366,17 +1366,17 @@ void ReportDescParserBase::PrintTelephonyPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x08) - Notify((char*)pgm_read_word(&telTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&telTitles0[usage - 1]), 0x80); else if (usage > 0x1f && usage < 0x32) - Notify((char*)pgm_read_word(&telTitles1[usage - 0x1f]), 0x80); + Notify((char*)pgm_read_pointer(&telTitles1[usage - 0x1f]), 0x80); else if (usage > 0x4f && usage < 0x54) - Notify((char*)pgm_read_word(&telTitles2[usage - 0x4f]), 0x80); + Notify((char*)pgm_read_pointer(&telTitles2[usage - 0x4f]), 0x80); else if (usage > 0x6f && usage < 0x75) - Notify((char*)pgm_read_word(&telTitles3[usage - 0x6f]), 0x80); + Notify((char*)pgm_read_pointer(&telTitles3[usage - 0x6f]), 0x80); else if (usage > 0x8f && usage < 0x9f) - Notify((char*)pgm_read_word(&telTitles4[usage - 0x8f]), 0x80); + Notify((char*)pgm_read_pointer(&telTitles4[usage - 0x8f]), 0x80); else if (usage > 0xaf && usage < 0xc0) - Notify((char*)pgm_read_word(&telTitles5[usage - 0xaf]), 0x80); + Notify((char*)pgm_read_pointer(&telTitles5[usage - 0xaf]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1385,35 +1385,35 @@ void ReportDescParserBase::PrintConsumerPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x07) - Notify((char*)pgm_read_word(&consTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles0[usage - 1]), 0x80); else if (usage > 0x1f && usage < 0x23) - Notify((char*)pgm_read_word(&consTitles1[usage - 0x1f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles1[usage - 0x1f]), 0x80); else if (usage > 0x2f && usage < 0x37) - Notify((char*)pgm_read_word(&consTitles2[usage - 0x2f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles2[usage - 0x2f]), 0x80); else if (usage > 0x3f && usage < 0x49) - Notify((char*)pgm_read_word(&consTitles3[usage - 0x3f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles3[usage - 0x3f]), 0x80); else if (usage > 0x5f && usage < 0x67) - Notify((char*)pgm_read_word(&consTitles4[usage - 0x5f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles4[usage - 0x5f]), 0x80); else if (usage > 0x7f && usage < 0xa5) - Notify((char*)pgm_read_word(&consTitles5[usage - 0x7f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles5[usage - 0x7f]), 0x80); else if (usage > 0xaf && usage < 0xcf) - Notify((char*)pgm_read_word(&consTitles6[usage - 0xaf]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles6[usage - 0xaf]), 0x80); else if (usage > 0xdf && usage < 0xeb) - Notify((char*)pgm_read_word(&consTitles7[usage - 0xdf]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles7[usage - 0xdf]), 0x80); else if (usage > 0xef && usage < 0xf6) - Notify((char*)pgm_read_word(&consTitles8[usage - 0xef]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles8[usage - 0xef]), 0x80); else if (usage > 0xff && usage < 0x10e) - Notify((char*)pgm_read_word(&consTitles9[usage - 0xff]), 0x80); + Notify((char*)pgm_read_pointer(&consTitles9[usage - 0xff]), 0x80); else if (usage > 0x14f && usage < 0x156) - Notify((char*)pgm_read_word(&consTitlesA[usage - 0x14f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitlesA[usage - 0x14f]), 0x80); else if (usage > 0x15f && usage < 0x16b) - Notify((char*)pgm_read_word(&consTitlesB[usage - 0x15f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitlesB[usage - 0x15f]), 0x80); else if (usage > 0x16f && usage < 0x175) - Notify((char*)pgm_read_word(&consTitlesC[usage - 0x16f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitlesC[usage - 0x16f]), 0x80); else if (usage > 0x17f && usage < 0x1c8) - Notify((char*)pgm_read_word(&consTitlesD[usage - 0x17f]), 0x80); + Notify((char*)pgm_read_pointer(&consTitlesD[usage - 0x17f]), 0x80); else if (usage > 0x1ff && usage < 0x29d) - Notify((char*)pgm_read_word(&consTitlesE[usage - 0x1ff]), 0x80); + Notify((char*)pgm_read_pointer(&consTitlesE[usage - 0x1ff]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1422,11 +1422,11 @@ void ReportDescParserBase::PrintDigitizerPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x0e) - Notify((char*)pgm_read_word(&digitTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&digitTitles0[usage - 1]), 0x80); else if (usage > 0x1f && usage < 0x23) - Notify((char*)pgm_read_word(&digitTitles1[usage - 0x1f]), 0x80); + Notify((char*)pgm_read_pointer(&digitTitles1[usage - 0x1f]), 0x80); else if (usage > 0x2f && usage < 0x47) - Notify((char*)pgm_read_word(&digitTitles2[usage - 0x2f]), 0x80); + Notify((char*)pgm_read_pointer(&digitTitles2[usage - 0x2f]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1435,11 +1435,11 @@ void ReportDescParserBase::PrintAlphanumDisplayPageUsage(uint16_t usage) { Notify(pstrSpace, 0x80); if (usage > 0x00 && usage < 0x03) - Notify((char*)pgm_read_word(&aplphanumTitles0[usage - 1]), 0x80); + Notify((char*)pgm_read_pointer(&aplphanumTitles0[usage - 1]), 0x80); else if (usage > 0x1f && usage < 0x4e) - Notify((char*)pgm_read_word(&aplphanumTitles1[usage - 0x1f]), 0x80); + Notify((char*)pgm_read_pointer(&aplphanumTitles1[usage - 0x1f]), 0x80); else if (usage > 0x7f && usage < 0x96) - Notify((char*)pgm_read_word(&digitTitles2[usage - 0x80]), 0x80); + Notify((char*)pgm_read_pointer(&digitTitles2[usage - 0x80]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } @@ -1450,17 +1450,17 @@ void ReportDescParserBase::PrintMedicalInstrumentPageUsage(uint16_t usage) { if (usage == 1) Notify(pstrUsageMedicalUltrasound, 0x80); else if (usage > 0x1f && usage < 0x28) - Notify((char*)pgm_read_word(&medInstrTitles0[usage - 0x1f]), 0x80); + Notify((char*)pgm_read_pointer(&medInstrTitles0[usage - 0x1f]), 0x80); else if (usage > 0x3f && usage < 0x45) - Notify((char*)pgm_read_word(&medInstrTitles1[usage - 0x40]), 0x80); + Notify((char*)pgm_read_pointer(&medInstrTitles1[usage - 0x40]), 0x80); else if (usage > 0x5f && usage < 0x62) - Notify((char*)pgm_read_word(&medInstrTitles2[usage - 0x60]), 0x80); + Notify((char*)pgm_read_pointer(&medInstrTitles2[usage - 0x60]), 0x80); else if (usage == 0x70) Notify(pstrUsageDepthGainCompensation, 0x80); else if (usage > 0x7f && usage < 0x8a) - Notify((char*)pgm_read_word(&medInstrTitles3[usage - 0x80]), 0x80); + Notify((char*)pgm_read_pointer(&medInstrTitles3[usage - 0x80]), 0x80); else if (usage > 0x9f && usage < 0xa2) - Notify((char*)pgm_read_word(&medInstrTitles4[usage - 0xa0]), 0x80); + Notify((char*)pgm_read_pointer(&medInstrTitles4[usage - 0xa0]), 0x80); else Notify(pstrUsagePageUndefined, 0x80); } From ef57346376c90124cce13cca859bb650d6c68749 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sat, 5 Oct 2013 01:12:43 +0200 Subject: [PATCH 07/12] Check if CORE_TEENSY is defined and added missing semicolon --- avrpins.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/avrpins.h b/avrpins.h index c4ddf556..566b2dc9 100644 --- a/avrpins.h +++ b/avrpins.h @@ -742,7 +742,7 @@ public: #endif // __AVR__ -#if defined(__arm__) +#if defined(__arm__) && defined(CORE_TEENSY) // pointers are 32 bits on ARM #define pgm_read_pointer(p) pgm_read_dword(p) @@ -773,7 +773,7 @@ public: \ static uint8_t IsSet() { \ return *(GPIO_BITBAND_PTR(baseReg, pinNum) + 512); \ } \ -} +}; MAKE_PIN(P0, CORE_PIN0_PORTREG, CORE_PIN0_BIT, CORE_PIN0_CONFIG); MAKE_PIN(P1, CORE_PIN1_PORTREG, CORE_PIN1_BIT, CORE_PIN1_CONFIG); From 41f9979a25c38a7a994f456425e9574f297c3a98 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 21 Oct 2013 01:50:40 +0200 Subject: [PATCH 08/12] Teensy 3.0 confirmed working --- README.md | 2 +- UsbCore.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f282ce5c..3852db97 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ By default serial debugging is disabled. To turn it on simply change ```ENABLE_U Currently the following boards are supported by the library: * All official Arduino AVR boards (Uno, Duemilanove, Mega, Mega 2560, Mega ADK, Leonardo etc.) -* Teensy (Teensy 1.0, Teensy 2.0 and Teensy++ 2.0) +* Teensy (Teensy++ 1.0, Teensy 2.0, Teensy++ 2.0, and Teensy 3.0) * Balanduino * Sanguino * Black Widdow diff --git a/UsbCore.h b/UsbCore.h index 6d1c205d..85b0402e 100644 --- a/UsbCore.h +++ b/UsbCore.h @@ -18,13 +18,13 @@ #ifdef BOARD_BLACK_WIDDOW typedef MAX3421e MAX3421E; // Black Widow #elif defined(CORE_TEENSY) && (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)) -typedef MAX3421e MAX3421E; // Teensy++ 2.0 & 1.0 +typedef MAX3421e MAX3421E; // Teensy++ 1.0 and 2.0 #elif defined(BOARD_MEGA_ADK) typedef MAX3421e MAX3421E; // Arduino Mega ADK #elif defined(ARDUINO_AVR_BALANDUINO) typedef MAX3421e MAX3421E; // Balanduino #else -typedef MAX3421e MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo etc.) +typedef MAX3421e MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo etc.) or Teensy 2.0 and 3.0 #endif /* Common setup data constant combinations */ From 6752a52b8263f9715949d685475cbc1953e40026 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 21 Oct 2013 01:59:29 +0200 Subject: [PATCH 09/12] Added error messages and updated some comments --- avrpins.h | 7 +++++-- usbhost.h | 11 +++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/avrpins.h b/avrpins.h index fd4fdf39..5eb679fb 100644 --- a/avrpins.h +++ b/avrpins.h @@ -613,7 +613,7 @@ public: // Arduino Leonardo pin numbers #elif defined(CORE_TEENSY) && (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)) -// Teensy++ 2.0 pin numbers +// Teensy++ 1.0 and 2.0 pin numbers // http://www.pjrc.com/teensy/pinout.html #define P0 Pd0 #define P1 Pd1 @@ -661,7 +661,7 @@ public: #define P43 Pf5 #define P44 Pf6 #define P45 Pf7 -// Teensy++ 2.0 +// Teensy++ 1.0 and 2.0 #elif defined(ARDUINO_AVR_BALANDUINO) && (defined(__AVR_ATmega644__) || defined(__AVR_ATmega1284P__)) // Balanduino pin numbers @@ -738,6 +738,9 @@ public: #define P31 Pa7 // Sanguino +#else +#error "Please define board in avrpins.h" + #endif // Arduino pin definitions #endif // __AVR__ diff --git a/usbhost.h b/usbhost.h index 04341923..26b8036f 100644 --- a/usbhost.h +++ b/usbhost.h @@ -44,15 +44,14 @@ public: /* SPI pin definitions. see avrpins.h */ #if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) typedef SPi< Pb1, Pb2, Pb3, Pb0 > spi; -#endif -#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) +#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) typedef SPi< Pb5, Pb3, Pb4, Pb2 > spi; -#endif -#if defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) +#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi; -#endif -#if defined(__MK20DX128__) +#elif defined(__MK20DX128__) typedef SPi< P13, P11, P12, P10 > spi; +#else +#error "No SPI entry in usbhost.h" #endif typedef enum { From 82c52542cc522982b026bf7f6cdd9059e5064d4a Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 21 Oct 2013 02:17:47 +0200 Subject: [PATCH 10/12] Use pin definition in avrpins.h to set MAX_RESET pin on Arduino ADK --- avrpins.h | 4 ++++ usbhost.h | 7 +++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/avrpins.h b/avrpins.h index 5eb679fb..5a7e1756 100644 --- a/avrpins.h +++ b/avrpins.h @@ -510,7 +510,11 @@ public: #define P51 Pb2 #define P52 Pb1 #define P53 Pb0 + +#ifdef BOARD_MEGA_ADK // These pins are not broken out on the Arduino ADK #define P54 Pe6 // INT on Arduino ADK +#define P55 Pj2 // MAX_RESET on Arduino ADK +#endif // "Mega" pin numbers diff --git a/usbhost.h b/usbhost.h index 26b8036f..5a8ca4bb 100644 --- a/usbhost.h +++ b/usbhost.h @@ -95,10 +95,9 @@ template< typename SS, typename INTR > MAX3421e< SS, INTR >::MAX3421e() { // Leaving ADK hardware setup in here, for now. This really belongs with the other parts. #ifdef BOARD_MEGA_ADK - /* For Mega ADK, which has Max3421e on-board, set MAX_RESET to Output mode, and pull Reset to HIGH */ - DDRJ |= _BV(PJ2); - PORTJ &= ~_BV(PJ2); - PORTJ |= _BV(PJ2); + // For Mega ADK, which has a Max3421e on-board, set MAX_RESET to output mode, and then set it to HIGH + P55::SetDirWrite(); + P55::Set(); #endif }; From ac1bad4eeebebf2e6ddacfc0c3d896e3b7d6f7ca Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 21 Oct 2013 17:36:42 +0200 Subject: [PATCH 11/12] Faster processors like the Teensy 3.0 needed a delay before setting the configuration to the PS3 controller --- BTD.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/BTD.cpp b/BTD.cpp index a714b5d6..9cf2436b 100755 --- a/BTD.cpp +++ b/BTD.cpp @@ -167,6 +167,7 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) { goto FailSetDevTblEntry; if (VID == PS3_VID && (PID == PS3_PID || PID == PS3NAVIGATION_PID || PID == PS3MOVE_PID)) { + delay(100); rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, 1); // We only need the Control endpoint, so we don't have to initialize the other endpoints of device if (rcode) goto FailSetConfDescr; From d20ae89818bd381e4d8262c95da8cfc6c66f3530 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 21 Oct 2013 18:41:47 +0200 Subject: [PATCH 12/12] Only calculate angle when it is needed --- Wii.cpp | 35 ++++++++---------------- Wii.h | 33 ++++++++++++++-------- examples/Bluetooth/Wii/Wii.ino | 4 +-- examples/Bluetooth/WiiMulti/WiiMulti.ino | 4 +-- keywords.txt | 6 ++-- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/Wii.cpp b/Wii.cpp index 0b911e25..0689f43c 100755 --- a/Wii.cpp +++ b/Wii.cpp @@ -285,11 +285,9 @@ void WII::ACLData(uint8_t* l2capinbuf) { } } if (l2capinbuf[9] == 0x31 || l2capinbuf[9] == 0x33 || l2capinbuf[9] == 0x35 || l2capinbuf[9] == 0x37) { // Read the accelerometer - accX = ((l2capinbuf[12] << 2) | (l2capinbuf[10] & 0x60 >> 5)) - 500; - accY = ((l2capinbuf[13] << 2) | (l2capinbuf[11] & 0x20 >> 4)) - 500; - accZ = ((l2capinbuf[14] << 2) | (l2capinbuf[11] & 0x40 >> 5)) - 500; - wiimotePitch = (atan2(accY, accZ) + PI) * RAD_TO_DEG; - wiimoteRoll = (atan2(accX, accZ) + PI) * RAD_TO_DEG; + accXwiimote = ((l2capinbuf[12] << 2) | (l2capinbuf[10] & 0x60 >> 5)) - 500; + accYwiimote = ((l2capinbuf[13] << 2) | (l2capinbuf[11] & 0x20 >> 4)) - 500; + accZwiimote = ((l2capinbuf[14] << 2) | (l2capinbuf[11] & 0x40 >> 5)) - 500; } switch (l2capinbuf[9]) { case 0x20: // Status Information - (a1) 20 BB BB LF 00 00 VV @@ -416,14 +414,10 @@ void WII::ACLData(uint8_t* l2capinbuf) { case 0x30: // Core buttons - (a1) 30 BB BB break; case 0x31: // Core Buttons and Accelerometer - (a1) 31 BB BB AA AA AA - pitch = wiimotePitch; // The pitch is just equal to the angle calculated from the wiimote as there is no Motion Plus connected - roll = wiimoteRoll; break; case 0x32: // Core Buttons with 8 Extension bytes - (a1) 32 BB BB EE EE EE EE EE EE EE EE break; case 0x33: // Core Buttons with Accelerometer and 12 IR bytes - (a1) 33 BB BB AA AA AA II II II II II II II II II II II II - pitch = wiimotePitch; // The pitch is just equal to the angle calculated from the wiimote as there is no Motion Plus data available - roll = wiimoteRoll; #ifdef WIICAMERA // Read the IR data IR_object_x1 = (l2capinbuf[15] | ((uint16_t)(l2capinbuf[17] & 0x30) << 4)); // x position @@ -486,8 +480,8 @@ void WII::ACLData(uint8_t* l2capinbuf) { if (!(l2capinbuf[19] & 0x02)) // Check if fast more is used rollGyroSpeed *= 4.545; - pitch = (0.93 * (pitch + (pitchGyroSpeed * (double)(micros() - timer) / 1000000)))+(0.07 * wiimotePitch); // Use a complimentary filter to calculate the angle - roll = (0.93 * (roll + (rollGyroSpeed * (double)(micros() - timer) / 1000000)))+(0.07 * wiimoteRoll); + compPitch = (0.93 * (compPitch + (pitchGyroSpeed * (double)(micros() - timer) / 1000000)))+(0.07 * getWiimotePitch()); // Use a complimentary filter to calculate the angle + compRoll = (0.93 * (compRoll + (rollGyroSpeed * (double)(micros() - timer) / 1000000)))+(0.07 * getWiimoteRoll()); gyroYaw += (yawGyroSpeed * ((double)(micros() - timer) / 1000000)); gyroRoll += (rollGyroSpeed * ((double)(micros() - timer) / 1000000)); @@ -533,11 +527,9 @@ void WII::ACLData(uint8_t* l2capinbuf) { if (nunchuckConnected) { hatValues[HatX] = l2capinbuf[15]; hatValues[HatY] = l2capinbuf[16]; - accX = ((l2capinbuf[17] << 2) | (l2capinbuf[20] & 0x10 >> 3)) - 416; - accY = ((l2capinbuf[18] << 2) | (l2capinbuf[20] & 0x20 >> 4)) - 416; - accZ = (((l2capinbuf[19] & 0xFE) << 2) | (l2capinbuf[20] & 0xC0 >> 5)) - 416; - nunchuckPitch = (atan2(accY, accZ) + PI) * RAD_TO_DEG; - nunchuckRoll = (atan2(accX, accZ) + PI) * RAD_TO_DEG; + accXnunchuck = ((l2capinbuf[17] << 2) | (l2capinbuf[20] & 0x10 >> 3)) - 416; + accYnunchuck = ((l2capinbuf[18] << 2) | (l2capinbuf[20] & 0x20 >> 4)) - 416; + accZnunchuck = (((l2capinbuf[19] & 0xFE) << 2) | (l2capinbuf[20] & 0xC0 >> 5)) - 416; } //else if(classicControllerConnected) { } } @@ -563,14 +555,9 @@ void WII::ACLData(uint8_t* l2capinbuf) { } else if (nunchuckConnected) { hatValues[HatX] = l2capinbuf[15]; hatValues[HatY] = l2capinbuf[16]; - accX = ((l2capinbuf[17] << 2) | (l2capinbuf[20] & 0x0C >> 2)) - 416; - accY = ((l2capinbuf[18] << 2) | (l2capinbuf[20] & 0x30 >> 4)) - 416; - accZ = ((l2capinbuf[19] << 2) | (l2capinbuf[20] & 0xC0 >> 6)) - 416; - nunchuckPitch = (atan2(accY, accZ) + PI) * RAD_TO_DEG; - nunchuckRoll = (atan2(accX, accZ) + PI) * RAD_TO_DEG; - - pitch = wiimotePitch; // The pitch is just equal to the angle calculated from the wiimote as there is no Motion Plus connected - roll = wiimoteRoll; + accXnunchuck = ((l2capinbuf[17] << 2) | (l2capinbuf[20] & 0x0C >> 2)) - 416; + accYnunchuck = ((l2capinbuf[18] << 2) | (l2capinbuf[20] & 0x30 >> 4)) - 416; + accZnunchuck = ((l2capinbuf[19] << 2) | (l2capinbuf[20] & 0xC0 >> 6)) - 416; } else if (wiiUProControllerConnected) { hatValues[LeftHatX] = (l2capinbuf[15] | l2capinbuf[16] << 8); hatValues[RightHatX] = (l2capinbuf[17] | l2capinbuf[18] << 8); diff --git a/Wii.h b/Wii.h index 33623f2a..f1ba6a90 100755 --- a/Wii.h +++ b/Wii.h @@ -152,7 +152,9 @@ public: * @return Pitch in the range from 0-360. */ double getPitch() { - return pitch; + if (motionPlusConnected) + return compPitch; + return getWiimotePitch(); }; /** @@ -160,7 +162,9 @@ public: * @return Roll in the range from 0-360. */ double getRoll() { - return roll; + if (motionPlusConnected) + return compRoll; + return getWiimoteRoll(); }; /** @@ -250,21 +254,28 @@ public: /**@{*/ /** Pitch and roll calculated from the accelerometer inside the Wiimote. */ - double wiimotePitch; - double wiimoteRoll; + double getWiimotePitch() { + return (atan2(accYwiimote, accZwiimote) + PI) * RAD_TO_DEG; + }; + double getWiimoteRoll() { + return (atan2(accXwiimote, accZwiimote) + PI) * RAD_TO_DEG; + }; /**@}*/ /**@{*/ /** Pitch and roll calculated from the accelerometer inside the Nunchuck. */ - double nunchuckPitch; - double nunchuckRoll; + double getNunchuckPitch() { + return (atan2(accYnunchuck, accZnunchuck) + PI) * RAD_TO_DEG; + }; + double getNunchuckRoll() { + return (atan2(accXnunchuck, accZnunchuck) + PI) * RAD_TO_DEG; + }; /**@}*/ /**@{*/ /** Accelerometer values used to calculate pitch and roll. */ - int16_t accX; - int16_t accY; - int16_t accZ; + int16_t accXwiimote, accYwiimote, accZwiimote; + int16_t accXnunchuck, accYnunchuck, accZnunchuck; /**@}*/ /* Variables for the gyro inside the Motion Plus */ @@ -476,8 +487,8 @@ private: void initMotionPlus(); void activateMotionPlus(); - double pitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected - double roll; // Fusioned angle using a complimentary filter if the Motion Plus is connected + double compPitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected + double compRoll; // Fusioned angle using a complimentary filter if the Motion Plus is connected bool activateNunchuck; bool motionValuesReset; // This bool is true when the gyro values has been reset diff --git a/examples/Bluetooth/Wii/Wii.ino b/examples/Bluetooth/Wii/Wii.ino index 082766e2..0aab3776 100644 --- a/examples/Bluetooth/Wii/Wii.ino +++ b/examples/Bluetooth/Wii/Wii.ino @@ -83,9 +83,9 @@ void loop() { } if(Wii.nunchuckConnected) { Serial.print(F("\tNunchuck Pitch: ")); - Serial.print(Wii.nunchuckPitch); + Serial.print(Wii.getNunchuckPitch()); Serial.print(F("\tNunchuck Roll: ")); - Serial.print(Wii.nunchuckRoll); + Serial.print(Wii.getNunchuckRoll()); } } } diff --git a/examples/Bluetooth/WiiMulti/WiiMulti.ino b/examples/Bluetooth/WiiMulti/WiiMulti.ino index 2df008c2..f02a23b5 100644 --- a/examples/Bluetooth/WiiMulti/WiiMulti.ino +++ b/examples/Bluetooth/WiiMulti/WiiMulti.ino @@ -91,9 +91,9 @@ void loop() { } if(Wii[i]->nunchuckConnected) { Serial.print(F("\tNunchuck Pitch: ")); - Serial.print(Wii[i]->nunchuckPitch); + Serial.print(Wii[i]->getNunchuckPitch()); Serial.print(F("\tNunchuck Roll: ")); - Serial.print(Wii[i]->nunchuckRoll); + Serial.print(Wii[i]->getNunchuckRoll()); } } } diff --git a/keywords.txt b/keywords.txt index fb28eec1..b68eda9c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -245,6 +245,10 @@ setRumbleToggle KEYWORD2 getPitch KEYWORD2 getRoll KEYWORD2 getYaw KEYWORD2 +getWiimotePitch KEYWORD2 +getWiimoteRoll KEYWORD2 +getNunchuckPitch KEYWORD2 +getNunchuckRoll KEYWORD2 PAIR KEYWORD2 statusRequest KEYWORD2 getBatteryLevel KEYWORD2 @@ -267,8 +271,6 @@ ZL LITERAL1 ZR LITERAL1 HatX LITERAL1 HatY LITERAL1 -nunchuckPitch LITERAL1 -nunchuckRoll LITERAL1 #################################################### # Methods and Functions for the IR Camera