USBDevice Mouse example.

Dependencies:   USBDevice mbed

Committer:
bcostm
Date:
Thu Aug 03 08:38:47 2017 +0000
Revision:
1:fbc704b10edb
Parent:
0:ca0d5482bb4e
Add message for DISCO_L072CZ_LRWAN1 board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:ca0d5482bb4e 1 #include "mbed.h"
bcostm 0:ca0d5482bb4e 2 #include <math.h>
bcostm 0:ca0d5482bb4e 3 #include "USBMouse.h"
bcostm 0:ca0d5482bb4e 4
bcostm 1:fbc704b10edb 5 /* -----------------------------------------------------------------------------
bcostm 1:fbc704b10edb 6 This program continously moves the cursor in a circular way on the screen.
bcostm 1:fbc704b10edb 7 Press the button to stop/start the cursor movement.
bcostm 1:fbc704b10edb 8
bcostm 1:fbc704b10edb 9 DISCO_L072CZ_LRWAN1 board: SB15 and SB16 must be closed to enable the USB pins
bcostm 1:fbc704b10edb 10 ----------------------------------------------------------------------------- */
bcostm 1:fbc704b10edb 11
bcostm 0:ca0d5482bb4e 12
bcostm 0:ca0d5482bb4e 13 InterruptIn button(USER_BUTTON);
bcostm 0:ca0d5482bb4e 14
bcostm 0:ca0d5482bb4e 15 USBMouse mouse(ABS_MOUSE);
bcostm 0:ca0d5482bb4e 16
bcostm 0:ca0d5482bb4e 17 volatile bool send=true;
bcostm 0:ca0d5482bb4e 18
bcostm 0:ca0d5482bb4e 19 void handlerbutton()
bcostm 0:ca0d5482bb4e 20 {
bcostm 0:ca0d5482bb4e 21 send=!send;
bcostm 0:ca0d5482bb4e 22 }
bcostm 0:ca0d5482bb4e 23
bcostm 0:ca0d5482bb4e 24 int main(void)
bcostm 0:ca0d5482bb4e 25 {
bcostm 0:ca0d5482bb4e 26 int x_center = (X_MAX_ABS - X_MIN_ABS)/2;
bcostm 0:ca0d5482bb4e 27 int y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
bcostm 0:ca0d5482bb4e 28 int16_t x_screen = 0;
bcostm 0:ca0d5482bb4e 29 int16_t y_screen = 0;
bcostm 0:ca0d5482bb4e 30
bcostm 0:ca0d5482bb4e 31 int32_t x_origin = x_center;
bcostm 0:ca0d5482bb4e 32 int32_t y_origin = y_center;
bcostm 0:ca0d5482bb4e 33 int32_t radius = 5000;
bcostm 0:ca0d5482bb4e 34 int32_t angle = 0;
bcostm 0:ca0d5482bb4e 35
bcostm 0:ca0d5482bb4e 36 button.rise(&handlerbutton);
bcostm 0:ca0d5482bb4e 37
bcostm 0:ca0d5482bb4e 38 while (1) {
bcostm 0:ca0d5482bb4e 39 x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
bcostm 0:ca0d5482bb4e 40 y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
bcostm 0:ca0d5482bb4e 41
bcostm 0:ca0d5482bb4e 42 printf("cos: %f, sin: %f\r\n", cos((double)angle*3.14/180.0)*radius, sin((double)angle)*radius);
bcostm 0:ca0d5482bb4e 43
bcostm 0:ca0d5482bb4e 44 if (send) {
bcostm 0:ca0d5482bb4e 45 mouse.move(x_screen, y_screen);
bcostm 0:ca0d5482bb4e 46 }
bcostm 0:ca0d5482bb4e 47
bcostm 0:ca0d5482bb4e 48 angle += 3;
bcostm 0:ca0d5482bb4e 49 wait(0.01);
bcostm 0:ca0d5482bb4e 50 }
bcostm 0:ca0d5482bb4e 51 }