Files
TrainControllerUi/src/microcontroller.cpp
2022-02-01 22:52:25 +01:00

200 lines
4.6 KiB
C++

#include "microcontroller.h"
#include <chrono>
#include <thread>
#include "items/train.h"
#include "items/turnout.h"
void Microcontroller::trainSetSpeed(uint8_t id, int8_t speed)
{
qDebug()<<__func__;
std::stringstream ss;
ss<<"train "<<(unsigned)id<<" speed "<<(int)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() >= 14 && buffer.startsWith("TRAIN NUMBER:"))
{
return std::shared_ptr<Item>(new Train(bufferList[2].toInt(), bufferList[4].toInt(), bufferList[13].toInt()));
}
return nullptr;
}
std::shared_ptr<Item> Microcontroller::processTurnoutLine(const QString& buffer)
{
QStringList bufferList = buffer.split(' ');
if(bufferList.size() >= 7 && buffer.startsWith("TURNOUT NUMBER:"))
{
return std::shared_ptr<Item>(new Turnout(bufferList[2].toInt(), bufferList[2].toInt(), bufferList[6].toInt()));
}
return nullptr;
}
void Microcontroller::processList(const QString& buffer)
{
QStringList bufferList = buffer.split(' ');
qDebug()<<__func__<<" :"<<buffer<<" list mode: "<<listMode;
if(bufferList.size() >= 10 && buffer.contains("NUMBER:"))
{
std::shared_ptr<Item> item;
if(listMode == TRAIN_LIST)
item = processTrainLine(buffer);
else if(listMode == TURNOUT_LIST)
item = processTurnoutLine(buffer);
if(item)
itemList.push_back(item);
}
else
{
listMode = false;
if(!itemList.empty())
{
qDebug()<<"got item list " << itemList.size();
gotItemList(itemList);
itemList.clear();
}
}
}
void Microcontroller::processItemState(const QString& buffer)
{
if(_buffer.startsWith("TRAIN NUMBER:"))
itemChanged(static_cast<ItemData>(*processTrainLine(buffer)));
else if(_buffer.startsWith("TURNOUT NUMBER:"))
itemChanged(static_cast<ItemData>(*processTurnoutLine(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("TRAIN NUMBER:") || _buffer.startsWith("TURNOUT 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();
}
}
}