Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
states.cpp
- Committer:
- gwappa
- Date:
- 2018-06-25
- Revision:
- 12:06ea96546af1
- Parent:
- 11:897ecd5413e0
- Child:
- 13:8ea85a33e37a
File content as of revision 12:06ea96546af1:
#include "states.h" #include "rig.h" #include "automaton.h" #include "events.h" #include "IO.h" #define STATE_IS_LOGGED #ifdef STATE_IS_LOGGED #define LOGSTATE(S) IO::info(#S); #else #define LOGSTATE(S) #endif void tickBuzzer() { audioOut = !audioOut; } void finalize() { audioOut.write(0); // if any trial.markTrialEnd(); } void Delay::setup() { LOGSTATE(Delay) // initialize the trial-related params trial.reset(task); // TODO: reset visualOut with the nice parameters // visualOut.reset(task, (task.mode.value==Associate)||(task.mode.value==Motion)); if ((task.mode.value == Report) || (task.mode.value == Associate)) { visualOut.attachTurnOnCallback(&events::gate); } audioOut.write(0); rewardOut.setDuration(ms_to_us(task.reward_ms.value)); events::setup(true); // set up the timeout for the next state 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; default: stateTimeout.attach_us(&automaton::jump<Delay,Prepare>, ms_to_us(trial.delay_dur_ms)); } trial.markTrialStart(); } void Delay::teardown() { // do nothing } void Paired::setup() { LOGSTATE(Paired) trial.markEndOfWait(); trial.response |= TrialFlags::Cues; events::lickhandler = &automaton::jump<Paired,WithResp>; // reward & visual feedback visualOut.direct(true); rewardOut.start(); stateTimeout.attach_us(&automaton::jump<Paired,NoResp>, ms_to_us(task.aud_dur_ms.value)); } void Paired::teardown() { visualOut.direct(false); events::lickhandler = 0; } void Prepare::setup() { // mostly the same with for Delay // except that the animal cannot lick freely LOGSTATE(Prepare) // configure the interrupts // no whisk handler events::lickhandler = &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)); } void Prepare::teardown() { // de-register lick inhibition events::lickhandler = 0; } void Cued::setup() { LOGSTATE(Cued) trial.markEndOfWait(); // configure the interrupts events::lickhandler = &automaton::jump<Cued,Abort>; // "cue" comes from either internal visual stimulus generation (Report, Associate) // or the animal's whisker motion (Motion) if (task.mode.value == Motion) { events::whiskhandler = &Cued::gate; } else { events::gatehandler = &Cued::gate; } // start cue output if (task.mode.value == Motion) { audioOut.write(1); buzzerTicker.attach_us(&tickBuzzer, trial.aud_ticker_cycle); } // sets the timeout for the next state stateTimeout.attach_us(&automaton::jump<Cued,NoResp>, ms_to_us(task.aud_dur_ms.value)); } void Cued::gate() { events::gatehandler = 0; events::whiskhandler = 0; trial.response |= TrialFlags::Cues; events::lickhandler = &automaton::jump<Cued,WithResp>; } void Cued::teardown() { events::whiskhandler = 0; events::lickhandler = 0; events::gatehandler = 0; // end cue output buzzerTicker.detach(); audioOut.write(0); if (task.mode.value != Motion) { visualOut.stop(); } } void Abort::setup() { LOGSTATE(Abort) trial.markEndOfWait(); trial.response = TrialFlags::Licked; stateTimeout.attach_us(&automaton::done<Abort>, ms_to_us(task.post_dur_ms.value)); } void Abort::teardown() { finalize(); } void WithResp::setup() { LOGSTATE(WithResp) trial.response |= TrialFlags::Responded; if (task.mode.value != Pair) { // open/close the valve rewardOut.start(); } stateTimeout.attach_us(&automaton::done<WithResp>, ms_to_us(task.post_dur_ms.value)); } void WithResp::teardown() { finalize(); } void NoResp::setup() { LOGSTATE(NoResp) // no reward here no matter the mode stateTimeout.attach_us(&automaton::done<NoResp>, ms_to_us(task.post_dur_ms.value)); } void NoResp::teardown() { finalize(); } void TestReward::setup() { LOGSTATE(TestReward) // open/close the valve rewardOut.setDuration(ms_to_us(task.reward_ms.value)); rewardOut.start(); stateTimeout.attach_us(&automaton::done<TestReward>, ms_to_us(task.reward_ms.value+20)); } void TestReward::teardown() { // do nothing }