JIAWEI ZHANG / Mbed 2 deprecated ele350ku

Dependencies:   mbed

Dependents:   Exercise8_1-2-3

Fork of ele350 by JIAWEI ZHANG

Committer:
GGHHHH
Date:
Fri Dec 18 16:27:23 2015 +0000
Revision:
54:aee1b44e62ec
Parent:
50:daa64b483569
h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GGHHHH 29:8ec9451b1a59 1 #include "square_app.h"
GGHHHH 29:8ec9451b1a59 2
GGHHHH 54:aee1b44e62ec 3 //Constructor. Calls the bass class (App) constructor and creact an AnalogOut object.//
GGHHHH 29:8ec9451b1a59 4 SquareApp::SquareApp(Serial* serial) : App("Square 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 SquareApp::start() //(Overriden) Method that runs when the app starts.//
GGHHHH 29:8ec9451b1a59 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 SquareApp::run() //(Overriden) Method that is called repeatedly when the app starts.//
GGHHHH 29:8ec9451b1a59 23 {
GGHHHH 54:aee1b44e62ec 24 float p; //Define the float p.//
GGHHHH 54:aee1b44e62ec 25 p = (this->frequence*timer.read())-floor(this->frequence*timer.read()); //p expression.//
GGHHHH 54:aee1b44e62ec 26 if ( p < 0.5f ) { //Define the mini p value and check it.//
GGHHHH 54:aee1b44e62ec 27 this->analogOut->write(0); //If < mini value, output 0.//
GGHHHH 54:aee1b44e62ec 28 } else { this->analogOut->write(this->amplitude/3.0f);} //Set the output to the sin expression.//
GGHHHH 47:124bc7d77d5a 29
GGHHHH 54:aee1b44e62ec 30 if ( this->frequence > 50.0f ) { //Define the max frequence value and check it.//
GGHHHH 54:aee1b44e62ec 31 this->analogOut->write(0); //If >max value, output 0.//
GGHHHH 50:daa64b483569 32 }
GGHHHH 29:8ec9451b1a59 33 }
GGHHHH 29:8ec9451b1a59 34
GGHHHH 54:aee1b44e62ec 35 void SquareApp::stop() //(Overriden) Method that runs when the app stops.//
GGHHHH 29:8ec9451b1a59 36 {
GGHHHH 54:aee1b44e62ec 37 App::stop(); //Call bass class's stop() method.//
GGHHHH 54:aee1b44e62ec 38 this->analogOut->write(0.0f); //Set output to 0V.//
GGHHHH 54:aee1b44e62ec 39 this->timer.stop(); //Stop the time and reset it.//
GGHHHH 29:8ec9451b1a59 40 this->timer.reset();
GGHHHH 34:498a218cf53e 41 }
GGHHHH 54:aee1b44e62ec 42 void SquareApp::setamplitude(float newamplitude) //(Overriden) Method that could set amplitude.//
GGHHHH 34:498a218cf53e 43 {
GGHHHH 54:aee1b44e62ec 44 this->amplitude = newamplitude; //Set the new amplitude.//
GGHHHH 40:ca1789c40924 45 }
GGHHHH 54:aee1b44e62ec 46 void SquareApp::setfrequence(float newfrequence) //(Overriden) Method that could set frequence.//
GGHHHH 40:ca1789c40924 47 {
GGHHHH 54:aee1b44e62ec 48 this->frequence = newfrequence; //Set the new frequence.//
GGHHHH 40:ca1789c40924 49 }