Mbed writing library
From emboxit
- Step 1 - Refactoring....................................... Step 2 - Making it a Class
<cpp> //main.cpp
DigitalOut pin(LED2); void flash(int n) { for(int i=0; i<n*2; i++) { pin = !pin; wait(0.2); } } int main() { // do something... // flash 5 times flash(5); // do something else // flash 2 times flash(2); // do another thing // // // } </cpp> |
<cpp> //main.cpp
class Flasher { public: Flasher(PinName pin) : _pin(pin) { // _pin(pin) means pass pin to the DigitalOut constructor _pin = 0; // default the output to 0 } void flash(int n) { for(int i=0; i<n*2; i++) { _pin = !_pin; wait(0.2); } } private: DigitalOut _pin; }; Flasher led(LED2); Flasher out(p6); int main() { led.flash(5); led.flash(2); out.flash(10); } </cpp> |
- Step 3 - Separating the code into files
<cpp> //Flasher.h
class Flasher { public: Flasher(PinName pin); void flash(int n); private: DigitalOut _pin; };
</cpp> |
<cpp> //Flasher.cpp
Flasher::Flasher(PinName pin) : _pin(pin) { _pin = 0; } void Flasher::flash(int n) { for(int i=0; i<n*2; i++) { _pin = !_pin; wait(0.2); } } // // </cpp> |
<cpp> //main.cpp
Flasher led(LED2); int main() { led.flash(5); led.flash(2); } // // // // // // </cpp> |
TextLCD library
- TextLCD Library uses more than one pin:
- File:TextLCD.h local
- File:TextLCD.cpp local