switched from qsettings to json added editng of actors
This commit is contained in:
parent
b04fbfb5bc
commit
df27b622a0
141 changed files with 4402 additions and 5068 deletions
|
|
@ -1,15 +1,16 @@
|
|||
#include "microcontroller.h"
|
||||
|
||||
//relays
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
static constexpr bool debug = true;
|
||||
|
||||
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);
|
||||
state ? write("relay on ") : write("relay off ");
|
||||
write(buffer, length);
|
||||
}
|
||||
|
||||
void Microcontroller::relayOn(int relay)
|
||||
|
|
@ -26,48 +27,61 @@ void Microcontroller::relayOff(int relay)
|
|||
|
||||
void Microcontroller::rgbOn()
|
||||
{
|
||||
if(_port != nullptr) _port->write("rgb on\n", sizeof("rgb on\n")-1);
|
||||
write("rgb on\n");
|
||||
}
|
||||
|
||||
void Microcontroller::rgbOff()
|
||||
{
|
||||
if(_port != nullptr) _port->write( "rgb off\n", sizeof("rgb off\n")-1);
|
||||
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() );
|
||||
if(_port != nullptr) _port->write(buffer, length);
|
||||
std::cout<<buffer;
|
||||
int length = sprintf(buffer, "rgb set %03d %03d %03d\n", color.red(), color.green(), color.blue());
|
||||
write(buffer, length);
|
||||
std::cout<<buffer;
|
||||
}
|
||||
|
||||
void Microcontroller::setAuxPwm(int duty)
|
||||
{
|
||||
char buffer[64];
|
||||
int length = sprintf(buffer, "aux set %03d\n", duty );
|
||||
write(buffer, length);
|
||||
}
|
||||
|
||||
void Microcontroller::setAuxPwm(int duty)
|
||||
void Microcontroller::write(const QByteArray& buffer)
|
||||
{
|
||||
if(duty != _auxState)
|
||||
#ifndef Q_OS_ANDROID
|
||||
if constexpr(debug) std::cerr<<buffer.data();
|
||||
#endif
|
||||
if(_port != nullptr)
|
||||
{
|
||||
if(duty > 1)
|
||||
{
|
||||
if(_auxState <= 1 && _port != nullptr) _port->write("aux on\n");
|
||||
char buffer[64];
|
||||
int length = sprintf(buffer, "aux set %03d \n", duty );
|
||||
if(_port != nullptr) _port->write(buffer, length);
|
||||
}
|
||||
else if(_port != nullptr)_port->write("aux off\n");
|
||||
_port->write(buffer);
|
||||
_port->waitForBytesWritten(1000);
|
||||
}
|
||||
_auxState = duty;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
||||
}
|
||||
|
||||
void Microcontroller::write(char* buffer, const size_t length)
|
||||
{
|
||||
#ifndef Q_OS_ANDROID
|
||||
if constexpr(debug) std::cerr<<buffer;
|
||||
#endif
|
||||
if(_port != nullptr)
|
||||
{
|
||||
_port->write(buffer, length);
|
||||
_port->waitForBytesWritten(1000);
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
||||
}
|
||||
|
||||
void Microcontroller::setPattern(int pattern)
|
||||
{
|
||||
if(_port != nullptr)
|
||||
{
|
||||
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);
|
||||
}
|
||||
write("rgb pattern ");
|
||||
int length = sprintf(buffer, "%d\n", pattern);
|
||||
write(buffer, length);
|
||||
}
|
||||
|
||||
void Microcontroller::startSunrise()
|
||||
|
|
@ -96,12 +110,7 @@ void Microcontroller::run()
|
|||
|
||||
void Microcontroller::requestState()
|
||||
{
|
||||
if(_port != nullptr) _port->write("state\n", sizeof("state\n"));
|
||||
}
|
||||
|
||||
void Microcontroller::requestRelayList()
|
||||
{
|
||||
if(_port != nullptr) _port->write("relay list\n", sizeof("relay list\n"));
|
||||
write("state\n");
|
||||
}
|
||||
|
||||
void Microcontroller::abort()
|
||||
|
|
@ -112,16 +121,12 @@ void Microcontroller::abort()
|
|||
}
|
||||
}
|
||||
|
||||
std::vector<bool> Microcontroller::getLastState()
|
||||
{
|
||||
return _relayStates;
|
||||
}
|
||||
|
||||
//housekeeping
|
||||
|
||||
Microcontroller::Microcontroller(QIODevice* port): _port(port)
|
||||
{
|
||||
|
||||
_port->readAll();
|
||||
_port->write("\n");
|
||||
}
|
||||
|
||||
Microcontroller::Microcontroller()
|
||||
|
|
@ -136,31 +141,60 @@ Microcontroller::~Microcontroller()
|
|||
void Microcontroller::setIODevice(QIODevice *port)
|
||||
{
|
||||
_port = port;
|
||||
_port->readAll();
|
||||
_port->write("\n");
|
||||
}
|
||||
|
||||
std::shared_ptr<Relay> Microcontroller::processRelayLine(const QString& buffer)
|
||||
{
|
||||
QStringList bufferList = buffer.split(' ');
|
||||
if(bufferList.size() >= 8 && buffer.startsWith("RELAY NUMBER:"))
|
||||
{
|
||||
QString name;
|
||||
for(int i = 8; 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));
|
||||
qDebug()<<"Relay "<<bufferList[2].toInt()<<"Name "<<name<<" id "<<bufferList[4].toInt(nullptr, 2)<<" state "<<bufferList[6].toInt();
|
||||
return std::shared_ptr<Relay>( new Relay(nullptr, this, bufferList[2].toInt(), name, bufferList[4].toInt(nullptr, 2), bufferList[6].toInt()));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Microcontroller::processList(const QString& buffer)
|
||||
{
|
||||
QStringList bufferList = buffer.split(' ');
|
||||
if(bufferList.size() >= 8 && buffer.startsWith("RELAY NUMBER:"))
|
||||
{
|
||||
relayList.push_back(processRelayLine(buffer));
|
||||
}
|
||||
else if(buffer.contains("EOL"))
|
||||
{
|
||||
listMode = false;
|
||||
qDebug()<<"got relay list " << relayList.size();
|
||||
gotRelayList(relayList);
|
||||
relayList.clear();
|
||||
}
|
||||
else listMode = false;
|
||||
}
|
||||
|
||||
void Microcontroller::processRelayState(const QString& buffer)
|
||||
{
|
||||
itemChanged(static_cast<ItemData>(*processRelayLine(buffer)));
|
||||
}
|
||||
|
||||
void Microcontroller::processSensorState(const QString& buffer)
|
||||
{
|
||||
QStringList bufferList = buffer.split(' ');
|
||||
Sensor sensor(bufferList[2].toUInt(), bufferList[4].toUInt(), bufferList[6].toUInt());
|
||||
if(sensor.type == Sensor::TYPE_HUMIDITY || sensor.type == Sensor::TYPE_TEMPERATURE) sensor.field = sensor.field/10;
|
||||
if(bufferList.size() >= 7)gotSensorState(sensor);
|
||||
}
|
||||
|
||||
|
||||
void Microcontroller::processMicroReturn()
|
||||
{
|
||||
QString workbuff = _buffer;
|
||||
|
||||
if(listMode)
|
||||
{
|
||||
QStringList workbufList = workbuff.split(' ');
|
||||
if(workbufList.size() >= 5 && workbufList[0] == "NUMBER:")
|
||||
{
|
||||
QString name;
|
||||
for(int i = 5; i < workbufList.size(); i++) name.append(workbufList[i] + ' ');
|
||||
name.remove(name.size()-1, 1);
|
||||
relayList.push_back(RelayStore(workbufList[1].toInt(), name, workbufList[3].toInt()));
|
||||
}
|
||||
else if(_buffer.startsWith("EOL"))
|
||||
{
|
||||
listMode = false;
|
||||
gotRelayList(relayList);
|
||||
relayList.clear();
|
||||
}
|
||||
else listMode = false;
|
||||
}
|
||||
qDebug()<<_buffer;
|
||||
if(listMode) processList(_buffer);
|
||||
else
|
||||
{
|
||||
if(_buffer.startsWith("Relays:"))
|
||||
|
|
@ -168,43 +202,8 @@ void Microcontroller::processMicroReturn()
|
|||
listMode = true;
|
||||
relayList.clear();
|
||||
}
|
||||
else if(_buffer.startsWith("ST"))
|
||||
{
|
||||
|
||||
workbuff.remove(0, 2);
|
||||
QStringList workbufList = workbuff.split(',');
|
||||
|
||||
//aux state
|
||||
if(workbufList[0].toInt() != _auxState)
|
||||
{
|
||||
_auxState = workbufList[0].toInt();
|
||||
auxStateChanged(_auxState);
|
||||
}
|
||||
|
||||
//relay state
|
||||
uint_fast8_t numberOfRelays = workbufList[1].toInt();
|
||||
if(workbufList.size() >= numberOfRelays+2)
|
||||
{
|
||||
_relayStates.resize(numberOfRelays, false);
|
||||
for(uint_fast8_t i = 0; i < numberOfRelays; i++)
|
||||
{
|
||||
if(_relayStates[i] != static_cast<bool>(workbufList[i+2].toInt()))
|
||||
{
|
||||
_relayStates[i] = static_cast<bool>(workbufList[i+2].toInt());
|
||||
relayStateChanged(RelayStore(i, "", 0, _relayStates[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(workbuff.contains("Door Open Warning"))
|
||||
{
|
||||
doorOpenTimeout();
|
||||
}
|
||||
else if(workbuff.size() > 2 && workbuff[0]=='D' && workbuff[1]=='2')
|
||||
{
|
||||
if(workbuff[3] == 'O') doorOpened(1);
|
||||
else if(workbuff[3] == 'C') doorClosed(1);
|
||||
}
|
||||
else if(_buffer.startsWith("RELAY NUMBER:"))processRelayState(_buffer);
|
||||
else if(_buffer.startsWith("SENSOR")) processSensorState(_buffer);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue