Shun SKNN / Mbed 2 deprecated frdm_mouse_K22F

Dependencies:   FXOS8700Q USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBMouseKeyboard.h"
00003 #include "FXOS8700Q.h"
00004 
00005 USBMouseKeyboard key_mouse;                                 // Use USBMouseKeyboard which emulates USB mouse & keyboard
00006 FXOS8700Q_acc acc( PTB3, PTB2, FXOS8700CQ_SLAVE_ADDR2 );    // Use accelerometer
00007 MotionSensorDataUnits acc_data;
00008 DigitalIn sw2(SW2);
00009 DigitalIn sw3(SW3);
00010 
00011 int calcMouseMovement(float f) {
00012     if( f > 0.3f ) {
00013         // Board is listing to the right or to the front
00014         return 10;
00015     } else if( f < -0.3f ) {
00016         // Board is listing to the left or to the back
00017         return -10;
00018     } else {
00019         return 0;
00020     }
00021 }
00022 
00023 int main(void) {
00024     
00025     acc.enable();
00026     
00027     while (1) {
00028         acc.getAxis(acc_data);
00029         float x = acc_data.x;               // Substitute x for x-axis value of accelerometer
00030         float y = acc_data.y;               // Substitute y for y-axis value of accelerometer
00031         
00032         key_mouse.move(calcMouseMovement(x), 0);    // Move the mouse cursor in x-axis direction
00033         key_mouse.move(0, calcMouseMovement(-y));   // Move the mouse cursor in y-axis direction
00034         printf("%f => key_mouse.move(%d, 0)\r\n", x, calcMouseMovement(x));
00035         printf("%f => key_mouse.move(0, %d)\r\n", y, calcMouseMovement(y));
00036         
00037         if( sw2 == 1 ) {
00038             // SW2 is not pressed
00039             key_mouse.release(MOUSE_LEFT);  // Release the left button
00040         } else {
00041             // SW2 is pressed
00042             key_mouse.press(MOUSE_LEFT);    // Press the left button
00043         }
00044         
00045         if( sw3 == 1 ) {
00046             // SW3 is not pressed
00047             key_mouse.release(MOUSE_RIGHT); // Release the right button
00048         } else {
00049             // SW3 is pressed
00050             key_mouse.press(MOUSE_RIGHT);   // Press the right button
00051         }
00052         wait(0.1);
00053     }
00054 }