ECE 4180 Lab 1 Part 5

Dependencies:   USBDevice mbed

Fork of USBMouse_NavSwitch by jim hamblen

Committer:
4180_1
Date:
Mon Jun 26 17:27:42 2017 +0000
Revision:
5:679a07e00e4c
Parent:
3:b8caa902d79e
ver 1.0;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 2:e7b766501add 1 #include "mbed.h"
samux 2:e7b766501add 2 #include "USBMouse.h"
4180_1 5:679a07e00e4c 3 //USB mouse demo using a 5-way Navigation Switch (Digital Joystick)
4180_1 5:679a07e00e4c 4 //Needs USB connector breakout with D+, D-, and Gnd to mbed LLP1768
samux 2:e7b766501add 5 USBMouse mouse;
samux 2:e7b766501add 6
4180_1 5:679a07e00e4c 7 class Nav_Switch
4180_1 5:679a07e00e4c 8 {
4180_1 5:679a07e00e4c 9 public:
4180_1 5:679a07e00e4c 10 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
4180_1 5:679a07e00e4c 11 int read();
4180_1 5:679a07e00e4c 12 //boolean functions to test each switch
4180_1 5:679a07e00e4c 13 bool up();
4180_1 5:679a07e00e4c 14 bool down();
4180_1 5:679a07e00e4c 15 bool left();
4180_1 5:679a07e00e4c 16 bool right();
4180_1 5:679a07e00e4c 17 bool fire();
4180_1 5:679a07e00e4c 18 //automatic read on RHS
4180_1 5:679a07e00e4c 19 operator int ();
4180_1 5:679a07e00e4c 20 //index to any switch array style
4180_1 5:679a07e00e4c 21 bool operator[](int index) {
4180_1 5:679a07e00e4c 22 return _pins[index];
4180_1 5:679a07e00e4c 23 };
4180_1 5:679a07e00e4c 24 private:
4180_1 5:679a07e00e4c 25 BusIn _pins;
4180_1 5:679a07e00e4c 26
4180_1 5:679a07e00e4c 27 };
4180_1 5:679a07e00e4c 28 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
4180_1 5:679a07e00e4c 29 _pins(up, down, left, right, fire)
4180_1 5:679a07e00e4c 30 {
4180_1 5:679a07e00e4c 31 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
4180_1 5:679a07e00e4c 32 wait(0.001); //delays just a bit for pullups to pull inputs high
4180_1 5:679a07e00e4c 33 }
4180_1 5:679a07e00e4c 34 inline bool Nav_Switch::up()
4180_1 5:679a07e00e4c 35 {
4180_1 5:679a07e00e4c 36 return !(_pins[0]);
4180_1 5:679a07e00e4c 37 }
4180_1 5:679a07e00e4c 38 inline bool Nav_Switch::down()
4180_1 5:679a07e00e4c 39 {
4180_1 5:679a07e00e4c 40 return !(_pins[1]);
4180_1 5:679a07e00e4c 41 }
4180_1 5:679a07e00e4c 42 inline bool Nav_Switch::left()
4180_1 5:679a07e00e4c 43 {
4180_1 5:679a07e00e4c 44 return !(_pins[2]);
4180_1 5:679a07e00e4c 45 }
4180_1 5:679a07e00e4c 46 inline bool Nav_Switch::right()
4180_1 5:679a07e00e4c 47 {
4180_1 5:679a07e00e4c 48 return !(_pins[3]);
4180_1 5:679a07e00e4c 49 }
4180_1 5:679a07e00e4c 50 inline bool Nav_Switch::fire()
4180_1 5:679a07e00e4c 51 {
4180_1 5:679a07e00e4c 52 return !(_pins[4]);
4180_1 5:679a07e00e4c 53 }
4180_1 5:679a07e00e4c 54 inline int Nav_Switch::read()
4180_1 5:679a07e00e4c 55 {
4180_1 5:679a07e00e4c 56 return _pins.read();
4180_1 5:679a07e00e4c 57 }
4180_1 5:679a07e00e4c 58 inline Nav_Switch::operator int ()
4180_1 5:679a07e00e4c 59 {
4180_1 5:679a07e00e4c 60 return _pins.read();
4180_1 5:679a07e00e4c 61 }
4180_1 5:679a07e00e4c 62
4180_1 5:679a07e00e4c 63 Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun Nav SW breakout
4180_1 5:679a07e00e4c 64
4180_1 5:679a07e00e4c 65 int main()
4180_1 5:679a07e00e4c 66 {
samux 2:e7b766501add 67 int16_t x = 0;
samux 2:e7b766501add 68 int16_t y = 0;
4180_1 5:679a07e00e4c 69 uint8_t left_click = 0;
samux 2:e7b766501add 70 while (1) {
4180_1 5:679a07e00e4c 71 //check relative mouse movement
4180_1 5:679a07e00e4c 72 x=0;
4180_1 5:679a07e00e4c 73 y=0;
4180_1 5:679a07e00e4c 74 if (myNav.up()) x=-1;
4180_1 5:679a07e00e4c 75 if (myNav.down()) x=1;
4180_1 5:679a07e00e4c 76 if (myNav.left()) y=1;
4180_1 5:679a07e00e4c 77 if (myNav.right()) y=-1;
4180_1 5:679a07e00e4c 78 //check mouse left button click
4180_1 5:679a07e00e4c 79 if (myNav.fire()) left_click = 1;
4180_1 5:679a07e00e4c 80 if (!myNav.fire())left_click = 0;
4180_1 5:679a07e00e4c 81 //send a mouse data packet to PC
4180_1 5:679a07e00e4c 82 mouse.update(x, y, left_click, 0);
samux 2:e7b766501add 83 wait(0.001);
samux 2:e7b766501add 84 }
samux 0:48fd0c31cef5 85 }