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.
generate.cpp
00001 #include "main.h" 00002 #include "rtos.h" 00003 00004 DigitalOut greenLed (PD_12); 00005 DigitalOut orangeLed (PD_13); 00006 DigitalOut redLed (PD_14); 00007 DigitalOut blueLed (PD_15); 00008 00009 AnalogOut *aout; 00010 Timer timer; 00011 const float pi = 3.141592653589793238462f; 00012 00013 00014 //Mutex signalMutex; 00015 bool signalHasChanged; 00016 Signal signalToGenerate; 00017 00018 void updateSignal (Signal signal) { 00019 //signalMutex.lock(); // synchronise access with generateSignalFunc() 00020 signalToGenerate = signal; // specify that the signal 00021 signalHasChanged = 1; // to generate has changed 00022 //signalMutex.unlock(); 00023 } 00024 00025 00026 void generateSignalFunc (void const* args) { 00027 enum SIGNAL_TYPE type; 00028 float a, f, p, t; 00029 timer.start(); 00030 00031 while(1) { 00032 if(signalHasChanged) { 00033 //signalMutex.lock(); // prevent changes to occur while 00034 type = signalToGenerate.type; // signal parameters are being read 00035 a = signalToGenerate.amplitude/3.3f; // scale from [0,3.3] to [0,1] 00036 f = signalToGenerate.frequency; 00037 signalHasChanged = 0; // reset flag 00038 //signalMutex.unlock(); // release lock 00039 greenLed = type==CONSTANT; 00040 orangeLed = type==SINE; 00041 redLed = type==SQUARE; 00042 } 00043 t = timer.read(); // get time t in seconds 00044 if(t>20) timer.reset(); // accuracy drops as t increases 00045 switch(type) { 00046 case CONSTANT: 00047 *aout = a; 00048 break; 00049 case SINE: 00050 *aout = a*0.5f*(1+sin(f*pi*2*t)); 00051 break; 00052 case SQUARE: 00053 p = t*f - floor(t*f); 00054 *aout = (p < 0.5f) ? 0 : a; 00055 break; 00056 default: *aout = 0; 00057 } 00058 } 00059 } 00060
Generated on Thu Jul 14 2022 20:41:36 by
1.7.2