示例#1
0
        public void CopyFileToSecondaryPath(string RatName)
        {
            string base_path           = JawBoothConfiguration.GetInstance().SavePath;
            string secondary_base_path = JawBoothConfiguration.GetInstance().SecondarySavePath;

            //If the base path doesn't end with a back-slash, then add one to the end of it
            if (!base_path.EndsWith(@"\"))
            {
                base_path = base_path + @"\";
            }

            //If the base path doesn't end with a back-slash, then add one to the end of it
            if (!secondary_base_path.EndsWith(@"\"))
            {
                secondary_base_path = secondary_base_path + @"\";
            }

            string full_path = this.PrimaryFullPath;

            string file_name = RatName + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".txt";
            string secondary_base_path_plus_rat_folder = secondary_base_path + RatName + @"\";
            string secondary_full_path = secondary_base_path_plus_rat_folder + file_name;

            //Create directory if it doesn't exist
            new FileInfo(secondary_full_path).Directory.Create();

            //FileInfo k = new FileInfo(full_path);
            //k.CopyTo(secondary_full_path);
            System.IO.File.Copy(full_path, secondary_full_path);
        }
示例#2
0
        public MainWindow()
        {
            InitializeComponent();

            //Load the config file
            JawBoothConfiguration.GetInstance().LoadConfigurationFile();

            //Have the user select a booth
            BoothSelectionUI booth_selection_window = new BoothSelectionUI();

            booth_selection_window.ShowDialog();

            //Get the result of the booth selection
            string port_name = string.Empty;
            BoothSelectorViewModel booth_selection_result = BoothSelectorViewModel.GetInstance();

            if (booth_selection_result.ResultOK && booth_selection_result.AvailablePortCount > 0)
            {
                port_name = booth_selection_result.AvailablePorts[booth_selection_result.SelectedPortIndex].Model.DeviceID;
            }

            if (!string.IsNullOrEmpty(port_name))
            {
                //Set the data context
                DataContext = new SessionViewModel(port_name);
            }
            else
            {
                //Close the main window and exit the program if no booth was selected
                this.Close();
            }
        }
示例#3
0
        /// <summary>
        /// </summary>
        private BoothSelectorViewModel()
        {
            //Read in the booth pairings before instantiating this view model
            JawBoothConfiguration.GetInstance().ReadBoothPairings();

            //Query the devices
            ToggleRefresh();
        }
        public static JawBoothConfiguration GetInstance()
        {
            if (_instance == null)
            {
                lock (_instance_lock)
                {
                    if (_instance == null)
                    {
                        _instance = new JawBoothConfiguration();
                    }
                }
            }

            return(_instance);
        }
示例#5
0
        private void _background_thread_DoWork(object sender, DoWorkEventArgs e)
        {
            _timer.Reset();

            //Connect to the Arduino board
            BehaviorBoard board = new BehaviorBoard();

            board.ConnectToArduino(_com_port);

            //Grab the booth number
            var booth_num = board.GetBoothNumber();

            BoothLabel = booth_num.ToString();

            //Set the default IR threshold for the nosepoke
            _ir_threshold = JawBoothConfiguration.GetInstance().IR_Threshold;

            //Check to see if their is a booth-specific IR threshold
            var booth_names = JawBoothConfiguration.GetInstance().Booth_Specific_IR_Thresholds.Keys.ToList();

            if (booth_names.Contains(BoothLabel))
            {
                _ir_threshold = JawBoothConfiguration.GetInstance().Booth_Specific_IR_Thresholds[BoothLabel];
            }

            //SLeep for 50 milliseconds to allow the previous operation to complete
            Thread.Sleep(50);

            //Enable streaming from the board
            board.StreamEnable(true);

            while (!_background_thread.CancellationPending)
            {
                HandleState(board);

                //Update the GUI based on what is happening in the background thread
                _background_thread.ReportProgress(0);

                //Sleep the thread for a bit
                Thread.Sleep(33);
            }

            //Disconnect from the Arduino board
            board.DisconnectFromArduino();
        }
示例#6
0
        public void OpenFileForWriting(string RatName, bool StimEnabled)
        {
            string base_path = JawBoothConfiguration.GetInstance().SavePath;

            //If the base path doesn't end with a back-slash, then add one to the end of it
            if (!base_path.EndsWith(@"\"))
            {
                base_path = base_path + @"\";
            }

            string base_path_plus_rat_folder = base_path + RatName + @"\";
            string file_name = RatName + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".txt";
            string full_path = base_path_plus_rat_folder + file_name;

            this.PrimaryFullPath = full_path;

            //Create directory if it doesn't exist
            new FileInfo(full_path).Directory.Create();

            _writer = new StreamWriter(full_path, false);
            _writer.WriteLine(RatName);
            _writer.WriteLine("Stimulation: " + StimEnabled.ToString());
            _writer.WriteLine("Start: " + DateTime.Now.ToString("yyyyMMddTHHmmss"));
        }
示例#7
0
        public static bool QueryIndividualArduinoDevice(string com_port)
        {
            BehaviorBoard b       = new BehaviorBoard();
            bool          success = b.ConnectToArduino(com_port);

            if (success)
            {
                //Get the booth number
                int booth_number = b.GetBoothNumber();

                //Disconnect from the Arduino
                b.DisconnectFromArduino();

                //Update the booth pairing
                JawBoothConfiguration.GetInstance().UpdateBoothPairing(com_port, booth_number.ToString());
                JawBoothConfiguration.GetInstance().SaveBoothPairings();

                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#8
0
        private void HandleState(BehaviorBoard board)
        {
            bool inc_feeder   = false;
            bool inc_nosepoke = false;
            bool inc_stim     = false;

            //Read in nosepoke data from the board
            var nosepoke_input = board.ReadStream();

            //Check to see if the nosepoke state has changed
            if (nosepoke_input.Any(x => x > _ir_threshold) && !NosepokeState)
            {
                NosepokeState = true;

                inc_nosepoke = true;
                session_writer.WriteEvent(BehaviorBoard.BehaviorEvent.NosepokeEnter, DateTime.Now);
                _background_properties_to_update.Add("NosepokeState");
            }
            else if (nosepoke_input.All(x => x <= _ir_threshold) && NosepokeState)
            {
                NosepokeState = false;

                session_writer.WriteEvent(BehaviorBoard.BehaviorEvent.NosepokeExit, DateTime.Now);
                _background_properties_to_update.Add("NosepokeState");
            }

            if (_execute_manual_feed)
            {
                _execute_manual_feed = false;
                board.TriggerFeeder(1);

                if (SessionState == SessionStateEnum.Running)
                {
                    last_actual_feed_time = DateTime.Now;
                    session_writer.WriteEvent(BehaviorBoard.BehaviorEvent.FeederTriggered, DateTime.Now);
                    inc_feeder = true;
                }

                FeederState = true;
                _feeder_timer.Restart();
                _background_properties_to_update.Add("FeederState");
            }

            if (_execute_manual_stim)
            {
                _execute_manual_stim = false;
                board.TriggerStimulator();
                _vns_trigger_timer.Restart();

                if (SessionState == SessionStateEnum.Running)
                {
                    session_writer.WriteEvent(BehaviorBoard.BehaviorEvent.VNSTriggered, DateTime.Now);
                    VNSTriggerCount++;
                    _background_properties_to_update.Add("VNSTriggerCount");
                }

                VNSTriggerState = true;
                _background_properties_to_update.Add("VNSTriggerState");
            }

            //Update the feeder in the GUI
            if (_feeder_timer.ElapsedMilliseconds > 1000)
            {
                FeederState = false;
                _feeder_timer.Reset();
                _background_properties_to_update.Add("FeederState");
            }

            //Update the VNS trigger in the GUI
            if (_vns_trigger_timer.ElapsedMilliseconds > 1000)
            {
                VNSTriggerState = false;
                _vns_trigger_timer.Reset();
                _background_properties_to_update.Add("VNSTriggerState");
            }

            switch (SessionState)
            {
            case SessionStateEnum.NotRunning:
                break;

            case SessionStateEnum.SetUpNewSession:

                //Reset all the counts to be 0
                FeederCount = 0;
                _background_properties_to_update.Add("FeederCount");

                NosepokeCount = 0;
                _background_properties_to_update.Add("NosepokeCount");

                VNSTriggerCount = 0;
                _background_properties_to_update.Add("VNSTriggerCount");

                //Progress the state to be "running"
                SessionState = SessionStateEnum.Running;

                //Start the timer
                _timer.Restart();

                //Set the last time that a feed occurred to be now
                last_trial_completion_time = DateTime.Now;

                //Set up a new file for this rat
                session_writer.OpenFileForWriting(RatName, StimulationEnabled);

                break;

            case SessionStateEnum.Running:

                if (_timer.Elapsed.TotalMinutes >= JawBoothConfiguration.GetInstance().MaximumDuration)
                {
                    //Finalize the session if we have exceeded the maximum duration of a session
                    SessionState = SessionStateEnum.FinalizeSession;
                    break;
                }

                if (_current_trial_state == TrialStateEnum.Completed)
                {
                    //Check to see if it is time to feed
                    var current_time = DateTime.Now;
                    var time_elapsed = current_time - last_trial_completion_time;
                    if (time_elapsed.TotalSeconds >= JawBoothConfiguration.GetInstance().FeederTiming)
                    {
                        last_trial_completion_time = DateTime.Now;

                        //Finalize the session if we have exceeded the maximum number of feeds
                        if (FeederCount >= JawBoothConfiguration.GetInstance().MaximumFeeds)
                        {
                            SessionState = SessionStateEnum.FinalizeSession;
                            break;
                        }

                        //Now choose whether to actually feed or not
                        int rnd_result = rnd_gen.Next(1, 100);
                        if (rnd_result <= JawBoothConfiguration.GetInstance().FeederPercentage)
                        {
                            _current_trial_state = TrialStateEnum.WaitingForNosepokeEntry;

                            board.TriggerFeeder(1);
                            last_actual_feed_time = DateTime.Now;

                            session_writer.WriteEvent(BehaviorBoard.BehaviorEvent.FeederTriggered, DateTime.Now);
                            FeederState = true;
                            inc_feeder  = true;
                            _feeder_timer.Restart();
                            _background_properties_to_update.Add("FeederState");
                        }
                    }
                }

                if (inc_feeder)
                {
                    FeederCount++;
                    _background_properties_to_update.Add("FeederCount");
                }

                if (inc_nosepoke)
                {
                    if (last_left_nosepoke_entry < last_actual_feed_time)
                    {
                        last_left_nosepoke_entry   = DateTime.Now;
                        last_trial_completion_time = DateTime.Now;
                        _current_trial_state       = TrialStateEnum.Completed;

                        if (StimulationEnabled)
                        {
                            inc_stim = true;
                        }
                    }

                    NosepokeCount++;
                    _background_properties_to_update.Add("NosepokeCount");
                }

                if (inc_stim)
                {
                    vns_delay_active = true;
                    vns_delay_start  = DateTime.Now;
                    inc_stim         = false;
                }

                if (vns_delay_active)
                {
                    var time_elapsed = DateTime.Now - vns_delay_start;
                    if (time_elapsed.TotalMilliseconds >= JawBoothConfiguration.GetInstance().VNS_Delay)
                    {
                        vns_delay_active = false;

                        board.TriggerStimulator();
                        _vns_trigger_timer.Restart();

                        session_writer.WriteEvent(BehaviorBoard.BehaviorEvent.VNSTriggered, DateTime.Now);

                        VNSTriggerState = true;
                        VNSTriggerCount++;
                        _background_properties_to_update.Add("VNSTriggerState");
                        _background_properties_to_update.Add("VNSTriggerCount");
                    }
                }

                break;

            case SessionStateEnum.FinalizeSession:

                //Progress the state to be "not running"
                SessionState = SessionStateEnum.NotRunning;

                _timer.Stop();
                //_timer.Reset();

                session_writer.CloseFile();
                session_writer.CopyFileToSecondaryPath(RatName);

                break;
            }

            _background_properties_to_update.Add("SecondsElapsed");
        }