Blame view

src/ConnectionConfig.h 2.35 KB
32017af3   Peter M. Groen   Setting up workin...
1
2
3
  #pragma once
  
  #include <string>
cadcf24a   Peter M. Groen   Setting up workin...
4
  #include <unordered_map>
32017af3   Peter M. Groen   Setting up workin...
5
  
cadcf24a   Peter M. Groen   Setting up workin...
6
  enum class ConnectionPort : unsigned int
32017af3   Peter M. Groen   Setting up workin...
7
  {
cadcf24a   Peter M. Groen   Setting up workin...
8
      CP_EXTERNAL     = 0,
32017af3   Peter M. Groen   Setting up workin...
9
10
11
12
      CP_IOBUS,
      CP_TCP
  };
  
cadcf24a   Peter M. Groen   Setting up workin...
13
  enum class Parity : unsigned int
32017af3   Peter M. Groen   Setting up workin...
14
  {
cadcf24a   Peter M. Groen   Setting up workin...
15
16
17
18
19
20
21
22
23
24
      PAR_ODD,
      PAR_EVEN,
      PAR_NONE
  };
  
  enum class ConnectionType : unsigned int
  {
      CT_SERIAL,
      CT_TCP,
      CT_UNKNOWN
32017af3   Peter M. Groen   Setting up workin...
25
26
27
28
29
  };
  
  class ConnectionConfig
  {
  public:
cadcf24a   Peter M. Groen   Setting up workin...
30
31
32
33
34
35
36
37
38
39
      ConnectionConfig( ConnectionPort port, int baud = 115200, Parity parity = Parity::PAR_NONE, int dataBits = 8, int stopBits = 1, int timeOut = -1 )
          : m_port( port )
          , m_baudRate( baud )
          , m_parity( parity )
          , m_dataBits( dataBits )
          , m_stopBits( stopBits )
          , m_ipaddress()
          , m_portnumber( -1 )
          , m_timeOut( timeOut )
      {}
32017af3   Peter M. Groen   Setting up workin...
40
41
  
      ConnectionConfig( ConnectionPort port, const std::string &ip, int portnum, int timeOut = -1  )
cadcf24a   Peter M. Groen   Setting up workin...
42
43
44
45
46
47
48
49
50
          : m_port( port )
          , m_baudRate( -1 )
          , m_parity( Parity::PAR_NONE )
          , m_dataBits( -1 )
          , m_stopBits( -1 )
          , m_ipaddress( ip )
          , m_portnumber( portnum )
          , m_timeOut( timeOut )
      {}
32017af3   Peter M. Groen   Setting up workin...
51
  
cadcf24a   Peter M. Groen   Setting up workin...
52
53
54
55
56
57
58
59
60
61
62
      // Getters and Setters. Implemented to avoid outside meddling on the member variables.
      ConnectionType getType() const { return m_conType; }
      std::string getPort() const { return m_portMap.at(m_port); }
      int getBaudRate() const { return m_baudRate; }
      char getParity() const { return m_parityMap.at(m_parity); }
      int getDataBits() const { return m_dataBits; }
      int getStopBits() const { return m_stopBits; }
  
      std::string getIpAddress() const { return m_ipaddress; }
      int getTcpPort() const { return m_portnumber; }
      int getTimeOut() const { return m_timeOut; }
32017af3   Peter M. Groen   Setting up workin...
63
64
65
  
  private:
  
cadcf24a   Peter M. Groen   Setting up workin...
66
67
      ConnectionType  m_conType;
  
32017af3   Peter M. Groen   Setting up workin...
68
      /// Serial connections
cadcf24a   Peter M. Groen   Setting up workin...
69
      ConnectionPort  m_port;
32017af3   Peter M. Groen   Setting up workin...
70
71
72
73
74
75
      int             m_baudRate;
      Parity          m_parity;
      int             m_dataBits;
      int             m_stopBits;
  
      /// TCP connections
cadcf24a   Peter M. Groen   Setting up workin...
76
77
      std::string     m_ipaddress;
      int             m_portnumber;
32017af3   Peter M. Groen   Setting up workin...
78
  
cadcf24a   Peter M. Groen   Setting up workin...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
      /// Generic
      int             m_timeOut;
      std::unordered_map<ConnectionPort, std::string> m_portMap =
      {
          { ConnectionPort::CP_EXTERNAL, "/dev/ttyUSB0" },
          { ConnectionPort::CP_IOBUS, "/dev/ttyUSB1" }
      };
  
      std::unordered_map<Parity, char> m_parityMap =
      {
          { Parity::PAR_EVEN, 'E' },
          { Parity::PAR_ODD, 'O' },
          { Parity::PAR_NONE, 'N' }
      };
32017af3   Peter M. Groen   Setting up workin...
93
  };