Commit 42842563 authored by ahoms's avatar ahoms
Browse files

* implemented Frelon::SerialLine sendFmtCmd and decodeFmtResp,

  with error and warning detection (available through getLastWarning)
* added default Frelon::SerialLine::MaxReadLen 
* added Frelon::Camera sendCmd and write/readRegister;
  first implemented methods: hardReset and getVersion



git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@75 45c4679d-1946-429d-baad-37912b19538b
parent cf7bf57a
......@@ -11,10 +11,20 @@ namespace Frelon
class Camera
{
public:
Camera(Espia::SerialLine& espia_ser_line);
SerialLine& getSerialLine();
void writeRegister(Reg reg, int val);
void readRegister (Reg reg, int& val);
void hardReset();
void getVersion(std::string& ver);
private:
void sendCmd(Cmd cmd);
SerialLine m_ser_line;
};
......
......@@ -16,6 +16,14 @@ class SerialLine : public HwSerialLine
MsgSync, MsgCmd, MsgVal, MsgReq, MsgTerm,
};
enum AnsPart {
AnsResp, AnsWarn, AnsErr,
};
enum {
MaxReadLen = 10000,
};
static const double TimeoutSingle, TimeoutNormal, TimeoutMultiLine,
TimeoutReset;
......@@ -23,16 +31,20 @@ class SerialLine : public HwSerialLine
virtual void write(const std::string& buffer,
bool no_wait = false);
virtual void read(std::string& buffer, int max_len,
virtual void read(std::string& buffer,
int max_len = MaxReadLen,
double timeout = TimeoutDefault);
virtual void readStr(std::string& buffer, int max_len,
const std::string& term,
double timeout = TimeoutDefault);
virtual void readLine(std::string& buffer, int max_len,
virtual void readLine(std::string& buffer,
int max_len = MaxReadLen,
double timeout = TimeoutDefault);
virtual void readSingleLine(std::string& buffer, int max_len,
virtual void readSingleLine(std::string& buffer,
int max_len = MaxReadLen,
double timeout = TimeoutDefault);
virtual void readMultiLine(std::string& buffer, int max_len);
virtual void readMultiLine(std::string& buffer,
int max_len = MaxReadLen);
virtual void flush();
......@@ -43,11 +55,17 @@ class SerialLine : public HwSerialLine
void splitMsg(const std::string& msg,
std::map<MsgPart, std::string>& msg_parts) const;
void decodeFmtResp(const std::string& ans, std::string& fmt_resp);
void sendFmtCmd(const std::string& cmd, std::string& resp);
int getLastWarning();
private:
Espia::SerialLine& m_espia_ser_line;
bool m_multi_line_cmd;
bool m_reset_cmd;
int m_last_warn;
};
......
#include "FrelonCamera.h"
#include "RegEx.h"
#include <sstream>
using namespace lima::Frelon;
using namespace std;
......@@ -9,3 +11,40 @@ Camera::Camera(Espia::SerialLine& espia_ser_line)
}
SerialLine& Camera::getSerialLine()
{
return m_ser_line;
}
void Camera::sendCmd(Cmd cmd)
{
string resp;
m_ser_line.sendFmtCmd(CmdStrMap[cmd], resp);
}
void Camera::writeRegister(Reg reg, int val)
{
ostringstream cmd;
cmd << RegStrMap[reg] << val;
string resp;
m_ser_line.sendFmtCmd(cmd.str(), resp);
}
void Camera::readRegister(Reg reg, int& val)
{
string resp, cmd = RegStrMap[reg] + "?";
m_ser_line.sendFmtCmd(cmd, resp);
istringstream is(resp);
is >> val;
}
void Camera::hardReset()
{
sendCmd(Reset);
}
void Camera::getVersion(string& ver)
{
string cmd = RegStrMap[Version] + "?";
m_ser_line.sendFmtCmd(cmd, ver);
}
#include "FrelonSerialLine.h"
#include "RegEx.h"
#include <sstream>
using namespace lima::Frelon;
using namespace std;
......@@ -15,8 +16,12 @@ SerialLine::SerialLine(Espia::SerialLine& espia_ser_line)
{
m_espia_ser_line.setLineTerm("\r\n");
m_espia_ser_line.setTimeout(TimeoutNormal);
m_multi_line_cmd = false;
m_reset_cmd = false;
m_last_warn = 0;
flush();
}
void SerialLine::write(const string& buffer, bool no_wait)
......@@ -122,10 +127,10 @@ void SerialLine::splitMsg(const string& msg,
{
msg_parts.clear();
RegEx re("^(?P<sync>>)?"
"(?P<cmd>[A-Z]+)"
"((?P<req>\\?)|(?P<val>[0-9]+))?"
"(?P<term>[\r\n]+)?$");
const static RegEx re("^(?P<sync>>)?"
"(?P<cmd>[A-Z]+)"
"((?P<req>\\?)|(?P<val>[0-9]+))?"
"(?P<term>[\r\n]+)?$");
RegEx::FullNameMatchType match;
if (!re.matchName(msg, match))
......@@ -147,3 +152,48 @@ void SerialLine::splitMsg(const string& msg,
}
}
void SerialLine::decodeFmtResp(const string& ans, string& fmt_resp)
{
fmt_resp.clear();
const static RegEx re("!(OK(:(?P<resp>[^\r]+))?|"
"W\a?:(?P<warn>[^\r]+)|"
"E\a?:(?P<err>[^\r]+))\r\n");
RegEx::FullNameMatchType match;
if (!re.matchName(ans, match))
throw LIMA_HW_EXC(Error, "Invalid Frelon answer");
RegEx::SingleMatchType& err = match["err"];
if (err) {
string err_str(err.start, err.end);
string err_desc = string("Frelon Error: ") + err_str;
throw LIMA_HW_EXC(Error, err_desc);
}
RegEx::SingleMatchType& warn = match["warn"];
if (warn) {
string warn_str(err.start, err.end);
istringstream is(warn_str);
is >> m_last_warn;
return;
}
RegEx::SingleMatchType& resp = match["resp"];
fmt_resp = string(resp.start, resp.end);
}
void SerialLine::sendFmtCmd(const string& cmd, string& resp)
{
write(cmd);
string ans;
readLine(ans);
decodeFmtResp(ans, resp);
}
int SerialLine::getLastWarning()
{
int last_warn = m_last_warn;
m_last_warn = 0;
return last_warn;
}
......@@ -5,71 +5,93 @@
using namespace lima;
using namespace std;
typedef Espia::SerialLine ESL;
typedef Frelon::SerialLine FSL;
void print_str(const string& desc, const string& str)
{
cout << desc << " \"" << str << "\"" << endl;
}
void split_msg(const FSL& frelon_ser_line, const string& msg)
void split_msg(const Frelon::SerialLine& frelon_ser_line, const string& msg)
{
map<FSL::MsgPart, string> msg_parts;
map<Frelon::SerialLine::MsgPart, string> msg_parts;
frelon_ser_line.splitMsg(msg, msg_parts);
print_str("Msg", msg);
print_str("Sync", msg_parts[FSL::MsgSync]);
print_str("Cmd ", msg_parts[FSL::MsgCmd]);
print_str("Val ", msg_parts[FSL::MsgVal]);
print_str("Req ", msg_parts[FSL::MsgReq]);
print_str("Term", msg_parts[FSL::MsgTerm]);
print_str("Sync", msg_parts[Frelon::SerialLine::MsgSync]);
print_str("Cmd ", msg_parts[Frelon::SerialLine::MsgCmd]);
print_str("Val ", msg_parts[Frelon::SerialLine::MsgVal]);
print_str("Req ", msg_parts[Frelon::SerialLine::MsgReq]);
print_str("Term", msg_parts[Frelon::SerialLine::MsgTerm]);
cout << endl;
}
void frelon_cmd(FSL& frelon_ser_line, const string& cmd)
void frelon_cmd(Frelon::SerialLine& frelon_ser_line, const string& cmd)
{
split_msg(frelon_ser_line, cmd);
frelon_ser_line.write(cmd);
Timestamp t0, t1;
string ans;
int max_len = 10000;
t0 = Timestamp::now();
frelon_ser_line.readLine(ans, max_len);
frelon_ser_line.readLine(ans);
t1 = Timestamp::now();
cout << "Elapsed " << (t1 - t0) << " sec" << endl;
print_str("Ans", ans);
}
void test_frelon()
void frelon_read_reg(Frelon::Camera& frelon_cam, Frelon::Reg reg)
{
Timestamp t0, t1;
int val;
print_str("Reg", Frelon::RegStrMap[reg]);
t0 = Timestamp::now();
frelon_cam.readRegister(reg, val);
t1 = Timestamp::now();
cout << "Elapsed " << (t1 - t0) << " sec" << endl;
cout << "Val" << " " << val << endl;
}
void test_frelon(bool do_reset)
{
Espia::Dev espia(0);
ESL espia_ser_line(espia);
FSL frelon_ser_line(espia_ser_line);
frelon_ser_line.flush();
Espia::SerialLine espia_ser_line(espia);
Frelon::SerialLine frelon_ser_line(espia_ser_line);
Frelon::Camera frelon_cam(espia_ser_line);
string msg;
msg = "RST";
frelon_cmd(frelon_ser_line, msg);
if (do_reset) {
cout << "Resetting camera ... " << endl;
frelon_cam.hardReset();
cout << " Done!" << endl;
}
string ver;
frelon_cam.getVersion(ver);
print_str("Ver", ver);
msg = ">C\r\n";
frelon_cmd(frelon_ser_line, msg);
msg = ">I?\r\n";
frelon_read_reg(frelon_cam, Frelon::ExpTime);
frelon_cam.writeRegister(Frelon::ExpTime, 200);
frelon_read_reg(frelon_cam, Frelon::ExpTime);
msg = ">C\r\n";
frelon_cmd(frelon_ser_line, msg);
frelon_cam.writeRegister(Frelon::ExpTime, 100);
frelon_read_reg(frelon_cam, Frelon::ExpTime);
}
int main(int argc, char *argv[])
{
try {
test_frelon();
bool do_reset = (argc > 1) && (string(argv[1]) == "reset");
test_frelon(do_reset);
} catch (Exception e) {
cerr << "LIMA Exception: " << e << endl;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment