test joystick demo
Dependencies: mbed QEI USBDevice USBJoystick
main.cpp@3:5edd4d529cfd, 2019-11-13 (annotated)
- Committer:
- sean3381212
- Date:
- Wed Nov 13 16:15:44 2019 +0000
- Revision:
- 3:5edd4d529cfd
- Parent:
- 2:712ceeae4f56
add "USER BUTTON"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:e43878690c0e | 1 | /* mbed USBJoystick Library Demo |
wim | 0:e43878690c0e | 2 | * Copyright (c) 2012, v01: Initial version, WH, |
wim | 0:e43878690c0e | 3 | * Modified USBMouse code ARM Limited. |
wim | 0:e43878690c0e | 4 | * (c) 2010-2011 mbed.org, MIT License |
wim | 0:e43878690c0e | 5 | * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button |
wim | 0:e43878690c0e | 6 | * |
wim | 0:e43878690c0e | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:e43878690c0e | 8 | * of this software and associated documentation files (the "Software"), to deal |
wim | 0:e43878690c0e | 9 | * in the Software without restriction, inclumosig without limitation the rights |
wim | 0:e43878690c0e | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:e43878690c0e | 11 | * copies of the Software, and to permit persons to whom the Software is |
wim | 0:e43878690c0e | 12 | * furnished to do so, subject to the following conditions: |
wim | 0:e43878690c0e | 13 | * |
wim | 0:e43878690c0e | 14 | * The above copyright notice and this permission notice shall be included in |
wim | 0:e43878690c0e | 15 | * all copies or substantial portions of the Software. |
wim | 0:e43878690c0e | 16 | * |
wim | 0:e43878690c0e | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:e43878690c0e | 18 | * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:e43878690c0e | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:e43878690c0e | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:e43878690c0e | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:e43878690c0e | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:e43878690c0e | 23 | * THE SOFTWARE. |
wim | 0:e43878690c0e | 24 | */ |
wim | 0:e43878690c0e | 25 | |
wim | 0:e43878690c0e | 26 | #include "mbed.h" |
wim | 0:e43878690c0e | 27 | #include "USBJoystick.h" |
sean3381212 | 2:712ceeae4f56 | 28 | #include "QEI.h" |
wim | 0:e43878690c0e | 29 | |
wim | 0:e43878690c0e | 30 | //#define LANDTIGER 1 |
wim | 0:e43878690c0e | 31 | |
wim | 0:e43878690c0e | 32 | //USBMouse mouse; |
wim | 0:e43878690c0e | 33 | USBJoystick joystick; |
wim | 0:e43878690c0e | 34 | |
wim | 0:e43878690c0e | 35 | // Variables for Heartbeat and Status monitoring |
wim | 0:e43878690c0e | 36 | DigitalOut myled1(LED1); |
wim | 0:e43878690c0e | 37 | DigitalOut myled2(LED2); |
wim | 0:e43878690c0e | 38 | DigitalOut myled3(LED3); |
wim | 0:e43878690c0e | 39 | DigitalOut heartbeatLED(LED4); |
wim | 0:e43878690c0e | 40 | |
sean3381212 | 3:5edd4d529cfd | 41 | InterruptIn button(USER_BUTTON); |
sean3381212 | 3:5edd4d529cfd | 42 | volatile bool send=true; |
sean3381212 | 3:5edd4d529cfd | 43 | |
wim | 0:e43878690c0e | 44 | Ticker heartbeat; |
wim | 0:e43878690c0e | 45 | Serial pc(USBTX, USBRX); // tx, rx |
wim | 0:e43878690c0e | 46 | |
sean3381212 | 2:712ceeae4f56 | 47 | //Use X4 encoding. |
sean3381212 | 3:5edd4d529cfd | 48 | QEI wheel(PA_6, PA_7, NC, 1000, QEI::X4_ENCODING); |
sean3381212 | 2:712ceeae4f56 | 49 | |
wim | 0:e43878690c0e | 50 | // Heartbeat monitor |
wim | 0:e43878690c0e | 51 | void pulse() { |
wim | 0:e43878690c0e | 52 | heartbeatLED = !heartbeatLED; |
wim | 0:e43878690c0e | 53 | } |
wim | 0:e43878690c0e | 54 | |
wim | 0:e43878690c0e | 55 | void heartbeat_start() { |
wim | 0:e43878690c0e | 56 | heartbeatLED=1; |
wim | 0:e43878690c0e | 57 | heartbeat.attach(&pulse, 0.5); |
wim | 0:e43878690c0e | 58 | } |
wim | 0:e43878690c0e | 59 | |
wim | 0:e43878690c0e | 60 | void heartbeat_stop() { |
wim | 0:e43878690c0e | 61 | heartbeat.detach(); |
wim | 0:e43878690c0e | 62 | } |
wim | 0:e43878690c0e | 63 | |
sean3381212 | 3:5edd4d529cfd | 64 | void handlerbutton() |
sean3381212 | 3:5edd4d529cfd | 65 | { |
sean3381212 | 3:5edd4d529cfd | 66 | send=!send; |
sean3381212 | 3:5edd4d529cfd | 67 | } |
sean3381212 | 3:5edd4d529cfd | 68 | |
wim | 0:e43878690c0e | 69 | |
wim | 0:e43878690c0e | 70 | int main() { |
wim | 0:e43878690c0e | 71 | uint16_t i = 0; |
wim | 0:e43878690c0e | 72 | int16_t throttle = 0; |
wim | 0:e43878690c0e | 73 | int16_t rudder = 0; |
wim | 0:e43878690c0e | 74 | int16_t x = 0; |
wim | 0:e43878690c0e | 75 | int16_t y = 0; |
wim | 0:e43878690c0e | 76 | int32_t radius = 120; |
wim | 0:e43878690c0e | 77 | int32_t angle = 0; |
wim | 0:e43878690c0e | 78 | uint8_t tmp = 0; |
wim | 0:e43878690c0e | 79 | uint32_t buttons = 0; |
wim | 0:e43878690c0e | 80 | uint8_t hat = 0; |
sean3381212 | 3:5edd4d529cfd | 81 | int16_t k = 0; |
wim | 0:e43878690c0e | 82 | |
wim | 0:e43878690c0e | 83 | pc.printf("Hello World from Joystick!\n\r"); |
wim | 0:e43878690c0e | 84 | |
wim | 0:e43878690c0e | 85 | heartbeat_start(); |
sean3381212 | 3:5edd4d529cfd | 86 | button.rise(&handlerbutton); |
wim | 0:e43878690c0e | 87 | |
wim | 0:e43878690c0e | 88 | while (1) { |
wim | 0:e43878690c0e | 89 | // Basic Joystick |
sean3381212 | 3:5edd4d529cfd | 90 | //throttle = (i >> 8) & 0xFF; // value -127 .. 128 |
sean3381212 | 3:5edd4d529cfd | 91 | //rudder = (i >> 8) & 0xFF; // value -127 .. 128 |
sean3381212 | 3:5edd4d529cfd | 92 | if(send == true){ |
sean3381212 | 3:5edd4d529cfd | 93 | i = 0; |
sean3381212 | 3:5edd4d529cfd | 94 | k = (i >> 8) & 0x0F; |
sean3381212 | 3:5edd4d529cfd | 95 | pc.printf("OK TRUE! %d \n\r",k); |
sean3381212 | 3:5edd4d529cfd | 96 | } |
sean3381212 | 3:5edd4d529cfd | 97 | else{ |
sean3381212 | 3:5edd4d529cfd | 98 | i = 256; |
sean3381212 | 3:5edd4d529cfd | 99 | k = (i >> 8) & 0x0F; |
sean3381212 | 3:5edd4d529cfd | 100 | pc.printf("OK FAUSE! %d \n\r",k); |
sean3381212 | 3:5edd4d529cfd | 101 | } |
wim | 0:e43878690c0e | 102 | |
wim | 0:e43878690c0e | 103 | #if (BUTTONS4 == 1) |
wim | 0:e43878690c0e | 104 | buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button |
wim | 0:e43878690c0e | 105 | #endif |
wim | 0:e43878690c0e | 106 | #if (BUTTONS8 == 1) |
sean3381212 | 3:5edd4d529cfd | 107 | //buttons = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button |
wim | 0:e43878690c0e | 108 | #endif |
wim | 0:e43878690c0e | 109 | #if (BUTTONS32 == 1) |
sean3381212 | 3:5edd4d529cfd | 110 | //tmp = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button |
sean3381212 | 3:5edd4d529cfd | 111 | //buttons = (( tmp << 0) & 0x000000FF); |
sean3381212 | 3:5edd4d529cfd | 112 | //buttons = buttons | ((~tmp << 8) & 0x0000FF00); |
sean3381212 | 3:5edd4d529cfd | 113 | //buttons = buttons | (( tmp << 16) & 0x00FF0000); |
sean3381212 | 3:5edd4d529cfd | 114 | //buttons = buttons | ((~tmp << 24) & 0xFF000000); |
wim | 0:e43878690c0e | 115 | #endif |
wim | 0:e43878690c0e | 116 | |
wim | 0:e43878690c0e | 117 | #if (HAT4 == 1) |
sean3381212 | 3:5edd4d529cfd | 118 | //hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral |
wim | 0:e43878690c0e | 119 | #endif |
wim | 0:e43878690c0e | 120 | #if (HAT8 == 1) |
sean3381212 | 3:5edd4d529cfd | 121 | //hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral |
wim | 0:e43878690c0e | 122 | #endif |
sean3381212 | 3:5edd4d529cfd | 123 | //i++; |
wim | 0:e43878690c0e | 124 | |
sean3381212 | 2:712ceeae4f56 | 125 | //x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 |
sean3381212 | 2:712ceeae4f56 | 126 | //y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 |
sean3381212 | 2:712ceeae4f56 | 127 | //angle += 3; |
sean3381212 | 2:712ceeae4f56 | 128 | x = wheel.getPulses(); |
sean3381212 | 3:5edd4d529cfd | 129 | x = x/4; |
sean3381212 | 3:5edd4d529cfd | 130 | joystick.update(throttle, rudder, -x, y, buttons, hat); |
wim | 0:e43878690c0e | 131 | wait(0.001); |
wim | 0:e43878690c0e | 132 | } |
wim | 0:e43878690c0e | 133 | |
wim | 0:e43878690c0e | 134 | pc.printf("Bye World!\n\r"); |
wim | 0:e43878690c0e | 135 | } |