Demo for USBJoystick updated for 32 buttons.
Dependencies: USBDevice USBJoystick_SIM mbed USBJoystick_2
Fork of USBJoystick_HelloWorld2 by
Revision 4:dc3556a31262, committed 2018-09-29
- Comitter:
- Cirrus01
- Date:
- Sat Sep 29 09:28:24 2018 +0000
- Parent:
- 3:3ddaf1227e1b
- Commit message:
- Bug fixes for Descriptor and Hat Button reading
Changed in this revision
diff -r 3ddaf1227e1b -r dc3556a31262 USBJoystick.lib --- a/USBJoystick.lib Sun Sep 16 09:57:12 2018 +0000 +++ b/USBJoystick.lib Sat Sep 29 09:28:24 2018 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/Cirrus01/code/USBJoystick_SIM/#550cca870c98 +https://os.mbed.com/users/Cirrus01/code/USBJoystick_SIM/#71835900760f
diff -r 3ddaf1227e1b -r dc3556a31262 USBJoystick_SIM.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBJoystick_SIM.lib Sat Sep 29 09:28:24 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Cirrus01/code/USBJoystick_2/#3ddaf1227e1b
diff -r 3ddaf1227e1b -r dc3556a31262 config.h --- a/config.h Sun Sep 16 09:57:12 2018 +0000 +++ b/config.h Sat Sep 29 09:28:24 2018 +0000 @@ -69,10 +69,10 @@ }; PinName hatMap[] = { - PA_13, // button 1 - PA_14, // button 2 - PA_15, // button 3 - PB_7, // button 4 + PC_2, // button 1 + PC_3, // button 2 + PC_10, // button 3 + PC_12, // button 4 NC, // button 5 NC, // button 6 NC, // button 7
diff -r 3ddaf1227e1b -r dc3556a31262 main.cpp --- a/main.cpp Sun Sep 16 09:57:12 2018 +0000 +++ b/main.cpp Sat Sep 29 09:28:24 2018 +0000 @@ -35,12 +35,12 @@ DigitalOut myled3(LED3); DigitalOut heartbeatLED(LED1); -AnalogIn inX(A0); -AnalogIn inY(A1); -AnalogIn inRudder(A2); -AnalogIn inThrottle(A3); -AnalogIn inBreaks(A4); -AnalogIn inFlaps(A5); +AnalogIn inX(A0); // X +AnalogIn inY(A1); // Y +AnalogIn inRudder(A2); // Rz (Rudder) +AnalogIn inThrottle(A3); // Slider (Throttle) +AnalogIn inBreaks(A4); // Z (Breaks) +AnalogIn inFlaps(A5); // Rx (Flaps) Ticker heartbeat; Serial pc(USBTX, USBRX); // tx, rx @@ -91,7 +91,7 @@ void initButtons() { // create the digital inputs - for (int i = 0 ; i < countof(buttonDigIn) ; ++i) + for (int i = 0 ; i < countof(buttonDigIn) ; i++) { if (i < countof(buttonMap) && buttonMap[i] != NC) buttonDigIn[i] = new DigitalIn(buttonMap[i]); @@ -120,7 +120,7 @@ uint32_t bit = 1; DigitalIn **di = buttonDigIn; ButtonState *bs = buttonState; - for (int i = 0 ; i < countof(buttonDigIn) ; ++i, ++di, ++bs, bit <<= 1) + for (int i = 0 ; i < countof(buttonDigIn) ; i++, di++, bs++, bit <<= 1) { // read this button if (*di != 0) @@ -157,6 +157,7 @@ // if it's pressed, OR its bit into the state if (bs->pressed) buttons |= bit; + pc.printf("Buttons: %d\n", buttons); } } @@ -184,13 +185,16 @@ void initHat() { // create the digital inputs - for (int i = 0 ; i < countof(hatDigIn) ; ++i) + for (int i = 0 ; i <= countof(hatDigIn) ; i++) { if (i < countof(hatMap) && hatMap[i] != NC) hatDigIn[i] = new DigitalIn(hatMap[i]); else hatDigIn[i] = 0; } + + // start the hat timer + hatTimer.start(); } // read the hat button input state @@ -209,7 +213,7 @@ uint8_t bit = 1; DigitalIn **di = hatDigIn; HatState *hs = hatState; - for (int i = 0 ; i < countof(hatDigIn) ; ++i, ++di, ++hs, bit <<= 1) + for (int i = 0 ; i < countof(hatDigIn) ; i++, di++, hs++) { // read this button if (*di != 0) @@ -246,8 +250,9 @@ // if it's pressed, OR its bit into the state if (hs->pressed) hat |= bit; - pc.printf("Hat: %d\n", hat); + pc.printf("Hat: %d\n", hat); } + bit <<= 1; } // translate values read to descriptor values @@ -305,34 +310,24 @@ #endif // return the new button list - //pc.printf("Hat: %d", hat); + pc.printf("Return Hat: %d", hat); return hat; } int main() { - //uint16_t i = 0; - int16_t throttle = 0; - int16_t rudder = 0; - int16_t breaks = 0; - int16_t flaps = 0; - int16_t x = 0; - int16_t y = 0; - //int32_t radius = 120; - //int32_t angle = 0; - //uint8_t tmp = 0; - //uint32_t tmp = 1; + uint16_t x = 0; + uint16_t y = 0; + uint16_t breaks = 0; + uint16_t flaps = 0; + uint16_t rudder = 0; + uint16_t throttle = 0; + uint8_t hat = 0; uint32_t buttons = 0; - uint8_t hat = 0; -#if (SBYTE == 1) - const int16_t l = 127; - const int16_t m = 256; -#endif -#if (SWORD == 1) - const int16_t l = 32767; - const int16_t m = 65535; -#endif + +// const int16_t l = 32767; + const uint16_t m = 65535; pc.printf("Hello World from Joystick!\n\r"); @@ -341,92 +336,22 @@ heartbeat_start(); + pc.printf("x, y, breaks, flaps, rudder, throttle, hat, buttons\n\n\r"); + while (1) { - // Basic Joystick - // throttle = (i >> 8) & 0xFF; // value -127 .. 128 - // rudder = (i >> 8) & 0xFF; // value -127 .. 128 - // breaks = (i >> 8) & 0xFF; // value 0 .. 255 - // flaps = (i >> 8) & 0xFF; // value 0 .. 255 -/* -#if (BUTTONS4 == 1) - buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button -#endif -#if (BUTTONS8 == 1) - buttons = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button -#endif -#if (BUTTONS32 == 1) - //tmp = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button - //buttons = (( tmp << 0) & 0x000000FF); - //buttons = buttons | ((~tmp << 8) & 0x0000FF00); - //buttons = buttons | (( tmp << 16) & 0x00FF0000); - //buttons = buttons | ((~tmp << 24) & 0xFF000000);// - buttons = tmp; - tmp += 1; - pc.printf("Tmp: %u\n", tmp); -#endif - -#if (HAT4 == 1) - hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral -#endif -#if (HAT8 == 1) - hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral -#endif - i++; - - //x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 - //y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 - //angle += 3; -*/ - + x = inX.read() * m; + y = inY.read() * m; + breaks = inBreaks.read() * m; + flaps = inFlaps.read() * m; + rudder = inRudder.read() * m; + throttle = inThrottle.read() * m; + hat = readHat(); buttons = readButtons(); - hat = readHat(); - //pc.printf("Hat: %d\n", hat); - - throttle = inThrottle.read() * m - l; - if(throttle < -l) - throttle = -l; - if(throttle > l) - throttle = l; - rudder = inRudder.read() * m - l; - if(rudder < -l) - rudder = -l; - if(rudder > l) - rudder = l; - breaks = inBreaks.read() * m - l; - if(breaks < -l) - breaks = -l; - if(breaks > l) - breaks = l; - flaps = inFlaps.read() * m - l; - if(flaps < -l) - flaps = -l; - if(flaps > l) - flaps = l; + pc.printf("%d, %d, %d, %d, %d, %d, %d, %d\n\r", x, y, breaks, flaps, rudder, throttle, hat, buttons); - x = inX.read() * m - l; - if(x < -l) - x = -l; - if(x > l) - x = l; - - y = inY.read() * m - l; - if(y < -l) - y = -l; - if(y > l) - y = l; - - -/* x = int(((double)inX.read() - 0.5) * 254); - y = int(((double)inY.read() - 0.5) * 254); - flaps = int(((double)inFlaps.read() - 0.5) * 254); - breaks = int(((double)inBreaks.read() - 0.5) * 254); - rudder = int(((double)inRudder.read() - 0.5) * 254); - throttle = int(((double)inThrottle.read() - 0.5) * 254); -*/ - - joystick.update(throttle, rudder, breaks, flaps, x, y, buttons, hat); + joystick.update(x, y, breaks, flaps, rudder, throttle, hat, buttons); wait(0.01); }