Dwayne Dilbeck
/
HW2_3_USB_POT_Mouse
Homework #2 Problem #3
Fork of USBMouse_HelloWorld by
Revision 6:e35a1f72d90f, committed 2013-07-17
- Comitter:
- jakowisp
- Date:
- Wed Jul 17 02:00:07 2013 +0000
- Parent:
- 5:2a28b4664b0a
- Commit message:
- USB Device Interface: Architecture, Protocols, and Programing:; Homework#2 Problem #3
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 2a28b4664b0a -r e35a1f72d90f main.cpp --- a/main.cpp Wed Jun 26 03:00:13 2013 +0000 +++ b/main.cpp Wed Jul 17 02:00:07 2013 +0000 @@ -1,32 +1,39 @@ #include "mbed.h" #include "USBMouse.h" +struct positionXY { + int x; + int y; + } mousePosition; + USBMouse mouse(ABS_MOUSE); AnalogIn potA(p20); AnalogIn potB(p19); -BusIn button(p12,p15,p13,p16); -DigitalIn fire(p14); +BusIn button(p12,p15,p13); int lastButtonState=0,buttonChanged=0,buttonState=0; -float x,y; +void DetermineMousePressReleaseAndSend(int buttonState) { + buttonChanged = buttonState ^ lastButtonState; + mouse.press(buttonState & buttonChanged); + mouse.release(!buttonState & buttonChanged); +} +int ReadAnalogValueAndShift(AnalogIn *pot) { + return pot->read_u16() >> 1; +} + +void DetermineXAndY(struct positionXY *position) { + position->x= ReadAnalogValueAndShift(&potA); + position->y= ReadAnalogValueAndShift(&potB); +} int main() { while (1) { - #ifdef FLOATPOT - x= 0x7fff * potA.read(); - y= 0x7fff * potB.read(); - #else - x= ( potA.read_u16() & 0xfff0 ) >> 1; - y= ( potB.read_u16() & 0xfff0 ) >> 1; - #endif + DetermineXAndY(&mousePosition); + mouse.move(mousePosition.x, mousePosition.y); buttonState = button.read() & 0x7; - buttonChanged = buttonState ^ lastButtonState; - mouse.move(x, y); - mouse.press(buttonState & buttonChanged); - mouse.release(!buttonState & buttonChanged); - + DetermineMousePressReleaseAndSend(buttonState); wait(0.001); } -} \ No newline at end of file +}