4180 lab 1

Dependencies:   mbed MCP23S17 PinDetect USBDevice

Files at this revision

API Documentation at this revision

Comitter:
emilywilson
Date:
Wed Jan 22 13:08:48 2020 +0000
Parent:
11:2cfdab516b21
Commit message:
mouse extra credit and power management extra credit

Changed in this revision

USBDevice.lib 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
mouse_EC.h Show annotated file Show diff for this revision Revisions of this file
powermanagement_ec.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Wed Jan 22 13:08:48 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#c5e178adb138
--- a/main.cpp	Tue Jan 21 21:06:20 2020 +0000
+++ b/main.cpp	Wed Jan 22 13:08:48 2020 +0000
@@ -7,7 +7,8 @@
 //#include "part8.h"
 //#include "part9.h"
 //#include "watchdog_ec.h"
-#include "powermanagement_ec.h"
+//#include "powermanagement_ec.h"
+#include "mouse_ec.h"
 
 //DigitalOut myled(p26);
 //PwmOut builtinLED(LED1);
@@ -108,7 +109,11 @@
         // Watchdog Extra Credit
 //        run_watchdogEC();
 
-        run_powermanagementEC();
+        // Power Management Extra Credit
+//        run_powermanagementEC();
+        
+        // USB Mouse Extra Credit
+        run_mouseEC();
         
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mouse_EC.h	Wed Jan 22 13:08:48 2020 +0000
@@ -0,0 +1,98 @@
+#include "mbed.h"
+#include "USBMouse.h"
+//USB mouse demo using a 5-way Navigation Switch (Digital Joystick)
+//Needs USB connector breakout with D+, D-, and Gnd to mbed LLP1768
+USBMouse mouse;
+
+class Nav_Switch
+{
+public:
+    Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
+    int read();
+//boolean functions to test each switch
+    bool up();
+    bool down();
+    bool left();
+    bool right();
+    bool fire();
+//automatic read on RHS
+    operator int ();
+//index to any switch array style
+    bool operator[](int index) {
+        return _pins[index];
+    };
+private:
+    BusIn _pins;
+
+};
+Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
+    _pins(up, down, left, right, fire)
+{
+    _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
+    wait(0.001); //delays just a bit for pullups to pull inputs high
+}
+inline bool Nav_Switch::up()
+{
+    return !(_pins[0]);
+}
+inline bool Nav_Switch::down()
+{
+    return !(_pins[1]);
+}
+inline bool Nav_Switch::left()
+{
+    return !(_pins[2]);
+}
+inline bool Nav_Switch::right()
+{
+    return !(_pins[3]);
+}
+inline bool Nav_Switch::fire()
+{
+    return !(_pins[4]);
+}
+inline int Nav_Switch::read()
+{
+    return _pins.read();
+}
+inline Nav_Switch::operator int ()
+{
+    return _pins.read();
+}
+
+Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun Nav SW breakout
+
+AnalogIn pot(p20);
+int potPrev = 0;
+
+int run_mouseEC()
+{
+    int16_t x = 0;
+    int16_t y = 0;
+    uint8_t left_click = 0;
+    int relPot = 0;
+    while (1) {
+        //check relative mouse movement
+        x=0;
+        y=0;
+        if (myNav.up()) x=-1;
+        if (myNav.down()) x=1;
+        if (myNav.left()) y=1;
+        if (myNav.right()) y=-1;
+        //check mouse left button click
+        if (myNav.fire()) left_click = 1;
+        if (!myNav.fire())left_click = 0;
+        
+        if (potPrev < pot) {
+            relPot = 1;
+        } else if (potPrev > pot) {
+            relPot = -1;
+        } else {
+            relPot = 0;
+        }
+        potPrev = pot;
+        //send a mouse data packet to PC
+        mouse.update(x, y, left_click, relPot);
+        wait(0.001);
+    }
+}
\ No newline at end of file
--- a/powermanagement_ec.h	Tue Jan 21 21:06:20 2020 +0000
+++ b/powermanagement_ec.h	Wed Jan 22 13:08:48 2020 +0000
@@ -13,6 +13,12 @@
  
 DigitalOut myled(p26);
 DigitalIn pb(p22);
+
+Ticker blinker;
+
+void blink() {
+    myled = !pb;
+}
  
 int run_powermanagementEC() {
     int result;
@@ -41,9 +47,8 @@
 // Sleep halts and waits for an interrupt instead of executing instructions
 // power is saved by not constantly fetching and decoding instructions
 // Exact power level reduction depends on the amount of time spent in Sleep mode
-//    blinker.attach(&blink, 0.0625);
+    blinker.attach(&blink, 0.0625);
     while (1) {
-//        Sleep();
-        myled = !pb;
+        Sleep();
     }
 }
\ No newline at end of file