You are viewing an older revision! See the latest version
Writing a Library
Writing a Library¶
This page discusses the steps in writing and publishing an mbed Library.
What is a Library¶
A library is a collection of code, usually focused on enabling a specific component, peripheral or .
It is effectively a collection of functions and classes, without a "main()" program.
main.cpp
#include "mbed.h" DigitalOut pin(LED2); void flash(int n) { for(int i=0; i<n*2; n++) { pin = !pin; wait(0.2); } } int main() { flash(5); }
main.cpp
#include "mbed.h" class Flasher { public: Flasher(PinName pin) : _pin(pin) { _pin = 0; } void flash(int n) { for(int i=0; i<n*2; n++) { _pin = !_pin; wait(0.2); } } private: DigitalOut _pin; }; Flasher led(LED2); int main() { led.flash(5); }
Flasher.h
#ifndef MBED_FLASHER_H #define MBED_FLASHER_H #include "mbed.h" class Flasher { public: Flasher(PinName pin); void flash(int n); private: DigitalOut _pin; }; #endif
Flasher.cpp
#include "Flasher.h" #include "mbed.h" Flasher::Flasher(PinName pin) : _pin(pin) { _pin = 0; } void Flasher::flash(int n) { for(int i=0; i<n*2; n++) { _pin = !_pin; wait(0.2); } }
main.cpp
#include "mbed.h" #include "Flasher.h" Flasher led(LED2); int main() { led.flash(5); }
main.cpp
#include "mbed.h" #include "Flasher.h" Flasher led(LED2); Flasher external_led(p20); int main() { led.flash(5); external_led.flash(8); }