inital commit
This commit is contained in:
166
src/microcontroller.cpp
Normal file
166
src/microcontroller.cpp
Normal file
@ -0,0 +1,166 @@
|
||||
#include "microcontroller.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include "items/train.h"
|
||||
|
||||
void Microcontroller::itemSetSpeed(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::itemReverse(uint8_t id)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss<<"train "<<(unsigned)id<<" reverse\n";
|
||||
write(ss.str().c_str());
|
||||
}
|
||||
|
||||
void Microcontroller::itemSetFunction(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::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");
|
||||
}
|
||||
|
||||
//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() >= 9 && buffer.startsWith("NUMBER:"))
|
||||
{
|
||||
return std::shared_ptr<Item>(new Train(bufferList[1].toInt(), bufferList[3].toInt(), bufferList[12].toInt()));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Microcontroller::processList(const QString& buffer)
|
||||
{
|
||||
QStringList bufferList = buffer.split(' ');
|
||||
if(bufferList.size() >= 13 && buffer.startsWith("NUMBER:"))
|
||||
{
|
||||
itemList.push_back(processTrainLine(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 = true;
|
||||
itemList.clear();
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user