ParameterList.h 5.26 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 45 46 47
class ParameterList
{
public:

    class const_iterator
48
    {
49
        friend class ParameterList;
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
    public:
        inline const_iterator() {}
        const_iterator(const const_iterator& other);

        const_iterator& operator+=(int i) {
            index += i;
            return *this;
        }
        bool operator<(const const_iterator& other) const {
            return (this->paramList == other.paramList)
                   &&(this->index<other.index);
        }
        bool operator==(const const_iterator& other) const {
            return (this->paramList == other.paramList)
                   &&(this->index==other.index);
        }
        bool operator!=(const const_iterator& other) const {
            return !((*this) == other);
        }
        const Parameter& operator*() const {
            return paramList[index];
        }
        const Parameter* operator->() const {
            return &paramList[index];
        }

        const_iterator& operator++() {
            ++index;
            return *this;
        }
    private:
        const_iterator(QList<Parameter>);
        QList<Parameter> paramList;
        int index;
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 170 171 172 173


    ParameterList();
    ~ParameterList();

    /** Count the number of parameters in the list.
      \return Total number of parameters
      */
    int count();

    /** 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);
    }

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

protected:
    /** 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
       */
    QMap<int, QMap<QGCParamID, Parameter> > *params;
    /**
      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;
    /**
      List of parameters which are necessary to control the servos.
      */
    QStringList *reqdServoParams;
    /**
      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);

    /**
      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);
};
174 175
}
#endif // PARAMETERLIST_H