Keisuke Sehara / Mbed 2 deprecated STM32_Whisking

Dependencies:   mbed

Committer:
gwappa
Date:
Mon Jun 25 12:10:31 2018 +0000
Revision:
12:06ea96546af1
Parent:
11:897ecd5413e0
Child:
13:8ea85a33e37a
extend Pulse class to accommodate the usage for visual cue output

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 12:06ea96546af1 4 #include "events.h"
gwappa 4:fcf597f82632 5 #include "IO.h"
gwappa 4:fcf597f82632 6
gwappa 4:fcf597f82632 7 #define STATE_IS_LOGGED
gwappa 4:fcf597f82632 8
gwappa 4:fcf597f82632 9 #ifdef STATE_IS_LOGGED
gwappa 4:fcf597f82632 10 #define LOGSTATE(S) IO::info(#S);
gwappa 4:fcf597f82632 11 #else
gwappa 4:fcf597f82632 12 #define LOGSTATE(S)
gwappa 4:fcf597f82632 13 #endif
gwappa 4:fcf597f82632 14
gwappa 11:897ecd5413e0 15 void tickBuzzer() {
gwappa 11:897ecd5413e0 16 audioOut = !audioOut;
gwappa 9:e136394bdb39 17 }
gwappa 9:e136394bdb39 18
gwappa 4:fcf597f82632 19 void finalize() {
gwappa 11:897ecd5413e0 20 audioOut.write(0); // if any
gwappa 11:897ecd5413e0 21 trial.markTrialEnd();
gwappa 4:fcf597f82632 22 }
gwappa 4:fcf597f82632 23
gwappa 4:fcf597f82632 24 void Delay::setup() {
gwappa 4:fcf597f82632 25 LOGSTATE(Delay)
gwappa 4:fcf597f82632 26
gwappa 4:fcf597f82632 27 // initialize the trial-related params
gwappa 4:fcf597f82632 28 trial.reset(task);
gwappa 12:06ea96546af1 29 // TODO: reset visualOut with the nice parameters
gwappa 12:06ea96546af1 30 // visualOut.reset(task, (task.mode.value==Associate)||(task.mode.value==Motion));
gwappa 12:06ea96546af1 31 if ((task.mode.value == Report) || (task.mode.value == Associate)) {
gwappa 12:06ea96546af1 32 visualOut.attachTurnOnCallback(&events::gate);
gwappa 12:06ea96546af1 33 }
gwappa 11:897ecd5413e0 34 audioOut.write(0);
gwappa 11:897ecd5413e0 35 rewardOut.setDuration(ms_to_us(task.reward_ms.value));
gwappa 12:06ea96546af1 36 events::setup(true);
gwappa 4:fcf597f82632 37
gwappa 11:897ecd5413e0 38 // set up the timeout for the next state
gwappa 9:e136394bdb39 39 switch (task.mode.value) {
gwappa 9:e136394bdb39 40 case Pair:
gwappa 9:e136394bdb39 41 stateTimeout.attach_us(&automaton::jump<Delay,Paired>, ms_to_us(trial.delay_dur_ms + task.prep_dur_ms.value));
gwappa 9:e136394bdb39 42 break;
gwappa 9:e136394bdb39 43 default:
gwappa 7:6744ec9ccc25 44 stateTimeout.attach_us(&automaton::jump<Delay,Prepare>, ms_to_us(trial.delay_dur_ms));
gwappa 7:6744ec9ccc25 45 }
gwappa 4:fcf597f82632 46
gwappa 11:897ecd5413e0 47 trial.markTrialStart();
gwappa 4:fcf597f82632 48 }
gwappa 4:fcf597f82632 49
gwappa 4:fcf597f82632 50 void Delay::teardown() {
gwappa 4:fcf597f82632 51 // do nothing
gwappa 4:fcf597f82632 52 }
gwappa 4:fcf597f82632 53
gwappa 11:897ecd5413e0 54 void Paired::setup() {
gwappa 11:897ecd5413e0 55 LOGSTATE(Paired)
gwappa 11:897ecd5413e0 56 trial.markEndOfWait();
gwappa 11:897ecd5413e0 57
gwappa 12:06ea96546af1 58 trial.response |= TrialFlags::Cues;
gwappa 12:06ea96546af1 59 events::lickhandler = &automaton::jump<Paired,WithResp>;
gwappa 11:897ecd5413e0 60
gwappa 11:897ecd5413e0 61 // reward & visual feedback
gwappa 11:897ecd5413e0 62 visualOut.direct(true);
gwappa 11:897ecd5413e0 63 rewardOut.start();
gwappa 11:897ecd5413e0 64
gwappa 11:897ecd5413e0 65 stateTimeout.attach_us(&automaton::jump<Paired,NoResp>, ms_to_us(task.aud_dur_ms.value));
gwappa 11:897ecd5413e0 66 }
gwappa 11:897ecd5413e0 67
gwappa 11:897ecd5413e0 68 void Paired::teardown() {
gwappa 11:897ecd5413e0 69 visualOut.direct(false);
gwappa 12:06ea96546af1 70 events::lickhandler = 0;
gwappa 11:897ecd5413e0 71 }
gwappa 11:897ecd5413e0 72
gwappa 7:6744ec9ccc25 73 void Prepare::setup() {
gwappa 7:6744ec9ccc25 74 // mostly the same with for Delay
gwappa 7:6744ec9ccc25 75 // except that the animal cannot lick freely
gwappa 7:6744ec9ccc25 76 LOGSTATE(Prepare)
gwappa 7:6744ec9ccc25 77
gwappa 7:6744ec9ccc25 78 // configure the interrupts
gwappa 12:06ea96546af1 79 // no whisk handler
gwappa 12:06ea96546af1 80 events::lickhandler = &automaton::jump<Prepare,Abort>;
gwappa 7:6744ec9ccc25 81
gwappa 7:6744ec9ccc25 82 // set timeout for the next state
gwappa 7:6744ec9ccc25 83 stateTimeout.attach_us(&automaton::jump<Prepare,Cued>, ms_to_us(task.prep_dur_ms.value));
gwappa 7:6744ec9ccc25 84 }
gwappa 7:6744ec9ccc25 85
gwappa 7:6744ec9ccc25 86 void Prepare::teardown() {
gwappa 7:6744ec9ccc25 87 // de-register lick inhibition
gwappa 12:06ea96546af1 88 events::lickhandler = 0;
gwappa 9:e136394bdb39 89 }
gwappa 9:e136394bdb39 90
gwappa 4:fcf597f82632 91 void Cued::setup() {
gwappa 4:fcf597f82632 92 LOGSTATE(Cued)
gwappa 4:fcf597f82632 93
gwappa 11:897ecd5413e0 94 trial.markEndOfWait();
gwappa 7:6744ec9ccc25 95
gwappa 4:fcf597f82632 96 // configure the interrupts
gwappa 12:06ea96546af1 97 events::lickhandler = &automaton::jump<Cued,Abort>;
gwappa 11:897ecd5413e0 98
gwappa 11:897ecd5413e0 99 // "cue" comes from either internal visual stimulus generation (Report, Associate)
gwappa 11:897ecd5413e0 100 // or the animal's whisker motion (Motion)
gwappa 11:897ecd5413e0 101 if (task.mode.value == Motion) {
gwappa 12:06ea96546af1 102 events::whiskhandler = &Cued::gate;
gwappa 7:6744ec9ccc25 103 } else {
gwappa 12:06ea96546af1 104 events::gatehandler = &Cued::gate;
gwappa 7:6744ec9ccc25 105 }
gwappa 7:6744ec9ccc25 106
gwappa 7:6744ec9ccc25 107 // start cue output
gwappa 12:06ea96546af1 108 if (task.mode.value == Motion) {
gwappa 11:897ecd5413e0 109 audioOut.write(1);
gwappa 11:897ecd5413e0 110 buzzerTicker.attach_us(&tickBuzzer, trial.aud_ticker_cycle);
gwappa 11:897ecd5413e0 111 }
gwappa 4:fcf597f82632 112
gwappa 4:fcf597f82632 113 // sets the timeout for the next state
gwappa 11:897ecd5413e0 114 stateTimeout.attach_us(&automaton::jump<Cued,NoResp>, ms_to_us(task.aud_dur_ms.value));
gwappa 4:fcf597f82632 115 }
gwappa 4:fcf597f82632 116
gwappa 7:6744ec9ccc25 117 void Cued::gate() {
gwappa 12:06ea96546af1 118 events::gatehandler = 0;
gwappa 12:06ea96546af1 119 events::whiskhandler = 0;
gwappa 12:06ea96546af1 120 trial.response |= TrialFlags::Cues;
gwappa 12:06ea96546af1 121 events::lickhandler = &automaton::jump<Cued,WithResp>;
gwappa 7:6744ec9ccc25 122 }
gwappa 7:6744ec9ccc25 123
gwappa 4:fcf597f82632 124 void Cued::teardown() {
gwappa 12:06ea96546af1 125 events::whiskhandler = 0;
gwappa 12:06ea96546af1 126 events::lickhandler = 0;
gwappa 12:06ea96546af1 127 events::gatehandler = 0;
gwappa 7:6744ec9ccc25 128 // end cue output
gwappa 11:897ecd5413e0 129 buzzerTicker.detach();
gwappa 11:897ecd5413e0 130 audioOut.write(0);
gwappa 11:897ecd5413e0 131 if (task.mode.value != Motion) {
gwappa 12:06ea96546af1 132 visualOut.stop();
gwappa 7:6744ec9ccc25 133 }
gwappa 7:6744ec9ccc25 134 }
gwappa 7:6744ec9ccc25 135
gwappa 7:6744ec9ccc25 136 void Abort::setup() {
gwappa 7:6744ec9ccc25 137 LOGSTATE(Abort)
gwappa 11:897ecd5413e0 138 trial.markEndOfWait();
gwappa 8:973dcd190672 139 trial.response = TrialFlags::Licked;
gwappa 8:973dcd190672 140 stateTimeout.attach_us(&automaton::done<Abort>, ms_to_us(task.post_dur_ms.value));
gwappa 7:6744ec9ccc25 141 }
gwappa 7:6744ec9ccc25 142
gwappa 7:6744ec9ccc25 143 void Abort::teardown() {
gwappa 7:6744ec9ccc25 144 finalize();
gwappa 4:fcf597f82632 145 }
gwappa 4:fcf597f82632 146
gwappa 4:fcf597f82632 147 void WithResp::setup() {
gwappa 4:fcf597f82632 148 LOGSTATE(WithResp)
gwappa 4:fcf597f82632 149
gwappa 4:fcf597f82632 150 trial.response |= TrialFlags::Responded;
gwappa 4:fcf597f82632 151
gwappa 9:e136394bdb39 152 if (task.mode.value != Pair) {
gwappa 9:e136394bdb39 153 // open/close the valve
gwappa 11:897ecd5413e0 154 rewardOut.start();
gwappa 9:e136394bdb39 155 }
gwappa 4:fcf597f82632 156
gwappa 5:849446d19406 157 stateTimeout.attach_us(&automaton::done<WithResp>, ms_to_us(task.post_dur_ms.value));
gwappa 4:fcf597f82632 158 }
gwappa 4:fcf597f82632 159
gwappa 4:fcf597f82632 160 void WithResp::teardown() {
gwappa 4:fcf597f82632 161 finalize();
gwappa 4:fcf597f82632 162 }
gwappa 4:fcf597f82632 163
gwappa 4:fcf597f82632 164 void NoResp::setup() {
gwappa 4:fcf597f82632 165 LOGSTATE(NoResp)
gwappa 4:fcf597f82632 166
gwappa 11:897ecd5413e0 167 // no reward here no matter the mode
gwappa 7:6744ec9ccc25 168
gwappa 5:849446d19406 169 stateTimeout.attach_us(&automaton::done<NoResp>, ms_to_us(task.post_dur_ms.value));
gwappa 4:fcf597f82632 170 }
gwappa 4:fcf597f82632 171 void NoResp::teardown() {
gwappa 4:fcf597f82632 172 finalize();
gwappa 8:973dcd190672 173 }
gwappa 8:973dcd190672 174
gwappa 8:973dcd190672 175 void TestReward::setup() {
gwappa 8:973dcd190672 176 LOGSTATE(TestReward)
gwappa 8:973dcd190672 177
gwappa 8:973dcd190672 178 // open/close the valve
gwappa 11:897ecd5413e0 179 rewardOut.setDuration(ms_to_us(task.reward_ms.value));
gwappa 11:897ecd5413e0 180 rewardOut.start();
gwappa 8:973dcd190672 181 stateTimeout.attach_us(&automaton::done<TestReward>, ms_to_us(task.reward_ms.value+20));
gwappa 8:973dcd190672 182 }
gwappa 8:973dcd190672 183
gwappa 8:973dcd190672 184 void TestReward::teardown() {
gwappa 8:973dcd190672 185 // do nothing
gwappa 4:fcf597f82632 186 }