File indexing completed on 2024-07-21 06:28:23

0001 /*
0002     SPDX-FileCopyrightText: 2022 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "indirotator.h"
0008 
0009 namespace ISD
0010 {
0011 
0012 bool Rotator::setAbsoluteAngle(double angle)
0013 {
0014     auto nvp = getNumber("ABS_ROTATOR_ANGLE");
0015 
0016     if (!nvp)
0017         return false;
0018 
0019     if (std::abs(angle - nvp->at(0)->getValue()) < 0.01)
0020         return true;
0021 
0022     nvp->at(0)->setValue(angle);
0023 
0024     sendNewProperty(nvp);
0025     return true;
0026 }
0027 
0028 bool Rotator::setAbsoluteSteps(uint32_t steps)
0029 {
0030     auto nvp = getNumber("ABS_ROTATOR_POSITION");
0031 
0032     if (!nvp)
0033         return false;
0034 
0035     if (steps == static_cast<uint32_t>(nvp->at(0)->getValue()))
0036         return true;
0037 
0038     nvp->at(0)->setValue(steps);
0039 
0040     sendNewProperty(nvp);
0041     return true;
0042 }
0043 
0044 bool Rotator::setReversed(bool enabled)
0045 {
0046     auto svp = getSwitch("ROTATOR_REVERSE");
0047 
0048     if (!svp)
0049         return false;
0050 
0051     if ( (enabled && svp->sp[0].s == ISS_ON) ||
0052             (!enabled && svp->sp[1].s == ISS_ON))
0053         return true;
0054 
0055     svp->reset();
0056     svp->at(0)->setState(enabled ? ISS_ON : ISS_OFF);
0057     svp->at(1)->setState(enabled ? ISS_OFF : ISS_ON);
0058 
0059     sendNewProperty(svp);
0060     return true;
0061 }
0062 
0063 void Rotator::registerProperty(INDI::Property prop)
0064 {
0065     if (prop.isNameMatch("ABS_ROTATOR_ANGLE"))
0066         processNumber(prop);
0067     else if (prop.isNameMatch("ROTATOR_REVERSE"))
0068         processSwitch(prop);
0069 }
0070 
0071 void Rotator::processNumber(INDI::Property prop)
0072 {
0073     auto nvp = prop.getNumber();
0074     if (nvp->isNameMatch("ABS_ROTATOR_ANGLE"))
0075     {
0076         if (std::abs(nvp->at(0)->getValue() - m_AbsoluteAngle) > 0 || nvp->getState() != m_AbsoluteAngleState)
0077         {
0078             m_AbsoluteAngle = nvp->at(0)->getValue();
0079             m_AbsoluteAngleState = nvp->getState();
0080             emit newAbsoluteAngle(m_AbsoluteAngle, m_AbsoluteAngleState);
0081         }
0082     }
0083 }
0084 
0085 void Rotator::processSwitch(INDI::Property prop)
0086 {
0087     auto svp = prop.getSwitch();
0088     if (svp->isNameMatch("ROTATOR_REVERSE"))
0089     {
0090         auto reverse = svp->findOnSwitchIndex() == 0;
0091         if (m_Reversed != reverse)
0092         {
0093             m_Reversed = reverse;
0094             emit reverseToggled(m_Reversed);
0095         }
0096     }
0097 }
0098 
0099 }