206 lines
4 KiB
C++
206 lines
4 KiB
C++
#include "microcontroller.h"
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
void Microcontroller::relayToggle(int state, int relay)
|
|
{
|
|
char buffer[8];
|
|
int length = sprintf(buffer, "%d \n", relay);
|
|
state ? write("item on ") : write("item off ");
|
|
write(buffer, length);
|
|
}
|
|
|
|
void Microcontroller::relayOn(int relay)
|
|
{
|
|
relayToggle(true, relay);
|
|
}
|
|
|
|
void Microcontroller::relayOff(int relay)
|
|
{
|
|
relayToggle(false, relay);
|
|
}
|
|
|
|
//rgb lights
|
|
|
|
void Microcontroller::rgbOn()
|
|
{
|
|
write("rgb on\n");
|
|
}
|
|
|
|
void Microcontroller::rgbOff()
|
|
{
|
|
write( "rgb off\n");
|
|
}
|
|
|
|
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());
|
|
write(buffer, length);
|
|
}
|
|
|
|
void Microcontroller::setAuxPwm(int duty)
|
|
{
|
|
char buffer[64];
|
|
int length = sprintf(buffer, "aux set %03d\n", duty );
|
|
write(buffer, length);
|
|
}
|
|
|
|
void Microcontroller::write(const QByteArray& 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)
|
|
{
|
|
if(_port != nullptr)
|
|
{
|
|
_port->write(buffer, length);
|
|
_port->waitForBytesWritten(1000);
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
|
}
|
|
|
|
void Microcontroller::setPattern(int pattern)
|
|
{
|
|
char buffer[4];
|
|
write("rgb pattern ");
|
|
int length = sprintf(buffer, "%d\n", pattern);
|
|
write(buffer, length);
|
|
}
|
|
|
|
void Microcontroller::startSunrise()
|
|
{
|
|
setPattern(4);
|
|
}
|
|
|
|
bool Microcontroller::connected()
|
|
{
|
|
if(_port != nullptr) return _port->isOpen();
|
|
else return false;
|
|
}
|
|
|
|
void Microcontroller::refresh()
|
|
{
|
|
write("state\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<Relay> Microcontroller::processRelayLine(const QString& buffer)
|
|
{
|
|
QStringList bufferList = buffer.split(' ');
|
|
if(bufferList.size() >= 9 && buffer.startsWith("ITEM NUMBER:"))
|
|
{
|
|
QString name;
|
|
for(int i = 10; i < bufferList.size(); i++)
|
|
name.append(bufferList[i] + ' ');
|
|
if(name.size() > 1)
|
|
name.remove(name.size()-1, 1);
|
|
else
|
|
name = "Relay " + QString::number(bufferList[1].toInt(nullptr, 2));
|
|
return std::shared_ptr<Relay>(new Relay(bufferList[2].toInt(),
|
|
name,
|
|
bufferList[6].toInt(nullptr, 2),
|
|
bufferList[8].toInt()));
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void Microcontroller::processList(const QString& buffer)
|
|
{
|
|
QStringList bufferList = buffer.split(' ');
|
|
if(bufferList.size() >= 8 && buffer.startsWith("ITEM NUMBER:"))
|
|
{
|
|
relayList.push_back(processRelayLine(buffer));
|
|
}
|
|
else if(buffer.contains("EOL"))
|
|
{
|
|
listMode = false;
|
|
gotItems(relayList, ITEM_UPDATE_BACKEND);
|
|
relayList.clear();
|
|
}
|
|
else listMode = false;
|
|
}
|
|
|
|
void Microcontroller::processRelayState(const QString& buffer)
|
|
{
|
|
ItemUpdateRequest update;
|
|
update.type = ITEM_UPDATE_BACKEND;
|
|
update.payload = static_cast<ItemData>(*processRelayLine(buffer));
|
|
updateItems({update});
|
|
}
|
|
|
|
void Microcontroller::processSensorState(const QString& buffer)
|
|
{
|
|
Sensor sensor = Sensor::sensorFromString(buffer);
|
|
if(sensor.type != Sensor::TYPE_DUMMY)
|
|
gotSensorState(sensor);
|
|
}
|
|
|
|
|
|
void Microcontroller::processMicroReturn()
|
|
{
|
|
if(listMode)
|
|
{
|
|
processList(_buffer);
|
|
}
|
|
else
|
|
{
|
|
if(_buffer.startsWith("Items:"))
|
|
{
|
|
listMode = true;
|
|
relayList.clear();
|
|
}
|
|
else if(_buffer.startsWith("ITEM NUMBER:"))
|
|
{
|
|
processRelayState(_buffer);
|
|
}
|
|
else if(_buffer.startsWith("SENSOR"))
|
|
{
|
|
processSensorState(_buffer);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|