File indexing completed on 2024-04-14 14:10:54
0001 /* 0002 SPDX-FileCopyrightText: 2012 Jasem Mutlaq <mutlaqja@ikarustech.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <basedevice.h> 0008 #include "indifocuser.h" 0009 #include "clientmanager.h" 0010 0011 namespace ISD 0012 { 0013 0014 void Focuser::registerProperty(INDI::Property prop) 0015 { 0016 if (!prop.getRegistered()) 0017 return; 0018 0019 if (prop.isNameMatch("FOCUS_MAX")) 0020 { 0021 auto nvp = prop.getNumber(); 0022 m_maxPosition = nvp->at(0)->getValue(); 0023 } 0024 0025 ConcreteDevice::registerProperty(prop); 0026 } 0027 0028 void Focuser::processNumber(INDI::Property prop) 0029 { 0030 auto nvp = prop.getNumber(); 0031 if (prop.isNameMatch("FOCUS_MAX")) 0032 { 0033 m_maxPosition = nvp->at(0)->getValue(); 0034 } 0035 } 0036 0037 bool Focuser::focusIn() 0038 { 0039 auto focusProp = getSwitch("FOCUS_MOTION"); 0040 0041 if (!focusProp) 0042 return false; 0043 0044 auto inFocus = focusProp->findWidgetByName("FOCUS_INWARD"); 0045 0046 if (!inFocus) 0047 return false; 0048 0049 if (inFocus->getState() == ISS_ON) 0050 return true; 0051 0052 focusProp->reset(); 0053 inFocus->setState(ISS_ON); 0054 0055 sendNewProperty(focusProp); 0056 0057 return true; 0058 } 0059 0060 bool Focuser::stop() 0061 { 0062 auto focusStop = getSwitch("FOCUS_ABORT_MOTION"); 0063 0064 if (!focusStop) 0065 return false; 0066 0067 focusStop->at(0)->setState(ISS_ON); 0068 sendNewProperty(focusStop); 0069 0070 return true; 0071 } 0072 0073 bool Focuser::focusOut() 0074 { 0075 auto focusProp = getSwitch("FOCUS_MOTION"); 0076 0077 if (!focusProp) 0078 return false; 0079 0080 auto outFocus = focusProp->findWidgetByName("FOCUS_OUTWARD"); 0081 0082 if (!outFocus) 0083 return false; 0084 0085 if (outFocus->getState() == ISS_ON) 0086 return true; 0087 0088 focusProp->reset(); 0089 outFocus->setState(ISS_ON); 0090 0091 sendNewProperty(focusProp); 0092 0093 return true; 0094 } 0095 0096 bool Focuser::getFocusDirection(ISD::Focuser::FocusDirection *dir) 0097 { 0098 auto focusProp = getSwitch("FOCUS_MOTION"); 0099 0100 if (!focusProp) 0101 return false; 0102 0103 auto inFocus = focusProp->findWidgetByName("FOCUS_INWARD"); 0104 0105 if (!inFocus) 0106 return false; 0107 0108 if (inFocus->getState() == ISS_ON) 0109 *dir = FOCUS_INWARD; 0110 else 0111 *dir = FOCUS_OUTWARD; 0112 0113 return true; 0114 } 0115 0116 bool Focuser::moveByTimer(int msecs) 0117 { 0118 auto focusProp = getNumber("FOCUS_TIMER"); 0119 0120 if (!focusProp) 0121 return false; 0122 0123 focusProp->at(0)->setValue(msecs); 0124 0125 sendNewProperty(focusProp); 0126 0127 return true; 0128 } 0129 0130 bool Focuser::moveAbs(int steps) 0131 { 0132 auto focusProp = getNumber("ABS_FOCUS_POSITION"); 0133 0134 if (!focusProp) 0135 return false; 0136 0137 focusProp->at(0)->setValue(steps); 0138 0139 sendNewProperty(focusProp); 0140 0141 return true; 0142 } 0143 0144 bool Focuser::canAbsMove() 0145 { 0146 auto focusProp = getNumber("ABS_FOCUS_POSITION"); 0147 0148 if (!focusProp) 0149 return false; 0150 else 0151 return true; 0152 } 0153 0154 bool Focuser::moveRel(int steps) 0155 { 0156 INDI::PropertyView<INumber> *focusProp; 0157 0158 if(canManualFocusDriveMove()) 0159 { 0160 focusProp = getNumber("manualfocusdrive"); 0161 0162 FocusDirection dir; 0163 if (!getFocusDirection(&dir)) 0164 return false; 0165 if (dir == FOCUS_INWARD) 0166 steps = -abs(steps); 0167 else if (dir == FOCUS_OUTWARD) 0168 steps = abs(steps); 0169 0170 //manualfocusdrive needs different steps value at every turn 0171 if (steps == getLastManualFocusDriveValue()) 0172 steps += 1; 0173 0174 //Nikon Z6 fails if step is -1, 0, 1 0175 if (deviation == NIKONZ6) 0176 { 0177 if (abs(steps) < 2) 0178 steps = 2; 0179 } 0180 } 0181 else 0182 { 0183 focusProp = getNumber("REL_FOCUS_POSITION"); 0184 } 0185 0186 if (!focusProp) 0187 return false; 0188 0189 focusProp->at(0)->setValue(steps); 0190 0191 sendNewProperty(focusProp); 0192 0193 return true; 0194 } 0195 0196 bool Focuser::canRelMove() 0197 { 0198 auto focusProp = getNumber("REL_FOCUS_POSITION"); 0199 0200 if (!focusProp) 0201 return false; 0202 else 0203 return true; 0204 } 0205 0206 bool Focuser::canManualFocusDriveMove() 0207 { 0208 auto focusProp = getNumber("manualfocusdrive"); 0209 0210 if (!focusProp) 0211 return false; 0212 else 0213 return true; 0214 } 0215 0216 double Focuser::getLastManualFocusDriveValue() 0217 { 0218 auto focusProp = getNumber("manualfocusdrive"); 0219 0220 if (!focusProp) 0221 return 0; 0222 0223 return focusProp->at(0)->getValue(); 0224 } 0225 0226 0227 bool Focuser::canTimerMove() 0228 { 0229 auto focusProp = getNumber("FOCUS_TIMER"); 0230 0231 if (!focusProp) 0232 return false; 0233 else 0234 return true; 0235 } 0236 0237 bool Focuser::setMaxPosition(uint32_t steps) 0238 { 0239 auto focusProp = getNumber("FOCUS_MAX"); 0240 0241 if (!focusProp) 0242 return false; 0243 0244 focusProp->at(0)->setValue(steps); 0245 sendNewProperty(focusProp); 0246 0247 return true; 0248 } 0249 0250 bool Focuser::hasBacklash() 0251 { 0252 auto focusProp = getNumber("FOCUS_BACKLASH_STEPS"); 0253 return (focusProp != nullptr); 0254 } 0255 0256 bool Focuser::setBacklash(int32_t steps) 0257 { 0258 auto focusToggle = getSwitch("FOCUS_BACKLASH_TOGGLE"); 0259 if (!focusToggle) 0260 return false; 0261 0262 // Make sure focus compensation is enabled. 0263 if (steps != 0 && focusToggle->at(0)->getState() != ISS_ON) 0264 { 0265 focusToggle->reset(); 0266 focusToggle->at(0)->setState(ISS_ON); 0267 focusToggle->at(1)->setState(ISS_OFF); 0268 sendNewProperty(focusToggle); 0269 } 0270 0271 auto focusProp = getNumber("FOCUS_BACKLASH_STEPS"); 0272 if (!focusProp) 0273 return false; 0274 0275 focusProp->at(0)->setValue(steps); 0276 sendNewProperty(focusProp); 0277 0278 // If steps = 0, disable compensation 0279 if (steps == 0 && focusToggle->at(0)->getState() == ISS_ON) 0280 { 0281 focusToggle->reset(); 0282 focusToggle->at(0)->setState(ISS_OFF); 0283 focusToggle->at(1)->setState(ISS_ON); 0284 sendNewProperty(focusToggle); 0285 } 0286 return true; 0287 } 0288 0289 int32_t Focuser::getBacklash() 0290 { 0291 auto focusProp = getNumber("FOCUS_BACKLASH_STEPS"); 0292 if (!focusProp) 0293 return -1; 0294 0295 return focusProp->at(0)->getValue(); 0296 } 0297 0298 bool Focuser::hasDeviation() 0299 { 0300 if (getDeviceName() == "Nikon DSLR Z6") 0301 { 0302 deviation = NIKONZ6; 0303 return true; 0304 } 0305 return false; 0306 } 0307 0308 }