FrelonSerialLine.h 3.1 KB
Newer Older
1
2
3
4
#ifndef FRELONSERIALLINE_H
#define FRELONSERIALLINE_H

#include "Frelon.h"
5
#include "ThreadUtils.h"
6
7
8
9
10
11
12
13
14

namespace lima
{

namespace Frelon
{

class SerialLine : public HwSerialLine
{
15
	DEB_CLASS_NAMESPC(DebModCameraCom, "SerialLine", "Frelon");
16

17
18
19
20
21
 public:
	enum MsgPart {
		MsgSync, MsgCmd, MsgVal, MsgReq, MsgTerm, 
	};
	
22
23
24
25
26
27
28
29
	enum AnsPart {
		AnsResp, AnsWarn, AnsErr,
	};
	
	enum {
		MaxReadLen = 10000,
	};

ahoms's avatar
ahoms committed
30
31
	typedef std::map<MsgPart, std::string> MsgPartStrMapType;

32
	static const double TimeoutSingle, TimeoutNormal, TimeoutMultiLine, 
33
34
35
			    TimeoutReset;
	
	SerialLine(Espia::SerialLine& espia_ser_line);
36
37
	virtual ~SerialLine();

38
39
	Espia::SerialLine& getEspiaSerialLine();

40
41
	virtual void write(const std::string& buffer, 
			   bool no_wait = false);
42
43
	virtual void read(std::string& buffer, 
			  int max_len = MaxReadLen, 
44
			  double timeout = TimeoutDefault);
45
46
	virtual void readStr(std::string& buffer, int max_len, 
			     const std::string& term, 
47
			     double timeout = TimeoutDefault);
48
49
	virtual void readLine(std::string& buffer, 
			      int max_len = MaxReadLen, 
50
			      double timeout = TimeoutDefault);
51
52
	virtual void readSingleLine(std::string& buffer, 
				    int max_len = MaxReadLen, 
53
				    double timeout = TimeoutDefault);
54
55
	virtual void readMultiLine(std::string& buffer, 
				   int max_len = MaxReadLen);
56
	
57
58
	virtual void flush();

59
	virtual void getNbAvailBytes(int &avail);
60
	
61
62
63
	virtual void setTimeout(double timeout);
	virtual void getTimeout(double& timeout) const;

64
	void splitMsg(const std::string& msg, 
ahoms's avatar
ahoms committed
65
		      MsgPartStrMapType& msg_parts) const;
66
67
68
69
	void decodeFmtResp(const std::string& ans, std::string& fmt_resp);

	void sendFmtCmd(const std::string& cmd, std::string& resp);

70
71
72
	void writeRegister(Reg reg, int  val);
	void readRegister (Reg reg, int& val);

73
	int getLastWarning();
74

75
76
77
78
	void clearCache();
	void setCacheActive(bool  cache_act);
	void getCacheActive(bool& cache_act);

79
 private:
80
81
82
	enum RegOp {
		None, DoCmd, ReadReg, WriteReg, DoReset, MultiRead
	};
83
	friend std::ostream& operator <<(std::ostream& os, RegOp op);
84

85
	typedef std::map<Reg, int> RegValMapType;
86
87
88
89
90
91
92
93
94

	AutoMutex lock(int mode);

	virtual void writeCmd(const std::string& buffer, 
			      bool no_wait = false);
	virtual void readResp(std::string& buffer, 
			      int max_len = MaxReadLen, 
			      double timeout = TimeoutDefault);

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
	class ReadRespCleanup {
	public:
		ReadRespCleanup(SerialLine& ser_line)
			: m_ser_line(ser_line)
		{}

		~ReadRespCleanup()
		{ m_ser_line.readRespCleanup(); }

	private:
		SerialLine& m_ser_line;
	};
	friend class RespCleanup;

	void readRespCleanup();

111
	bool isRegCacheable(Reg reg);
112
	bool getRegCacheVal(Reg reg, int& val);
113

114
115
	double getRegSleepTime(Reg reg);
 
116
	Espia::SerialLine& m_espia_ser_line;
117
	Cond m_cond;
118
	int m_last_warn;
119

120
	RegValMapType m_reg_cache;
121
122
123
124
125
126
	bool m_cache_act;
	RegOp m_curr_op;
	Reg m_curr_reg;
	bool m_curr_cache;
	std::string m_curr_resp;
	std::string m_curr_fmt_resp;
127
128
};

129
130
131
std::ostream& operator <<(std::ostream& os, SerialLine::RegOp op);


132
133
134
135
136
inline AutoMutex SerialLine::lock(int mode)
{
	return AutoMutex(m_cond.mutex(), mode);
}

137
138
139
140
141
142
143
144
145

} // namespace Frelon



} // namespace lima


#endif // FRELONSERIALLINE_H