fast-feedback virtual target task code on STM Nucleo

Dependencies:   mbed

Committer:
gwappa
Date:
Thu Jul 05 20:15:37 2018 +0000
Revision:
26:b4421d1ee57a
Parent:
25:56c4b22ec034
Child:
27:b31ea8d74f9e
transform event feature into the InterruptHandler class; organize files a little bit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gwappa 4:fcf597f82632 1 #include "states.h"
gwappa 4:fcf597f82632 2 #include "rig.h"
gwappa 4:fcf597f82632 3 #include "automaton.h"
gwappa 4:fcf597f82632 4 #include "IO.h"
gwappa 4:fcf597f82632 5
gwappa 4:fcf597f82632 6 #define STATE_IS_LOGGED
gwappa 4:fcf597f82632 7
gwappa 4:fcf597f82632 8 #ifdef STATE_IS_LOGGED
gwappa 4:fcf597f82632 9 #define LOGSTATE(S) IO::info(#S);
gwappa 4:fcf597f82632 10 #else
gwappa 4:fcf597f82632 11 #define LOGSTATE(S)
gwappa 4:fcf597f82632 12 #endif
gwappa 4:fcf597f82632 13
gwappa 4:fcf597f82632 14 void finalize() {
gwappa 11:897ecd5413e0 15 trial.markTrialEnd();
gwappa 4:fcf597f82632 16 }
gwappa 4:fcf597f82632 17
gwappa 4:fcf597f82632 18 void Delay::setup() {
gwappa 4:fcf597f82632 19 LOGSTATE(Delay)
gwappa 4:fcf597f82632 20
gwappa 4:fcf597f82632 21 // initialize the trial-related params
gwappa 4:fcf597f82632 22 trial.reset(task);
gwappa 4:fcf597f82632 23
gwappa 11:897ecd5413e0 24 // set up the timeout for the next state
gwappa 9:e136394bdb39 25 switch (task.mode.value) {
gwappa 9:e136394bdb39 26 case Pair:
gwappa 16:33c17c62840e 27 case MotionAlt:
gwappa 16:33c17c62840e 28 stateTimeout.attach_us(&automaton::jump<Delay,Cued>, ms_to_us(trial.delay_dur_ms + task.prep_dur_ms.value));
gwappa 16:33c17c62840e 29 break;
gwappa 9:e136394bdb39 30 default:
gwappa 7:6744ec9ccc25 31 stateTimeout.attach_us(&automaton::jump<Delay,Prepare>, ms_to_us(trial.delay_dur_ms));
gwappa 7:6744ec9ccc25 32 }
gwappa 4:fcf597f82632 33
gwappa 11:897ecd5413e0 34 trial.markTrialStart();
gwappa 4:fcf597f82632 35 }
gwappa 4:fcf597f82632 36
gwappa 4:fcf597f82632 37 void Delay::teardown() {
gwappa 4:fcf597f82632 38 // do nothing
gwappa 4:fcf597f82632 39 }
gwappa 4:fcf597f82632 40
gwappa 7:6744ec9ccc25 41 void Prepare::setup() {
gwappa 7:6744ec9ccc25 42 // mostly the same with for Delay
gwappa 7:6744ec9ccc25 43 // except that the animal cannot lick freely
gwappa 7:6744ec9ccc25 44 LOGSTATE(Prepare)
gwappa 7:6744ec9ccc25 45
gwappa 7:6744ec9ccc25 46 // configure the interrupts
gwappa 26:b4421d1ee57a 47 lickIn.attach(&automaton::jump<Prepare,Abort>);
gwappa 7:6744ec9ccc25 48
gwappa 7:6744ec9ccc25 49 // set timeout for the next state
gwappa 7:6744ec9ccc25 50 stateTimeout.attach_us(&automaton::jump<Prepare,Cued>, ms_to_us(task.prep_dur_ms.value));
gwappa 7:6744ec9ccc25 51 }
gwappa 7:6744ec9ccc25 52
gwappa 7:6744ec9ccc25 53 void Prepare::teardown() {
gwappa 7:6744ec9ccc25 54 // de-register lick inhibition
gwappa 26:b4421d1ee57a 55 lickIn.detach();
gwappa 9:e136394bdb39 56 }
gwappa 9:e136394bdb39 57
gwappa 4:fcf597f82632 58 void Cued::setup() {
gwappa 4:fcf597f82632 59 LOGSTATE(Cued)
gwappa 4:fcf597f82632 60
gwappa 11:897ecd5413e0 61 trial.markEndOfWait();
gwappa 7:6744ec9ccc25 62
gwappa 21:e51733fc1c36 63 switch (task.mode.value) {
gwappa 21:e51733fc1c36 64 case Pair:
gwappa 21:e51733fc1c36 65 case Report:
gwappa 21:e51733fc1c36 66 case Associate:
gwappa 21:e51733fc1c36 67 // wait for the visual cue to flag "cued"
gwappa 21:e51733fc1c36 68 break;
gwappa 21:e51733fc1c36 69 case Motion:
gwappa 21:e51733fc1c36 70 case MotionAlt:
gwappa 19:50663f8815b8 71 trial.flag.cued = true;
gwappa 21:e51733fc1c36 72 break;
gwappa 19:50663f8815b8 73 }
gwappa 15:20f7f737c256 74
gwappa 17:0b241aa1f5b6 75 // configure lick handler
gwappa 16:33c17c62840e 76 if (task.mode.value != MotionAlt) {
gwappa 17:0b241aa1f5b6 77 // licking without a visual "go-cue" will be considered a "catch" response
gwappa 26:b4421d1ee57a 78 lickIn.attach(&automaton::jump<Cued,WithResp>);
gwappa 17:0b241aa1f5b6 79 } else {
gwappa 17:0b241aa1f5b6 80 // for MotionAlt
gwappa 16:33c17c62840e 81 }
gwappa 11:897ecd5413e0 82
gwappa 17:0b241aa1f5b6 83 // configure gate handler
gwappa 26:b4421d1ee57a 84 // use visual feedback trigger as the "gate" response
gwappa 17:0b241aa1f5b6 85 switch (task.mode.value) {
gwappa 21:e51733fc1c36 86 case Pair:
gwappa 17:0b241aa1f5b6 87 case Report:
gwappa 17:0b241aa1f5b6 88 case Associate:
gwappa 26:b4421d1ee57a 89 case Motion:
gwappa 26:b4421d1ee57a 90 gateIn.attach(&Cued::gate);
gwappa 17:0b241aa1f5b6 91 break;
gwappa 17:0b241aa1f5b6 92 case MotionAlt:
gwappa 26:b4421d1ee57a 93 gateIn.attach(&automaton::jump<Cued,WithResp>);
gwappa 26:b4421d1ee57a 94 break;
gwappa 26:b4421d1ee57a 95 default:
gwappa 21:e51733fc1c36 96 // do nothing
gwappa 17:0b241aa1f5b6 97 break;
gwappa 7:6744ec9ccc25 98 }
gwappa 7:6744ec9ccc25 99
gwappa 7:6744ec9ccc25 100 // start cue output
gwappa 21:e51733fc1c36 101 visualOut.run();
gwappa 21:e51733fc1c36 102 audioOut.run();
gwappa 4:fcf597f82632 103
gwappa 4:fcf597f82632 104 // sets the timeout for the next state
gwappa 24:e236faf66935 105 stateTimeout.attach_us(&automaton::jump<Cued,NoResp>, trial.cued_dur_us);
gwappa 4:fcf597f82632 106 }
gwappa 4:fcf597f82632 107
gwappa 7:6744ec9ccc25 108 void Cued::gate() {
gwappa 26:b4421d1ee57a 109 gateIn.detach();
gwappa 26:b4421d1ee57a 110 lickIn.attach(&automaton::jump<Cued,WithResp>);
gwappa 26:b4421d1ee57a 111 trial.flag.cued = true; // in case it has not been (as in Pair/Report/Associate)
gwappa 7:6744ec9ccc25 112 }
gwappa 7:6744ec9ccc25 113
gwappa 4:fcf597f82632 114 void Cued::teardown() {
gwappa 26:b4421d1ee57a 115 lickIn.detach();
gwappa 20:4c06d3041337 116
gwappa 7:6744ec9ccc25 117 // end cue output
gwappa 20:4c06d3041337 118 switch (task.mode.value) {
gwappa 20:4c06d3041337 119 case Report:
gwappa 20:4c06d3041337 120 case Associate:
gwappa 12:06ea96546af1 121 visualOut.stop();
gwappa 7:6744ec9ccc25 122 }
gwappa 20:4c06d3041337 123
gwappa 20:4c06d3041337 124 switch (task.mode.value) {
gwappa 20:4c06d3041337 125 case Associate:
gwappa 20:4c06d3041337 126 case Motion:
gwappa 20:4c06d3041337 127 case MotionAlt:
gwappa 20:4c06d3041337 128 audioOut.stop();
gwappa 20:4c06d3041337 129 }
gwappa 7:6744ec9ccc25 130 }
gwappa 7:6744ec9ccc25 131
gwappa 7:6744ec9ccc25 132 void Abort::setup() {
gwappa 7:6744ec9ccc25 133 LOGSTATE(Abort)
gwappa 26:b4421d1ee57a 134 lickIn.detach(); // if any
gwappa 26:b4421d1ee57a 135 gateIn.detach(); // if any
gwappa 11:897ecd5413e0 136 trial.markEndOfWait();
gwappa 19:50663f8815b8 137 trial.flag.reset = true;
gwappa 8:973dcd190672 138 stateTimeout.attach_us(&automaton::done<Abort>, ms_to_us(task.post_dur_ms.value));
gwappa 7:6744ec9ccc25 139 }
gwappa 7:6744ec9ccc25 140
gwappa 7:6744ec9ccc25 141 void Abort::teardown() {
gwappa 7:6744ec9ccc25 142 finalize();
gwappa 4:fcf597f82632 143 }
gwappa 4:fcf597f82632 144
gwappa 4:fcf597f82632 145 void WithResp::setup() {
gwappa 4:fcf597f82632 146 LOGSTATE(WithResp)
gwappa 4:fcf597f82632 147
gwappa 19:50663f8815b8 148 trial.flag.responded = true;
gwappa 26:b4421d1ee57a 149 lickIn.detach(); // if any
gwappa 26:b4421d1ee57a 150 gateIn.detach(); // if any
gwappa 4:fcf597f82632 151
gwappa 22:41163fb3fdc6 152 // open/close the valve, only when the cue was there
gwappa 22:41163fb3fdc6 153 // (for Pair, reward will be there no matter the response)
gwappa 20:4c06d3041337 154 if ((task.mode.value == Pair) || trial.flag.cued ) {
gwappa 11:897ecd5413e0 155 rewardOut.start();
gwappa 9:e136394bdb39 156 }
gwappa 4:fcf597f82632 157
gwappa 5:849446d19406 158 stateTimeout.attach_us(&automaton::done<WithResp>, ms_to_us(task.post_dur_ms.value));
gwappa 4:fcf597f82632 159 }
gwappa 4:fcf597f82632 160
gwappa 4:fcf597f82632 161 void WithResp::teardown() {
gwappa 4:fcf597f82632 162 finalize();
gwappa 4:fcf597f82632 163 }
gwappa 4:fcf597f82632 164
gwappa 4:fcf597f82632 165 void NoResp::setup() {
gwappa 4:fcf597f82632 166 LOGSTATE(NoResp)
gwappa 26:b4421d1ee57a 167 lickIn.detach(); // if any
gwappa 26:b4421d1ee57a 168 gateIn.detach(); // if any
gwappa 4:fcf597f82632 169
gwappa 22:41163fb3fdc6 170 // no reward here, except for Pair
gwappa 22:41163fb3fdc6 171 if (task.mode.value == Pair) {
gwappa 22:41163fb3fdc6 172 rewardOut.start();
gwappa 22:41163fb3fdc6 173 }
gwappa 7:6744ec9ccc25 174
gwappa 5:849446d19406 175 stateTimeout.attach_us(&automaton::done<NoResp>, ms_to_us(task.post_dur_ms.value));
gwappa 4:fcf597f82632 176 }
gwappa 4:fcf597f82632 177 void NoResp::teardown() {
gwappa 4:fcf597f82632 178 finalize();
gwappa 8:973dcd190672 179 }
gwappa 8:973dcd190672 180
gwappa 8:973dcd190672 181 void TestReward::setup() {
gwappa 8:973dcd190672 182 LOGSTATE(TestReward)
gwappa 8:973dcd190672 183
gwappa 8:973dcd190672 184 // open/close the valve
gwappa 16:33c17c62840e 185 rewardOut.setOnset(0);
gwappa 16:33c17c62840e 186 rewardOut.setDuration(ms_to_us(task.reward_dur_ms.value));
gwappa 11:897ecd5413e0 187 rewardOut.start();
gwappa 16:33c17c62840e 188 stateTimeout.attach_us(&automaton::done<TestReward>, ms_to_us(task.reward_dur_ms.value+100));
gwappa 8:973dcd190672 189 }
gwappa 8:973dcd190672 190
gwappa 8:973dcd190672 191 void TestReward::teardown() {
gwappa 8:973dcd190672 192 // do nothing
gwappa 4:fcf597f82632 193 }