MIRA
CANSDOPlugin.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 MetraLabs GmbH (MLAB), GERMANY.
3  * All rights reserved.
4  * Contact: info@MetraLabs.com
5  *
6  * Commercial Usage:
7  * Licensees holding valid commercial licenses may use this file in
8  * accordance with the commercial license agreement provided with the
9  * software or, alternatively, in accordance with the terms contained in
10  * a written agreement between you and MetraLabs.
11  *
12  * GNU General Public License Usage:
13  * Alternatively, this file may be used under the terms of the GNU
14  * General Public License version 3.0 as published by the Free Software
15  * Foundation and appearing in the file LICENSE.GPL3 included in the
16  * packaging of this file. Please review the following information to
17  * ensure the GNU General Public License version 3.0 requirements will be
18  * met: http://www.gnu.org/copyleft/gpl.html.
19  * Alternatively you may (at your option) use any later version of the GNU
20  * General Public License if such license has been publicly approved by
21  * MetraLabs (or its successors, if any).
22  *
23  * IN NO EVENT SHALL "MLAB" BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
24  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE
25  * OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLABS" HAS BEEN
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * "MLAB" SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
31  * "AS IS" BASIS, AND "MLAB" HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
32  * SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
33  */
34 
42 #ifndef _MLAB_CANSDOPLUGIN_H_
43 #define _MLAB_CANSDOPLUGIN_H_
44 
46 
47 #include <gui/views/CANToolsView.h>
49 
50 #include <QTableWidget>
51 #include <QVBoxLayout>
52 #include <QPushButton>
53 #include <QCompleter>
54 #include <QLineEdit>
55 #include <QComboBox>
56 #include <QStringListModel>
57 #include <QErrorMessage>
58 
59 namespace mira { namespace can {
60 
62 
67  public CANToolsViewPlugin
68 {
69  Q_OBJECT;
71  ("Name", "CAN SDO viewer")
72  ("Category", "CANOpen")
73  ("Description", "Read and write SDOs")
74  ("Title", "SDOs"))
75 
76 public:
77 
79  template<typename Reflector>
80  void reflect(Reflector& r)
81  {
82  r.member("SDOMap",
83  getter<std::vector<SDOInfo>>(&CANSDOPlugin::getSDOInfo, this),
84  setter<std::vector<SDOInfo>>(&CANSDOPlugin::setSDOInfo, this), "");
85  r.member("ColumnWidth",
86  getter<std::vector<int>>(&CANSDOPlugin::getColumnWidth, this),
87  setter<std::vector<int>>(&CANSDOPlugin::setColumnWidth, this), "", serialization::IgnoreMissing());
88  }
89 
90  bool start(uint8 node, mira::can::CANOpenNodeInfo nodeInfo);
91  bool stop(uint8 node);
92 
93 private:
94 
95  struct SDOInfo
96  {
97  enum Access
98  {
99  NONE = 0x00,
100  READ_SDO = 0x01,
101  WRITE_SDO = 0x02
102  };
104 
105  template <typename Reflector>
106  void reflect(Reflector& r)
107  {
108  r.member("Module",
109  getter<std::string>(boost::bind(&SDOInfo::get<uint16>, this, &module)),
110  setter<std::string>(boost::bind(&SDOInfo::set<uint16>, this, _1, &module)), "");
111  r.member("Index",
112  getter<std::string>(boost::bind(&SDOInfo::get<uint16>, this, &index)),
113  setter<std::string>(boost::bind(&SDOInfo::set<uint16>, this, _1, &index)), "");
114  r.member("Sub",
115  getter<std::string>(boost::bind(&SDOInfo::get<uint16>, this, &sub)),
116  setter<std::string>(boost::bind(&SDOInfo::set<uint16>, this, _1, &sub)), "");
117  r.member("Length",
118  getter<std::string>(&SDOInfo::getLength, this),
119  setter<std::string>(&SDOInfo::setLength, this), "");
120  r.member("Access",
121  getter<std::string>(&SDOInfo::getAccess, this),
122  setter<std::string>(&SDOInfo::setAccess, this), "");
123  r.member("Description", description, "");
124  r.member("Type",
125  getter<std::string>(&SDOInfo::getType, this),
126  setter<std::string>(&SDOInfo::setType, this), "");
127  }
128 
129  template<typename T>
130  std::string get(const T* ptr) const
131  {
132  return (boost::format("0x%|02X|")%*ptr).str();
133  }
134 
135  template<typename T>
136  void set(const std::string& v, T* ptr)
137  {
138  *ptr = fromString<FromHex<T>>(v);
139  }
140 
141  std::string getLength() const
142  {
143  switch(length)
144  {
145  case 0:
146  return "int8";
147  break;
148  case 1:
149  return "uint8";
150  break;
151  case 2:
152  return "int16";
153  break;
154  case 3:
155  return "uint16";
156  break;
157  case 4:
158  return "int32";
159  break;
160  case 5:
161  return "uint32";
162  break;
163  }
164  return "";
165  }
166 
167  void setLength(const std::string& v)
168  {
169  if (v == "int8")
170  length = 0;
171  if (v== "uint8")
172  length = 1;
173  if (v== "int16")
174  length = 2;
175  if (v== "uint16")
176  length = 3;
177  if (v== "int32")
178  length = 4;
179  if (v== "uint32")
180  length = 5;
181  }
182 
183  std::string getAccess() const
184  {
185  std::string a = "none";
186  if ((access & READ_SDO) && (access & WRITE_SDO))
187  a = "rw";
188  else if ((access & READ_SDO))
189  a = "ro";
190  else if ((access & WRITE_SDO))
191  a = "wo";
192  return a;
193  }
194 
195  void setAccess(const std::string& v)
196  {
197  access = NONE;
198  if (v == "ro")
199  access = READ_SDO;
200  if (v == "wo")
201  access = WRITE_SDO;
202  if (v == "rw")
203  access = READ_SDO | WRITE_SDO;
204  }
205 
206  std::string getType() const
207  {
208  switch(type)
209  {
210  case 0:
211  return "dec";
212  break;
213  case 1:
214  return "hex";
215  break;
216  case 2:
217  return "bin";
218  break;
219  }
220  return "";
221  }
222 
223  void setType(const std::string& v)
224  {
225  if (v == "dec")
226  type = 0;
227  if (v== "hex")
228  type = 1;
229  if (v== "bin")
230  type = 2;
231  }
232 
233  uint16 module;
234  uint16 index;
235  uint16 sub;
236  int length;
237  Access access;
238  std::string description;
239  int type;
240 
241  };
242 
243  void createComboboxes(int row);
244  void restore(const std::vector<SDOInfo>& sdoMap);
245  SDOInfo getInfoFromRow(int row);
246 
247 public slots:
248 
249  void load();
250  void save();
251  void clear();
252  void addRow();
253  void readSDO();
254  void writeSDO();
255  void selectionChanged(int cbIndex);
256 
257 protected:
258 
259  void setSDOInfo(const std::vector<SDOInfo>& info);
260  std::vector<SDOInfo> getSDOInfo();
261  void setColumnWidth(const std::vector<int>& width);
262  std::vector<int> getColumnWidth();
263 
264  int findObject(QObject* widget);
265  void readRow(int row);
266 
267  virtual void initialize();
268 
269  QTableWidget* mList;
270  bool mLoading;
271 };
272 
274 
275 }} // namespaces
276 
277 #endif /* CANSDOPLUGIN_H_ */
std::vector< SDOInfo > getSDOInfo()
TEigenFormat< Derived > format(Eigen::MatrixBase< Derived > &matrix, Eigen::IOFormat format=EigenFormat::matlab())
The base class for all CAN tools.
Definition: CANToolsViewPlugin.h:61
A view for CAN tools.
Setter< T > setter(void(*f)(const T &))
#define MIRA_ENUM_TO_FLAGS_INCLASS(EnumType)
Plugin for showing SDO informations.
Definition: CANSDOPlugin.h:66
#define MIRA_META_OBJECT(classIdentifier, MetaInfo)
PropertyHint type(const std::string &t)
void setSDOInfo(const std::vector< SDOInfo > &info)
Getter< T > getter(T(*f)())
void reflect(Reflector &r, LogRecord &record)
#define MLAB_CANGUI_EXPORT
Definition: CANGUIExports.h:57
QTableWidget * mList
Definition: CANSDOPlugin.h:269
bool mLoading
Definition: CANSDOPlugin.h:270
A struct, which contains standard information about a CANopen node.
Definition: CANOpenDefs.h:165
std::vector< int > getColumnWidth()
The base class for CAN tools.
void setColumnWidth(const std::vector< int > &width)