4180 yoloswag / Mbed 2 deprecated Lab1Part5

Dependencies:   mbed USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 
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 int main()
00066 {
00067     int16_t x = 0;
00068     int16_t y = 0;
00069     uint8_t left_click = 0;
00070     while (1) {
00071         //check relative mouse movement
00072         x=0;
00073         y=0;
00074         if (myNav.up()) x=-1;
00075         if (myNav.down()) x=1;
00076         if (myNav.left()) y=1;
00077         if (myNav.right()) y=-1;
00078         //check mouse left button click
00079         if (myNav.fire()) left_click = 1;
00080         if (!myNav.fire())left_click = 0;
00081         //send a mouse data packet to PC
00082         mouse.update(x, y, left_click, 0);
00083         wait(0.001);
00084     }
00085 }