Yet another implementation of wave function generator

Dependencies:   mbed vt100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wave.h Source File

wave.h

00001 /** mbed wave.h 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 #ifndef _WAVE_H_
00014 #define _WAVE_H_
00015 
00016 /** wave class basic class of wave form generator
00017  *
00018  */
00019 class wave {
00020 public:
00021 
00022 /** constructor
00023  *
00024  * @param float v amplitude in volt 0.0 ~ 3.28 for FRDM-KL25Z
00025  * @param int f Frequency in Hz (1~10000Hz)
00026  * @param int duty Duty in percent (0 ~ 100%)
00027  * @param int p Phase in degree (0 ~ 359 degree)
00028  */
00029 wave(float v, int f, int duty = 50, int p = 0) ; // volt, freq, duty, phase
00030 
00031 /** destructor */
00032 ~wave(void) ;
00033 
00034 /** mutator for _amp but in float voltage
00035  * 
00036  * @param float newvalue vlotage to assign (0.0 ~ 3.28V for FRDM-KL25Z)
00037  */
00038 void volt(float newvalue) ; // set voltage
00039 
00040 /** inspector for _amp but in float voltage
00041  *
00042  * @return float amplitude in vlot (0.0 ~ 3.28V for FRDM-KL25Z)
00043  */
00044 float volt(void) ;          // get voltage
00045 
00046 /** mutator for _amp in int value 
00047  *
00048  * @param int newvalue value for amplitude (0x0000 ~ 0xFFFF)
00049  */
00050 void amp(int newvalue) ;   // set amp
00051 
00052 /** inspector for _amp 
00053  *
00054  * @returns amplitude in unsigned short (0x0000 ~ 0xFFFF)
00055  */
00056 int  amp(void) ;           // get amp
00057 
00058 /** mutator for _cycle
00059  *
00060  * @param int newvalue length of 1 cycle of the wave 
00061  */
00062 void cycle(int newvalue) ; // set cycle
00063 
00064 /** inspector for _cycle
00065  *
00066  * @returns int length of 1 cycle in sample time unit
00067  */
00068 int  cycle(void) ;         // get cycle
00069 
00070 /** mutator for _phase
00071  *
00072  * @param int newvalue value for _phase in degree (0 ~ 359 degree)
00073  */
00074 void phase(int newvalue) ; // set phase
00075 
00076 /** inspector for _phase
00077  *
00078  * @return int phase in degree (0 ~ 359 degree)
00079  */
00080 int  phase(void) ;         // get phase
00081 
00082 /** mutator for _cycle but in frequency (Hz)
00083  *
00084  * @param int newvalue frequcney (1 ~ 10000Hz)
00085  */
00086 void freq(int newvalue) ; // set freq
00087 
00088 /** inspector for _cycle but in frequency (Hz)
00089  *
00090  * @returns int current frequency in Hz (1~10000Hz)
00091  */
00092 int freq(void) ;         // get freq
00093 
00094 /** mutator for _pos current positon in _cycle
00095  *
00096  * @param int newvalue (0 ~ _cycle-1)
00097  */
00098 void pos(int newvalue) ;   // set position
00099 
00100 /** insepector for _pos
00101  *
00102  * @returns int _pos current position in the _cycle
00103  */
00104 int  pos(void) ;           // get position
00105 
00106 /** mutator for _duty the percentage of positive value part
00107  *
00108  * @param int newvalue new duty value in percent (0~100%)
00109  */
00110 virtual void duty(int newvalue) ; // set duty
00111 
00112 /** inspector for _duty 
00113  *
00114  * @returns int duty (0~100%)
00115  */
00116 virtual int duty(void) ;           // get duty
00117 
00118 /** update internal value consistancy
00119  */
00120 virtual void update(void) ;        // adjust for time unit change
00121 
00122 /** advance position to be accessed through value() 
00123  *
00124  * @param int s steps to advance, 1 for default
00125  */
00126 virtual void advance(int s = 1) ;     // advance 1 tu
00127 
00128 /** inspector for the value at _pos
00129  *
00130  * @returns the value at _pos
00131  */
00132 virtual int  value(void) ;         // returns current value
00133 
00134 /** inspector to return class name
00135  */
00136 virtual const char *name(void) ; 
00137 
00138 protected:
00139 /** utility function to convert phase to position
00140  *
00141  * @param p phase value to be converted to the position
00142  * @returns position of specified phase
00143  */
00144 inline int phase2pos(int p) ;
00145 
00146 /** utility function to convert position to phase
00147  *
00148  * @param p position to be converted to the phase
00149  * @returns phase of specified position
00150  */
00151 inline int pos2phase(int p) ;
00152 
00153     int _amp ; /* 0~0xFFFF (12bit for Kinetis) */
00154     int _cycle ; /* 2~20000 10kHz ~ 1Hz */
00155     int _phase ; /* 0-360 degree */
00156     int _freq ; /* 1/cycle 50kHz ~ 1Hz */
00157     int _pos  ; /* position in cycle */
00158     int _duty ; /* 0-100 % */
00159     char *_name ;
00160 private:
00161 } ;
00162 
00163 extern int sample_time ; // sampling interval in us
00164 extern float vref ; // voltage at 0xFFFF
00165 
00166 #endif