Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@0:0ca7f9aff195, 2015-09-23 (annotated)
- Committer:
- bjo3rn
- Date:
- Wed Sep 23 20:36:10 2015 +0000
- Revision:
- 0:0ca7f9aff195
initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| bjo3rn | 0:0ca7f9aff195 | 1 | #include "mbed.h" |
| bjo3rn | 0:0ca7f9aff195 | 2 | |
| bjo3rn | 0:0ca7f9aff195 | 3 | /******************** |
| bjo3rn | 0:0ca7f9aff195 | 4 | * Interactive Device Design Custom Class Demonstration |
| bjo3rn | 0:0ca7f9aff195 | 5 | * V2 - Improved version without re-creating PwmOut objects at every call |
| bjo3rn | 0:0ca7f9aff195 | 6 | * bjoern@eecs.berkeley.edu, 9/23/2015 |
| bjo3rn | 0:0ca7f9aff195 | 7 | ********************/ |
| bjo3rn | 0:0ca7f9aff195 | 8 | |
| bjo3rn | 0:0ca7f9aff195 | 9 | |
| bjo3rn | 0:0ca7f9aff195 | 10 | class RGBLed { |
| bjo3rn | 0:0ca7f9aff195 | 11 | public: |
| bjo3rn | 0:0ca7f9aff195 | 12 | |
| bjo3rn | 0:0ca7f9aff195 | 13 | // constructor - same name as class - this one takes pin names |
| bjo3rn | 0:0ca7f9aff195 | 14 | RGBLed (PinName redPN, PinName greenPN, PinName bluePN) { |
| bjo3rn | 0:0ca7f9aff195 | 15 | //create new PwmOut objects |
| bjo3rn | 0:0ca7f9aff195 | 16 | redPin = new PwmOut(redPN); |
| bjo3rn | 0:0ca7f9aff195 | 17 | greenPin = new PwmOut (greenPN); |
| bjo3rn | 0:0ca7f9aff195 | 18 | bluePin = new PwmOut (bluePN); |
| bjo3rn | 0:0ca7f9aff195 | 19 | } |
| bjo3rn | 0:0ca7f9aff195 | 20 | |
| bjo3rn | 0:0ca7f9aff195 | 21 | // set red, green, blue values of on-board LED |
| bjo3rn | 0:0ca7f9aff195 | 22 | // r,g,b range from 0.0(off) to 1.0(full on) |
| bjo3rn | 0:0ca7f9aff195 | 23 | void setColor(float r, float g, float b){ |
| bjo3rn | 0:0ca7f9aff195 | 24 | redPin->write(1.0-r); //invert: PWM 0=on, 1.0=off |
| bjo3rn | 0:0ca7f9aff195 | 25 | greenPin->write(1.0-g); |
| bjo3rn | 0:0ca7f9aff195 | 26 | bluePin->write(1.0-b); |
| bjo3rn | 0:0ca7f9aff195 | 27 | } |
| bjo3rn | 0:0ca7f9aff195 | 28 | private: |
| bjo3rn | 0:0ca7f9aff195 | 29 | RGBLed() {} // cannot use this from the outside because it is private |
| bjo3rn | 0:0ca7f9aff195 | 30 | |
| bjo3rn | 0:0ca7f9aff195 | 31 | PwmOut* redPin; // we don't know which pins these will be connected to yet, |
| bjo3rn | 0:0ca7f9aff195 | 32 | PwmOut* bluePin; // so only create pointers and allocate the objects later |
| bjo3rn | 0:0ca7f9aff195 | 33 | PwmOut* greenPin; // inside the constructor |
| bjo3rn | 0:0ca7f9aff195 | 34 | }; |
| bjo3rn | 0:0ca7f9aff195 | 35 | |
| bjo3rn | 0:0ca7f9aff195 | 36 | |
| bjo3rn | 0:0ca7f9aff195 | 37 | RGBLed myLed(LED_RED,LED_GREEN,LED_BLUE); //declare a variable of our new class type and pass in pin names |
| bjo3rn | 0:0ca7f9aff195 | 38 | |
| bjo3rn | 0:0ca7f9aff195 | 39 | int main() { |
| bjo3rn | 0:0ca7f9aff195 | 40 | while(1) { |
| bjo3rn | 0:0ca7f9aff195 | 41 | myLed.setColor(0.0, 0.0, 1.0); //call the member function setColor() - this is blue |
| bjo3rn | 0:0ca7f9aff195 | 42 | wait(0.2); |
| bjo3rn | 0:0ca7f9aff195 | 43 | myLed.setColor(1.0, 1.0, 1.0); //this is white |
| bjo3rn | 0:0ca7f9aff195 | 44 | wait(0.2); |
| bjo3rn | 0:0ca7f9aff195 | 45 | } |
| bjo3rn | 0:0ca7f9aff195 | 46 | } |