ParameterList.h 5.44 KB
Newer Older
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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

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

    QGROUNDCONTROL 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

#ifndef PARAMETERLIST_H
#define PARAMETERLIST_H

#include <QMap>
28
#include <QVector>
29 30 31 32 33
#include <QIODevice>
#include <QFile>
#include <QDir>
#include <QApplication>
#include <QtXml>
34
#include <QStringList>
35 36 37 38 39 40

#include "mavlink_types.h"
#include "QGCParamID.h"
#include "Parameter.h"
#include "OpalRT.h"

41 42
namespace OpalRT
{
43 44
    class ParameterList
    {
45 46
    public:

47 48
        class const_iterator
        {
49 50
            friend class ParameterList;

51
        public:
52 53 54 55
            inline const_iterator() {}
            const_iterator(const const_iterator& other);

            const_iterator& operator+=(int i) {index += i; return *this;}
56
            bool operator<(const const_iterator& other) const {return (this->paramList == other.paramList)
57
                                                         &&(this->index<other.index);}
58
            bool operator==(const const_iterator& other) const {return (this->paramList == other.paramList)
59
                                                          &&(this->index==other.index);}
60
            bool operator!=(const const_iterator& other) const {return !((*this) == other);}
61 62 63 64
            const Parameter& operator*() const {return paramList[index];}
            const Parameter* operator->() const {return &paramList[index];}

            const_iterator& operator++() {++index; return *this;}
65
        private:
66 67 68
            const_iterator(QList<Parameter>);
            QList<Parameter> paramList;
            int index;
69
        };
70 71


72 73
        ParameterList();
        ~ParameterList();
74 75 76 77

        /** Count the number of parameters in the list.
          \return Total number of parameters
          */
78
        int count();
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        /** Find p in the list and return its index.
          \note In order to use this index to look up p, the component is also needed.
          \return the index of p or -1 if p is not found
          \example
          \code
          int compid = OpalRT::CONTROLLER_ID;
          Parameter p("simulinkpath", "simulinkparamname", compid, QGCParamID("PID_GAIN"));
          ParameterList pList;
          if ((int index=pList.indexOf(p)) != -1)
             qDebug() << "PID_GAIN is at index " << index;
          \endcode          
          */
        int indexOf(const Parameter& p);
        bool contains(int compid, QGCParamID paramid) const {return (*params)[compid].contains(paramid);}

        /// Get a parameter from the list
        const Parameter getParameter(int compid, QGCParamID paramid) const {return (*params)[compid][paramid];}
        Parameter& getParameter(int compid, QGCParamID paramid) {return (*params)[compid][paramid];}
        const Parameter getParameter(int compid, int index) const {return *((*paramList)[compid][index]);}        

        /** Convenient syntax for calling OpalRT::Parameter::getParameter() */
        Parameter& operator()(int compid, QGCParamID paramid) {return getParameter(compid, paramid);}
        Parameter& operator()(uint8_t compid, QGCParamID paramid) {return getParameter(static_cast<int>(compid), paramid);}
103 104 105

        const_iterator begin() const;
        const_iterator end() const;
106

107
    protected:
108 109 110 111 112 113 114 115
        /** Store the parameters mapped by componentid, and paramid.
           \code
           // Look up a parameter
           int compid = 1;
           QGCParamID paramid("PID_GAIN");
           Parameter p = params[compid][paramid];
           \endcode
           */
116
        QMap<int, QMap<QGCParamID, Parameter> > *params;
117 118 119 120 121 122
        /**
          Store pointers to the parameters to allow fast lookup by index.
          This variable may be changed to const pointers to ensure all changes
          are made through the map container.
          */
        QList<QList<Parameter*> > *paramList;
123 124 125 126
        /**
          List of parameters which are necessary to control the servos.
          */
        QStringList *reqdServoParams;
127 128 129 130 131
        /**
          Get the list of available parameters from Opal-RT.
          \param[out] opalParams Map of parameter paths/names to ids which are valid in Opal-RT
          */
        void getParameterList(QMap<QString, unsigned short>* opalParams);
132 133 134 135 136 137 138 139 140 141 142 143 144

        /**
          Open a file for reading in the xml config data
          */
        bool open(QString filename=QString());
        /**
          Attempt to read XML configuration data from device
          \param[in] the device to read the xml data from
          \return true if the configuration was read successfully, false otherwise
          */
        bool read(QIODevice *device);

        void parseBlock(const QDomElement &block);
145 146 147
    };
}
#endif // PARAMETERLIST_H