114 lines
2.3 KiB
C++
114 lines
2.3 KiB
C++
#include "microcontroller.h"
|
|
|
|
Microcontroller::Microcontroller(QIODevice* port): _port(port)
|
|
{
|
|
}
|
|
|
|
Microcontroller::Microcontroller()
|
|
{
|
|
}
|
|
|
|
Microcontroller::~Microcontroller()
|
|
{
|
|
if(_port != nullptr) delete _port;
|
|
}
|
|
|
|
void Microcontroller::setIODevice(QIODevice *port)
|
|
{
|
|
_port = port;
|
|
}
|
|
|
|
void Microcontroller::relayToggle(int state, int relay)
|
|
{
|
|
char buffer[8];
|
|
int length = sprintf(buffer, "%d \n", relay);
|
|
if(_port != nullptr) state ? _port->write("relay on ", sizeof("relay on ")-1) : _port->write("relay off ", sizeof("relay off ")-1);
|
|
state ? std::cout<<"relay on " : std::cout<<"relay off ";
|
|
std::cout<<buffer;
|
|
if(_port != nullptr) _port->write(buffer, length);
|
|
}
|
|
|
|
void Microcontroller::rgbOn()
|
|
{
|
|
if(_port != nullptr) _port->write("rgb on\n", sizeof("rgb on\n")-1);
|
|
}
|
|
|
|
void Microcontroller::rgbOff()
|
|
{
|
|
if(_port != nullptr) _port->write( "rgb off\n", sizeof("rgb off\n")-1);
|
|
}
|
|
|
|
void Microcontroller::changeRgbColor(const QColor color)
|
|
{
|
|
char buffer[64];
|
|
int length = sprintf(buffer, "rgb set %03d %03d %03d \n", color.red(), color.green(), color.blue() );
|
|
if(_port != nullptr) _port->write(buffer, length);
|
|
std::cout<<buffer;
|
|
}
|
|
|
|
void Microcontroller::setPattern(int pattern)
|
|
{
|
|
char buffer[4];
|
|
std::cout<<"pattern apply\n";
|
|
_port->write("rgb pattern ", sizeof("rgb pattern ")-1);
|
|
int length = sprintf(buffer, "%d \n", pattern);
|
|
_port->write(buffer, length);
|
|
}
|
|
|
|
void Microcontroller::relayOn(int relay)
|
|
{
|
|
relayToggle(true, relay);
|
|
}
|
|
|
|
void Microcontroller::relayOff(int relay)
|
|
{
|
|
relayToggle(false, relay);
|
|
}
|
|
|
|
bool Microcontroller::connected()
|
|
{
|
|
if(_port != nullptr) return _port->isOpen();
|
|
else return false;
|
|
}
|
|
|
|
void Microcontroller::run()
|
|
{
|
|
abort();
|
|
|
|
loop.reset(new QEventLoop);
|
|
QTimer timer;
|
|
connect(&timer, SIGNAL(timeout()), this, SLOT(doTick()));
|
|
timer.setInterval(500);
|
|
timer.start();
|
|
|
|
loop->exec();
|
|
}
|
|
|
|
|
|
void Microcontroller::abort()
|
|
{
|
|
if (!loop.isNull())
|
|
{
|
|
loop->quit();
|
|
}
|
|
}
|
|
|
|
void Microcontroller::doTick()
|
|
{
|
|
if(_port != nullptr)
|
|
{
|
|
char charBuf;
|
|
while(_port->getChar(&charBuf))
|
|
{
|
|
_buffer.push_back(charBuf);
|
|
std::cout<<charBuf<<std::endl;
|
|
if( _buffer.endsWith('\n') )
|
|
{
|
|
|
|
textRecived(_buffer);
|
|
_buffer.clear();
|
|
}
|
|
}
|
|
}
|
|
}
|