summaryrefslogtreecommitdiff
path: root/qt/RGBController/old_src/serial.cpp
blob: f3f4f1ed53cf297b1529058744ded6d1bbebcf3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "serial.h"
#include "controllerwindow.h"
serial::serial(QObject *parent) : QObject(parent)
{
	ser = new QSerialPort(this);
	connect(ser, &QSerialPort::readyRead, this, &serial::read);
	
}

bool serial::serial_connect(QString port)
{
	/* this function will attempt a ser.connection if we are not already connected */
	if (!ser->isOpen())
	{
		ser->setPortName(port);
		ser->setBaudRate(QSerialPort::Baud9600);
		ser->setDataBits(QSerialPort::Data8);
		ser->setParity(QSerialPort::NoParity);
		ser->setStopBits(QSerialPort::OneStop);
		ser->setFlowControl(QSerialPort::NoFlowControl);
		ser->open(QIODevice::ReadWrite);
		ser->waitForBytesWritten(9000);
		if (ser->isWritable()) {
			return true;
		}
	}
	return false;
}

bool serial::serial_disconnect()
{
	/* this function disconnects from the serial port if it is connected already */
	if (ser->isOpen())
	{
		ser->close();
		return true;
	}
	return false;
}

void serial::send(QString com)
{
    data = "";
    //qDebug() << com;
    data.append(com + "\n");
    if (ser->isOpen())
        ser->write(data);
}

void serial::rgb_change(int r, int g, int b)
{
    // NOT USED FUNCTION

	/* here we send our rgb values to the serial port */
	data = "";
	/* the 0 is a hack, i need to look into it at some point */
    data.append("red=" + QString::number(r) + "," + QString::number(g) + "," + QString::number(b) + "\n");
	if (ser->isOpen())
		ser->write(data);
}

void serial::read()
{
	if (serdata[serdata.size() - 1] != '\n')
		serdata = serdata + ser->readAll();
	if (serdata[serdata.size() - 1] == '\n')
	{
		qDebug() << "got data" << serdata;
		parse(QString(serdata));
		serdata = "";
	}
}

void serial::parse(QString message)
{
	if (message.contains("="))
	{
		QString command = message.split("=")[0];
		QString text = message.split("=")[1];
		qDebug() << "command:" << command << "message:" << text;
		
//		win->info_log(text);
		//((controllerWindow*)(parent()))->info_log("tesT");
			
	}
}