Instrument Neutral Distributed Interface INDI  2.0.2
mydcp4esp32.h
Go to the documentation of this file.
1 /*
2  myDCP4ESP32
3  Copyright (C) 2023 Stephen Hillier
4 
5  Based on MyFocuserPro2 Focuser
6  Copyright (C) 2019 Alan Townshend
7 
8  As well as USB_Dewpoint
9  Copyright (C) 2017-2023 Jarno Paananen
10 
11  And INDI Sky Quality Meter Driver
12  Copyright(c) 2016 Jasem Mutlaq. All rights reserved.
13 
14  This library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU Lesser General Public
16  License as published by the Free Software Foundation; either
17  version 2.1 of the License, or (at your option) any later version.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 
28 */
29 
30 #pragma once
31 
32 #include <defaultdevice.h>
33 
34 #include <time.h> // for nsleep()
35 #include <errno.h> // for nsleep()
36 
37 #define CDRIVER_VERSION_MAJOR 1
38 #define CDRIVER_VERSION_MINOR 0
39 
40 /***************************** myDCP4ESP32 Commands **************************/
41 //
42 // Commands and responses are string types
43 // Commands start with a ':' and are terminated with a '#', for example, ':01#'
44 // Commands are case sensitive
45 // Responses have a lead character, then values – more than one value is separated with a comma [,] and the string is
46 // terminated with a '#'
47 
48 #define MDCP_CMD_LENGTH 32 // Command buffer size
49 #define MDCP_CMD_TERMINATOR "#"
50 #define MDCP_GET_CONTROLLER_CODE_CMD ":00#" // Get controller code - Response: 0code#
51 #define MDCP_GET_VERSION_CMD ":01#" // Get version number - Respnse: 1version#
52 #define MDCP_REBOOT_CMD ":02#" // Reboot controller - Response: none
53 #define MDCP_GET_TRACKING_MODE_CMD ":03#" // Get tracking mode (1=Ambient, 2=Dewpoint, 3=Midpoint) - Response: 3trackingmode#
54 #define MDCP_SET_TRACKING_MODE_CMD ":04%1u#" // Set tracking mode (1=Ambient, 2=Dewpoint, 3=Midpoint) - Response: none
55 #define MDCP_GET_TRACKING_OFFSET_CMD ":05#" // Get tracking mode offset value (-4 to +3) - Response: 5trackingmodeoffset#
56 #define MDCP_ZERO_TRACKING_OFFSET_CMD ":06#" // Set tracking mode offset to 0 - Response: none
57 #define MDCP_DECREASE_TRACKING_OFFSET_CMD ":07#" // Decrease tracking mode offset - Response: none
58 #define MDCP_INCREASE_TRACKING_OFFSET_CMD ":08#" // Increase tracking mode offset - Response: none
59 #define MDCP_GET_NUMBER_PROBES_CMD ":09#" // Get the number of temperature probes attached - Response: 9number_of_probes#
60 #define MDCP_SAVE_CONTROLLER_SETTINGS_CMD ":10#" // Write variables immediately to SPIFFS - Response: none
61 #define MDCP_SET_DEFAULT_SETTINGS_CMD ":11#" // controller settings to default values - Response: none
62 #define MDCP_GET_AMBIENT_TEMPERATURE_CMD ":12#" // Get the Ambient temperature - Response: avalue#
63 #define MDCP_GET_AMBIENT_OFFSET_CMD ":13#" // Get the Ambient offset - Response: bvalue$
64 #define MDCP_SET_AMBIENT_OFFSET_CMD ":14%1.2f#" // Set the Ambient offset, Num is a value between +3 and -4 - Response: none
65 #define MDCP_GET_HUMIDITY_CMD ":15#" // Get the Relative Humidity % - Resonse: cvalue#
66 #define MDCP_GET_DEWPOINT_CMD ":16#" // Get the Dew Point - Response: dvalue#
67 #define MDCP_GET_TEMP_MODE_CMD ":17#" // Get Temp Mode, 0=Celsius, 1=Fahrenheit - Response: eTempMode
68 #define MDCP_SET_TEMP_MODE_C_CMD ":181#" // Display in Celsius - Response: none
69 #define MDCP_SET_TEMP_MODE_F_CMD ":182#" // Display in Fahrenheit - Response: none
70 #define MDCP_SET_TRACKING_OFFSET_CMD ":19%d#" // Set the tracking mode offset value [-4 to +3] - Response: none
71 #define MDCP_GET_MAC_ADDRESS_CMD ":26#" // Get MAC address - Response: hString#
72 #define MDCP_GET_IP_ADDRESS_CMD ":27#" // Get IP address - Response: iString#
73 #define MDCP_GET_CHANNEL_TEMPS_CMD ":28#" // Get the temp in Celsius for Channel1-4 - Response: jch1temp,ch2temp,ch3temp,ch4temp#
74 #define MDCP_SET_CH1_OFFSET_CMD ":29%01.2f#" // Set ch1offset value (expects a float) - Response: none
75 #define MDCP_SET_CH2_OFFSET_CMD ":30%01.2f#" // Set ch2offset value (expects a float) - Response: none
76 #define MDCP_SET_CH3_OFFSET_CMD ":31%01.2f#" // Set ch3offset value (expects a float) - Response: none
77 #define MDCP_SET_CH4_OFFSET_CMD ":32%01.2f#" // Set ch4offset value (expects a float) - Response: none
78 #define MDCP_ZERO_ALL_CH_OFFSET_CMD ":33#" // Clear ch1offset-ch4offset values to 0.0 - Response: none
79 #define MDCP_GET_ALL_CH_OFFSET_CMD ":34#" // Get the ch1offset=ch4offset values - Response: kch1offset,ch2offset,ch3offset#
80 #define MDCP_SET_CH_100_CMD ":35%d#" // Set boost 100% to channel x - Response: none
81 #define MDCP_GET_CH_OVERIDE_CMD ":36%d#" // Get Override mode for channel x - Response: return value is 0 or 1
82 #define MDCP_SET_CONTROLLER_MODE_CMD ":37%d#" // Set controller mode – controlledbyapp = 0 or 1 - Response: none
83 #define MDCP_GET_CONTROLLER_MODE_CMD ":38#" // Get controlledbyapp setting - Response: ux#
84 #define MDCP_RESET_CH_100_CMD ":39%d#" // Turn off 100% boost to channel number x [1-4, 5=all] - Response: none
85 #define MDCP_GET_ALL_CH_POWER_CMD ":40#" // Get the power settings for ch1/ch2/ch3/ch4 - Response: lch1pwr,ch2pwr,ch3pwr,ch4pwr#
86 #define MDCP_SET_CH3_MODE_CMD ":41%d#" // Set the Channel 3 mode (0=disabled, 1=dewstrap1, 2=dewstrap2, 3=Manual, 4=use temp probe3) - Response: none
87 #define MDCP_GET_CH3_MODE_CMD ":42#" // Get the Channel 3 mode (0=disabled, 1=dewstrap1, 2=dewstrap2, 3=Manual, 4=use temp probe3) - Response: mmode#
88 #define MDCP_SET_CH3_MANUAL_POWER_CMD ":43%d#" // Set the Channel 3 power to a manual seting of Num - Response: none
89 
90 /**************************** myDCP4ESP32 Command Responses **************************/
91 
92 #define MDCP_RESPONSE_LENGTH 64 // Response buffer size
93 #define MDCP_GET_CONTROLLER_CODE_RES "0%s" // Get controller code - Response: 0code#
94 #define MDCP_GET_VERSION_RES "1%d" // Get version number - Respnse: 1version#
95 #define MDCP_REBOOT_RES "" // Reboot controller - Response: none
96 #define MDCP_GET_TRACKING_MODE_RES "3%d" // Get tracking mode (1=Ambient, 2=Dewpoint, 3=Midpoint) - Response: 3trackingmode#
97 #define MDCP_SET_TRACKING_MODE_RES "" // Set tracking mode (1=Ambient, 2=Dewpoint, 3=Midpoint) - Response: none
98 #define MDCP_GET_TRACKING_OFFSET_RES "5%d" // Get tracking mode offset value (-4 to +3) - Response: 5trackingmodeoffset#
99 #define MDCP_ZERO_TRACKING_OFFSET_RES "" // Set tracking mode offset to 0 - Response: none
100 #define MDCP_DECREASE_TRACKING_OFFSET_RES "" // Decrease tracking mode offset - Response: none
101 #define MDCP_INCREASE_TRACKING_OFFSET_RES "" // Increase tracking mode offset - Response: none
102 #define MDCP_GET_NUMBER_PROBES_RES "9%d" // Get the number of temperature probes attached - Response: 9number_of_probes#
103 #define MDCP_SAVE_CONTROLLER_SETTINGS_RES "" // Write variables immediately to SPIFFS - Response: none
104 #define MDCP_SET_DEFAULT_SETTINGS_RES "" // controller settings to default values - Response: none
105 #define MDCP_GET_AMBIENT_TEMPERATURE_RES "a%f" // Get the Ambient temperature - Response: avalue#
106 #define MDCP_GET_AMBIENT_OFFSET_RES "b%f" // Get the Ambient offset - Response: bvalue#
107 #define MDCP_SET_AMBIENT_OFFSET_RES "" // Set the Ambient offset, Num is a value between +3 and -4 - Response: none
108 #define MDCP_GET_HUMIDITY_RES "c%f" // Get the Relative Humidity % - Resonse: cvalue#
109 #define MDCP_GET_DEWPOINT_RES "d%f" // Get the Dew Point - Response: dvalue#
110 #define MDCP_GET_TEMP_MODE_RES "e%d" // Get Temp Mode, 0=Celsius, 1=Fahrenheit - Response: eTempMode
111 #define MDCP_SET_TEMP_MODE_C_RES "" // Display in Celsius - Response: none
112 #define MDCP_SET_TEMP_MODE_F_RES "" // Display in Fahrenheit - Response: none
113 #define MDCP_SET_TRACKING_OFFSET_RES "" // Set the tracking mode offset value [-4 to +3] - Response: none
114 #define MDCP_GET_MAC_ADDRESS_RES "h%s" // Get MAC address - Response: hString#
115 #define MDCP_GET_IP_ADDRESS_RES "i%s" // Get IP address - Response: iString#
116 #define MDCP_GET_CHANNEL_TEMPS_RES "j%f,%f,%f,%f" // Get the temp in Celsius for Channel1-4 - Response: jch1temp,ch2temp,ch3temp,ch4temp#
117 #define MDCP_SET_CH1_OFFSET_RES "" // Set ch1offset value (expects a float) - Response: none
118 #define MDCP_SET_CH2_OFFSET_RES "" // Set ch2offset value (expects a float) - Response: none
119 #define MDCP_SET_CH3_OFFSET_RES "" // Set ch3offset value (expects a float) - Response: none
120 #define MDCP_SET_CH4_OFFSET_RES "" // Set ch4offset value (expects a float) - Response: none
121 #define MDCP_ZERO_ALL_CH_OFFSET_RES "" // Clear ch1offset-ch4offset values to 0.0 - Response: none
122 #define MDCP_GET_ALL_CH_OFFSET_RES "k%f,%f,%f,%f" // Get the ch1offset=ch4offset values - Response: kch1offset,ch2offset,ch3offset#
123 #define MDCP_SET_CH_100_RES "" // Set boost 100% to channel x - Response: none
124 #define MDCP_GET_CH_OVERIDE_RES "t%d" // Get Override mode for channel x - Response: return value is 0 or 1
125 #define MDCP_SET_CONTROLLER_MODE_RES "" // Set controller mode – controlledbyapp = 0 or 1 - Response: none
126 #define MDCP_GET_CONTROLLER_MODE_RES "u%d" // Get controlledbyapp setting - Response: ux#
127 #define MDCP_RESET_CH_100_RES "" // Turn off 100% boost to channel number x [1-4, 5=all] - Response: none
128 #define MDCP_GET_ALL_CH_POWER_RES "l%d,%d,%d,%d" // Get the power settings for ch1/ch2/ch3/ch4 - Response: lch1pwr,ch2pwr,ch3pwr,ch4pwr#
129 #define MDCP_SET_CH3_MODE_RES "" // Set the Channel 3 mode (0=disabled, 1=dewstrap1, 2=dewstrap2, 3=Manual, 4=use temp probe3) - Response: none
130 #define MDCP_GET_CH3_MODE_RES "m%d" // Get the Channel 3 mode (0=disabled, 1=dewstrap1, 2=dewstrap2, 3=Manual, 4=use temp probe3) - Response: mmode#
131 #define MDCP_SET_CH3_MANUAL_POWER_RES "" // Set the Channel 3 power to a manual seting of Num - Response: none
132 
133 /******************************************************************************/
134 
136 {
137  public:
138  MyDCP4ESP();
139  virtual ~MyDCP4ESP() = default;
140 
141  virtual const char *getDefaultName() override;
142  virtual bool initProperties() override;
143  virtual bool updateProperties() override;
144  virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
145  virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
146  virtual void TimerHit() override;
147 
152  typedef enum
153  {
154  CONNECTION_NONE = 1 << 0,
156  CONNECTION_TCP = 1 << 2
158 
159  private:
160  int timerIndex;
161  int myDCP4Firmware = 0;
162  bool ch3ManualPower = false;
163  float channelActive[4] = {1};
164  int msleep( long duration);
165  bool sendCommand(const char *cmd, char *response);
166  bool Handshake();
167  bool Ack();
168  bool rebootController();
169  bool readSettings();
170  bool setChannelOffset(unsigned int channel, float value);
171  bool setAmbientOffset(float value);
172  bool setTrackingMode(unsigned int value);
173  bool setTrackingOffset(int value);
174  bool setCh3Mode(unsigned int value);
175  bool setCh3Output(unsigned int value);
176  bool setChannelBoost(unsigned int channel, unsigned int value);
177  bool getActiveChannels();
178 
179  Connection::Serial *serialConnection { nullptr };
180  Connection::TCP *tcpConnection { nullptr };
181 
182  int PortFD { -1 };
183 
184  uint8_t mdcpConnection { CONNECTION_SERIAL | CONNECTION_TCP };
185 
186  // MyDCP4ESP Timeouts
187  static const uint8_t MDCP_READ_TIMEOUT { 10 }; // seconds
188  static const long MDCP_SMALL_DELAY { 50 }; // 50ms delay from send command to read response
189 
190  enum
191  {
192  CH3MODE_DISABLED,
193  CH3MODE_CH1POWER,
194  CH3MODE_CH2POWER,
195  CH3MODE_MANUAL,
196  CH3MODE_CH3TEMP
197  };
198 
199  INDI::PropertyNumber ChannelPowerNP{4};
200  INDI::PropertySwitch TempProbeFoundSP{4};
201  INDI::PropertyNumber TemperatureNP{4};
202  INDI::PropertyNumber ChannelOffsetNP{4};
203  INDI::PropertySwitch ChannelBoostSP{5};
204  INDI::PropertyNumber AmbientTemperatureNP{1};
205  INDI::PropertyNumber AmbientOffsetNP{1};
206  INDI::PropertyNumber HumidityNP{1};
207  INDI::PropertyNumber DewpointNP{1};
208  INDI::PropertySwitch TrackingModeSP{3};
209  INDI::PropertyNumber TrackingOffsetNP{1};
210  INDI::PropertyNumber Ch3ManualPowerNP{1};
211  INDI::PropertySwitch Ch3ModeSP{5};
212  INDI::PropertySwitch RebootSP{1};
213  INDI::PropertyText CheckCodeTP{1};
214  INDI::PropertyNumber FWversionNP{1};
215 
216 };
The Serial class manages connection with serial devices including Bluetooth. Serial communication is ...
The TCP class manages connection with devices over the network via TCP/IP. Upon successfull connectio...
Definition: connectiontcp.h:38
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
virtual const char * getDefaultName() override
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: mydcp4esp32.cpp:53
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual ~MyDCP4ESP()=default
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
@ CONNECTION_NONE
Definition: mydcp4esp32.h:154
@ CONNECTION_TCP
Definition: mydcp4esp32.h:156
@ CONNECTION_SERIAL
Definition: mydcp4esp32.h:155
ISState
Switch state.
Definition: indiapi.h:150
__u8 cmd[4]
Definition: pwc-ioctl.h:2