test joystick demo

Dependencies:   mbed QEI USBDevice USBJoystick

Files at this revision

API Documentation at this revision

Comitter:
sean3381212
Date:
Wed Nov 13 16:15:44 2019 +0000
Parent:
2:712ceeae4f56
Commit message:
add "USER BUTTON"

Changed in this revision

USBJoystick.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 712ceeae4f56 -r 5edd4d529cfd USBJoystick.lib
--- a/USBJoystick.lib	Tue Nov 12 07:17:38 2019 +0000
+++ b/USBJoystick.lib	Wed Nov 13 16:15:44 2019 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/wim/code/USBJoystick/#e086541742c3
+https://os.mbed.com/users/sean3381212/code/USBJoystick/#b7f518bacb3d
diff -r 712ceeae4f56 -r 5edd4d529cfd main.cpp
--- a/main.cpp	Tue Nov 12 07:17:38 2019 +0000
+++ b/main.cpp	Wed Nov 13 16:15:44 2019 +0000
@@ -38,11 +38,14 @@
 DigitalOut myled3(LED3);
 DigitalOut heartbeatLED(LED4);
 
+InterruptIn button(USER_BUTTON);
+volatile bool send=true;
+
 Ticker heartbeat;
 Serial pc(USBTX, USBRX); // tx, rx
 
 //Use X4 encoding.
-QEI wheel(PA_6, PA_7, NC, 30, QEI::X4_ENCODING);
+QEI wheel(PA_6, PA_7, NC, 1000, QEI::X4_ENCODING);
 
 // Heartbeat monitor
 void pulse() {
@@ -58,6 +61,11 @@
   heartbeat.detach();
 }
 
+void handlerbutton()
+{
+    send=!send;
+}
+
 
 int main() {
   uint16_t i = 0;
@@ -70,44 +78,56 @@
   uint8_t tmp = 0;
   uint32_t buttons = 0;    
   uint8_t hat = 0;    
+  int16_t k = 0;
     
   pc.printf("Hello World from Joystick!\n\r");
 
   heartbeat_start();
+  button.rise(&handlerbutton);
 
   while (1) {
     // Basic Joystick
-    throttle = (i >> 8) & 0xFF; // value -127 .. 128
-    rudder = (i >> 8) & 0xFF;   // value -127 .. 128        
+    //throttle = (i >> 8) & 0xFF; // value -127 .. 128
+    //rudder = (i >> 8) & 0xFF;   // value -127 .. 128
+    if(send == true){
+        i = 0;
+        k = (i >> 8) & 0x0F;
+        pc.printf("OK TRUE!  %d \n\r",k);
+    }
+    else{
+        i = 256;
+        k = (i >> 8) & 0x0F;
+        pc.printf("OK FAUSE! %d \n\r",k);
+    }
 
 #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     
+    //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);
+    //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);
 #endif        
 
 #if (HAT4 == 1)        
-    hat    = (i >> 8) & 0x03;   // value 0, 1, 2, 3 or 4 for neutral 
+    //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
+    //hat    = (i >> 8) & 0x07;   // value 0..7 or 8 for neutral
 #endif        
-    i++;        
+    //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 = wheel.getPulses();
-
-    joystick.update(throttle, rudder, x, y, buttons, hat);
+    x = x/4;
+    joystick.update(throttle, rudder, -x, y, buttons, hat);
     wait(0.001);
   }