Generates a pulses train on the order of micro-seconds.

Revision:
1:1b5cd3c20187
Parent:
0:986e03d93263
--- a/PulseGenerator.cpp	Thu Nov 08 21:58:49 2018 +0000
+++ b/PulseGenerator.cpp	Tue Jul 02 09:04:15 2019 +0000
@@ -13,6 +13,7 @@
     m_clockperiod = GCD(m_width, m_period);
     m_rt = (m_period - m_width) / m_clockperiod;
     m_ft = m_width / m_clockperiod;
+    m_wavestatus = false;
 }
 
 int8_t PulseGenerator::setWaveform(
@@ -27,7 +28,7 @@
     //  freq is required over 15 due to the range of the type of the period
     m_period= Period_us(arg_pulsefreq > 15 ? arg_pulsefreq : 16);
 
-    if(m_width > m_period) {
+    if(m_width > m_period / 2) {
         m_width  = tmp_width;
         m_period = tmp_period;
         return -1;
@@ -41,9 +42,11 @@
     static uint16_t l_counter = 0;
 
     l_counter++;
-    if(l_counter == m_rt ) {
+    if(m_wavestatus == false && l_counter >= m_rt ) {
         m_wavestatus = true;
-    } else if (l_counter >= m_ft) {
+        l_counter = 0;
+    } else 
+    if (m_wavestatus == true && l_counter >= m_ft) {
         m_wavestatus = false;
         l_counter = 0;
     }
@@ -54,13 +57,12 @@
     m_clockperiod = GCD(m_width, m_period);
     m_rt = (m_period - m_width) / m_clockperiod;
     m_ft = m_width / m_clockperiod;
-    
-    //m_ticker.attach_us(this, &PulseGenerator::generatePulseWave, m_clockperiod);
     m_ticker.attach_us(callback(this, &PulseGenerator::generatePulseWave), m_clockperiod);
 }
 
 void PulseGenerator::stopPulseWave()
 {
+    m_wavestatus = false;
     m_ticker.detach();
 }
 
@@ -72,7 +74,7 @@
     l_period = (uint16_t)(1000000 / arg_freq);
 
     //  round factorial part of 1/freq * 10^6
-    l_carried = (uint16_t)((1000000 % arg_freq > arg_freq / 2) ? 1 : 0);
+    l_carried = (uint16_t)(((1000000 % arg_freq) > (arg_freq / 2)) ? 1 : 0);
 
     return l_period + l_carried;
 }