fast-feedback virtual target task code on STM Nucleo

Dependencies:   mbed

Revision:
9:e136394bdb39
Parent:
8:973dcd190672
Child:
10:7c216d528c35
--- a/states.cpp	Thu May 31 15:25:37 2018 +0000
+++ b/states.cpp	Mon Jun 18 10:43:45 2018 +0000
@@ -33,9 +33,19 @@
     }
 }
 
+void turnOff_trialStart() {
+    trialStart.write(0);
+}
+
+void stopReward() {
+    rewardOut.write(0);
+}
+
 void finalize() {
-    taskOut.write(0);
     cueOut.write(0); // if any
+    trialEnd.write(1);
+    wait_us(ms_to_us(TRIGGER_DUR_MS));
+    
     trial.writeToSerial();
     timer.stop();
     timer.reset();
@@ -48,9 +58,8 @@
     trial.reset(task);
     
     // configure the flags
-    taskOut.write(1);
     cueOut.write(0);
-    if ( (task.mode.value == Pair) || (task.mode.value == Report) ) {
+    if ( (task.mode.value == WithCue) || (task.mode.value == Report) ) {
         enableOut.write(1);
     } else {
         enableOut.write(0);
@@ -65,12 +74,21 @@
     logged = true;
     
     // sets the timeout for the next state
-    if (task.mode.value == Pair) {
-        stateTimeout.attach_us(&automaton::jump<Delay,Cued>, ms_to_us(trial.delay_dur_ms));
-    } else {
+    switch (task.mode.value) {
+    case Pair:
+        stateTimeout.attach_us(&automaton::jump<Delay,Paired>, ms_to_us(trial.delay_dur_ms + task.prep_dur_ms.value));
+        break;
+    case WithCue:
+        stateTimeout.attach_us(&automaton::jump<Delay,Cued>, ms_to_us(trial.delay_dur_ms + task.prep_dur_ms.value));
+        break;
+    default:
         stateTimeout.attach_us(&automaton::jump<Delay,Prepare>, ms_to_us(trial.delay_dur_ms));
     }
     
+    // trigger trialStart
+    trialStart.write(1);
+    triggerTimeout.attach_us(&turnOff_trialStart, ms_to_us(TRIGGER_DUR_MS));
+    
     // update the timestamp
     trial.starting = timer.read_ms();
     timer.start();
@@ -98,6 +116,24 @@
     lickhandler = 0;
 }
 
+void Paired::setup() {
+    LOGSTATE(Paired)
+    trial.response |= TrialFlags::Cues;
+    lickhandler     = &automaton::jump<Paired,WithResp>;
+    
+    // reward & visual feedback
+    enableOut.write(1);
+    
+    rewardOut.write(1);
+    rewardTimeout.attach_us(&stopReward, ms_to_us(task.reward_ms.value));
+    
+    stateTimeout.attach_us(&automaton::jump<Paired,NoResp>, ms_to_us(task.cue_dur_ms.value));
+}
+
+void Paired::teardown() {
+    enableOut.write(0);
+}
+
 void Cued::setup() {
     LOGSTATE(Cued)
     
@@ -106,7 +142,7 @@
     }
     
     // configure the interrupts
-    if (task.mode.value == Pair) {
+    if (task.mode.value == WithCue) {
         whiskhandler    = &automaton::jump<Cued,WithResp>;
         trial.response |= TrialFlags::Cues;
         lickhandler     = 0;
@@ -165,17 +201,15 @@
     whiskhandler = 0;
     lickhandler  = 0;
     
-    // open/close the valve
-    rewardOut.write(1);
-    rewardTimeout.attach_us(&WithResp::stopReward, ms_to_us(task.reward_ms.value));
+    if (task.mode.value != Pair) {
+        // open/close the valve
+        rewardOut.write(1);
+        rewardTimeout.attach_us(&stopReward, ms_to_us(task.reward_ms.value));
+    }
     
     stateTimeout.attach_us(&automaton::done<WithResp>, ms_to_us(task.post_dur_ms.value));
 }
 
-void WithResp::stopReward() {
-    rewardOut.write(0);
-}
-
 void WithResp::teardown() {
     finalize();
 }
@@ -196,7 +230,7 @@
     
     // open/close the valve
     rewardOut.write(1);
-    rewardTimeout.attach_us(&WithResp::stopReward, ms_to_us(task.reward_ms.value));
+    rewardTimeout.attach_us(&stopReward, ms_to_us(task.reward_ms.value));
     stateTimeout.attach_us(&automaton::done<TestReward>, ms_to_us(task.reward_ms.value+20));
 }