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@2:0053ee456979, 2015-03-21 (annotated)
- Committer:
- sknn
- Date:
- Sat Mar 21 15:54:47 2015 +0000
- Revision:
- 2:0053ee456979
- Parent:
- 1:3c6efa018b57
Fixed y-axis mouse cursor movement (it was opposite directions).; Enabled printf debugging.; Tiny refactoring.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sknn | 0:2a59676aa462 | 1 | #include "mbed.h" |
sknn | 0:2a59676aa462 | 2 | #include "USBMouseKeyboard.h" |
sknn | 0:2a59676aa462 | 3 | #include "FXOS8700Q.h" |
sknn | 0:2a59676aa462 | 4 | |
sknn | 2:0053ee456979 | 5 | USBMouseKeyboard key_mouse; // Use USBMouseKeyboard which emulates USB mouse & keyboard |
sknn | 2:0053ee456979 | 6 | FXOS8700Q_acc acc( PTB3, PTB2, FXOS8700CQ_SLAVE_ADDR2 ); // Use accelerometer |
sknn | 0:2a59676aa462 | 7 | MotionSensorDataUnits acc_data; |
sknn | 0:2a59676aa462 | 8 | DigitalIn sw2(SW2); |
sknn | 0:2a59676aa462 | 9 | DigitalIn sw3(SW3); |
sknn | 0:2a59676aa462 | 10 | |
sknn | 2:0053ee456979 | 11 | int calcMouseMovement(float f) { |
sknn | 2:0053ee456979 | 12 | if( f > 0.3f ) { |
sknn | 2:0053ee456979 | 13 | // Board is listing to the right or to the front |
sknn | 2:0053ee456979 | 14 | return 10; |
sknn | 2:0053ee456979 | 15 | } else if( f < -0.3f ) { |
sknn | 2:0053ee456979 | 16 | // Board is listing to the left or to the back |
sknn | 2:0053ee456979 | 17 | return -10; |
sknn | 2:0053ee456979 | 18 | } else { |
sknn | 2:0053ee456979 | 19 | return 0; |
sknn | 2:0053ee456979 | 20 | } |
sknn | 2:0053ee456979 | 21 | } |
sknn | 2:0053ee456979 | 22 | |
sknn | 0:2a59676aa462 | 23 | int main(void) { |
sknn | 0:2a59676aa462 | 24 | |
sknn | 0:2a59676aa462 | 25 | acc.enable(); |
sknn | 0:2a59676aa462 | 26 | |
sknn | 0:2a59676aa462 | 27 | while (1) { |
sknn | 0:2a59676aa462 | 28 | acc.getAxis(acc_data); |
sknn | 1:3c6efa018b57 | 29 | float x = acc_data.x; // Substitute x for x-axis value of accelerometer |
sknn | 1:3c6efa018b57 | 30 | float y = acc_data.y; // Substitute y for y-axis value of accelerometer |
sknn | 0:2a59676aa462 | 31 | |
sknn | 2:0053ee456979 | 32 | key_mouse.move(calcMouseMovement(x), 0); // Move the mouse cursor in x-axis direction |
sknn | 2:0053ee456979 | 33 | key_mouse.move(0, calcMouseMovement(-y)); // Move the mouse cursor in y-axis direction |
sknn | 2:0053ee456979 | 34 | printf("%f => key_mouse.move(%d, 0)\r\n", x, calcMouseMovement(x)); |
sknn | 2:0053ee456979 | 35 | printf("%f => key_mouse.move(0, %d)\r\n", y, calcMouseMovement(y)); |
sknn | 0:2a59676aa462 | 36 | |
sknn | 0:2a59676aa462 | 37 | if( sw2 == 1 ) { |
sknn | 1:3c6efa018b57 | 38 | // SW2 is not pressed |
sknn | 1:3c6efa018b57 | 39 | key_mouse.release(MOUSE_LEFT); // Release the left button |
sknn | 0:2a59676aa462 | 40 | } else { |
sknn | 1:3c6efa018b57 | 41 | // SW2 is pressed |
sknn | 1:3c6efa018b57 | 42 | key_mouse.press(MOUSE_LEFT); // Press the left button |
sknn | 0:2a59676aa462 | 43 | } |
sknn | 0:2a59676aa462 | 44 | |
sknn | 0:2a59676aa462 | 45 | if( sw3 == 1 ) { |
sknn | 1:3c6efa018b57 | 46 | // SW3 is not pressed |
sknn | 1:3c6efa018b57 | 47 | key_mouse.release(MOUSE_RIGHT); // Release the right button |
sknn | 0:2a59676aa462 | 48 | } else { |
sknn | 1:3c6efa018b57 | 49 | // SW3 is pressed |
sknn | 1:3c6efa018b57 | 50 | key_mouse.press(MOUSE_RIGHT); // Press the right button |
sknn | 0:2a59676aa462 | 51 | } |
sknn | 0:2a59676aa462 | 52 | wait(0.1); |
sknn | 0:2a59676aa462 | 53 | } |
sknn | 0:2a59676aa462 | 54 | } |