Advertisement
Guest User

Untitled

a guest
Aug 13th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include "mbed.h"
  2. #include "USBMouseKeyboard.h"
  3. #include "MMA8451Q.h"
  4.  
  5. DigitalOut led(LED_RED);
  6.  
  7. MMA8451Q acc(PTE25, PTE24, (0x1d<<1));
  8.  
  9. USBMouseKeyboard keyboard(REL_MOUSE);
  10.  
  11. int main()
  12. {
  13.     float level_XYZ[3]{};
  14.     acc.getAccAllAxis(&level_XYZ[0]);
  15.    
  16.     float twist = 0.1;
  17.  
  18.     //still
  19.     float threshold_Z = 0.05;
  20.     //walk
  21.     float threshold_A = 0.07;
  22.     //walk
  23.     float threshold_B = 0.15;
  24.     //run
  25.     float threshold_C = 0.18;
  26.     //sprint
  27.  
  28.     float x=0;
  29.     float y=0;
  30.     float z=0;
  31.     uint8_t left = 0, right = 0, up = 0, down = 0, walk = 0, sprint = 0, crouch = 0;
  32.     while (true)
  33.     {
  34.         x = acc.getAccX() - level_XYZ[0];
  35.         y = acc.getAccY() - level_XYZ[1];
  36.         z = acc.getAccZ() - level_XYZ[2];
  37.        
  38.         //keyboard.printf("x: %f, y: %f, z: %f\n", x, y, z);
  39.                
  40.         /*
  41.         if(fabs(z) > twist)//twist
  42.         {
  43.             keyboard.printf("z: %f\n", z);
  44.             //keyboard.update(z*10, 0, 0, 0);
  45.         }*/
  46.  
  47.         if(x > threshold_Z)//left
  48.         {
  49.             if(right)right=0;
  50.             if(!left)left=LEFT_ARROW;
  51.         }else if(x < -threshold_Z)//right
  52.         {
  53.             if(!right)right=RIGHT_ARROW;
  54.             if(left)left=0;
  55.         }else
  56.         {
  57.             if(right)right=0;
  58.             if(left)left=0;
  59.         }
  60.        
  61.         if(y < -threshold_Z)//up
  62.         {
  63.             if(down)down=0;
  64.             if(!up)up=UP_ARROW;
  65.         }else if(y > threshold_Z)//down
  66.         {
  67.             if(!down)down=DOWN_ARROW;
  68.             if(up)up=0;
  69.         }else
  70.         {
  71.             if(up)up=0;
  72.             if(down)down=0;
  73.         }
  74.        
  75.         //thresholds
  76.         float mag = fabs(x) + fabs(y);
  77.        
  78.         /*
  79.         if(mag < threshold_A)
  80.         {
  81.             if(!crouch)crouch=KEY_SHIFT;
  82.         } else crouch=0;
  83.         **/
  84.  
  85.         if(mag < threshold_B)
  86.         {
  87.             if(!walk)walk='c';//+c
  88.             if(sprint)sprint=0;//-e
  89.         }
  90.        
  91.         if(mag > threshold_B && mag < threshold_C)
  92.         {
  93.             if(walk)walk=0;//-c
  94.             if(sprint)sprint=0;//-e
  95.         }
  96.        
  97.         if(mag > threshold_C)
  98.         {
  99.             if(walk)walk=0;//-c
  100.             if(!sprint)sprint='e';//+e
  101.         }
  102.        
  103.         keyboard.keyCode_ATH(left, right, up, down, walk, sprint, crouch);
  104.  
  105.         led = !led; // toggle led
  106.  
  107.         wait(0.05f);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement