private void HardQueryOfDevices() { _available_port_list = BehaviorBoard.QueryConnectedArduinoDevices(); NotifyPropertyChanged("AvailablePorts"); NotifyPropertyChanged("AvailablePortCount"); NotifyPropertyChanged("NoBoothsTextVisibility"); }
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(); }
/// <summary> /// Kicks off a refresh of the booth listing in the booth selection box /// </summary> public void ToggleRefresh() { if (_refresh_thread == null || !_refresh_thread.IsBusy) { //Set up the necessary code for the background thread to refresh the booth listing _refresh_thread = new BackgroundWorker(); _refresh_thread.WorkerReportsProgress = true; _refresh_thread.WorkerSupportsCancellation = true; _refresh_thread.DoWork += delegate { _available_port_list = BehaviorBoard.QueryConnectedArduinoDevices(); foreach (var port in _available_port_list) { bool success = BehaviorBoard.QueryIndividualArduinoDevice(port.DeviceID); port.IsPortBusy = !success; } }; _refresh_thread.ProgressChanged += delegate { //code goes here }; _refresh_thread.RunWorkerCompleted += delegate { _currently_refreshing = false; NotifyPropertyChanged("AvailablePorts"); NotifyPropertyChanged("AvailablePortCount"); NotifyPropertyChanged("NoBoothsTextVisibility"); NotifyPropertyChanged("NoBoothsDetectedText"); NotifyPropertyChanged("SelectBoothButtonContent"); NotifyPropertyChanged("SelectBoothButtonEnabled"); }; //Set a flag indicating that the booth list is being refreshed _currently_refreshing = true; //Run the background thread to refresh the booth listing _refresh_thread.RunWorkerAsync(); } }
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); } }
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"); }