New Sensor-> Actor -> Item system (half implemented, relay support
only), new ui, Relay dehardcoeding.
This commit is contained in:
parent
74f117db69
commit
b04fbfb5bc
66 changed files with 3905 additions and 123 deletions
229
src/microcontroller.cpp
Normal file
229
src/microcontroller.cpp
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
#include "microcontroller.h"
|
||||
|
||||
//relays
|
||||
|
||||
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::relayOn(int relay)
|
||||
{
|
||||
relayToggle(true, relay);
|
||||
}
|
||||
|
||||
void Microcontroller::relayOff(int relay)
|
||||
{
|
||||
relayToggle(false, relay);
|
||||
}
|
||||
|
||||
//rgb lights
|
||||
|
||||
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::setAuxPwm(int duty)
|
||||
{
|
||||
if(duty != _auxState)
|
||||
{
|
||||
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");
|
||||
}
|
||||
_auxState = duty;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void Microcontroller::startSunrise()
|
||||
{
|
||||
setPattern(4);
|
||||
}
|
||||
|
||||
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::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"));
|
||||
}
|
||||
|
||||
void Microcontroller::abort()
|
||||
{
|
||||
if (!loop.isNull())
|
||||
{
|
||||
loop->quit();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<bool> Microcontroller::getLastState()
|
||||
{
|
||||
return _relayStates;
|
||||
}
|
||||
|
||||
//housekeeping
|
||||
|
||||
Microcontroller::Microcontroller(QIODevice* port): _port(port)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Microcontroller::Microcontroller()
|
||||
{
|
||||
}
|
||||
|
||||
Microcontroller::~Microcontroller()
|
||||
{
|
||||
if(_port != nullptr) delete _port;
|
||||
}
|
||||
|
||||
void Microcontroller::setIODevice(QIODevice *port)
|
||||
{
|
||||
_port = port;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_buffer.startsWith("Relays:"))
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Microcontroller::doTick()
|
||||
{
|
||||
if(_port != nullptr)
|
||||
{
|
||||
char charBuf;
|
||||
while(_port->getChar(&charBuf))
|
||||
{
|
||||
_buffer.push_back(charBuf);
|
||||
if( _buffer.endsWith('\n') )
|
||||
{
|
||||
_buffer.remove('\n');
|
||||
processMicroReturn();
|
||||
textRecived(_buffer);
|
||||
_buffer.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue