1 /*******************************************************************************
2   Copyright(c) 2019 Jasem Mutlaq. All rights reserved.
3 
4   Pegasus Pocket Power Box
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 
30 #include <vector>
31 #include <stdint.h>
32 
33 namespace Connection
34 {
35 class Serial;
36 }
37 
38 class PegasusPPB : public INDI::DefaultDevice, public INDI::WeatherInterface
39 {
40     public:
41         PegasusPPB();
42 
43         virtual bool initProperties() override;
44         virtual bool updateProperties() override;
45 
46         virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
47         virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
48 
49     protected:
50         const char *getDefaultName() override;
51         virtual bool saveConfigItems(FILE *fp) override;
52 
53         // Event loop
54         virtual void TimerHit() override;
55 
56         // Weather Overrides
updateWeather()57         virtual IPState updateWeather() override
58         {
59             return IPS_OK;
60         }
61 
62 
63     private:
64         bool Handshake();
65 
66         // Get Data
67         bool sendFirmware();
68         bool getSensorData();
69         std::vector<std::string> split(const std::string &input, const std::string &regex);
70         enum
71         {
72             PA_NAME,
73             PA_VOLTAGE,
74             PA_CURRENT,
75             PA_TEMPERATURE,
76             PA_HUMIDITY,
77             PA_DEW_POINT,
78             PA_PORT_STATUS,
79             PA_DSLR_STATUS,
80             PA_DEW_1,
81             PA_DEW_2,
82             PA_AUTO_DEW,
83             PA_N,
84         };
85 
86         // Device Control
87         bool reboot();
88 
89         // Power
90         bool setPowerEnabled(uint8_t port, bool enabled);
91         bool setPowerOnBoot();
92 
93         // Dew
94         bool setAutoDewEnabled(bool enabled);
95         bool setDewPWM(uint8_t id, uint8_t value);
96 
97         /**
98          * @brief sendCommand Send command to unit.
99          * @param cmd Command
100          * @param res if nullptr, respones is ignored, otherwise read response and store it in the buffer.
101          * @return
102          */
103         bool sendCommand(const char *cmd, char *res);
104 
105         int PortFD { -1 };
106         bool setupComplete { false };
107 
108         Connection::Serial *serialConnection { nullptr };
109 
110         ////////////////////////////////////////////////////////////////////////////////////
111         /// Main Control
112         ////////////////////////////////////////////////////////////////////////////////////
113         /// Reboot Device
114         ISwitch RebootS[1];
115         ISwitchVectorProperty RebootSP;
116 
117         // Power Sensors
118         INumber PowerSensorsN[2];
119         INumberVectorProperty PowerSensorsNP;
120         enum
121         {
122             SENSOR_VOLTAGE,
123             SENSOR_CURRENT,
124         };
125 
126         ////////////////////////////////////////////////////////////////////////////////////
127         /// Power Group
128         ////////////////////////////////////////////////////////////////////////////////////
129 
130         // Cycle all power on/off
131         ISwitch PowerCycleAllS[2];
132         ISwitchVectorProperty PowerCycleAllSP;
133         enum
134         {
135             POWER_CYCLE_OFF,
136             POWER_CYCLE_ON,
137         };
138 
139         ISwitch DSLRPowerS[2];
140         ISwitchVectorProperty DSLRPowerSP;
141         enum
142         {
143             DSLR_OFF,
144             DSLR_ON,
145         };
146 
147         // Select which power is ON on bootup
148         ISwitch PowerOnBootS[4];
149         ISwitchVectorProperty PowerOnBootSP;
150 
151         ////////////////////////////////////////////////////////////////////////////////////
152         /// Dew Group
153         ////////////////////////////////////////////////////////////////////////////////////
154 
155         // Auto Dew
156         ISwitch AutoDewS[2];
157         ISwitchVectorProperty AutoDewSP;
158 
159         // Dew PWM
160         INumber DewPWMN[2];
161         INumberVectorProperty DewPWMNP;
162         enum
163         {
164             DEW_PWM_A,
165             DEW_PWM_B,
166         };
167 
168         std::vector<std::string> lastSensorData;
169         char stopChar { 0xD };
170 
171         static constexpr const uint8_t PEGASUS_TIMEOUT {3};
172         static constexpr const uint8_t PEGASUS_LEN {128};
173         static constexpr const char *DEW_TAB {"Dew"};
174         static constexpr const char *ENVIRONMENT_TAB {"Environment"};
175 };
176