summaryrefslogtreecommitdiffstats
path: root/indi-celestronaux/auxproto.h
blob: 3593eee6a452721cca14a639b5556c76f0ff3a73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
    Celestron Aux Command

    Copyright (C) 2020 Paweł T. Jochym
    Copyright (C) 2020 Fabrizio Pollastri
    Copyright (C) 2021 Jasem Mutlaq

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#pragma once

#include <vector>
#include <stddef.h>
#include <stdint.h>

typedef std::vector<uint8_t> AUXBuffer;

enum AUXCommands
{
    MC_GET_POSITION      = 0x01,
    MC_GOTO_FAST         = 0x02,
    MC_SET_POSITION      = 0x04,
    MC_SET_POS_GUIDERATE = 0x06,
    MC_SET_NEG_GUIDERATE = 0x07,
    MC_LEVEL_START       = 0x0b,
    MC_LEVEL_DONE        = 0x12,
    MC_SLEW_DONE         = 0x13,
    MC_GOTO_SLOW         = 0x17,
    MC_SEEK_DONE         = 0x18,
    MC_SEEK_INDEX        = 0x19,
    MC_MOVE_POS          = 0x24,
    MC_MOVE_NEG          = 0x25,
    MC_AUX_GUIDE         = 0x26,
    MC_AUX_GUIDE_ACTIVE  = 0x27,
    MC_ENABLE_CORDWRAP   = 0x38,
    MC_DISABLE_CORDWRAP  = 0x39,
    MC_SET_CORDWRAP_POS  = 0x3a,
    MC_POLL_CORDWRAP     = 0x3b,
    MC_GET_CORDWRAP_POS  = 0x3c,
    MC_SET_AUTOGUIDE_RATE = 0x46,
    MC_GET_AUTOGUIDE_RATE = 0x47,
    GET_VER              = 0xfe,
    GPS_GET_LAT          = 0x01,
    GPS_GET_LONG         = 0x02,
    GPS_GET_DATE         = 0x03,
    GPS_GET_YEAR         = 0x04,
    GPS_GET_TIME         = 0x33,
    GPS_TIME_VALID       = 0x36,
    GPS_LINKED           = 0x37
};

enum AUXTargets
{
    ANY   = 0x00,
    MB    = 0x01,
    HC    = 0x04,
    HCP   = 0x0d,
    AZM   = 0x10,
    ALT   = 0x11,
    APP   = 0x20,
    GPS   = 0xb0,
    WiFi  = 0xb5,
    BAT   = 0xb6,
    CHG   = 0xb7,
    LIGHT = 0xbf
};

#define CAUX_DEFAULT_IP   "1.2.3.4"
#define CAUX_DEFAULT_PORT 2000

void logBytes(unsigned char *buf, int n, const char *deviceName, uint32_t debugLevel);

class AUXCommand
{
    public:
        AUXCommand();
        AUXCommand(const AUXBuffer &buf);
        AUXCommand(AUXCommands command, AUXTargets source, AUXTargets destination, const AUXBuffer &data);
        AUXCommand(AUXCommands command, AUXTargets source, AUXTargets destination);

        ///////////////////////////////////////////////////////////////////////////////
        /// Buffer Management
        ///////////////////////////////////////////////////////////////////////////////
        void fillBuf(AUXBuffer &buf);
        void parseBuf(AUXBuffer buf);
        void parseBuf(AUXBuffer buf, bool do_checksum);

        ///////////////////////////////////////////////////////////////////////////////
        /// Getters
        ///////////////////////////////////////////////////////////////////////////////
        const AUXTargets &source() const
        {
            return m_Source;
        }
        const AUXTargets &destination() const
        {
            return m_Destination;
        }
        const AUXBuffer &data() const
        {
            return m_Data;
        }
        AUXCommands command() const
        {
            return m_Command;
        }
        size_t dataSize() const
        {
            return m_Data.size();
        }
        const char * commandName() const
        {
            return commandName(m_Command);
        }

        ///////////////////////////////////////////////////////////////////////////////
        /// Set and Get data
        ///////////////////////////////////////////////////////////////////////////////
        /**
         * @brief getData Parses data packet and convert it to a 32bit unsigned integer
         * @param bytes How many bytes to interpret the data.
         * @return
         */
        uint32_t getData();
        void setData(uint32_t value, uint8_t bytes = 3);

        ///////////////////////////////////////////////////////////////////////////////
        /// Check sum
        ///////////////////////////////////////////////////////////////////////////////
        uint8_t checksum(AUXBuffer buf);

        ///////////////////////////////////////////////////////////////////////////////
        /// Logging
        ///////////////////////////////////////////////////////////////////////////////
        const char * commandName(AUXCommands command) const;
        const char * moduleName(AUXTargets n);
        int responseDataSize();
        void logResponse();
        void logCommand();
        static void setDebugInfo(const char *deviceName, uint8_t debugLevel);

        static uint8_t DEBUG_LEVEL;
        static char DEVICE_NAME[64];

    private:
        uint8_t len {0};
        bool valid {false};

        AUXCommands m_Command;
        AUXTargets m_Source, m_Destination;
        AUXBuffer m_Data;



};