RGB Dimmer mit Klassen
Dependencies: mbed
Revision 0:2af9d1c6c6ee, committed 2014-12-12
- Comitter:
- Kilian
- Date:
- Fri Dec 12 17:21:54 2014 +0000
- Commit message:
- RGB Dimmerprogramm mit Klassen
Changed in this revision
diff -r 000000000000 -r 2af9d1c6c6ee BtnEventM0.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BtnEventM0.h Fri Dec 12 17:21:54 2014 +0000 @@ -0,0 +1,132 @@ + + +class BtnEventM0 +{ +public: + int16_t pressed; + + BtnEventM0(PinName pin) : _isr(pin) { + pressed=0; + } + + int CheckFlag() { + if( pressed ) { + pressed=0; + return 1; + } + return 0; + } + + void CheckButton() { + if( _isr.read() ) + pressed = 1; + } + + void Init() { + _isr.rise(this, &BtnEventM0::RisingISR); + } + + void RisingISR() { + if( _isr.read() ) + pressed = 1; + } +protected: + InterruptIn _isr; +}; + +/* +class BtnEventM02 : public BtnEventM0 +{ +public: + BtnEventM02(PinName pin) : BtnEventM0(pin) { + _tm.stop(); + _tm.reset(); + _state=1; + } + + void Init() { + _isr.rise(this, &BtnEventM02::RisingISR); + } + + void RisingISR() { + if( !_isr.read() ) + return; + pressed = 1; + _tm.start(); + _state = 2; + } + + void CheckButton() { + if( _state==1 ) + return; + if( _state==2 ) { + if( !_isr.read() ) { + _state = 1; + return; + } + if( _tm.read_ms()>500 ) { + _tm.reset(); + _state = 3; + pressed = 1; + } + } else if( _state==3 ) { + if( !_isr.read() ) { + _state = 1; + return; + } + if( _tm.read_ms()>100 ) { + _tm.reset(); + _state = 3; + pressed = 1; + } + } + } +private: + int16_t _state; + Timer _tm; +}; +*/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff -r 000000000000 -r 2af9d1c6c6ee ButtonEventCounter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ButtonEventCounter.h Fri Dec 12 17:21:54 2014 +0000 @@ -0,0 +1,42 @@ +class ButtonEventCounter +{ + public: + // Konstruktor wird aufgerufen wenn ein Objekt dieser Klasse angelegt wird + ButtonEventCounter(PinName aPin) : _btn(aPin) + { + val=0; + } + + void CheckButton(int aDown); //hier wird das rauf und runter zählen codiert //Check Button muss mit 10Hz aufgerufen werden + float GetValF(); //Wert im Range 0.0 ... 1.0 als Float + + public: + int val; //0...100 momentaner Wert des ButtonEventCounter + + private: + DigitalIn _btn; +}; + + + +void ButtonEventCounter::CheckButton(int aDown) +{ + if(_btn.read()) //wenn der _btn gedrückt ist + { + if(aDown) + val--; + else + val++; + + if (val<0) + val=0; + if (val>100) + val=100; + } +} + + +float ButtonEventCounter::GetValF() +{ + return (float)val/100.0; +} \ No newline at end of file
diff -r 000000000000 -r 2af9d1c6c6ee main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Dec 12 17:21:54 2014 +0000 @@ -0,0 +1,40 @@ +#include "mbed.h" +#include "ButtonEventCounter.h" + +PwmOut lr(p5), lg(p34), lb(p36); // Definition LED grün, gelb, rot PWM +BusOut leds(P1_13,P1_12,P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1); + +Serial pc(USBTX, USBRX); + +ButtonEventCounter btg(P1_16); //SW4 +ButtonEventCounter btb(P0_23); //SW4 +ButtonEventCounter btr(P0_10); //SW1 +DigitalIn updown(P0_15); //Sw2 + +int main() +{ + pc.baud(115000); + leds=0; + lr.period_ms(2); lg.period_ms(2); lb.period_ms(2); + lr.write(1.0); lg.write(1.0); lb.write(1.0); + + Timer tm; tm.start(); + while(1) + { + if (tm.read_ms()>100) + { + tm.reset(); + btr.CheckButton(updown.read()); + btg.CheckButton(updown.read()); + btb.CheckButton(updown.read()); + + pc.printf("V %d %d %d\n", btr.val, btg.val, btb.val); + + lr.write(1.0-btr.GetValF()); + lg.write(1.0-btg.GetValF()); + lb.write(1.0-btb.GetValF()); + + } + + } +}
diff -r 000000000000 -r 2af9d1c6c6ee mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Dec 12 17:21:54 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file