ec

Dependencies:   USBDevice mbed

Fork of USBMouse_NavSwitch by jim hamblen

Committer:
Josahty
Date:
Mon Jan 22 18:18:48 2018 +0000
Revision:
6:5032c1e1b3ce
Parent:
5:679a07e00e4c
with scrolling ec;

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;
Josahty 6:5032c1e1b3ce 6 PwmOut myled(LED1);
Josahty 6:5032c1e1b3ce 7 AnalogIn mypotentiometer(p18);
Josahty 6:5032c1e1b3ce 8 Serial pc(USBTX, USBRX);
samux 2:e7b766501add 9
4180_1 5:679a07e00e4c 10 class Nav_Switch
4180_1 5:679a07e00e4c 11 {
4180_1 5:679a07e00e4c 12 public:
4180_1 5:679a07e00e4c 13 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
4180_1 5:679a07e00e4c 14 int read();
4180_1 5:679a07e00e4c 15 //boolean functions to test each switch
4180_1 5:679a07e00e4c 16 bool up();
4180_1 5:679a07e00e4c 17 bool down();
4180_1 5:679a07e00e4c 18 bool left();
4180_1 5:679a07e00e4c 19 bool right();
4180_1 5:679a07e00e4c 20 bool fire();
4180_1 5:679a07e00e4c 21 //automatic read on RHS
4180_1 5:679a07e00e4c 22 operator int ();
4180_1 5:679a07e00e4c 23 //index to any switch array style
4180_1 5:679a07e00e4c 24 bool operator[](int index) {
4180_1 5:679a07e00e4c 25 return _pins[index];
4180_1 5:679a07e00e4c 26 };
4180_1 5:679a07e00e4c 27 private:
4180_1 5:679a07e00e4c 28 BusIn _pins;
4180_1 5:679a07e00e4c 29
4180_1 5:679a07e00e4c 30 };
4180_1 5:679a07e00e4c 31 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
4180_1 5:679a07e00e4c 32 _pins(up, down, left, right, fire)
4180_1 5:679a07e00e4c 33 {
4180_1 5:679a07e00e4c 34 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
4180_1 5:679a07e00e4c 35 wait(0.001); //delays just a bit for pullups to pull inputs high
4180_1 5:679a07e00e4c 36 }
4180_1 5:679a07e00e4c 37 inline bool Nav_Switch::up()
4180_1 5:679a07e00e4c 38 {
4180_1 5:679a07e00e4c 39 return !(_pins[0]);
4180_1 5:679a07e00e4c 40 }
4180_1 5:679a07e00e4c 41 inline bool Nav_Switch::down()
4180_1 5:679a07e00e4c 42 {
4180_1 5:679a07e00e4c 43 return !(_pins[1]);
4180_1 5:679a07e00e4c 44 }
4180_1 5:679a07e00e4c 45 inline bool Nav_Switch::left()
4180_1 5:679a07e00e4c 46 {
4180_1 5:679a07e00e4c 47 return !(_pins[2]);
4180_1 5:679a07e00e4c 48 }
4180_1 5:679a07e00e4c 49 inline bool Nav_Switch::right()
4180_1 5:679a07e00e4c 50 {
4180_1 5:679a07e00e4c 51 return !(_pins[3]);
4180_1 5:679a07e00e4c 52 }
4180_1 5:679a07e00e4c 53 inline bool Nav_Switch::fire()
4180_1 5:679a07e00e4c 54 {
4180_1 5:679a07e00e4c 55 return !(_pins[4]);
4180_1 5:679a07e00e4c 56 }
4180_1 5:679a07e00e4c 57 inline int Nav_Switch::read()
4180_1 5:679a07e00e4c 58 {
4180_1 5:679a07e00e4c 59 return _pins.read();
4180_1 5:679a07e00e4c 60 }
4180_1 5:679a07e00e4c 61 inline Nav_Switch::operator int ()
4180_1 5:679a07e00e4c 62 {
4180_1 5:679a07e00e4c 63 return _pins.read();
4180_1 5:679a07e00e4c 64 }
4180_1 5:679a07e00e4c 65
4180_1 5:679a07e00e4c 66 Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun Nav SW breakout
4180_1 5:679a07e00e4c 67
4180_1 5:679a07e00e4c 68 int main()
4180_1 5:679a07e00e4c 69 {
Josahty 6:5032c1e1b3ce 70 float pot_data = 0;
Josahty 6:5032c1e1b3ce 71 float scroll_amount = 0;
samux 2:e7b766501add 72 int16_t x = 0;
samux 2:e7b766501add 73 int16_t y = 0;
4180_1 5:679a07e00e4c 74 uint8_t left_click = 0;
samux 2:e7b766501add 75 while (1) {
4180_1 5:679a07e00e4c 76 //check relative mouse movement
4180_1 5:679a07e00e4c 77 x=0;
4180_1 5:679a07e00e4c 78 y=0;
Josahty 6:5032c1e1b3ce 79 scroll_amount = 0;
4180_1 5:679a07e00e4c 80 if (myNav.up()) x=-1;
4180_1 5:679a07e00e4c 81 if (myNav.down()) x=1;
4180_1 5:679a07e00e4c 82 if (myNav.left()) y=1;
4180_1 5:679a07e00e4c 83 if (myNav.right()) y=-1;
4180_1 5:679a07e00e4c 84 //check mouse left button click
4180_1 5:679a07e00e4c 85 if (myNav.fire()) left_click = 1;
4180_1 5:679a07e00e4c 86 if (!myNav.fire())left_click = 0;
4180_1 5:679a07e00e4c 87 //send a mouse data packet to PC
Josahty 6:5032c1e1b3ce 88 myled = mypotentiometer;
Josahty 6:5032c1e1b3ce 89 pot_data = mypotentiometer;
Josahty 6:5032c1e1b3ce 90
Josahty 6:5032c1e1b3ce 91 if (pot_data < 0.25) {
Josahty 6:5032c1e1b3ce 92 scroll_amount = -1; }
Josahty 6:5032c1e1b3ce 93 else if (pot_data > 0.75) {
Josahty 6:5032c1e1b3ce 94 scroll_amount = 1; }
Josahty 6:5032c1e1b3ce 95
Josahty 6:5032c1e1b3ce 96 pc.printf("%f \n\r",scroll_amount);
Josahty 6:5032c1e1b3ce 97 mouse.update(x, y, left_click, scroll_amount);
samux 2:e7b766501add 98 wait(0.001);
samux 2:e7b766501add 99 }
samux 0:48fd0c31cef5 100 }