Wim Huiskamp / Mbed 2 deprecated USBJoystick_HelloWorld2

Dependencies:   USBDevice USBJoystick mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed USBJoystick Library Demo
00002  * Copyright (c) 2012, v01:  Initial version, WH,
00003  *                           Modified USBMouse code ARM Limited.
00004  *                           (c) 2010-2011 mbed.org, MIT License
00005  *               2016, v02:  Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button 
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, inclumosig without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 #include "mbed.h"
00027 #include "USBJoystick.h"
00028 
00029 //#define LANDTIGER 1
00030 
00031 //USBMouse mouse;
00032 USBJoystick joystick;
00033 
00034 // Variables for Heartbeat and Status monitoring
00035 DigitalOut myled1(LED1);
00036 DigitalOut myled2(LED2);
00037 DigitalOut myled3(LED3);
00038 DigitalOut heartbeatLED(LED4);
00039 
00040 Ticker heartbeat;
00041 Serial pc(USBTX, USBRX); // tx, rx
00042 
00043 // Heartbeat monitor
00044 void pulse() {
00045   heartbeatLED = !heartbeatLED;
00046 }
00047 
00048 void heartbeat_start() {
00049   heartbeatLED=1;
00050   heartbeat.attach(&pulse, 0.5);
00051 }
00052 
00053 void heartbeat_stop() {
00054   heartbeat.detach();
00055 }
00056 
00057 
00058 int main() {
00059   uint16_t i = 0;
00060   int16_t throttle = 0;
00061   int16_t rudder = 0;    
00062   int16_t x = 0;
00063   int16_t y = 0;
00064   int32_t radius = 120;
00065   int32_t angle = 0;
00066   uint8_t tmp = 0;
00067   uint32_t buttons = 0;    
00068   uint8_t hat = 0;    
00069     
00070   pc.printf("Hello World from Joystick!\n\r");
00071 
00072   heartbeat_start();
00073 
00074   while (1) {
00075     // Basic Joystick
00076     throttle = (i >> 8) & 0xFF; // value -127 .. 128
00077     rudder = (i >> 8) & 0xFF;   // value -127 .. 128        
00078 
00079 #if (BUTTONS4 == 1)        
00080     buttons = (i >> 8) & 0x0F;   // value    0 ..  15, one bit per button     
00081 #endif        
00082 #if (BUTTONS8 == 1)        
00083     buttons = (i >> 8) & 0xFF;   // value    0 .. 255, one bit per button     
00084 #endif        
00085 #if (BUTTONS32 == 1)        
00086     tmp     = (i >> 8) & 0xFF;   // value    0 .. 255, one bit per button     
00087     buttons =           (( tmp <<  0) & 0x000000FF);
00088     buttons = buttons | ((~tmp <<  8) & 0x0000FF00);
00089     buttons = buttons | (( tmp << 16) & 0x00FF0000);
00090     buttons = buttons | ((~tmp << 24) & 0xFF000000);
00091 #endif        
00092 
00093 #if (HAT4 == 1)        
00094     hat    = (i >> 8) & 0x03;   // value 0, 1, 2, 3 or 4 for neutral 
00095 #endif
00096 #if (HAT8 == 1)
00097     hat    = (i >> 8) & 0x07;   // value 0..7 or 8 for neutral
00098 #endif        
00099     i++;        
00100         
00101     x = cos((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00102     y = sin((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00103     angle += 3;        
00104 
00105     joystick.update(throttle, rudder, x, y, buttons, hat);
00106     wait(0.001);
00107   }
00108     
00109   pc.printf("Bye World!\n\r");                           
00110 }