implemented serial port config
Stefan Schuermans

Stefan Schuermans commited on 2011-12-11 19:46:03
Showing 3 changed files, with 152 additions and 0 deletions.

... ...
@@ -0,0 +1,80 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <sstream>
7
+#include <string>
8
+
9
+#include "SerCfg.h"
10
+#include "StringParser.h"
11
+
12
+namespace Blinker {
13
+
14
+/// constructor
15
+SerCfg::SerCfg():
16
+  m_baud(9600),
17
+  m_data(8),
18
+  m_parity(ParityNone),
19
+  m_stop(1)
20
+{
21
+}
22
+
23
+/**
24
+ * @brief parse from string format
25
+ * @param[in] str string format
26
+ * @return if parsing was successful
27
+ */
28
+bool SerCfg::fromStr(const std::string &str)
29
+{
30
+  StringParser parser(str);
31
+  unsigned int baud, data, stop;
32
+  char parityChr;
33
+
34
+  if (!parser.uintMin(1, baud) ||
35
+      !parser.fixChr(','))
36
+    return false;
37
+  if (parser.uintMin(1, data)) { // data bits, parity
38
+    if (!parser.fixChr(',') ||
39
+        !parser.oneChrOf("nNeEoO", parityChr))
40
+      return false;
41
+  } else { // parity, data bits
42
+    if (!parser.oneChrOf("nNeEoO", parityChr) ||
43
+        !parser.fixChr(',') ||
44
+        !parser.uintMin(1, data))
45
+      return false;
46
+  }
47
+  if (!parser.fixChr('n') ||
48
+      !parser.uintMin(1, stop) ||
49
+      !parser.isDone())
50
+    return false;
51
+  m_baud = baud;
52
+  m_data = data;
53
+  switch (parityChr) {
54
+    case 'n': case 'N': m_parity = ParityNone; break;
55
+    case 'e': case 'E': m_parity = ParityEven; break;
56
+    case 'o': case 'O': m_parity = ParityOdd; break;
57
+  }
58
+  m_stop = stop;
59
+  return true;
60
+}
61
+
62
+/**
63
+ * @brief convert to string format
64
+ * @return string format
65
+ */
66
+std::string SerCfg::toStr() const
67
+{
68
+  std::stringstream strm;
69
+  strm << m_baud << "," << m_data << ",";
70
+  switch (m_parity) {
71
+    case ParityNone: strm << "N"; break;
72
+    case ParityEven: strm << "E"; break;
73
+    case ParityOdd:  strm << "O"; break;
74
+  }
75
+  strm << "," << m_stop;
76
+  return strm.str();
77
+}
78
+
79
+} // namespace Blinker
80
+
... ...
@@ -0,0 +1,52 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef SERCFG_H
7
+#define SERCFG_H
8
+
9
+#include <string>
10
+
11
+namespace Blinker {
12
+
13
+/// serial port configuration
14
+class SerCfg
15
+{
16
+public:
17
+  /// parity type
18
+  enum Parity {
19
+    ParityNone, ///< no parity
20
+    ParityEven, ///< even number of bits set to 1
21
+    ParityOdd ///< odd number of bits set to 1
22
+  };
23
+
24
+public:
25
+  /// constructor
26
+  SerCfg();
27
+
28
+public:
29
+  /**
30
+   * @brief parse from string format
31
+   * @param[in] str string format
32
+   * @return if parsing was successful
33
+   */
34
+  bool fromStr(const std::string &str);
35
+
36
+  /**
37
+   * @brief convert to string format
38
+   * @return string format
39
+   */
40
+  std::string toStr() const;
41
+
42
+public:
43
+  unsigned int m_baud;   ///< baudrate (meaning bits per second)
44
+  unsigned int m_data;   ///< number of data bits
45
+  Parity       m_parity; ///< parity type
46
+  unsigned int m_stop;   ///< number of stop bits
47
+}; // class SerCfg
48
+
49
+} // namespace Blinker
50
+
51
+#endif // #ifndef SERCFG_H
52
+
... ...
@@ -0,0 +1,20 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef SERCFGFILE_H
7
+#define SERCFGFILE_H
8
+
9
+#include "SerCfg.h"
10
+#include "SettingFile_impl.h"
11
+
12
+namespace Blinker {
13
+
14
+/// setting file containting serial port configuration
15
+typedef SettingFile<SerCfg> SerCfgFile;
16
+
17
+} // namespace Blinker
18
+
19
+#endif // #ifndef SERCFGFILE_H
20
+
0 21