Implements a basic USB mouse function using a 5-way Navigation Switch (Digital Joystick) on mbed LPC1768. A USB connector breakout board is needed.
Fork of USBMouse_HelloWorld by
Revision 5:679a07e00e4c, committed 2017-06-26
- Comitter:
- 4180_1
- Date:
- Mon Jun 26 17:27:42 2017 +0000
- Parent:
- 4:26ecbbc27530
- Commit message:
- ver 1.0;
Changed in this revision
diff -r 26ecbbc27530 -r 679a07e00e4c USBDevice.lib --- a/USBDevice.lib Fri Mar 01 13:26:13 2013 +0000 +++ b/USBDevice.lib Mon Jun 26 17:27:42 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/USBDevice/#335f2506f422 +http://mbed.org/users/mbed_official/code/USBDevice/#c5e178adb138
diff -r 26ecbbc27530 -r 679a07e00e4c main.cpp --- a/main.cpp Fri Mar 01 13:26:13 2013 +0000 +++ b/main.cpp Mon Jun 26 17:27:42 2017 +0000 @@ -1,20 +1,85 @@ #include "mbed.h" #include "USBMouse.h" - +//USB mouse demo using a 5-way Navigation Switch (Digital Joystick) +//Needs USB connector breakout with D+, D-, and Gnd to mbed LLP1768 USBMouse mouse; -int main() { +class Nav_Switch +{ +public: + Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); + int read(); +//boolean functions to test each switch + bool up(); + bool down(); + bool left(); + bool right(); + bool fire(); +//automatic read on RHS + operator int (); +//index to any switch array style + bool operator[](int index) { + return _pins[index]; + }; +private: + BusIn _pins; + +}; +Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): + _pins(up, down, left, right, fire) +{ + _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise + wait(0.001); //delays just a bit for pullups to pull inputs high +} +inline bool Nav_Switch::up() +{ + return !(_pins[0]); +} +inline bool Nav_Switch::down() +{ + return !(_pins[1]); +} +inline bool Nav_Switch::left() +{ + return !(_pins[2]); +} +inline bool Nav_Switch::right() +{ + return !(_pins[3]); +} +inline bool Nav_Switch::fire() +{ + return !(_pins[4]); +} +inline int Nav_Switch::read() +{ + return _pins.read(); +} +inline Nav_Switch::operator int () +{ + return _pins.read(); +} + +Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun Nav SW breakout + +int main() +{ int16_t x = 0; int16_t y = 0; - int32_t radius = 10; - int32_t angle = 0; - + uint8_t left_click = 0; while (1) { - x = cos((double)angle*3.14/180.0)*radius; - y = sin((double)angle*3.14/180.0)*radius; - - mouse.move(x, y); - angle += 3; + //check relative mouse movement + x=0; + y=0; + if (myNav.up()) x=-1; + if (myNav.down()) x=1; + if (myNav.left()) y=1; + if (myNav.right()) y=-1; + //check mouse left button click + if (myNav.fire()) left_click = 1; + if (!myNav.fire())left_click = 0; + //send a mouse data packet to PC + mouse.update(x, y, left_click, 0); wait(0.001); } } \ No newline at end of file
diff -r 26ecbbc27530 -r 679a07e00e4c mbed.bld --- a/mbed.bld Fri Mar 01 13:26:13 2013 +0000 +++ b/mbed.bld Mon Jun 26 17:27:42 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/3d0ef94e36ec \ No newline at end of file +https://mbed.org/users/mbed_official/code/mbed/builds/64910690c574 \ No newline at end of file