int CArduinoNeoPixelShutter::OnOnOff(MM::PropertyBase* pProp, MM::ActionType eAct) { CArduinoNeoPixelHub* hub = static_cast<CArduinoNeoPixelHub*>(GetParentHub()); if (eAct == MM::BeforeGet) { // use cached state pProp->Set((long)hub->GetShutterState()); } else if (eAct == MM::AfterSet) { long pos; pProp->Get(pos); int ret; std::string command = "I" + boost::lexical_cast<std::string>((long long)hub->GetIntensity()); ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; if (pos == 0) { std::string command = "P0\r"; int ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; } else { std::string command = "P111111111111111111111111111111111111111\r"; int ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; int multi = hub->GetMulti(); LogMessage("multi:"); if (multi & 0x1) { std::string command = "R" + boost::lexical_cast<std::string>((long long)hub->GetRedBrightness()) + "\r"; int ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; } else { std::string command = "R0\r"; int ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; } if (multi & 0x2) { std::string command = "G" + boost::lexical_cast<std::string>((long long)hub->GetGreenBrightness()) + "\r"; int ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; } else { std::string command = "G0\r"; int ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; } if (multi & 0x4) { std::string command = "B" + boost::lexical_cast<std::string>((long long)hub->GetBlueBrightness()) + "\r"; int ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; } else { std::string command = "B0\r"; int ret = hub->WriteToComPortH((const unsigned char*) command.c_str(), command.length()); if (ret != DEVICE_OK) return ret; } } hub->SetShutterState(pos); changedTime_ = GetCurrentMMTime(); } return DEVICE_OK; }
int SerialPort::GetAnswer(char* answer, unsigned bufLen, const char* term) { if (!initialized_) return ERR_PORT_NOTINITIALIZED; if (bufLen < 1) { LogMessage("BUFFER_OVERRUN error occured!"); return ERR_BUFFER_OVERRUN; } std::ostringstream logMsg; unsigned long answerOffset = 0; memset(answer,0,bufLen); char theData; MM::MMTime startTime = GetCurrentMMTime(); MM::MMTime retryWarnTime(0); MM::MMTime answerTimeout(answerTimeoutMs_ * 1000.0); // warn of retries every 200 ms. MM::MMTime retryWarnInterval(0, 200000); int retryCounter = 0; while ((GetCurrentMMTime() - startTime) < answerTimeout) { MM::MMTime tNow = GetCurrentMMTime(); if ( retryWarnInterval < tNow - retryWarnTime) { retryWarnTime = tNow; if( 1 < retryCounter) { LogMessage((std::string("GetAnswer # retries = ") + boost::lexical_cast<std::string,int>(retryCounter)).c_str(), true); } retryCounter++; } bool anyRead = pPort_->ReadOneCharacter(theData); if( anyRead ) { if (bufLen <= answerOffset) { answer[answerOffset] = '\0'; LogMessage("BUFFER_OVERRUN error occured!"); return ERR_BUFFER_OVERRUN; } answer[answerOffset++] = theData; } else { //Yield to other threads: CDeviceUtils::SleepMs(1); ++retryCounter; } // look for the terminator if( 0 != term) { // check for terminating sequence char* termPos = strstr(answer, term); if (termPos != 0) { // found the terminator!! // erase the terminator from the answer; for( unsigned int iterm = 1; iterm <= strlen(term); ++iterm) { char *pAnswer = answer + answerOffset - iterm; if( answer <= pAnswer) pAnswer[0] = '\0'; } if( 2 < retryCounter ) LogMessage((std::string("GetAnswer # retries = ") + boost::lexical_cast<std::string,int>(retryCounter)).c_str(), true); LogMessage(( std::string("GetAnswer <- ") + std::string(answer)).c_str(), true); return DEVICE_OK; } } else { // a formatted answer without a terminator. LogMessage((std::string("GetAnswer without terminator returned after ") + boost::lexical_cast<std::string,long>((long)((GetCurrentMMTime() - startTime).getMsec())) + std::string("msec")).c_str(), true); if( 4 < retryCounter) { LogMessage(( std::string("GetAnswer <- ") + std::string(answer)).c_str(), true); return DEVICE_OK; } } } // end while LogMessage("TERM_TIMEOUT error occured!"); return ERR_TERM_TIMEOUT; }
int CArduinoNeoPixelShutter::Initialize() { CArduinoNeoPixelHub* hub = static_cast<CArduinoNeoPixelHub*>(GetParentHub()); if (!hub || !hub->IsPortAvailable()) { return ERR_NO_PORT_SET; } char hubLabel[MM::MaxStrLength]; hub->GetLabel(hubLabel); SetParentID(hubLabel); // for backward comp. // set property list // ----------------- // OnOff // ------ CPropertyAction* pAct = new CPropertyAction (this, &CArduinoNeoPixelShutter::OnOnOff); int ret = CreateProperty("OnOff", "1", MM::Integer, false, pAct); if (ret != DEVICE_OK) return ret; pAct = new CPropertyAction (this, &CArduinoNeoPixelShutter::OnIntensity); ret = CreateProperty("Intensity", "255", MM::Integer, false, pAct); SetPropertyLimits("Intensity", 0, 255); if (ret != DEVICE_OK) return ret; pAct = new CPropertyAction(this, &CArduinoNeoPixelShutter::OnRedBrightness); ret = CreateProperty("Red brightness", "255", MM::Integer, false, pAct); SetPropertyLimits("Red brightness", 0, 255); if (ret != DEVICE_OK) return ret; pAct = new CPropertyAction(this, &CArduinoNeoPixelShutter::OnGreenBrightness); ret = CreateProperty("Green brightness", "255", MM::Integer, false, pAct); SetPropertyLimits("Green brightness", 0, 255); if (ret != DEVICE_OK) return ret; pAct = new CPropertyAction(this, &CArduinoNeoPixelShutter::OnBlueBrightness); ret = CreateProperty("Blue brightness", "255", MM::Integer, false, pAct); SetPropertyLimits("Blue brightness", 0, 255); if (ret != DEVICE_OK) return ret; pAct = new CPropertyAction(this, &CArduinoNeoPixelShutter::OnMulti); ret = CreateProperty("Multi", "8", MM::Integer, false, pAct); SetPropertyLimits("Multi", 0, 1<<3); if (ret != DEVICE_OK) return ret; // set shutter into the off state //WriteToPort(0); std::vector<std::string> vals; vals.push_back("0"); vals.push_back("1"); ret = SetAllowedValues("OnOff", vals); if (ret != DEVICE_OK) return ret; ret = UpdateStatus(); if (ret != DEVICE_OK) return ret; changedTime_ = GetCurrentMMTime(); initialized_ = true; return DEVICE_OK; }
int LMM5Shutter::Initialize() { if (g_Interface == NULL) return DEVICE_NOT_CONNECTED; // Name CreateProperty(MM::g_Keyword_Name, name_.c_str(), MM::String, true); // Description CreateProperty(MM::g_Keyword_Description, "Spectral LMM5 Shutter", MM::String, true); int ret = g_Interface->DetectLaserLines(*this, *GetCoreCallback()); nrLines_= g_Interface->GetNrLines(); if (ret != DEVICE_OK) return ret; availableLines* lines = g_Interface->getAvailableLaserLines(); unsigned long lineMask = 0; for (int i=0; i < nrLines_; i++) if (lines[i].present) lineMask = lineMask | (1 << i); // outputs are available only since firmware 1.30. Our interface will always return 1 for firmware that is older ret = g_Interface->GetNumberOfOutputs(*this, *GetCoreCallback(), nrOutputs_); if (nrOutputs_ > 1) { CPropertyAction *pAct = new CPropertyAction(this, &LMM5Shutter::OnOutputSelect); CreateProperty("FiberOutput", "0", MM::Integer, false, pAct); for (int i = 0; i < nrOutputs_; i++) { std::ostringstream os; os << i; AddAllowedValue("FiberOutput", os.str().c_str()); } } // We roll our own implementation of State and Label here (since we are a Shutter device, not a State Device // State CPropertyAction* pAct = new CPropertyAction(this, &LMM5Shutter::OnState); ret = CreateProperty(MM::g_Keyword_State, "0", MM::Integer, false, pAct); if (ret != DEVICE_OK) return ret; for (unsigned long i=0; i<=lineMask; i++) { if ((i & lineMask) == i) { std::stringstream tmp; tmp << i; AddAllowedValue(MM::g_Keyword_State, tmp.str().c_str()); } } // Label pAct = new CPropertyAction(this, &LMM5Shutter::OnLabel); ret = CreateProperty(MM::g_Keyword_Label, "", MM::String, false, pAct); if (ret != DEVICE_OK) return ret; for (unsigned long i=0; i<=lineMask; i++) { if ((i & lineMask) == i) { std::string label = StateToLabel(i);; AddAllowedValue(MM::g_Keyword_Label, label.c_str()); } } for (long i=0; i < nrLines_; i++) { if (lines[i].present) { if (lines[i].waveLength >= 100 || nrOutputs_ == 1) { CPropertyActionEx *pActEx = new CPropertyActionEx(this, &LMM5Shutter::OnStateEx, i); CreateProperty(lines[i].name.c_str(), "0", MM::Integer, false, pActEx); SetPropertyLimits(lines[i].name.c_str(), 0, 1); } } } changedTime_ = GetCurrentMMTime(); ret = UpdateStatus(); if (ret != DEVICE_OK) return ret; return DEVICE_OK; }