L VD
/
DigitalOut_HelloWorld
Hello World for DigitalOut
Revision 12:6a944faf5dd8, committed 2020-08-27
- Comitter:
- elelthvd
- Date:
- Thu Aug 27 16:15:27 2020 +0800
- Parent:
- 11:759df8b71f32
- Commit message:
- Use "\r\n", add three other methods of blinking
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Aug 13 16:16:51 2020 +0800 +++ b/main.cpp Thu Aug 27 16:15:27 2020 +0800 @@ -17,21 +17,81 @@ DigitalOut myled(LED1); +// For approach Three +Ticker blinkticker; +void blink() +{ + myled = !myled; + printf("myled = %d (blinkticker)\r\n", (uint8_t) myled ); +} + +// For approach Four: // A class for blink()-ing a DigitalOut +class Blinker { +public: + Blinker(PinName pin) : _pin(pin) + { + _pin = 0; + } + void blink() + { + _pin = !_pin; + printf("_pin = %d (Blinker::blink)\r\n", (uint8_t) _pin ); + } +private: + DigitalOut _pin; +}; +Blinker myblinker(LED2); + +// For approach Five +PwmOut mypwm(PWM_OUT); +InterruptIn myint(D10); // Manually Wire D10 to PWM_OUT (D9) +void blinkinterrupt() +{ + myled = !myled; + printf("myled = %d (blinkinterrupt)\r\n", (uint8_t) myled ); +} + int main() { // check that myled object is initialized and connected to a pin if(myled.is_connected()) { - printf("myled is initialized and connected!\n\r"); + printf("myled is initialized and connected!\r\n"); } // Blink LED - while(1) { + for(int i=0; i<3; i++) { + + // one way myled = 1; // set LED1 pin to high - printf("myled = %d \n\r", (uint8_t)myled ); + printf("myled = %d (assign)\r\n", (uint8_t) myled ); ThisThread::sleep_for(500ms); + // another method myled.write(0); // set LED1 pin to low - printf("myled = %d \n\r",myled.read() ); + printf("myled = %d (write())\r\n",myled.read() ); ThisThread::sleep_for(500ms); } + + // A third blinker + blinkticker.attach(&blink, 500ms); // call function every 500ms + ThisThread::sleep_for(2500ms); // Let the main thread rest ... + blinkticker.detach(); + + // A fourth blinker + // the address of the object, member function, and interval + blinkticker.attach(callback(&myblinker, &Blinker::blink), 500ms); + ThisThread::sleep_for(2500ms); // Let the main thread rest ... + blinkticker.detach(); + + // A fifth blinker + blinkinterrupt(); // Test function works + myint.rise(callback(&blinkinterrupt)); // Start listening on rise events + mypwm.period_ms(500); // PWM period is 500ms + mypwm.write(0.5); // Duty Cycle: 50% HIGH time. + + printf("Good Bye.\r\n"); + while (true) { + ThisThread::sleep_for(1h); // Zzz + } + }