Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ele350 by
sine_app.cpp@65:9dbee72e3e6e, 2016-02-23 (annotated)
- Committer:
- GGHHHH
- Date:
- Tue Feb 23 16:16:34 2016 +0000
- Revision:
- 65:9dbee72e3e6e
- Parent:
- 58:0682cb4f5f3a
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GGHHHH | 29:8ec9451b1a59 | 1 | #include "sine_app.h" |
GGHHHH | 54:aee1b44e62ec | 2 | |
GGHHHH | 54:aee1b44e62ec | 3 | //Constructor. Calls the bass class (App) constructor and creact an AnalogOut object.// |
GGHHHH | 29:8ec9451b1a59 | 4 | SineApp::SineApp(Serial* serial) : App("Sine wave", serial) { |
GGHHHH | 54:aee1b44e62ec | 5 | //Set analogOut attribute to a new AnalogOut object.// |
GGHHHH | 29:8ec9451b1a59 | 6 | this->analogOut = new AnalogOut (PA_4); |
GGHHHH | 54:aee1b44e62ec | 7 | this->amplitude = 3.0f; //Set the amplitude value.// |
GGHHHH | 54:aee1b44e62ec | 8 | this->frequence = 50.0f; //Set the frequence value.// |
GGHHHH | 29:8ec9451b1a59 | 9 | } |
GGHHHH | 29:8ec9451b1a59 | 10 | |
GGHHHH | 54:aee1b44e62ec | 11 | void SineApp::start() //(Overriden) Method that runs when the app starts.// |
GGHHHH | 54:aee1b44e62ec | 12 | { |
GGHHHH | 54:aee1b44e62ec | 13 | App::start(); //Call bass class's start() method.// |
GGHHHH | 54:aee1b44e62ec | 14 | this->analogOut->write(this->amplitude/3.0f); //Set the output to the amplitude.// |
GGHHHH | 54:aee1b44e62ec | 15 | this->timer.start(); //Start timer.// |
GGHHHH | 54:aee1b44e62ec | 16 | if ( this->amplitude > 3.0f ) { //Define the max amplitude value and check it.// |
GGHHHH | 54:aee1b44e62ec | 17 | this->analogOut->write(0); //If >max value, output 0.// |
GGHHHH | 54:aee1b44e62ec | 18 | } else { this->analogOut->write(this->amplitude/3.0f);} //If <max value, out the right expression.// |
GGHHHH | 29:8ec9451b1a59 | 19 | |
GGHHHH | 29:8ec9451b1a59 | 20 | } |
GGHHHH | 29:8ec9451b1a59 | 21 | |
GGHHHH | 54:aee1b44e62ec | 22 | void SineApp::run() //(Overriden) Method that is called repeatedly when the app starts.// |
GGHHHH | 29:8ec9451b1a59 | 23 | { |
GGHHHH | 56:49640c23b101 | 24 | this->analogOut->write(this->amplitude/3.0f*0.5f*(1.0f+sin(this->frequence*6.28f*timer.read()))); //Set the output to the sin expression.// |
GGHHHH | 47:124bc7d77d5a | 25 | |
GGHHHH | 54:aee1b44e62ec | 26 | if ( this->frequence > 50.0f ) { //Define the max frequence value and check it.// |
GGHHHH | 54:aee1b44e62ec | 27 | this->analogOut->write(0); //If >max value, output 0.// |
GGHHHH | 58:0682cb4f5f3a | 28 | } else { this->analogOut->write(this->amplitude/3.0f*0.5f*(1.0f+sin(this->frequence*6.28f*timer.read())));} //If <max value, out the right expression.// |
GGHHHH | 29:8ec9451b1a59 | 29 | |
GGHHHH | 34:498a218cf53e | 30 | |
GGHHHH | 29:8ec9451b1a59 | 31 | } |
GGHHHH | 29:8ec9451b1a59 | 32 | |
GGHHHH | 54:aee1b44e62ec | 33 | void SineApp::stop() //(Overriden) Method that runs when the app stops.// |
GGHHHH | 29:8ec9451b1a59 | 34 | { |
GGHHHH | 54:aee1b44e62ec | 35 | App::stop(); //Call bass class's stop() method.// |
GGHHHH | 54:aee1b44e62ec | 36 | this->analogOut->write(0.0f); //Set output to 0V.// |
GGHHHH | 54:aee1b44e62ec | 37 | this->timer.stop(); //Stop the time and reset it.// |
GGHHHH | 29:8ec9451b1a59 | 38 | this->timer.reset(); |
GGHHHH | 34:498a218cf53e | 39 | } |
GGHHHH | 54:aee1b44e62ec | 40 | void SineApp::setamplitude(float newamplitude) //(Overriden) Method that could set amplitude.// |
GGHHHH | 34:498a218cf53e | 41 | { |
GGHHHH | 54:aee1b44e62ec | 42 | this->amplitude = newamplitude; //Set the new amplitude.// |
GGHHHH | 39:ab2557044815 | 43 | } |
GGHHHH | 54:aee1b44e62ec | 44 | void SineApp::setfrequence(float newfrequence) //(Overriden) Method that could set frequence.// |
GGHHHH | 39:ab2557044815 | 45 | { |
GGHHHH | 54:aee1b44e62ec | 46 | this->frequence = newfrequence; //Set the new frequence.// |
GGHHHH | 39:ab2557044815 | 47 | } |