Example of defining a custom class

Dependencies:   mbed

Committer:
bjo3rn
Date:
Wed Sep 23 20:39:45 2015 +0000
Revision:
1:aabc627fb0d1
Parent:
0:cdc3209dbb53
had green and blue flipped; fixed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bjo3rn 0:cdc3209dbb53 1 #include "mbed.h"
bjo3rn 0:cdc3209dbb53 2
bjo3rn 0:cdc3209dbb53 3 /********************
bjo3rn 0:cdc3209dbb53 4 * Interactive Device Design Custom Class Demonstration
bjo3rn 0:cdc3209dbb53 5 * bjoern@eecs.berkeley.edu, 9/23/2015
bjo3rn 0:cdc3209dbb53 6 ********************/
bjo3rn 0:cdc3209dbb53 7
bjo3rn 0:cdc3209dbb53 8
bjo3rn 0:cdc3209dbb53 9 class RGBLed {
bjo3rn 0:cdc3209dbb53 10 public:
bjo3rn 0:cdc3209dbb53 11
bjo3rn 0:cdc3209dbb53 12 // constructor - same name as class, does nothing
bjo3rn 0:cdc3209dbb53 13 RGBLed () {
bjo3rn 0:cdc3209dbb53 14 }
bjo3rn 0:cdc3209dbb53 15
bjo3rn 0:cdc3209dbb53 16 //set red, green, blue of on-board LED
bjo3rn 0:cdc3209dbb53 17 // r,g,b range from 0.0(off) to 1.0(full on)
bjo3rn 1:aabc627fb0d1 18 void setColor(float r, float g, float b){
bjo3rn 0:cdc3209dbb53 19 PwmOut redPin(LED_RED);
bjo3rn 0:cdc3209dbb53 20 PwmOut greenPin(LED_GREEN);
bjo3rn 0:cdc3209dbb53 21 PwmOut bluePin(LED_BLUE);
bjo3rn 0:cdc3209dbb53 22 redPin = 1.0-r; //invert: PWM 0=on, 1.0=off
bjo3rn 0:cdc3209dbb53 23 bluePin = 1.0-b;
bjo3rn 0:cdc3209dbb53 24 greenPin = 1.0-g;
bjo3rn 0:cdc3209dbb53 25 }
bjo3rn 0:cdc3209dbb53 26 private:
bjo3rn 0:cdc3209dbb53 27 //no private methods or fields
bjo3rn 0:cdc3209dbb53 28 };
bjo3rn 0:cdc3209dbb53 29
bjo3rn 0:cdc3209dbb53 30
bjo3rn 0:cdc3209dbb53 31 RGBLed myLed; //declare a variable of our new class type
bjo3rn 0:cdc3209dbb53 32
bjo3rn 0:cdc3209dbb53 33 int main() {
bjo3rn 0:cdc3209dbb53 34 while(1) {
bjo3rn 0:cdc3209dbb53 35 myLed.setColor(1.0, 0.0, 0.0); //call the member function setColor() - this is red
bjo3rn 0:cdc3209dbb53 36 wait(0.2);
bjo3rn 1:aabc627fb0d1 37 myLed.setColor(0.0, 1.0, 0.0); //this is green
bjo3rn 0:cdc3209dbb53 38 wait(0.2);
bjo3rn 0:cdc3209dbb53 39 }
bjo3rn 0:cdc3209dbb53 40 }