Akifumi Takahashi / PulseGenerator
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PulseGenerator.h Source File

PulseGenerator.h

00001 /**     Pulse Fenerator header file
00002  *
00003  *  \file   PulseFenerator.h
00004  *  \author Akifumi Takahashi
00005  *  \date   2018.nov.08-
00006  *  \version    1.0.2018.nov
00007  *  \version    1.1.2019.july
00008  */
00009 #ifndef PULSEGENERATOR_H
00010 #define PULSEGENERATOR_H
00011 #include "mbed.h"
00012 
00013 /**     \class PulseGenerator
00014  *      A class which generate us-order Pulse wave
00015  *
00016  *  This generates continual pulses which can be used for such as clock
00017  *  for another continual pulses having same pulse width and frequency and
00018  *  another pulse hight.
00019  *
00020  *  start________|--|________|--|________|--|________|--|_____...stop
00021  */
00022  
00023 class PulseGenerator
00024 {
00025 public://-----------------------------------------------------------------------
00026     /**    Default constructor
00027      *
00028      *  As default, the parameters of pulse form are set as followng.
00029      * -   pulse width  = 200   [us]
00030      * -   pulse period = 5000  [us] (refering to freq = 200 [Hz])
00031      */
00032     PulseGenerator();
00033 
00034     /**     Function to set pulse width and culc period from given frequency
00035      *
00036      *  This function set a value m_clockduration, m_rt, and m_ft.
00037      *  The clock is culcurated from pulse width and period (= 1/frequency)
00038      *  using Eucledean Algorithm.
00039      */
00040     int8_t setWaveform(
00041         const uint16_t arg_pulseWidth,///< required in [us]
00042         const uint16_t arg_pulseFreq  ///< required in [Hz] bigger than 15 Hz
00043     );
00044 
00045     /**     Function to start/resume generating Pulse wave
00046      *
00047      *  This function culculates a base clock freq (e.g. pulse periodand) 
00048      *  and a pulse rising/falling timing in the clock with which to generate 
00049      *  pulse wave. 
00050      *  And then exe TICKER.attach().
00051      */
00052     void startPulseWave();
00053 
00054     /**     Function to stop/pose generating pulse wave
00055      *
00056      *  This function exe TICKER.detach().
00057      */
00058     void stopPulseWave();
00059 
00060     /**     Function to refer whther the pulse wave is H xor L
00061      *  \retval (bool) H between pulse rising and then falling
00062      *  \retval (bool) L between pulse falling and then rising
00063      */
00064     bool getState();
00065     
00066 private://----------------------------------------------------------------------
00067     uint16_t m_width;       ///< Defalt: 200 [us]; Set in setWaveform()
00068     uint16_t m_period;      ///< culcurated from frequency given with setWaveform()
00069     bool     m_wavestatus;
00070     uint16_t m_clockperiod; ///< base clock's period
00071     uint16_t m_rt;   ///< Pulse rising timing in the clock (What times clock counting is required till a pulse rises)
00072     uint16_t m_ft;   ///< Pulse falling timing in the clock (What times clock counting is required till a pulse falls)
00073     Ticker   m_ticker;
00074 
00075     ///     Pulse Generate Hundler
00076     void generatePulseWave();
00077 };
00078 
00079 //  
00080 //  Inline Implementation
00081 //  ----------------------------------------------------------------------------
00082 inline bool PulseGenerator::getState()
00083 {
00084     return m_wavestatus;
00085 }
00086 
00087 //  
00088 //  Auxiliry Function
00089 //  ----------------------------------------------------------------------------
00090 /**     Culcurate Grate Common Divisor by Eukleides' Algorithm
00091  *
00092  *  \param[in]  a value included in Natural number excluding 0
00093  *  \param[in]  a value included in Natural number excluding 0
00094  *  \retval GCD
00095  */
00096 uint16_t GCD(uint16_t, uint16_t);
00097 
00098 /**     Culcurate Period in [us] from frequency in [Hz]
00099  *
00100  *  \param[in]  Frequency [Hz]
00101  *  \retval Period [us]
00102  */
00103 uint16_t Period_us(const uint16_t arg_freq);
00104 #endif