Example attaching a member function to a ticker

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  
00003 // A class for flip()-ing a DigitalOut 
00004 class Flipper {
00005 public:
00006     Flipper(PinName pin) : _pin(pin) {
00007         _pin = 0;
00008     }
00009     void flip() {
00010         _pin = !_pin;
00011     }
00012 private:
00013     DigitalOut _pin;
00014 };
00015  
00016 DigitalOut led1(LED1);
00017 Flipper f(LED2);
00018 Ticker t;
00019  
00020 int main() {
00021     t.attach(&f, &Flipper::flip, 2.0); // the address of the object, member function, and interval
00022  
00023     // spin in a main loop. flipper will interrupt it to call flip
00024     while(1) {
00025         led1 = !led1;
00026         wait(0.2);
00027     }
00028 }