Pulse consistency test using wait_us in a thread

Revision:
1:4dec185d33e9
Parent:
0:18409537564c
--- a/main.cpp	Sat Mar 30 06:37:35 2019 +0000
+++ b/main.cpp	Fri May 10 05:45:50 2019 +0000
@@ -4,221 +4,118 @@
  */
 
 #include <mbed.h>
-#include "SoftPWM.h"
 
-#define DUTY_CYCLE 0.55f
-
-#define MAX_INTENSITY 70
+#define CH_INIT             0
+#define CH_BOOST_START      1
+#define CH_BOOSTING         2
+#define CH_POST_BOOST       3
+#define CH_WAIT_200MS       4
+#define CH_WAIT_800MS       5
 
-SoftPWM LEDR(p2);
-SoftPWM LEDG(p4);
-SoftPWM LEDB(p3);
+#define SAMPLE_FLAG1 (1UL << 0)
 
-DigitalOut VEN_3V3B(p25);
+Serial pc(p6, p8);
 
-DigitalOut BOOST_EN(p14);
-PwmOut BOOST_PWM(p16);
+DigitalOut led1(LED1);  // Used to indicate boost state to user
+DigitalOut led2(LED2);  // Used to indicate boost state to user
 
-DigitalOut HB_CH1(p6);
-DigitalOut HB_CH2(p7);
+DigitalOut ch1(LED4);
 
-InterruptIn SW_ONOFF(p15);
-InterruptIn SW_MODE(p17);
-InterruptIn SW_PLUS(p13);
-InterruptIn SW_MINUS(p19);
+Timeout chStateTimeout;
+
+Timer t;
 
-Ticker stimTicker;
+Thread thread;
 
-volatile bool flip_power = false;
-volatile bool low_power = false;
+//Semaphore sem(0);
 
-int intensity = 0;
+EventFlags event_flags;
 
-volatile bool stim_mode = false;
-volatile bool stim_active = false;
-volatile bool stim_progress = true;
-short stim_count = 0;
+volatile bool ch_progress = false;
+volatile bool ch_boost_done = false;
 
