fast-feedback virtual target task code on STM Nucleo

Dependencies:   mbed

Revision:
27:b31ea8d74f9e
Parent:
26:b4421d1ee57a
Child:
28:797536a42b9f
--- a/states.cpp	Thu Jul 05 20:15:37 2018 +0000
+++ b/states.cpp	Sat Jul 21 00:36:38 2018 +0000
@@ -1,6 +1,7 @@
 #include "states.h"
 #include "rig.h"
 #include "automaton.h"
+#include "scheduler.h"
 #include "IO.h"
 
 #define STATE_IS_LOGGED
@@ -13,6 +14,7 @@
 
 void finalize() {
     trial.markTrialEnd();
+    scheduler::reset();
 }
 
 void Delay::setup() {
@@ -25,10 +27,10 @@
     switch (task.mode.value) {
     case Pair:
     case MotionAlt:
-        stateTimeout.attach_us(&automaton::jump<Delay,Cued>, ms_to_us(trial.delay_dur_ms + task.prep_dur_ms.value));
+        scheduler::set(ms_to_us(trial.delay_dur_ms + task.prep_dur_ms.value), &automaton::jump<Delay,Cued>);
         break;
     default:
-        stateTimeout.attach_us(&automaton::jump<Delay,Prepare>, ms_to_us(trial.delay_dur_ms));
+        scheduler::set(ms_to_us(trial.delay_dur_ms), &automaton::jump<Delay,Prepare>);
     }
     
     trial.markTrialStart();
@@ -47,7 +49,7 @@
     lickIn.attach(&automaton::jump<Prepare,Abort>);
     
     // set timeout for the next state
-    stateTimeout.attach_us(&automaton::jump<Prepare,Cued>, ms_to_us(task.prep_dur_ms.value));
+    scheduler::set(ms_to_us(task.prep_dur_ms.value), &automaton::jump<Prepare,Cued>);
 }
 
 void Prepare::teardown() {
@@ -64,37 +66,18 @@
     case Pair:
     case Report:
     case Associate:
+    case Motion:
         // wait for the visual cue to flag "cued"
-        break;
-    case Motion:
-    case MotionAlt:
-        trial.flag.cued = true;
-        break;
-    }
-    
-    // configure lick handler
-    if (task.mode.value != MotionAlt) {
+        // use visual feedback trigger as the "gate" response
         // licking without a visual "go-cue" will be considered a "catch" response
+        gateIn.attach(&Cued::gate);
         lickIn.attach(&automaton::jump<Cued,WithResp>);
-    } else {
-        // for MotionAlt
-    }
-    
-    // configure gate handler
-    // use visual feedback trigger as the "gate" response
-    switch (task.mode.value) {
-    case Pair:
-    case Report:
-    case Associate:
-    case Motion:
-        gateIn.attach(&Cued::gate);
         break;
     case MotionAlt:
+        // jumps directly to WithResp with the visual cue
+        Cued::gate();
         gateIn.attach(&automaton::jump<Cued,WithResp>);
         break;
-    default:
-        // do nothing
-        break;
     }
     
     // start cue output
@@ -102,17 +85,18 @@
     audioOut.run();
     
     // sets the timeout for the next state
-    stateTimeout.attach_us(&automaton::jump<Cued,NoResp>, trial.cued_dur_us);
+    scheduler::set(trial.cued_dur_us, &automaton::jump<Cued,NoResp>);
 }
 
 void Cued::gate() {
     gateIn.detach();
     lickIn.attach(&automaton::jump<Cued,WithResp>);
-    trial.flag.cued = true; // in case it has not been (as in Pair/Report/Associate)
+    trial.flag.cued = true; // in case it has not been (i.e. other than MotionAlt)
 }
 
 void Cued::teardown() {
     lickIn.detach();
+    gateIn.detach();
     
     // end cue output
     switch (task.mode.value) {
@@ -135,7 +119,7 @@
     gateIn.detach(); // if any
     trial.markEndOfWait();
     trial.flag.reset     = true;
-    stateTimeout.attach_us(&automaton::done<Abort>, ms_to_us(task.post_dur_ms.value));
+    scheduler::set(ms_to_us(task.post_dur_ms.value), &automaton::done<Abort>);
 }
 
 void Abort::teardown() {
@@ -155,7 +139,7 @@
         rewardOut.start();
     }
     
-    stateTimeout.attach_us(&automaton::done<WithResp>, ms_to_us(task.post_dur_ms.value));
+    scheduler::set(ms_to_us(task.post_dur_ms.value), &automaton::done<WithResp>);
 }
 
 void WithResp::teardown() {
@@ -172,7 +156,7 @@
         rewardOut.start();
     }
     
-    stateTimeout.attach_us(&automaton::done<NoResp>, ms_to_us(task.post_dur_ms.value));
+    scheduler::set(ms_to_us(task.post_dur_ms.value), &automaton::done<NoResp>);
 }
 void NoResp::teardown() {
     finalize();
@@ -185,7 +169,8 @@
     rewardOut.setOnset(0);
     rewardOut.setDuration(ms_to_us(task.reward_dur_ms.value));
     rewardOut.start();
-    stateTimeout.attach_us(&automaton::done<TestReward>, ms_to_us(task.reward_dur_ms.value+100));
+    
+    scheduler::set(ms_to_us(task.reward_dur_ms.value+100), &automaton::done<TestReward>);
 }
 
 void TestReward::teardown() {