Kevin Abraham / Mbed 2 deprecated 4180Lab1-3

Dependencies:   PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 
00004 //Class to control an RGB LED using three PWM pins
00005 class RGBLed
00006 {
00007 public:
00008     RGBLed(PinName redpin, PinName greenpin, PinName bluepin);
00009     void write(float red,float green, float blue);
00010 private:
00011     PwmOut _redpin;
00012     PwmOut _greenpin;
00013     PwmOut _bluepin;
00014 };
00015 
00016 RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin)
00017     : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin)
00018 {
00019     //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker)
00020     _redpin.period(0.0005);
00021 }
00022 
00023 void RGBLed::write(float red,float green, float blue)
00024 {
00025     _redpin = red;
00026     _greenpin = green;
00027     _bluepin = blue;
00028 }
00029 
00030 //Setup RGB led using PWM pins and class
00031 RGBLed myRGBled(p21,p22,p23); //RGB PWM pins
00032 
00033 DigitalIn r(p24);
00034 DigitalIn g(p25);
00035 DigitalIn b(p26);
00036 
00037 PinDetect pb1(p8);
00038 PinDetect pb2(p7);
00039 
00040 // Global pwm variable
00041 float volatile pwm=1.0f;
00042 
00043 // Callback routine is interrupt activated by a debounced pb1 hit
00044 void pb1_hit_callback (void)
00045 {
00046     pwm -= 0.1f;
00047     if (pwm < 0.0f) {
00048         pwm = 0.0f;
00049     }
00050 }
00051 
00052 // Callback routine is interrupt activated by a debounced pb2 hit
00053 void pb2_hit_callback (void)
00054 {
00055     pwm += 0.1f;
00056     if (pwm > 1.0f) {
00057         pwm = 1.0f;
00058     }
00059 }
00060 
00061 int main()
00062 {
00063     r.mode(PullUp);
00064     g.mode(PullUp);
00065     b.mode(PullUp);
00066 
00067     // Use internal pullups for pushbutton
00068     pb1.mode(PullUp);
00069     pb2.mode(PullUp);
00070     // Delay for initial pullup to take effect
00071     wait(.01);
00072     // Setup Interrupt callback functions for a pb hit
00073     pb1.attach_deasserted(&pb1_hit_callback);
00074     pb2.attach_deasserted(&pb2_hit_callback);
00075     // Start sampling pb inputs using interrupts
00076     pb1.setSampleFrequency();
00077     pb2.setSampleFrequency();
00078 
00079     while(1) {
00080         myRGBled.write(!r*pwm,!g*pwm,!b*pwm);
00081     }
00082 }