USBmouse

Dependencies:   USBDevice mbed

Fork of USBMouse_HelloWorld by Samuel Mokrani

Files at this revision

API Documentation at this revision

Comitter:
bhakti08
Date:
Wed Feb 19 05:23:20 2014 +0000
Parent:
4:26ecbbc27530
Commit message:
Mouse modified_02/18

Changed in this revision

DebouncedIn.cpp Show annotated file Show diff for this revision Revisions of this file
DebouncedIn.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 26ecbbc27530 -r 36a45d09bb56 DebouncedIn.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.cpp	Wed Feb 19 05:23:20 2014 +0000
@@ -0,0 +1,93 @@
+#include "DebouncedIn.h"
+#include "mbed.h"
+
+/*
+ * Constructor
+ */
+DebouncedIn::DebouncedIn(PinName in) 
+    : _in(in) {    
+        
+    // reset all the flags and counters    
+    _samples = 0;
+    _output = 0;
+    _output_last = 0;
+    _rising_flag = 0;
+    _falling_flag = 0;
+    _state_counter = 0;
+    
+    // Attach ticker
+    _ticker.attach(this, &DebouncedIn::_sample, 0.005);     
+}
+  
+void DebouncedIn::_sample() {
+
+    // take a sample
+    _samples = _samples >> 1; // shift left
+      
+    if (_in) {
+        _samples |= 0x80;
+    }  
+      
+    // examine the sample window, look for steady state
+    if (_samples == 0x00) {
+        _output = 0;
+    } 
+    else if (_samples == 0xFF) {
+        _output = 1;
+    }
+
+
+    // Rising edge detection
+    if ((_output == 1) && (_output_last == 0)) {
+        _rising_flag++;
+        _state_counter = 0;
+    }
+
+    // Falling edge detection
+    else if ((_output == 0) && (_output_last == 1)) {
+        _falling_flag++;
+        _state_counter = 0;
+    }
+    
+    // steady state
+    else {
+        _state_counter++;
+    }
+    
+   // update the output
+    _output_last = _output;
+    
+}
+
+
+
+// return number of rising edges
+int DebouncedIn::rising(void) {
+    int return_value = _rising_flag; 
+    _rising_flag = 0;
+    return(return_value);
+}
+
+// return number of falling edges
+int DebouncedIn::falling(void) {
+    int return_value = _falling_flag; 
+    _falling_flag = 0;
+    return(return_value);
+}
+
+// return number of ticsk we've bene steady for
+int DebouncedIn::steady(void) {
+return(_state_counter);
+}
+
+// return the debounced status
+int DebouncedIn::read(void) {
+    return(_output);
+}
+
+// shorthand for read()
+DebouncedIn::operator int() {
+    return read();
+}
+
+
diff -r 26ecbbc27530 -r 36a45d09bb56 DebouncedIn.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.h	Wed Feb 19 05:23:20 2014 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+
+    class DebouncedIn {
+        public:      
+             DebouncedIn(PinName in);
+
+             int read (void);
+             operator int();
+              
+             int rising(void);
+             int falling(void);
+             int steady(void);
+              
+        private :    
+               // objects
+               DigitalIn _in;    
+               Ticker _ticker;
+
+               // function to take a sample, and update flags
+               void _sample(void);
+
+               // counters and flags
+               int _samples;
+               int _output;
+               int _output_last;
+               int _rising_flag;
+               int _falling_flag;
+               int _state_counter;
+
+    };
+    
\ No newline at end of file
diff -r 26ecbbc27530 -r 36a45d09bb56 main.cpp
--- a/main.cpp	Fri Mar 01 13:26:13 2013 +0000
+++ b/main.cpp	Wed Feb 19 05:23:20 2014 +0000
@@ -1,20 +1,35 @@
 #include "mbed.h"
 #include "USBMouse.h"
+#include "DebouncedIn.h"
+
 
 USBMouse mouse;
+DigitalIn joy_left(p13);
+DigitalIn joy_right(p16);
+DigitalIn joy_up(p15);
+DigitalIn joy_down(p12);
 
 int main() {
-    int16_t x = 0;
-    int16_t y = 0;
-    int32_t radius = 10;
-    int32_t angle = 0;
+    int16_t x = 600;
+    int16_t y = 200;
+    //int32_t radius = 10;
+    //int32_t angle = 0;
 
     while (1) {
-        x = cos((double)angle*3.14/180.0)*radius;
-        y = sin((double)angle*3.14/180.0)*radius;
+        //x = cos((double)angle*3.14/180.0)*radius;
+        //y = sin((double)angle*3.14/180.0)*radius;
+        if (joy_left)
+           x = x-5;
+        if (joy_right)
+           x = x+5;
+        if (joy_up)
+           y = y+5;
+        if (joy_down)
+           y = y-5;
+            
         
         mouse.move(x, y);
-        angle += 3;
+        //angle += 3;
         wait(0.001);
     }
 }
\ No newline at end of file