Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
KPIT_MBED_worksheet/Topic2/FlasherCode.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53 lines (42 sloc)
1.36 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mbed.h" | |
//First We define the Flasher Class | |
class Flasher{ | |
/* Flasher. A Demonstration Class creating an object that flashes | |
LEDs. | |
*/ | |
//Classes can have Public and Private Attributes and Methods. | |
// - Private are only available within the class. | |
// - Public are available to all | |
private: | |
//Define our Class Attributes | |
int delay; //How Long to Pause between Flashes | |
DigitalOut pin; //The Pin our LED is attached to | |
public: | |
//Constructor Function, Used to Create the class. | |
//NOTE: In this case we use an Initialisation List. | |
Flasher(PinName thePin, int delayTime) : pin(thePin) { | |
//PinName thePin: The Digital Pin that the LED is attached to. | |
//This comes from a global Lookup table This is then passed to the | |
//constructor of the Class Attribute "pin" (of type DigitalOut) | |
//in ```: pin(thePin)``` of the function definition | |
delay = delayTime; //Set the Attribute delay, to the value of delay time | |
} | |
void flash(int times){ | |
/* Here we define a function available to the class */ | |
for (int x = 0; x< times; x++){ | |
pin= !pin; | |
wait_ms(delay); | |
} | |
} | |
}; | |
//Our Main Loop, as per all MBED programs. | |
int main() { | |
//Setup some Flasher Objects | |
Flasher ledOne = Flasher(LED1, 500); | |
Flasher ledTwo = Flasher(LED2, 250); | |
//Loop | |
while(1){ | |
ledOne.flash(5); | |
ledTwo.flash(10); | |
} | |
} | |