Ported a code that FRDM-KL46Z act as a USB mouse originally developed by jksoft, to FRDM-K22F. SW2 and SW3 act as left and right buttons respectively because K22F lacks a touch sensor, in contrast to KL46Z.

Dependencies:   FXOS8700Q USBDevice mbed

jksoftさんのUSBマウス(FRDM-KL46Z用)をFRDM-K22Fにポーティング。 KL46Zとは異なり、K22Fにはタッチセンサがありません。このため、SW2を左ボタン、SW3を右ボタンとしています。

Ported USB mouse for FRDM-KL46Z originally developed by jksoft to FRDM-K22F. SW2 and SW3 act as left and right buttons respectively because K22F lacks a touch sensor, in contrast to KL46Z.

main.cpp

Committer:
sknn
Date:
2015-03-21
Revision:
2:0053ee456979
Parent:
1:3c6efa018b57

File content as of revision 2:0053ee456979:

#include "mbed.h"
#include "USBMouseKeyboard.h"
#include "FXOS8700Q.h"

USBMouseKeyboard key_mouse;                                 // Use USBMouseKeyboard which emulates USB mouse & keyboard
FXOS8700Q_acc acc( PTB3, PTB2, FXOS8700CQ_SLAVE_ADDR2 );    // Use accelerometer
MotionSensorDataUnits acc_data;
DigitalIn sw2(SW2);
DigitalIn sw3(SW3);

int calcMouseMovement(float f) {
    if( f > 0.3f ) {
        // Board is listing to the right or to the front
        return 10;
    } else if( f < -0.3f ) {
        // Board is listing to the left or to the back
        return -10;
    } else {
        return 0;
    }
}

int main(void) {
    
    acc.enable();
    
    while (1) {
        acc.getAxis(acc_data);
        float x = acc_data.x;               // Substitute x for x-axis value of accelerometer
        float y = acc_data.y;               // Substitute y for y-axis value of accelerometer
        
        key_mouse.move(calcMouseMovement(x), 0);    // Move the mouse cursor in x-axis direction
        key_mouse.move(0, calcMouseMovement(-y));   // Move the mouse cursor in y-axis direction
        printf("%f => key_mouse.move(%d, 0)\r\n", x, calcMouseMovement(x));
        printf("%f => key_mouse.move(0, %d)\r\n", y, calcMouseMovement(y));
        
        if( sw2 == 1 ) {
            // SW2 is not pressed
            key_mouse.release(MOUSE_LEFT);  // Release the left button
        } else {
            // SW2 is pressed
            key_mouse.press(MOUSE_LEFT);    // Press the left button
        }
        
        if( sw3 == 1 ) {
            // SW3 is not pressed
            key_mouse.release(MOUSE_RIGHT); // Release the right button
        } else {
            // SW3 is pressed
            key_mouse.press(MOUSE_RIGHT);   // Press the right button
        }
        wait(0.1);
    }
}