1 /*******************************************************************************
2   Copyright(c) 2018 Jasem Mutlaq. All rights reserved.
3 
4   Pegasus Ultimate Power Box (v1 and v2)
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 "indifocuserinterface.h"
29 #include "indiweatherinterface.h"
30 #include <stdint.h>
31 
32 namespace Connection
33 {
34 class Serial;
35 }
36 
37 class PegasusUPB : public INDI::DefaultDevice, public INDI::FocuserInterface, public INDI::WeatherInterface
38 {
39     public:
40         PegasusUPB();
41 
42         enum
43         {
44             UPB_V1,
45             UPB_V2
46         };
47 
48         virtual bool initProperties() override;
49         virtual bool updateProperties() override;
50 
51         virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
52         virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
53         virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
54 
55     protected:
56         const char *getDefaultName() override;
57         virtual bool saveConfigItems(FILE *fp) override;
58 
59         // Event loop
60         virtual void TimerHit() override;
61 
62         // Focuser Overrides
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 ReverseFocuser(bool enabled) override;
67         virtual bool SyncFocuser(uint32_t ticks) override;
68 
69         virtual bool SetFocuserBacklash(int32_t steps) override;
70         virtual bool SetFocuserBacklashEnabled(bool enabled) override;
71 
72         // Weather Overrides
updateWeather()73         virtual IPState updateWeather() override
74         {
75             return IPS_OK;
76         }
77 
78 
79     private:
80         bool Handshake();
81 
82         // Get Data
83         bool setupParams();
84         bool sendFirmware();
85         bool getSensorData();
86         bool getPowerData();
87         bool getStepperData();
88         bool getDewAggData();
89         std::vector<std::string> split(const std::string &input, const std::string &regex);
90 
91         // Device Control
92         bool reboot();
93 
94         // Power
95         bool setPowerEnabled(uint8_t port, bool enabled);
96         bool setPowerLEDEnabled(bool enabled);
97         bool setPowerOnBoot();
98         bool getPowerOnBoot();
99         bool setAdjustableOutput(uint8_t voltage);
100 
101         // Dew
102         bool setAutoDewEnabled(bool enabled);
103         bool toggleAutoDewV2();
104         bool setAutoDewAgg(uint8_t value);
105         bool setDewPWM(uint8_t id, uint8_t value);
106 
107         // USB
108         bool setUSBHubEnabled(bool enabled);
109         bool setUSBPortEnabled(uint8_t port, bool enabled);
110 
111         // Focuser
112         bool setFocuserMaxSpeed(uint16_t maxSpeed);
113         //    bool setFocuserBacklash(uint16_t value);
114         //    bool setFocuserBacklashEnabled(bool enabled);
115 
116         /**
117          * @brief sendCommand Send command to unit.
118          * @param cmd Command
119          * @param res if nullptr, respones is ignored, otherwise read response and store it in the buffer.
120          * @return
121          */
122         bool sendCommand(const char *cmd, char *res);
123 
124         /**
125          * @brief cleanupResponse Removes all spaces
126          * @param response buffer
127          */
128         void cleanupResponse(char *response);
129 
130         /**
131          * @return Return true if sensor data different from last data
132          */
133         bool sensorUpdated(const std::vector<std::string> &result, uint8_t start, uint8_t end);
134 
135         int PortFD { -1 };
136         bool setupComplete { false };
137 
138         Connection::Serial *serialConnection { nullptr };
139 
140         ////////////////////////////////////////////////////////////////////////////////////
141         /// Main Control
142         ////////////////////////////////////////////////////////////////////////////////////
143         /// Reboot Device
144         ISwitch RebootS[1];
145         ISwitchVectorProperty RebootSP;
146 
147         // Power Sensors
148         INumber PowerSensorsN[3];
149         INumberVectorProperty PowerSensorsNP;
150         enum
151         {
152             SENSOR_VOLTAGE,
153             SENSOR_CURRENT,
154             SENSOR_POWER,
155         };
156 
157         // Power Consumption
158         INumber PowerConsumptionN[3];
159         INumberVectorProperty PowerConsumptionNP;
160         enum
161         {
162             CONSUMPTION_AVG_AMPS,
163             CONSUMPTION_AMP_HOURS,
164             CONSUMPTION_WATT_HOURS,
165         };
166 
167         ////////////////////////////////////////////////////////////////////////////////////
168         /// Power Group
169         ////////////////////////////////////////////////////////////////////////////////////
170 
171         // Cycle all power on/off
172         ISwitch PowerCycleAllS[2];
173         ISwitchVectorProperty PowerCycleAllSP;
174         enum
175         {
176             POWER_CYCLE_OFF,
177             POWER_CYCLE_ON,
178         };
179 
180         // Turn on/off power
181         ISwitch PowerControlS[4];
182         ISwitchVectorProperty PowerControlSP;
183 
184         // Rename the power controls above
185         IText PowerControlsLabelsT[4] = {};
186         ITextVectorProperty PowerControlsLabelsTP;
187 
188         // Current Draw
189         INumber PowerCurrentN[4];
190         INumberVectorProperty PowerCurrentNP;
191 
192         // Select which power is ON on bootup
193         ISwitch PowerOnBootS[4];
194         ISwitchVectorProperty PowerOnBootSP;
195 
196         // Overcurrent status
197         ILight OverCurrentL[7];
198         ILightVectorProperty OverCurrentLP;
199 
200         // Power LED
201         ISwitch PowerLEDS[2];
202         ISwitchVectorProperty PowerLEDSP;
203         enum
204         {
205             POWER_LED_ON,
206             POWER_LED_OFF,
207         };
208 
209         // Adjustable Output
210         INumber AdjustableOutputN[1];
211         INumberVectorProperty AdjustableOutputNP;
212 
213         ////////////////////////////////////////////////////////////////////////////////////
214         /// Dew Group
215         ////////////////////////////////////////////////////////////////////////////////////
216 
217         // Auto Dew v1
218         ISwitch AutoDewS[2];
219         ISwitchVectorProperty AutoDewSP;
220 
221         enum
222         {
223             DEW_PWM_A,
224             DEW_PWM_B,
225             DEW_PWM_C,
226         };
227 
228         // Auto Dew v2
229         ISwitch AutoDewV2S[3];
230         ISwitchVectorProperty AutoDewV2SP;
231 
232         // Rename the power controls above
233         IText DewControlsLabelsT[3] = {};
234         ITextVectorProperty DewControlsLabelsTP;
235 
236         // Auto Dew v2 Aggressiveness
237 
238         enum
239         {
240             AUTO_DEW_AGG,
241         };
242 
243         INumber AutoDewAggN[1];
244         INumberVectorProperty AutoDewAggNP;
245 
246         // Dew PWM
247         INumber DewPWMN[3];
248         INumberVectorProperty DewPWMNP;
249 
250         // Current Draw
251         INumber DewCurrentDrawN[3];
252         INumberVectorProperty DewCurrentDrawNP;
253 
254         ////////////////////////////////////////////////////////////////////////////////////
255         /// USB
256         ////////////////////////////////////////////////////////////////////////////////////
257 
258         // Turn on/off usb ports 1-5 (v1)
259         ISwitch USBControlS[2];
260         ISwitchVectorProperty USBControlSP;
261 
262         // Turn on/off usb ports 1-6 (v2)
263         ISwitch USBControlV2S[6];
264         ISwitchVectorProperty USBControlV2SP;
265 
266         // USB Port Status (1-6)
267         ILight USBStatusL[6];
268         ILightVectorProperty USBStatusLP;
269 
270         // Rename the USB controls above
271         IText USBControlsLabelsT[6] = {};
272         ITextVectorProperty USBControlsLabelsTP;
273 
274         ////////////////////////////////////////////////////////////////////////////////////
275         /// Focuser
276         ////////////////////////////////////////////////////////////////////////////////////
277 
278         // Focuser speed
279         INumber FocuserSettingsN[1];
280         INumberVectorProperty FocuserSettingsNP;
281         enum
282         {
283             //SETTING_BACKLASH,
284             SETTING_MAX_SPEED,
285         };
286 
287         ////////////////////////////////////////////////////////////////////////////////////
288         /// USB
289         ////////////////////////////////////////////////////////////////////////////////////
290         ITextVectorProperty FirmwareTP;
291         IText FirmwareT[2] {};
292         enum
293         {
294             FIRMWARE_VERSION,
295             FIRMWARE_UPTIME
296         };
297 
298 
299         std::vector<std::string> lastSensorData, lastPowerData, lastStepperData, lastDewAggData;
300         bool focusMotorRunning { false };
301         char stopChar { 0xD };
302         uint8_t version { UPB_V1 };
303 
304         static constexpr const uint8_t PEGASUS_TIMEOUT {3};
305         static constexpr const uint8_t PEGASUS_LEN {128};
306         static constexpr const char *DEW_TAB {"Dew"};
307         static constexpr const char *USB_TAB {"USB"};
308         static constexpr const char *ENVIRONMENT_TAB {"Environment"};
309         static constexpr const char *POWER_TAB {"Power"};
310         static constexpr const char *FIRMWARE_TAB {"Firmware"};
311 };
312