Example of usb mouse on smt32 platform using a analog 2-axis joystick

Dependencies:   USBDEVICE mbed

Fork of Nucleo_usbmouse by ST

Committer:
julgonmej
Date:
Tue May 09 16:04:16 2017 +0000
Revision:
8:427f11739ba4
Parent:
7:bb9d609a8493
Small changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamike 0:6410755cb90c 1 #include "mbed.h"
julgonmej 8:427f11739ba4 2 #include "USBMouse.h"
jamike 0:6410755cb90c 3 #include <math.h>
jamike 0:6410755cb90c 4
jamike 0:6410755cb90c 5 USBMouse mouse(ABS_MOUSE);
julgonmej 5:b3c465e3e54e 6 DigitalOut myled(LED1);
jamike 0:6410755cb90c 7
julgonmej 7:bb9d609a8493 8 AnalogIn ex(PC_2);
julgonmej 7:bb9d609a8493 9 AnalogIn ey(PC_3);
julgonmej 7:bb9d609a8493 10 DigitalIn click(PB_7, PullUp);
julgonmej 6:989396ea046f 11
jamike 0:6410755cb90c 12 int main(void) {
julgonmej 7:bb9d609a8493 13 unsigned int x_center = (X_MAX_ABS - X_MIN_ABS)/2;
julgonmej 7:bb9d609a8493 14 unsigned int y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
julgonmej 7:bb9d609a8493 15 uint16_t x_screen = x_center;
julgonmej 7:bb9d609a8493 16 uint16_t y_screen = y_center;
julgonmej 7:bb9d609a8493 17 uint16_t px;
julgonmej 7:bb9d609a8493 18 uint16_t py;
julgonmej 7:bb9d609a8493 19 bool sw = 0;
julgonmej 7:bb9d609a8493 20 bool swOld = 0;
julgonmej 5:b3c465e3e54e 21 myled = 0;
jamike 0:6410755cb90c 22
jamike 0:6410755cb90c 23 while (1) {
julgonmej 5:b3c465e3e54e 24 myled = !myled;
julgonmej 7:bb9d609a8493 25 px = ex.read_u16();
julgonmej 7:bb9d609a8493 26 py = ey.read_u16();
julgonmej 7:bb9d609a8493 27 sw = !click.read();
julgonmej 7:bb9d609a8493 28
julgonmej 7:bb9d609a8493 29 if(px > 40000 || px < 25000) {
julgonmej 7:bb9d609a8493 30 x_screen += (px - 32768)/250;
julgonmej 7:bb9d609a8493 31 }
julgonmej 7:bb9d609a8493 32 if(py > 40000 || py < 25000) {
julgonmej 7:bb9d609a8493 33 y_screen += (py - 32768)/250;
julgonmej 7:bb9d609a8493 34 }
jamike 0:6410755cb90c 35 mouse.move(x_screen, y_screen);
julgonmej 7:bb9d609a8493 36
julgonmej 7:bb9d609a8493 37 if(!swOld && sw) {
julgonmej 7:bb9d609a8493 38 mouse.update(x_screen, y_screen,1,0);
julgonmej 7:bb9d609a8493 39 swOld = 1;
julgonmej 7:bb9d609a8493 40 } else if (swOld && !sw){
julgonmej 7:bb9d609a8493 41 swOld = 0;
julgonmej 7:bb9d609a8493 42 mouse.update(x_screen, y_screen,0,0);
julgonmej 7:bb9d609a8493 43 }
julgonmej 7:bb9d609a8493 44
julgonmej 7:bb9d609a8493 45 wait(0.007);
jamike 0:6410755cb90c 46 }
jamike 0:6410755cb90c 47 }