fast-feedback virtual target task code on STM Nucleo

Dependencies:   mbed

Committer:
gwappa
Date:
Wed May 23 15:15:03 2018 +0000
Revision:
4:fcf597f82632
Child:
5:849446d19406
make Pair mode work

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 inline us_timestamp_t ms_to_us(const uint16_t& ms)
gwappa 4:fcf597f82632 15 {
gwappa 4:fcf597f82632 16 return ((us_timestamp_t)ms)*1000;
gwappa 4:fcf597f82632 17 }
gwappa 4:fcf597f82632 18
gwappa 4:fcf597f82632 19 void (*whiskhandler)() = 0;
gwappa 4:fcf597f82632 20 void (*lickhandler)() = 0;
gwappa 4:fcf597f82632 21 bool logged = false;
gwappa 4:fcf597f82632 22
gwappa 4:fcf597f82632 23 void whiskcallback() {
gwappa 4:fcf597f82632 24 if (logged) {
gwappa 4:fcf597f82632 25 trial.whisking_events.add(timer.read_ms());
gwappa 4:fcf597f82632 26 }
gwappa 4:fcf597f82632 27 if (whiskhandler != 0) {
gwappa 4:fcf597f82632 28 whiskhandler();
gwappa 4:fcf597f82632 29 }
gwappa 4:fcf597f82632 30 }
gwappa 4:fcf597f82632 31
gwappa 4:fcf597f82632 32 void lickcallback() {
gwappa 4:fcf597f82632 33 if (logged) {
gwappa 4:fcf597f82632 34 trial.licking_events.add(timer.read_ms());
gwappa 4:fcf597f82632 35 }
gwappa 4:fcf597f82632 36 if (lickhandler != 0) {
gwappa 4:fcf597f82632 37 lickhandler();
gwappa 4:fcf597f82632 38 }
gwappa 4:fcf597f82632 39 }
gwappa 4:fcf597f82632 40
gwappa 4:fcf597f82632 41 void finalize() {
gwappa 4:fcf597f82632 42 taskOut.write(0);
gwappa 4:fcf597f82632 43 trial.writeToSerial();
gwappa 4:fcf597f82632 44 timer.stop();
gwappa 4:fcf597f82632 45 timer.reset();
gwappa 4:fcf597f82632 46 }
gwappa 4:fcf597f82632 47
gwappa 4:fcf597f82632 48 void Delay::setup() {
gwappa 4:fcf597f82632 49 LOGSTATE(Delay)
gwappa 4:fcf597f82632 50
gwappa 4:fcf597f82632 51 // initialize the trial-related params
gwappa 4:fcf597f82632 52 trial.reset(task);
gwappa 4:fcf597f82632 53
gwappa 4:fcf597f82632 54 // configure the flags
gwappa 4:fcf597f82632 55 taskOut.write(1);
gwappa 4:fcf597f82632 56 cueOut.write(0);
gwappa 4:fcf597f82632 57 enableOut.write(1); // for Pair
gwappa 4:fcf597f82632 58 rewardOut.write(0);
gwappa 4:fcf597f82632 59
gwappa 4:fcf597f82632 60 // configure the interrupts
gwappa 4:fcf597f82632 61 whiskIn.rise(&whiskcallback);
gwappa 4:fcf597f82632 62 lickIn.rise(&lickcallback);
gwappa 4:fcf597f82632 63 // no whisk handler, no lick handler
gwappa 4:fcf597f82632 64 // only to be logged
gwappa 4:fcf597f82632 65 logged = true;
gwappa 4:fcf597f82632 66
gwappa 4:fcf597f82632 67 // sets the timeout for the next state
gwappa 4:fcf597f82632 68 stateTimeout.attach_us(&automaton::jump<Delay,Cued>, ms_to_us(trial.delay_dur_ms)); // for Pair
gwappa 4:fcf597f82632 69
gwappa 4:fcf597f82632 70 // update the timestamp
gwappa 4:fcf597f82632 71 trial.starting = timer.read_ms();
gwappa 4:fcf597f82632 72 timer.start();
gwappa 4:fcf597f82632 73 }
gwappa 4:fcf597f82632 74
gwappa 4:fcf597f82632 75 void Delay::teardown() {
gwappa 4:fcf597f82632 76 // do nothing
gwappa 4:fcf597f82632 77 }
gwappa 4:fcf597f82632 78
gwappa 4:fcf597f82632 79 void Cued::setup() {
gwappa 4:fcf597f82632 80 LOGSTATE(Cued)
gwappa 4:fcf597f82632 81
gwappa 4:fcf597f82632 82 // configure the interrupts
gwappa 4:fcf597f82632 83 whiskhandler = &automaton::jump<Cued,WithResp>;
gwappa 4:fcf597f82632 84 // no lick handler
gwappa 4:fcf597f82632 85 // remains to be logged
gwappa 4:fcf597f82632 86
gwappa 4:fcf597f82632 87 // sets the timeout for the next state
gwappa 4:fcf597f82632 88 stateTimeout.attach_us(&automaton::jump<Cued,NoResp>, ms_to_us(task.cue_dur_ms)); // for Pair
gwappa 4:fcf597f82632 89
gwappa 4:fcf597f82632 90 // update the timestamp
gwappa 4:fcf597f82632 91 trial.cuestarting = timer.read_ms();
gwappa 4:fcf597f82632 92 trial.waiting = trial.cuestarting - trial.starting;
gwappa 4:fcf597f82632 93 }
gwappa 4:fcf597f82632 94
gwappa 4:fcf597f82632 95 void Cued::teardown() {
gwappa 4:fcf597f82632 96 whiskhandler = 0;
gwappa 4:fcf597f82632 97 stateTimeout.detach();
gwappa 4:fcf597f82632 98 }
gwappa 4:fcf597f82632 99
gwappa 4:fcf597f82632 100 void WithResp::setup() {
gwappa 4:fcf597f82632 101 LOGSTATE(WithResp)
gwappa 4:fcf597f82632 102
gwappa 4:fcf597f82632 103 trial.response |= TrialFlags::Responded;
gwappa 4:fcf597f82632 104
gwappa 4:fcf597f82632 105 // TODO: open/close the valve
gwappa 4:fcf597f82632 106
gwappa 4:fcf597f82632 107 stateTimeout.attach_us(&automaton::done<WithResp>, ms_to_us(task.post_dur_ms));
gwappa 4:fcf597f82632 108 }
gwappa 4:fcf597f82632 109
gwappa 4:fcf597f82632 110 void WithResp::teardown() {
gwappa 4:fcf597f82632 111 finalize();
gwappa 4:fcf597f82632 112 }
gwappa 4:fcf597f82632 113
gwappa 4:fcf597f82632 114 void NoResp::setup() {
gwappa 4:fcf597f82632 115 LOGSTATE(NoResp)
gwappa 4:fcf597f82632 116
gwappa 4:fcf597f82632 117 trial.response &= ~TrialFlags::Responded;
gwappa 4:fcf597f82632 118
gwappa 4:fcf597f82632 119 stateTimeout.attach_us(&automaton::done<NoResp>, ms_to_us(task.post_dur_ms));
gwappa 4:fcf597f82632 120 }
gwappa 4:fcf597f82632 121 void NoResp::teardown() {
gwappa 4:fcf597f82632 122 finalize();
gwappa 4:fcf597f82632 123 }