Handle log, temperatures. Move controller to main tabpanel. More things

This commit is contained in:
Alessandro Ranellucci 2015-01-04 23:18:23 +01:00
parent 3ab4d4b094
commit 9af43bee52
8 changed files with 204 additions and 58 deletions

View file

@ -158,6 +158,34 @@ GCodeSender::resume_queue()
this->send();
}
// purge log and return its contents
std::vector<std::string>
GCodeSender::purge_log()
{
boost::lock_guard<boost::mutex> l(this->log_mutex);
std::vector<std::string> retval;
retval.reserve(this->log.size());
while (!this->log.empty()) {
retval.push_back(this->log.front());
this->log.pop();
}
return retval;
}
std::string
GCodeSender::getT() const
{
boost::lock_guard<boost::mutex> l(this->log_mutex);
return this->T;
}
std::string
GCodeSender::getB() const
{
boost::lock_guard<boost::mutex> l(this->log_mutex);
return this->B;
}
void
GCodeSender::do_close()
{
@ -214,8 +242,10 @@ GCodeSender::on_read(const boost::system::error_code& error,
}
// copy the read buffer into string
std::string line((std::istreambuf_iterator<char>(&this->read_buffer)),
std::istreambuf_iterator<char>());
std::istream is(&this->read_buffer);
std::string line;
std::getline(is, line);
// note that line might contain \r at its end
// parse incoming line
if (!this->connected
@ -236,10 +266,8 @@ GCodeSender::on_read(const boost::system::error_code& error,
} else if (boost::istarts_with(line, "resend") // Marlin uses "Resend: "
|| boost::istarts_with(line, "rs")) {
// extract the first number from line
using boost::lexical_cast;
using boost::bad_lexical_cast;
boost::algorithm::trim_left_if(line, !boost::algorithm::is_digit());
size_t toresend = lexical_cast<size_t>(line.substr(0, line.find_first_not_of("0123456789")));
size_t toresend = boost::lexical_cast<size_t>(line.substr(0, line.find_first_not_of("0123456789")));
if (toresend == this->sent) {
{
boost::lock_guard<boost::mutex> l(this->queue_mutex);
@ -250,6 +278,28 @@ GCodeSender::on_read(const boost::system::error_code& error,
} else {
printf("Cannot resend %lu (last was %lu)\n", toresend, this->sent);
}
} else if (boost::starts_with(line, "wait")) {
// ignore
} else {
// push any other line into the log
boost::lock_guard<boost::mutex> l(this->log_mutex);
this->log.push(line);
}
// parse temperature info
{
size_t pos = line.find("T:");
if (pos != std::string::npos && line.size() > pos + 2) {
// we got temperature info
boost::lock_guard<boost::mutex> l(this->log_mutex);
this->T = line.substr(pos+2, line.find_first_not_of("0123456789.", pos+2) - (pos+2));
pos = line.find("B:");
if (pos != std::string::npos && line.size() > pos + 2) {
// we got bed temperature info
this->B = line.substr(pos+2, line.find_first_not_of("0123456789.", pos+2) - (pos+2));
}
}
}
this->do_read();

View file

@ -27,6 +27,9 @@ class GCodeSender : private boost::noncopyable {
size_t queue_size() const;
void pause_queue();
void resume_queue();
std::vector<std::string> purge_log();
std::string getT() const;
std::string getB() const;
private:
asio::io_service io;
@ -46,6 +49,11 @@ class GCodeSender : private boost::noncopyable {
size_t sent;
std::string last_sent;
// this mutex guards log, T, B
mutable boost::mutex log_mutex;
std::queue<std::string> log;
std::string T, B;
void set_baud_rate(unsigned int baud_rate);
void set_error_status(bool e);
void do_send(const std::string &line);

View file

@ -18,6 +18,9 @@
void send(std::string s, bool priority = false);
void pause_queue();
void resume_queue();
std::vector<std::string> purge_log();
std::string getT();
std::string getB();
};
#endif