195 lines
4.5 KiB
C++
195 lines
4.5 KiB
C++
#include "microcontroller.h"
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
#include "items/train.h"
|
|
#include "items/turnout.h"
|
|
|
|
void Microcontroller::trainSetSpeed(uint8_t id, uint8_t speed)
|
|
{
|
|
qDebug()<<__func__;
|
|
std::stringstream ss;
|
|
ss<<"train "<<(unsigned)id<<" speed "<<(unsigned)speed<<'\n';
|
|
write(ss.str().c_str());
|
|
}
|
|
|
|
void Microcontroller::trainReverse(uint8_t id)
|
|
{
|
|
std::stringstream ss;
|
|
ss<<"train "<<(unsigned)id<<" reverse\n";
|
|
write(ss.str().c_str());
|
|
}
|
|
|
|
void Microcontroller::trainSetFunction(uint8_t id, uint8_t function, bool on)
|
|
{
|
|
std::stringstream ss;
|
|
ss<<"train "<<(unsigned)id<<" function "<<(unsigned)function<<' '<<(on ? "on" : "off")<<'\n';
|
|
write(ss.str().c_str());
|
|
}
|
|
|
|
void Microcontroller::tunoutSetDirection(uint8_t id, bool direction)
|
|
{
|
|
std::stringstream ss;
|
|
ss<<"turnout "<<(unsigned)id<<" set "<<(!direction ? "left" : "right")<<'\n';
|
|
write(ss.str().c_str());
|
|
}
|
|
|
|
void Microcontroller::setPower(bool on)
|
|
{
|
|
write(on ? "power on\n" : "power off\n");
|
|
}
|
|
|
|
void Microcontroller::estop()
|
|
{
|
|
write("stop\n");
|
|
}
|
|
|
|
void Microcontroller::write(const QByteArray& buffer)
|
|
{
|
|
qDebug()<<buffer;
|
|
if(_port != nullptr)
|
|
{
|
|
_port->write(buffer);
|
|
_port->waitForBytesWritten(1000);
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
|
}
|
|
|
|
void Microcontroller::write(char* buffer, const size_t length)
|
|
{
|
|
qDebug()<<buffer;
|
|
if(_port != nullptr)
|
|
{
|
|
_port->write(buffer, length);
|
|
_port->waitForBytesWritten(1000);
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
|
}
|
|
|
|
bool Microcontroller::connected()
|
|
{
|
|
if(_port != nullptr)
|
|
return _port->isOpen();
|
|
else return false;
|
|
}
|
|
|
|
void Microcontroller::requestState()
|
|
{
|
|
write("train list\n");
|
|
write("turnout list\n");
|
|
}
|
|
|
|
//housekeeping
|
|
|
|
Microcontroller::Microcontroller(QIODevice* port)
|
|
{
|
|
setIODevice(port);
|
|
}
|
|
|
|
Microcontroller::Microcontroller()
|
|
{
|
|
}
|
|
|
|
Microcontroller::~Microcontroller()
|
|
{
|
|
}
|
|
|
|
void Microcontroller::setIODevice(QIODevice *port)
|
|
{
|
|
_port = port;
|
|
QObject::connect(_port, &QIODevice::readyRead, this, &Microcontroller::isReadyRead);
|
|
}
|
|
|
|
std::shared_ptr<Item> Microcontroller::processTrainLine(const QString& buffer)
|
|
{
|
|
QStringList bufferList = buffer.split(' ');
|
|
if(bufferList.size() >= 13 && buffer.startsWith("NUMBER:"))
|
|
{
|
|
return std::shared_ptr<Item>(new Train(bufferList[1].toInt(), bufferList[3].toInt(), bufferList[12].toInt()));
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::shared_ptr<Item> Microcontroller::processTurnoutLine(const QString& buffer)
|
|
{
|
|
qDebug()<<__func__<<" :"<<buffer;
|
|
QStringList bufferList = buffer.split(' ');
|
|
if(bufferList.size() >= 6 && buffer.startsWith("NUMBER:"))
|
|
{
|
|
return std::shared_ptr<Item>(new Turnout(bufferList[1].toInt(), bufferList[5].toInt()));
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void Microcontroller::processList(const QString& buffer)
|
|
{
|
|
QStringList bufferList = buffer.split(' ');
|
|
qDebug()<<__func__<<" :"<<buffer<<" list mode: "<<listMode;
|
|
if(bufferList.size() >= 10 && buffer.startsWith("NUMBER:"))
|
|
{
|
|
if(listMode == TRAIN_LIST)
|
|
itemList.push_back(processTrainLine(buffer));
|
|
else if(listMode == TURNOUT_LIST)
|
|
itemList.push_back(processTurnoutLine(buffer));
|
|
}
|
|
else
|
|
{
|
|
listMode = false;
|
|
if(!itemList.empty())
|
|
{
|
|
qDebug()<<"got item list " << itemList.size();
|
|
gotItemList(itemList);
|
|
itemList.clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Microcontroller::processItemState(const QString& buffer)
|
|
{
|
|
itemChanged(static_cast<ItemData>(*processTrainLine(buffer)));
|
|
}
|
|
|
|
|
|
void Microcontroller::processMicroReturn()
|
|
{
|
|
if(listMode)
|
|
processList(_buffer);
|
|
else
|
|
{
|
|
if(_buffer.startsWith("Trains:"))
|
|
{
|
|
listMode = TRAIN_LIST;
|
|
itemList.clear();
|
|
}
|
|
else if(_buffer.startsWith("Turnouts:"))
|
|
{
|
|
listMode = TURNOUT_LIST;
|
|
}
|
|
else if(_buffer.startsWith("NUMBER:"))
|
|
{
|
|
processItemState(_buffer);
|
|
}
|
|
else if(_buffer.startsWith("TrainController"))
|
|
{
|
|
requestState();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void Microcontroller::isReadyRead()
|
|
{
|
|
char charBuf;
|
|
while(_port->getChar(&charBuf))
|
|
{
|
|
_buffer.push_back(charBuf);
|
|
if( _buffer.endsWith('\n') )
|
|
{
|
|
_buffer.remove('\n');
|
|
processMicroReturn();
|
|
textRecived(_buffer);
|
|
_buffer.clear();
|
|
}
|
|
}
|
|
}
|