direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
inifile.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #include "inifile.h"
22 
24 {
25  //------------------------------------------------------------------
26  // timer stuff
27  //------------------------------------------------------------------
28  //commandTimer = new QTimer();
29  //connect(commandTimer, SIGNAL(timeout()), SLOT(commandClock()));
30 
31  //------------------------------------------------------------------
32  // set the name of the programms ini-file
33  //------------------------------------------------------------------
34  mainIniFilename = "not_set";
35 
36  // the setings object is created in the setInifileName() method !
37 
38 
39  // Inizialize the (script) command counter
40  //commandCounter = 0;
41 
42  // Inizialize the last command for the robot
43  //lastCommand = "stop";
44 
45  // Name of the section to search for in the ini-file (script exection)
46  //iniSection = "Sequence1";
47 }
48 
49 
51 {
52 }
53 
54 
56 {
57  //
58  // check the current path from which the program started
59  //
60 
61  // get the current path and store it
62  programPath = QDir::currentPath();
63 
64  return programPath;
65 }
66 
67 
69 {
70  QString filename;
71 
72 
73  if (mainIniFilename == "not_set")
74  {
75  qDebug("Path for mainIniFilename not set in Inifile::CheckFiles().");
76  return false;
77  }
78 
79 
80  // path + filename for ini-file
81  filename = programPath;
82  filename.append("/");
83  filename.append(mainIniFilename);
84 
85  // check if ini-file exists
86  if (QFile::exists(filename) == false)
87  {
88  return false;
89  }
90  else
91  {
92  return true;
93  }
94 /*
95  // This variable holds the version of the Windows operating system on which the application is run (Windows only).
96  WinVersion QSysInfo::WindowsVersion;
97 */
98 
99 }
100 
101 
103 {
104  return mainIniFilename;
105 }
106 
107 
108 void Inifile::writeSetting(QString group, QString name, int value)
109 {
110  settings->beginGroup(group);
111 
112  // save setting
113  settings->setValue(name, value);
114 
115  settings->endGroup();
116 }
117 
118 
119 void Inifile::writeSetting(QString group, QString name, QString value)
120 {
121  settings->beginGroup(group);
122 
123  // save setting
124  settings->setValue(name, value);
125 
126  settings->endGroup();
127 }
128 
129 
130 int Inifile::readSetting(QString group, QString name)
131 {
132  // string for group+value in inifile
133  QString iniSection = group + "/" + name;
134 
135  // read value from ini-file
136  // return value or -1 on error.
137  return settings->value(iniSection, -1).toInt();
138 }
139 
140 
141 QString Inifile::readString(QString group, QString name)
142 {
143  // check if ini-file is writable
144  if (settings->isWritable() == false)
145  {
146  return "error2";
147  }
148 
149  // string for group+name in inifile
150  QString iniSection = group + "/" + name;
151 
152  if (settings->contains(iniSection) == false)
153  {
154  return "error1";
155  }
156 
157  // read value from ini-file
158  // return String
159  return settings->value(iniSection, name).toString();
160 }
161 
162 
163 float Inifile::readFloat(QString group, QString name)
164 {
165 
166  // string for group+value in inifile
167  QString iniSection = group + "/" + name;
168 
169  // read value from ini-file
170  // return the value or -1 on error.
171  return settings->value(iniSection, -1).toDouble();
172 }
173 
174 
175 /*
176 void Inifile::startStopCommandClock(int command)
177 {
178  //
179  // start or stop the command clock
180  //
181 
182  if (command == START)
183  {
184  // If timer is not running, start it!
185  if ( !commandTimer->isActive() )
186  {
187  // timeout every 1000 milli seconds
188  commandTimer->start(1000);
189  }
190  //ui.textEdit1->append("Command timer started...");
191  }
192  else
193  {
194  // If timer is running, stop it!
195  if ( commandTimer->isActive() )
196  {
197  // stop timer
198  commandTimer->stop();
199  }
200  //ui.textEdit1->append("Command timer stopped.");
201  }
202 }
203 */
204 
205 
206 /*
207 void Inifile::commandClock()
208 {
209  //------------------------------------------------------------
210  // Timer for processing the commands from the ini-file
211  //------------------------------------------------------------
212  //
213  //
214  // A T T E N T I O N:
215  //
216  // For the "duration" of the commands in the ini-file, an int-type is used.
217  // An int requires 32 bit and last from -2.147.483.648 to +2.147.483.648
218  //
219  // The measure unit for this duration is "seconds".
220  //
221  // 2.147.483.648 seconds are 35.791.394 minutes
222  // are 596.523 hours
223  // are 2.4855 days
224  // are 68 years.
225  //
226  // this should be enough for one command.
227  // I'm not sure, if the battery lasts for this time... ;-)
228  //
229  // These seconds are the duration for the command - how long one command
230  // should be executed.
231  // e.g. "forward 10" means "drive 10 seconds forward".
232  //
233  //----------------------------------------------------------------------------
234  QString iniKey;
235  QString iniString;
236  static int scriptDuration = 0;
237 
238 
239  // commandCounter + 1
240  commandCounter++;
241 
242  // String for the "Key" to read in the ini-file
243  iniKey = QString("command%1").arg(commandCounter);
244 
245  // read "Sequence1/Command1"
246  iniString = settings->value(iniSection + "/" + iniKey, "error").toString();
247 
248 
249  // on error, show message
250  if (iniString == "error")
251  {
252  // show message
253  ui.textEdit1->append("<font color=\"#FF0000\">Error reading ini-file!!</font>");
254  // stop timer (and so stop reading ini-file)
255  startStopCommandClock(STOP);
256 
257  scriptDuration = 0;
258 
259  return;
260  }
261  else
262  {
263  //----------------------------
264  // if reading was okay
265  //----------------------------
266  if (iniString != "error")
267  {
268  int commandDuration = 0;
269  QString command;
270  QString str;
271 
272  // show read result
273  //ui.textEdit1->append(iniString);
274 
275  // Find the instruction in the string (get from the first character to the first found space char)
276  //
277  // copy iniString to command
278  command = iniString;
279  // copy string from first char till the last space
280  command.truncate(iniString.lastIndexOf(" "));
281 
282  // show command
283  //str = QString("command: '%1'").arg(command);
284  //ui.textEdit1->append(str);
285 
287  //
288  // copy the rest of the iniString from last blank till the end
289  str = iniString.mid(iniString.lastIndexOf(" ")+1);
290 
291  // Attention: This duration-string is converted to int!!
292  commandDuration = str.toInt();
293 
294  // show commandDuration
295  //str = QString("commandDuration: %1").arg(commandDuration);
296  //ui.textEdit1->append(str);
297 
298 
299  //-------------------------------------------------
300  // "do" the commands
301  //-------------------------------------------------
302 
303  // start driving
304  if (command == "start")
305  {
306  // Set timer to the duration of the command
307  // That means, that the next command is read, when the next timer event occurs.
308  commandTimer->setInterval(commandDuration * 1000);
309 
310  // start MotorTimer
311  startStopMotorClock(START);
312 
313  ui.textEdit1->append("Motor clock started.");
314 
315  return;
316  }
317 
318  //
319  // stop driving
320  //
321  if (command == "stop")
322  {
323  // turn motors OFF
324  motorControl(MOTOR1, OFF, SAME);
325  motorControl(MOTOR2, OFF, SAME);
326  ui.textEdit1->append("Stopped.");
327 
328  // Set timer to the duration of the command
329  // That means, that the next command is read, when the next timer event occurs.
330  commandTimer->setInterval(commandDuration * 1000);
331 
332  // when normal script operation (no change maneuver),
333  // do the following
334  if (iniSection.startsWith("Sequence") == true)
335  {
336  //----------------
337  // stop MotorTimer
338  //----------------
339  startStopMotorClock(STOP);
340 
341  // reset command counter, because the last command is always "stop"
342  commandCounter = 0;
343 
344  // show script duration
345  str = QString("Script duration: %1 seconds").arg(scriptDuration);
346  ui.textEdit1->append(str);
347 
348  // reset commandDuration
349  scriptDuration = 0;
350 
351  // stop timer (and so stop reading ini-file)
352  startStopCommandClock(STOP);
353 
354  return;
355  }
356  else
357  {
358  // when method is called from "check For Obstacle"
359  // do nothing (don't stop driving and don't stop reading the script)
360  return;
361  }
362  }
363 
364 
365  // drive forward
366  if (command == "forward")
367  {
368  motorControl(MOTOR1, ON, CLOCKWISE);
369  motorControl(MOTOR2, ON, CLOCKWISE);
370 
371  // Set timer to the duration of the command (like "drive forward 10 secs").
372  // That means, that the next command is read, when the next timer event occurs.
373  // Meanwhile the robot "does" the command
374  commandTimer->setInterval(commandDuration * 1000);
375 
376  // for measuring the script duration
377  scriptDuration += commandDuration;
378 
379  // show command
380  str = QString("Driving %1 for %2 seconds.").arg(command).arg(commandDuration);
381  ui.textEdit1->append(str);
382 
383  return;
384  }
385 
386  // drive backwords
387  if (command == "backward")
388  {
389  motorControl(MOTOR1, ON, COUNTERCLOCKWISE);
390  motorControl(MOTOR2, ON, COUNTERCLOCKWISE);
391 
392  // Set timer to the duration of the command (like "drive forward 10 secs").
393  // That means, that the next command is read, when the next timer event occurs.
394  // Meanwhile the robot "does" the command
395  commandTimer->setInterval(commandDuration * 1000);
396 
397  // for measuring the script duration
398  scriptDuration += commandDuration;
399 
400  // show command
401  str = QString("Driving %1 for %2 seconds.").arg(command).arg(commandDuration);
402  ui.textEdit1->append(str);
403 
404  return;
405  }
406 
407  // turning right
408  if (command == "right")
409  {
410  motorControl(MOTOR1, ON, COUNTERCLOCKWISE);
411  motorControl(MOTOR2, ON, CLOCKWISE);
412 
413  commandTimer->setInterval(commandDuration * 1000);
414 
415  // for measuring the script duration
416  scriptDuration += commandDuration;
417 
418  // show command
419  str = QString("Turning %1 for %2 seconds.").arg(command).arg(commandDuration);
420  ui.textEdit1->append(str);
421 
422  return;
423  }
424 
425  // turning left
426  if (command == "left")
427  {
428  motorControl(MOTOR1, ON, CLOCKWISE);
429  motorControl(MOTOR2, ON, COUNTERCLOCKWISE);
430 
431  commandTimer->setInterval(commandDuration * 1000);
432 
433  // for measuring the script duration
434  scriptDuration += commandDuration;
435 
436  // show command
437  str = QString("Turning %1 for %2 seconds.").arg(command).arg(commandDuration);
438  ui.textEdit1->append(str);
439 
440  return;
441  }
442 
443  //
444  // stop executing script!
445  //
446  if (command == "end")
447  {
448  // change ini-section back to default
449  iniSection="Sequence1";
450 
451  // reset command counter, because the last command here is always "end"
452  commandCounter = 0;
453 
454  //------------------------------------------
455  // stop timer (and so stop reading ini-file)
456  //------------------------------------------
457  startStopCommandClock(STOP);
458 
459  // show script duration
460  str = QString("Change maneuver ended succesfully after %1 seconds.").arg(scriptDuration);
461  ui.textEdit1->append(str);
462 
463  // reset scriptDuration
464  scriptDuration = 0;
465 
466  // re-start sensor timer < < < < < < < ? ? ? ? < < < <
467  // ( stopped in btnExecuteScipt_clicked() !!)
468  startStopSensorClock(START);
469 
470  return;
471  }
472 
473  } // reading from ini-file was okay
474 
475  } // ini-file OK
476 }
477 */
478 
479 
480 void Inifile::sync(void)
481 {
482  settings->sync();
483 }
484 
485 
486 void Inifile::setFilename(QString filename)
487 {
488  if (mainIniFilename == "not_set")
489  {
490  // set the filename
491  mainIniFilename = filename;
492 
493  //------------------------------------------------------------------
494  // create the settings object. Use the ini-format
495  //------------------------------------------------------------------
496  settings = new QSettings(mainIniFilename, QSettings::IniFormat);
497 
498  // deactivate fallbacks (read only in the specified file)
499  settings->setFallbacksEnabled(false);
500  }
501 }