<commit_before>///////////////////////////////////////////////////////////////////////////////
// FILE:          CSUXHub.cpp
// PROJECT:       Micro-Manager
// SUBSYSTEM:     DeviceAdapters
//-----------------------------------------------------------------------------
// DESCRIPTION:   CSUX hub module. Required for operation of all 
//                CSUX devices
//                
// AUTHOR:        <NAME>, <EMAIL> 02/03/2007
// BASED ON:      TEHub.cpp by <NAME>
//
// COPYRIGHT:     University of California, San Francisco, 2006
//                All rights reserved
//
// LICENSE:       This library is free software; you can redistribute it and/or
//                modify it under the terms of the GNU Lesser General Public
//                License as published by the Free Software Foundation.
//                
//                You should have received a copy of the GNU Lesser General Public
//                License along with the source distribution; if not, write to
//                the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
//                Boston, MA  02111-1307  USA
//
//                This file is distributed in the hope that it will be useful,
//                but WITHOUT ANY WARRANTY; without even the implied warranty
//                of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
//                IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//                CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                   
//                INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.           
                                                                                     
#define _CRT_SECURE_NO_DEPRECATE

#include "CSUXHub.h"
#include "assert.h"
#include <memory.h>
#include <sstream>
#include <iostream>

#include "../../MMDevice/DeviceUtils.h"

using namespace std;

CSUXHub::CSUXHub ()
{
   ClearRcvBuf();
}

CSUXHub::~CSUXHub()
{
}


///////////////////////////////////////////////////////////////////////////////
// Device commands
///////////////////////////////////////////////////////////////////////////////

/*
 * Set FilterWheel Position
 * TODO: Make sure that CSUX can handle commands send in sequence without delay
 */
int CSUXHub::SetFilterWheelPosition(MM::Device& device, MM::Core& core, long wheelNr, long pos)
{
   // Set Filter Wheel 
   ostringstream os;
   os << "FW_POS, " << wheelNr << ", " << pos;
   bool succeeded = false;
   int counter = 0;
   // try up to 10 times, wait 50 ms in between tries
   int ret = DEVICE_OK;
   while (!succeeded && counter < 10)
   {
      ret = ExecuteCommand(device, core, os.str().c_str());
      if (ret != DEVICE_OK)
         return ret;
      ret = GetAcknowledgment(device,core);
      if (ret != DEVICE_OK)  {
         CDeviceUtils::SleepMs(50);
         counter++;
      } else
         succeeded = true;
   }
   if (!succeeded)
       return ret;

   return DEVICE_OK;
}

/*
 * Queries CSU for current filter wheel position
 */
int CSUXHub::GetFilterWheelPosition(MM::Device& device, MM::Core& core, long wheelNr, long& pos)
{
   ClearAllRcvBuf(device, core);
   ostringstream os;
   os << "FW_POS, " << wheelNr  << ", ?";
   int ret = ExecuteCommand(device, core, os.str().c_str());
   ret = core.GetSerialAnswer(&device, port_.c_str(), RCV_BUF_LENGTH, rcvBuf_, "\r");
   if (ret != DEVICE_OK)
      return ret;
   ret = Acknowledge();
   if (ret != DEVICE_OK)
      return ret;

   pos = atol(rcvBuf_);
   return DEVICE_OK;
}
    
 
/*
 * Sets speed of filter wheel (0-3)
 */
int CSUXHub::SetFilterWheelSpeed(MM::Device& device, MM::Core& core, long wheelNr, long speed)
{
   // Set Filter Wheel  Speeds
   ostringstream os;
   os << "FW_SPEED, " << wheelNr << ", " << speed;
   int ret = ExecuteCommand(device, core, os.str().c_str());
   if (ret != DEVICE_OK)
      return ret;
   ret = GetAcknowledgment(device,core);
   if (ret != DEVICE_OK) 
      return ret;

   return DEVICE_OK;
}

/*
 * Gets speed of filter wheel (0-3)
 */
int CSUXHub::GetFilterWheelSpeed(MM::Device& device, MM::Core& core, long wheelNr, long& speed)
{
   ClearAllRcvBuf(device, core);
   ostringstream os;
   os << "FW_SPEED, " << wheelNr  << ", ?";
   int ret = ExecuteCommand(device, core, os.str().c_str());
   ret = core.GetSerialAnswer(&device, port_.c_str(), RCV_BUF_LENGTH, rcvBuf_, "\r");
   if (ret != DEVICE_OK)
      return ret;
   ret = Acknowledge();
   if (ret != DEVICE_OK)
      return ret;
   speed = atol(rcvBuf_);
   return DEVICE_OK;
}

/*
 * 
 */
int CSUXHub::SetDichroicPosition(MM::Device& device, MM::Core& core, long dichroic)
{
   ostringstream os;
   os << "DM_POS, " <<  dichroic ;

   // send command
   int ret = ExecuteCommand(device, core, os.str().c_str());
   if (ret != DEVICE_OK)
      return ret;

   ret = GetAcknowledgment(device, core);
   if (ret != DEVICE_OK)
      return ret;
   return DEVICE_OK;
}

/*
 * 
 */<commit_msg>[Yokogawa/CSUX] Add retry logic for setting dichroic (c.f. <NAME>).<commit_after>///////////////////////////////////////////////////////////////////////////////
