Example of defining a custom class

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
bjo3rn
Date:
Wed Sep 23 20:24:41 2015 +0000
Child:
1:aabc627fb0d1
Commit message:
initial commit;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 23 20:24:41 2015 +0000
@@ -0,0 +1,40 @@
+#include "mbed.h"
+
+/********************
+ * Interactive Device Design Custom Class Demonstration
+ * bjoern@eecs.berkeley.edu, 9/23/2015
+ ********************/
+
+
+class RGBLed {
+public:
+    
+    // constructor - same name as class, does nothing
+    RGBLed () {
+    }
+    
+    //set red, green, blue of on-board LED
+    // r,g,b range from 0.0(off) to 1.0(full on)
+    void setColor(float r, float b, float g){
+        PwmOut redPin(LED_RED);
+        PwmOut greenPin(LED_GREEN);
+        PwmOut bluePin(LED_BLUE);
+        redPin = 1.0-r; //invert: PWM 0=on, 1.0=off
+        bluePin = 1.0-b;
+        greenPin = 1.0-g;
+    }
+private:
+    //no private methods or fields
+};
+
+    
+RGBLed myLed; //declare a variable of our new class type
+
+int main() {
+    while(1) {
+        myLed.setColor(1.0, 0.0, 0.0); //call the member function setColor() - this is red
+        wait(0.2);
+        myLed.setColor(0.0, 0.0, 1.0); //this is green
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Sep 23 20:24:41 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa
\ No newline at end of file