1 /*******************************************************************************
2   Copyright(c) 2019 Jasem Mutlaq. All rights reserved.
3 
4   Pegasus Pocket Power Box Advance
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the Free
8   Software Foundation; either version 2 of the License, or (at your option)
9   any later version.
10 
11   This program is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14   more details.
15 
16   You should have received a copy of the GNU Library General Public License
17   along with this library; see the file COPYING.LIB.  If not, write to
18   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19   Boston, MA 02110-1301, USA.
20 
21   The full GNU General Public License is included in this distribution in the
22   file called LICENSE.
23 *******************************************************************************/
24 
25 #pragma once
26 
27 #include "defaultdevice.h"
28 #include "indiweatherinterface.h"
29 #include "indifocuserinterface.h"
30 
31 #include <vector>
32 #include <stdint.h>
33 
34 namespace Connection
35 {
36 class Serial;
37 }
38 
39 class PegasusPPBA : public INDI::DefaultDevice, public INDI::FocuserInterface, public INDI::WeatherInterface
40 {
41     public:
42         PegasusPPBA();
43 
44         virtual bool initProperties() override;
45         virtual bool updateProperties() override;
46 
47         virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
48         virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
49 
50     protected:
51         const char *getDefaultName() override;
52         virtual bool saveConfigItems(FILE *fp) override;
53 
54         // Event loop
55         virtual void TimerHit() override;
56 
57         // Focuser Overrides
58         virtual IPState MoveAbsFocuser(uint32_t targetTicks) override;
59         virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override;
60         virtual bool AbortFocuser() override;
61         virtual bool ReverseFocuser(bool enabled) override;
62         virtual bool SyncFocuser(uint32_t ticks) override;
63 
64         virtual bool SetFocuserBacklash(int32_t steps) override;
65         virtual bool SetFocuserBacklashEnabled(bool enabled) override;
66 
67         // Weather Overrides
updateWeather()68         virtual IPState updateWeather() override
69         {
70             return IPS_OK;
71         }
72 
73 
74     private:
75         bool Handshake();
76 
77         // Get Data
78         bool sendFirmware();
79         bool getSensorData();
80         bool getConsumptionData();
81         bool getMetricsData();
82         bool findExternalMotorController();
83         bool getXMCStartupData();
84         std::vector<std::string> split(const std::string &input, const std::string &regex);
85         enum
86         {
87             PA_NAME,
88             PA_VOLTAGE,
89             PA_CURRENT,
90             PA_TEMPERATURE,
91             PA_HUMIDITY,
92             PA_DEW_POINT,
93             PA_PORT_STATUS,
94             PA_ADJ_STATUS,
95             PA_DEW_1,
96             PA_DEW_2,
97             PA_AUTO_DEW,
98             PA_PWR_WARN,
99             PA_PWRADJ,
100             PA_N,
101         };
102         enum
103         {
104             PS_NAME,
105             PS_AVG_AMPS,
106             PS_AMP_HOURS,
107             PS_WATT_HOURS,
108             PS_UPTIME,
109             PS_N,
110         };
111         enum
112         {
113             PC_NAME,
114             PC_TOTAL_CURRENT,
115             PC_12V_CURRENT,
116             PC_DEWA_CURRENT,
117             PC_DEWB_CURRENT,
118             PC_UPTIME,
119             PC_N,
120         };
121 
122         // Device Control
123         bool reboot();
124 
125         // Power
126         bool setPowerEnabled(uint8_t port, bool enabled);
127         bool setPowerOnBoot();
128 
129         // Dew
130         bool setAutoDewEnabled(bool enabled);
131         bool setDewPWM(uint8_t id, uint8_t value);
132 
133         // Focuser
134         void queryXMC();
135         bool setFocuserMaxSpeed(uint16_t maxSpeed);
136         bool setFocuserMicrosteps(int value);
137 
138         /**
139          * @brief sendCommand Send command to unit.
140          * @param cmd Command
141          * @param res if nullptr, respones is ignored, otherwise read response and store it in the buffer.
142          * @return
143          */
144         bool sendCommand(const char *cmd, char *res);
145 
146         int PortFD { -1 };
147         bool setupComplete { false };
148 
149         Connection::Serial *serialConnection { nullptr };
150 
151         ////////////////////////////////////////////////////////////////////////////////////
152         /// Main Control
153         ////////////////////////////////////////////////////////////////////////////////////
154         /// Reboot Device
155         ISwitch RebootS[1];
156         ISwitchVectorProperty RebootSP;
157 
158         // Power Sensors
159         INumber PowerSensorsN[9];
160         INumberVectorProperty PowerSensorsNP;
161         enum
162         {
163             SENSOR_VOLTAGE,
164             SENSOR_CURRENT,
165             SENSOR_AVG_AMPS,
166             SENSOR_AMP_HOURS,
167             SENSOR_WATT_HOURS,
168             SENSOR_TOTAL_CURRENT,
169             SENSOR_12V_CURRENT,
170             SENSOR_DEWA_CURRENT,
171             SENSOR_DEWB_CURRENT
172         };
173 
174         ////////////////////////////////////////////////////////////////////////////////////
175         /// Power Group
176         ////////////////////////////////////////////////////////////////////////////////////
177 
178         ISwitch QuadOutS[2];
179         ISwitchVectorProperty QuadOutSP;
180 
181         //        ISwitch AdjOutS[2];
182         //        ISwitchVectorProperty AdjOutSP;
183 
184         ISwitch AdjOutVoltS[6];
185         ISwitchVectorProperty AdjOutVoltSP;
186         enum
187         {
188             ADJOUT_OFF,
189             ADJOUT_3V,
190             ADJOUT_5V,
191             ADJOUT_8V,
192             ADJOUT_9V,
193             ADJOUT_12V,
194         };
195 
196         // Select which power is ON on bootup
197         ISwitch PowerOnBootS[4];
198         ISwitchVectorProperty PowerOnBootSP;
199 
200         // Short circuit warn
201         ILight PowerWarnL[1];
202         ILightVectorProperty PowerWarnLP;
203 
204         ISwitch LedIndicatorS[2];
205         ISwitchVectorProperty LedIndicatorSP;
206 
207         ////////////////////////////////////////////////////////////////////////////////////
208         /// Dew Group
209         ////////////////////////////////////////////////////////////////////////////////////
210 
211         // Auto Dew
212         ISwitch AutoDewS[2];
213         ISwitchVectorProperty AutoDewSP;
214 
215         // Dew PWM
216         INumber DewPWMN[2];
217         INumberVectorProperty DewPWMNP;
218         enum
219         {
220             DEW_PWM_A,
221             DEW_PWM_B,
222         };
223 
224         ////////////////////////////////////////////////////////////////////////////////////
225         /// Focuser
226         ////////////////////////////////////////////////////////////////////////////////////
227 
228         // Focuser speed
229         INumber FocuserSettingsN[1];
230         INumberVectorProperty FocuserSettingsNP;
231         enum
232         {
233             SETTING_MAX_SPEED,
234         };
235 
236         // Microstepping
237         ISwitchVectorProperty FocuserDriveSP;
238         ISwitch FocuserDriveS[4];
239         enum
240         {
241             STEP_FULL,
242             STEP_HALF,
243             STEP_FORTH,
244             STEP_EIGHTH
245         };
246 
247 
248 
249         ////////////////////////////////////////////////////////////////////////////////////
250         /// Firmware
251         ////////////////////////////////////////////////////////////////////////////////////
252 
253         ITextVectorProperty FirmwareTP;
254         IText FirmwareT[2] {};
255         enum
256         {
257             FIRMWARE_VERSION,
258             FIRMWARE_UPTIME,
259         };
260 
261         std::vector<std::string> lastSensorData;
262         std::vector<std::string> lastConsumptionData;
263         std::vector<std::string> lastMetricsData;
264         char stopChar { 0xD };
265         bool m_HasExternalMotor { false };
266 
267         static constexpr const uint8_t PEGASUS_TIMEOUT {3};
268         static constexpr const uint8_t PEGASUS_LEN {128};
269         static constexpr const char *DEW_TAB {"Dew"};
270         static constexpr const char *ENVIRONMENT_TAB {"Environment"};
271         static constexpr const char *FIRMWARE_TAB {"Firmware"};
272 };
273