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.
wave.cpp
00001 /** mbed wave.cpp general wave form class for function generator 00002 * Copyright (c) 2014, 2015 Motoo Tanaka @ Design Methodology Lab 00003 * 00004 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00005 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00006 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00007 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00008 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00009 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00010 * THE SOFTWARE. 00011 */ 00012 /** 00013 * wave.cpp 00014 * root class of wave form classes 00015 * because I assumed Kinetis KL25Z 00016 * for the device, time unit is 10us 00017 * there for frequencey vs cycle is 00018 * freq = 1000000 / (cycle * 10us) 00019 */ 00020 #include "wave.h" 00021 00022 int sample_time = 25 ; // 25us for single wave 00023 float vref = 3.28 ; // full range voltage on my FRDM-KL25Z 00024 00025 wave::wave(float v, int f, int d, int p) 00026 { 00027 volt( v ) ; 00028 freq( f ) ; 00029 duty( d ) ; 00030 phase( p ) ; 00031 _name = "wave" ; 00032 } 00033 00034 wave::~wave(void) { } 00035 00036 int wave::phase2pos(int ph) 00037 { 00038 int po = 0; 00039 if (_cycle != 0) { 00040 po = _cycle * ph / 360 ; 00041 } 00042 return( po ) ; 00043 } 00044 00045 int wave::pos2phase(int po) 00046 { 00047 int ph = 0 ; 00048 if (_cycle != 0) { 00049 ph = 360 * po / _cycle ; 00050 } 00051 return( ph ) ; 00052 } 00053 00054 void wave::update(void) 00055 { 00056 int tmp ; 00057 tmp = freq() ; 00058 freq(tmp) ; 00059 } 00060 00061 void wave::volt(float v) 00062 { 00063 _amp = 0xFFFF * v / vref ; 00064 } 00065 00066 float wave::volt(void) 00067 { 00068 return( _amp * vref / 0xFFFF ) ; 00069 } 00070 00071 void wave::amp(int newvalue) 00072 { 00073 _amp = newvalue ; 00074 } 00075 00076 int wave::amp(void) 00077 { 00078 return( _amp ) ; 00079 } 00080 00081 void wave::cycle(int newvalue) 00082 { 00083 _cycle = newvalue ; 00084 if (_cycle != 0) { 00085 _freq = 10000.0 / _cycle ; 00086 } else { // shall we take this as DC? 00087 _freq = 0.0 ; 00088 } 00089 } 00090 00091 int wave::cycle(void) 00092 { 00093 return( _cycle ) ; 00094 } 00095 00096 void wave::phase(int newvalue) 00097 { 00098 if (_cycle != 0) { 00099 _phase = newvalue ; 00100 _pos = phase2pos( _phase ) ; 00101 } else { 00102 _phase = 0 ; 00103 _pos = 0 ; 00104 } 00105 } 00106 00107 int wave::phase(void) 00108 { 00109 return( _phase ) ; 00110 } 00111 00112 void wave::freq(int newvalue) 00113 { 00114 _freq = newvalue ; 00115 if (_freq != 0.0) { 00116 _cycle = 1000000.0/(_freq * sample_time) ; 00117 } else { 00118 _cycle = 0 ; 00119 } 00120 } 00121 00122 int wave::freq(void) 00123 { 00124 return( _freq ) ; 00125 } 00126 00127 void wave::pos(int p) 00128 { 00129 if (_cycle != 0) { 00130 _pos = p % _cycle ; 00131 _phase = pos2phase( _pos ) ; 00132 } 00133 } 00134 00135 int wave::pos(void) 00136 { 00137 return( _pos ) ; 00138 } 00139 00140 void wave::duty(int newvalue) 00141 { 00142 _duty = newvalue ; 00143 } 00144 00145 int wave::duty(void) 00146 { 00147 return( _duty ) ; 00148 } 00149 00150 void wave::advance(int s) 00151 { 00152 if (_cycle != 0) { 00153 _pos = (_pos + s) % _cycle ; 00154 _phase = pos2phase( _pos ) ; 00155 } 00156 } 00157 00158 /** 00159 * value should return the value at phase 00160 * but in this case just returns _amp 00161 */ 00162 int wave::value(void) 00163 { 00164 return( _amp ) ; 00165 } 00166 00167 const char *wave::name(void) 00168 { 00169 return( _name ) ; 00170 }
Generated on Wed Jul 13 2022 01:58:56 by
1.7.2