Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MCP23S17 PinDetect USBDevice
mouse_EC.h
00001 #include "mbed.h" 00002 #include "USBMouse.h" 00003 //USB mouse demo using a 5-way Navigation Switch (Digital Joystick) 00004 //Needs USB connector breakout with D+, D-, and Gnd to mbed LLP1768 00005 USBMouse mouse; 00006 00007 class Nav_Switch 00008 { 00009 public: 00010 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); 00011 int read(); 00012 //boolean functions to test each switch 00013 bool up(); 00014 bool down(); 00015 bool left(); 00016 bool right(); 00017 bool fire(); 00018 //automatic read on RHS 00019 operator int (); 00020 //index to any switch array style 00021 bool operator[](int index) { 00022 return _pins[index]; 00023 }; 00024 private: 00025 BusIn _pins; 00026 00027 }; 00028 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): 00029 _pins(up, down, left, right, fire) 00030 { 00031 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise 00032 wait(0.001); //delays just a bit for pullups to pull inputs high 00033 } 00034 inline bool Nav_Switch::up() 00035 { 00036 return !(_pins[0]); 00037 } 00038 inline bool Nav_Switch::down() 00039 { 00040 return !(_pins[1]); 00041 } 00042 inline bool Nav_Switch::left() 00043 { 00044 return !(_pins[2]); 00045 } 00046 inline bool Nav_Switch::right() 00047 { 00048 return !(_pins[3]); 00049 } 00050 inline bool Nav_Switch::fire() 00051 { 00052 return !(_pins[4]); 00053 } 00054 inline int Nav_Switch::read() 00055 { 00056 return _pins.read(); 00057 } 00058 inline Nav_Switch::operator int () 00059 { 00060 return _pins.read(); 00061 } 00062 00063 Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun Nav SW breakout 00064 00065 AnalogIn pot(p20); 00066 int potPrev = 0; 00067 00068 int run_mouseEC() 00069 { 00070 int16_t x = 0; 00071 int16_t y = 0; 00072 uint8_t left_click = 0; 00073 int relPot = 0; 00074 while (1) { 00075 //check relative mouse movement 00076 x=0; 00077 y=0; 00078 if (myNav.up()) x=-1; 00079 if (myNav.down()) x=1; 00080 if (myNav.left()) y=1; 00081 if (myNav.right()) y=-1; 00082 //check mouse left button click 00083 if (myNav.fire()) left_click = 1; 00084 if (!myNav.fire())left_click = 0; 00085 00086 if (potPrev < pot) { 00087 relPot = 1; 00088 } else if (potPrev > pot) { 00089 relPot = -1; 00090 } else { 00091 relPot = 0; 00092 } 00093 potPrev = pot; 00094 //send a mouse data packet to PC 00095 mouse.update(x, y, left_click, relPot); 00096 wait(0.001); 00097 } 00098 }
Generated on Sun Jul 17 2022 17:11:11 by
1.7.2