-void flip_ONOFF() {
-    flip_power = true;
+// -----------------------------------------------------------------------------
+void timeoutHandler() {
+    ch_progress = true;
 }
 
-void flip_MODE() {
-  if (!low_power && !stim_mode) {
-    stim_mode = true;
-  }
-}
-
-void flip_PLUS() {
-    if (!low_power && !stim_mode) {
-        if (intensity < MAX_INTENSITY) intensity++;        
+// -----------------------------------------------------------------------------
+void chBoostThread() {
+    while (1) {
+        //sem.wait();
+        //Thread::signal_wait(0x1);
+        event_flags.wait_any(SAMPLE_FLAG1);
+        
+        t.start();
+        ch1 = 1;
+        wait_us(100);
+        ch1 = 0;
+        t.stop();
+        ch_boost_done = true;        
     }
 }
 
-void flip_MINUS() {
-    if (!low_power && !stim_mode) {
-        if (intensity > 0) intensity--;    
-    }    
-}
-
-void stimProgress() {
-    stim_progress = true;
-}
-
-void stimulate() {
-    // DAC = intensity
-    HB_CH1 = 1;
-    HB_CH2 = 0;
-    
-    wait_us(100);
-    
-    HB_CH1 = 0;
-    HB_CH2 = 1;
-    
-    wait_us(100);
-    
-    HB_CH1 = 0;
-    HB_CH2 = 0;
-    // DAC = 0     
-}
-
-void startStimProcess() {
-    stim_active = true;
-    stim_count = 0;
-    BOOST_EN = 1;
-    BOOST_PWM.write(DUTY_CYCLE);
-    stim_progress = false;
-    stimTicker.attach(&stimProgress, 1);    
-}
-
-void stopStimProcess() {
-    if (!stim_mode) return;
-    
-    stimTicker.detach();
-    
-    stim_count = 0;
-    stim_active = false;
-    stim_mode = false;
-    stim_progress = false;
-    
-    HB_CH1 = 0;
-    HB_CH2 = 0;
-    
-    BOOST_EN = 0;
-    BOOST_PWM.write(0);
-    
-    // DAC = 0                     
-}
-
+// -----------------------------------------------------------------------------
 int main()
 {
-    SW_ONOFF.mode(PullUp);
-    SW_MODE.mode(PullUp);
-    SW_PLUS.mode(PullUp);
-    SW_MINUS.mode(PullUp);
+    uint8_t ch_state = CH_INIT;
+    unsigned long dummy_count = 0, dummy_count_post = 0;
     
-    SW_ONOFF.fall(&flip_ONOFF);
-    SW_MODE.fall(&flip_MODE);
-    SW_PLUS.fall(&flip_PLUS);
-    SW_MINUS.fall(&flip_MINUS);    
+    ch1 = 0;
+    
+    thread.start(chBoostThread);    
     
-    HB_CH1 = 0;
-    HB_CH2 = 0;
-    BOOST_EN = 0;
-    VEN_3V3B = 0;    
-
-    BOOST_PWM.period_us(100);    
-    
-    LEDR.period_us(500);
-    LEDG.period_us(500);
-    LEDB.period_us(500);
-        
-    while(1)
-    {
-      if (!low_power) {
-        SW_MODE.enable_irq();
-        SW_PLUS.enable_irq();
-        SW_MINUS.enable_irq();
-        
-        VEN_3V3B = 1;
-        
-        intensity = 0;
+    while(1) {
+        switch (ch_state) {
+            case CH_INIT:
+                ch1 = 0;
+                ch_progress = false;
+                ch_boost_done = false;
+                dummy_count = 0;
+                dummy_count_post = 0;
+                ch_state = CH_BOOST_START;
+                break;
+            case CH_BOOST_START:          
+                ch_state = CH_BOOSTING;
+                led1 = 1;   // Give visual indication at start of boost stage
+                //sem.release();
+                //thread.signal_set(0x1);
+                event_flags.set(SAMPLE_FLAG1);
+            case CH_BOOSTING:
+                dummy_count++;  // measure response during 100us boost stage
+                
+                if (ch_boost_done) {
+                    ch_state = CH_POST_BOOST;
+                    chStateTimeout.attach_us(&timeoutHandler, 1000);
+                }
+                else break;
+            case CH_POST_BOOST:
+                dummy_count_post++;  // measuring response after boost stage for 1ms
+                
+                if (ch_progress) {
+                    ch_progress = false;
+                    ch_state = CH_WAIT_200MS;
+                    chStateTimeout.attach(&timeoutHandler, 0.2f);
+                }
+                break;
+            case CH_WAIT_200MS:
+                if (ch_progress) {
+                    ch_progress = false;
+                    led1 = 0;   // Turn off boost indication
+                    ch_state = CH_WAIT_800MS;
+                    chStateTimeout.attach(&timeoutHandler, 0.8f);
+                }
+                break;
+            case CH_WAIT_800MS:
+                if (ch_progress) {
+                    ch_progress = false;
+                    ch_state = CH_INIT;
+                    
+                    pc.printf("CH_BOOSTING COUNT: %lu; CH_POST_BOOST COUNT: %lu; TIME TAKEN: %d\r\n", dummy_count, dummy_count_post, t.read_us());    // print measured response;
+                    t.reset();
+                }
+                break;            
+        }
         
-        while (!low_power) {
-          if (stim_mode && !stim_active) {
-            startStimProcess();
-          }
-          
-          if (stim_active) {
-              LEDR.write(0.5f);
-              LEDG.write(0.25f);
-              LEDB.write(0);
-                  
-              if (stim_progress) {
-                  stim_progress = false;
-                  
-                  LEDR.write(0.5f);
-                  LEDG.write(0);
-                  LEDB.write(0.5f);
-                  
-                  stimulate();             
-                  
-                  if (++stim_count == 4) {
-                    stopStimProcess();
-                  }
-                  
-                  wait(0.2f);
-              }
-          }
-          else {
-            LEDR.write(0);
-            LEDG.write(0.5f);
-            LEDB.write(0);              
-          }
-          
-          if (flip_power) {
-            flip_power = false;
-            wait(0.2f);
-            low_power = true;
-          }
-          
-          __WFE();
-          __SEV();
-          __WFE();             
-        }
-      }
-      else {
-        SW_ONOFF.disable_irq();
-        SW_MODE.disable_irq();
-        SW_PLUS.disable_irq();
-        SW_MINUS.disable_irq();
-        
-        stopStimProcess();
-        
-        VEN_3V3B = 0;
-        
-        LEDR.write(0);
-        LEDG.write(0);
-        LEDB.write(0);
-        
-        wait(0.2f);
-        
-        SW_ONOFF.enable_irq();
-
-        nrf_gpio_cfg_sense_input(15, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
-        NRF_POWER->SYSTEMOFF = 1;            
-        
-        if (flip_power) {
-          flip_power = false;
-          wait(0.2f);
-          low_power = false;    
-        }
-      }
-      
-      __WFE();
-      __SEV();
-      __WFE();
-    }
-    
-    return 0;
+    } 
 }