// FILE:          CSUXHub.cpp
// PROJECT:       Micro-Manager
// SUBSYSTEM:     DeviceAdapters
//-----------------------------------------------------------------------------
// DESCRIPTION:   CSUX hub module. Required for operation of all 
//                CSUX devices
//                
// AUTHOR:        <NAME>, <EMAIL> 02/03/2007
// BASED ON:      TEHub.cpp by <NAME>
//
// COPYRIGHT:     University of California, San Francisco, 2006
//                All rights reserved
//
// LICENSE:       This library is free software; you can redistribute it and/or
//                modify it under the terms of the GNU Lesser General Public
//                License as published by the Free Software Foundation.
//                
//                You should have received a copy of the GNU Lesser General Public
//                License along with the source distribution; if not, write to
//                the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
//                Boston, MA  02111-1307  USA
//
//                This file is distributed in the hope that it will be useful,
//                but WITHOUT ANY WARRANTY; without even the implied warranty
//                of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
//                IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//                CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                   
//                INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.           
                                                                                     
#define _CRT_SECURE_NO_DEPRECATE

#include "CSUXHub.h"
#include "assert.h"
#include <memory.h>
#include <sstream>
#include <iostream>

#include "../../MMDevice/DeviceUtils.h"

using namespace std;

CSUXHub::CSUXHub ()
{
   ClearRcvBuf();
}

CSUXHub::~CSUXHub()
{
}


///////////////////////////////////////////////////////////////////////////////
// Device commands
///////////////////////////////////////////////////////////////////////////////

/*
 * Set FilterWheel Position
 * TODO: Make sure that CSUX can handle commands send in sequence without delay
 */
int CSUXHub::SetFilterWheelPosition(MM::Device& device, MM::Core& core, long wheelNr, long pos)
{
   // Set Filter Wheel 
   ostringstream os;
   os << "FW_POS, " << wheelNr << ", " << pos;
   bool succeeded = false;
   int counter = 0;
   // try up to 10 times, wait 50 ms in between tries
   int ret = DEVICE_OK;
   while (!succeeded && counter < 10)
   {
      ret = ExecuteCommand(device, core, os.str().c_str());
      if (ret != DEVICE_OK)
         return ret;
      ret = GetAcknowledgment(device,core);
      if (ret != DEVICE_OK)  {
         CDeviceUtils::SleepMs(50);
         counter++;
      } else
         succeeded = true;
   }
   if (!succeeded)
       return ret;

   return DEVICE_OK;
}

/*
 * Queries CSU for current filter wheel position
 */
int CSUXHub::GetFilterWheelPosition(MM::Device& device, MM::Core& core, long wheelNr, long& pos)
{
   ClearAllRcvBuf(device, core);
   ostringstream os;
   os << "FW_POS, " << wheelNr  << ", ?";
   int ret = ExecuteCommand(device, core, os.str().c_str());
   ret = core.GetSerialAnswer(&device, port_.c_str(), RCV_BUF_LENGTH, rcvBuf_, "\r");
   if (ret != DEVICE_OK)
      return ret;
   ret = Acknowledge();
   if (ret != DEVICE_OK)
      return ret;

   pos = atol(rcvBuf_);
   return DEVICE_OK;
}
    
 
/*
 * Sets speed of filter wheel (0-3)
 */
int CSUXHub::SetFilterWheelSpeed(MM::Device& device, MM::Core& core, long wheelNr, long speed)
{
   // Set Filter Wheel  Speeds
   ostringstream os;
   os << "FW_SPEED, " << wheelNr << ", " << speed;
   int ret = ExecuteCommand(device, core, os.str().c_str());
   if (ret != DEVICE_OK)
      return ret;
   ret = GetAcknowledgment(device,core);
   if (ret != DEVICE_OK) 
      return ret;

   return DEVICE_OK;
}

/*
 * Gets speed of filter wheel (0-3)
 */
int CSUXHub::GetFilterWheelSpeed(MM::Device& device, MM::Core& core, long wheelNr, long& speed)
{
   ClearAllRcvBuf(device, core);
   ostringstream os;
   os << "FW_SPEED, " << wheelNr  << ", ?";
   int ret = ExecuteCommand(device, core, os.str().c_str());
   ret = core.GetSerialAnswer(&device, port_.c_str(), RCV_BUF_LENGTH, rcvBuf_, "\r");
   if (ret != DEVICE_OK)
      return ret;
   ret = Acknowledge();
   if (ret != DEVICE_OK)
      return ret;
   speed = atol(rcvBuf_);
   return DEVICE_OK;
}

/*
 * 
 */
int CSUXHub::SetDichroicPosition(MM::Device& device, MM::Core& core, long dichroic)
{
   ostringstream os;
   os << "DM_POS, " <<  dichroic ;

   bool succeeded = false;
   int counter = 0;
   // try up to 10 times, wait 50 ms in between tries
   int ret = DEVICE_OK;
   while (!succeeded && counter < 10)
   {
      ret = ExecuteCommand(device, core, os.str().c_str());
      if (ret != DEVICE_OK)
         return ret;
      ret = GetAcknowledgment(device,core);
      if (ret != DEVICE_OK)  {
         CDeviceUtils::SleepMs(50);
         counter++;
      } else
         succeeded = true;
   }
   if (!succeeded)
       return ret;

   return DEVICE_OK;
}

/*
 * 
 */
{'fraction_non_alphanumeric': 0.17652268031093182, 'fraction_numerical': 0.008560464429794351, 'mean_word_length': 1.8526522593320236, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '801958', 'n_tokens_mistral': 3182, 'n_tokens_neox': 2981, 'n_words': 905}