ec
Fork of USBMouse_NavSwitch by
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
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 PwmOut myled(LED1); 00007 AnalogIn mypotentiometer(p18); 00008 Serial pc(USBTX, USBRX); 00009 00010 class Nav_Switch 00011 { 00012 public: 00013 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); 00014 int read(); 00015 //boolean functions to test each switch 00016 bool up(); 00017 bool down(); 00018 bool left(); 00019 bool right(); 00020 bool fire(); 00021 //automatic read on RHS 00022 operator int (); 00023 //index to any switch array style 00024 bool operator[](int index) { 00025 return _pins[index]; 00026 }; 00027 private: 00028 BusIn _pins; 00029 00030 }; 00031 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): 00032 _pins(up, down, left, right, fire) 00033 { 00034 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise 00035 wait(0.001); //delays just a bit for pullups to pull inputs high 00036 } 00037 inline bool Nav_Switch::up() 00038 { 00039 return !(_pins[0]); 00040 } 00041 inline bool Nav_Switch::down() 00042 { 00043 return !(_pins[1]); 00044 } 00045 inline bool Nav_Switch::left() 00046 { 00047 return !(_pins[2]); 00048 } 00049 inline bool Nav_Switch::right() 00050 { 00051 return !(_pins[3]); 00052 } 00053 inline bool Nav_Switch::fire() 00054 { 00055 return !(_pins[4]); 00056 } 00057 inline int Nav_Switch::read() 00058 { 00059 return _pins.read(); 00060 } 00061 inline Nav_Switch::operator int () 00062 { 00063 return _pins.read(); 00064 } 00065 00066 Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun Nav SW breakout 00067 00068 int main() 00069 { 00070 float pot_data = 0; 00071 float scroll_amount = 0; 00072 int16_t x = 0; 00073 int16_t y = 0; 00074 uint8_t left_click = 0; 00075 while (1) { 00076 //check relative mouse movement 00077 x=0; 00078 y=0; 00079 scroll_amount = 0; 00080 if (myNav.up()) x=-1; 00081 if (myNav.down()) x=1; 00082 if (myNav.left()) y=1; 00083 if (myNav.right()) y=-1; 00084 //check mouse left button click 00085 if (myNav.fire()) left_click = 1; 00086 if (!myNav.fire())left_click = 0; 00087 //send a mouse data packet to PC 00088 myled = mypotentiometer; 00089 pot_data = mypotentiometer; 00090 00091 if (pot_data < 0.25) { 00092 scroll_amount = -1; } 00093 else if (pot_data > 0.75) { 00094 scroll_amount = 1; } 00095 00096 pc.printf("%f \n\r",scroll_amount); 00097 mouse.update(x, y, left_click, scroll_amount); 00098 wait(0.001); 00099 } 00100 }
Generated on Sun Aug 7 2022 03:40:06 by
1.7.2
