1 /*******************************************************************************
2   Copyright(c) 2019 Jasem Mutlaq. All rights reserved.
3 
4  Starlight Instruments EFS Focuser
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9  .
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Library General Public License for more details.
14  .
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB.  If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 
21 #pragma once
22 
23 #include "indifocuser.h"
24 #include "hidapi.h"
25 
26 #include <map>
27 
28 class SIEFS : public INDI::Focuser
29 {
30     public:
31 
32     // SI EFS State
33     typedef enum { SI_NOOP,
34                    SI_IN,
35                    SI_OUT,
36                    SI_GOTO,
37                    SI_SET_POS,
38                    SI_MAX_POS,
39                    SI_FAST_IN  = 0x11,
40                    SI_FAST_OUT = 0x12,
41                    SI_HALT     = 0xFF
42                   } SI_COMMANDS;
43 
44 
45     // SI EFS Motor State
46     typedef enum { SI_NOT_MOVING,
47                    SI_MOVING_IN,
48                    SI_MOVING_OUT,
49                    SI_LOCKED = 5,
50                  } SI_MOTOR;
51 
52 
53         SIEFS();
54 
55         const char *getDefaultName() override;
56         virtual bool initProperties() override;
57 
58         virtual bool Connect() override;
59         virtual bool Disconnect() override;
60 
61         virtual void TimerHit() override;
62 
63         virtual IPState MoveAbsFocuser(uint32_t targetTicks) override;
64         virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override;
65         virtual bool AbortFocuser() override;
66         virtual bool SyncFocuser(uint32_t ticks) override;
67         virtual bool SetFocuserMaxPosition(uint32_t ticks) override;
68 
69     private:
70         /**
71          * @brief setPosition Set Position (Either Absolute or Maximum)
72          * @param ticks desired position
73          * @param cmdCode 0x20 to set Absolute position. 0x22 to set Maximum position
74          * @return True if successful, false otherwise.
75          */
76         bool setPosition(uint32_t ticks, uint8_t cmdCode);
77 
78         /**
79          * @brief getPosition Get Position (Either Absolute or Maximum)
80          * @param ticks pointer to store the returned position.
81          * @param cmdCode 0x21 to get Absolute position. 0x23 to get Maximum position
82          * @return True if successful, false otherwise.
83          */
84         bool getPosition(uint32_t *ticks, uint8_t cmdCode);
85 
86         // Set/Get Absolute Position
87         bool setAbsPosition(uint32_t ticks);
88         bool getAbsPosition(uint32_t *ticks);
89 
90         // Set/Get Maximum Position
91         bool setMaxPosition(uint32_t ticks);
92         bool getMaxPosition(uint32_t *ticks);
93 
94         bool sendCommand(SI_COMMANDS targetCommand);
95         bool getStatus();
96 
97         hid_device *handle { nullptr };
98         SI_MOTOR m_Motor { SI_NOT_MOVING };
99         int32_t simPosition { 0 };
100         uint32_t targetPosition { 0 };
101 
102         // Driver Timeout in ms
103         static const uint16_t SI_TIMEOUT { 1000 };
104 
105         static const std::map<SI_COMMANDS, std::string> CommandsMap;
106         static const std::map<SI_MOTOR, std::string> MotorMap;
107 };
108