4180 lab 1

Dependencies:   mbed MCP23S17 PinDetect USBDevice

Files at this revision

API Documentation at this revision

Comitter:
emilywilson
Date:
Wed Jan 15 20:06:00 2020 +0000
Parent:
5:d1ad3a964858
Child:
7:b7720a8623b5
Commit message:
part 4

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
part4.cpp Show diff for this revision Revisions of this file
part4.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jan 15 19:58:39 2020 +0000
+++ b/main.cpp	Wed Jan 15 20:06:00 2020 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
 #include "RGBLed.h"
 #include "PinDetect.h"
+#include "part4.h"
 
 DigitalOut myled(p26);
 PwmOut builtinLED(LED1);
@@ -8,7 +9,9 @@
 //DigitalIn pb2(p21);
 
 // p20 is most significant bit
-BusIn ledSelect(p18, p19, p20);
+PinDetect redSelect(p18);
+PinDetect blueSelect(p18);
+PinDetect greenSelect(p20);
 
 PinName redPin = p24;
 PinName greenPin = p25;
@@ -31,10 +34,15 @@
     }
 }
 
+RGBLed myRGBLed = RGBLed(redPin, greenPin, bluePin);
+
 int main() {
     
     pb1.mode(PullUp);
     pb2.mode(PullUp);
+    redSelect.mode(PullUp);
+    greenSelect.mode(PullUp);
+    blueSelect.mode(PullUp);
     wait(0.1);
     
     pb1.attach_asserted(&pb1_hit_callback);
@@ -42,6 +50,10 @@
     pb1.setSampleFrequency();
     pb2.setSampleFrequency();
     
+    float redVal = 0.0f;
+    float greenVal = 0.0f;
+    float blueVal = 0.0f;
+    
     while(1) {
         // Part 1
 //        myled = !pb;
@@ -51,24 +63,26 @@
 //        wait(0.5);
         
         // Part 3
-        RGBLed myRBGLed = RGBLed(redPin, greenPin, bluePin);
-        float redVal = 0.0f;
-        float greenVal = 0.0f;
-        float blueVal = 0.0f;
         
-        if (ledSelect & 0x1) {
+        if (!redSelect) {
             redVal = 1.0 * p;
+        } else {
+            redVal = 0.0f;
         }
-        if (ledSelect & 0x2) {
+        if (!greenSelect) {
             greenVal = 1.0 * p;
+        } else {
+            greenVal = 0.0f;
         }
-        if (ledSelect & 0x4) {
+        if (!blueSelect) {
             blueVal = 1.0 * p;
+        } else {
+            blueVal = 0.0f;
         }
         myRGBLed.write(redVal, greenVal, blueVal);
-        w
+        wait(0.5);
         
+        run();
 
-        // Part 4
     }
 }
--- a/part4.cpp	Wed Jan 15 19:58:39 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-#include "mbed.h"
- 
-BusOut mbedleds(LED1,LED2,LED3,LED4);
-//BusOut/In is faster than multiple DigitalOut/Ins
- 
-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 breakout
- 
-int main()
-{
-    while(1) {
-        //with pullups a button hit is a "0" - "~" inverts data to leds
-        mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs
-        if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button)
-        //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this
-        wait(0.02);
-    }
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part4.h	Wed Jan 15 20:06:00 2020 +0000
@@ -0,0 +1,73 @@
+#include "mbed.h"
+ 
+BusOut mbedleds(LED1,LED2,LED3,LED4);
+//BusOut/In is faster than multiple DigitalOut/Ins
+ 
+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 breakout
+ 
+int run()
+{
+    while(1) {
+        //with pullups a button hit is a "0" - "~" inverts data to leds
+        mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs
+        if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button)
+        //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this
+        wait(0.02);
+    }
+}
\ No newline at end of file