Browse code

Add source code of the XiPC emulator

Pawel Jablonski authored on 17/03/2023 16:46:08
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,101 @@
1
+/*
2
+ * Author: Pawel Jablonski
3
+ * E-mail: pj@xirx.net
4
+ * WWW: xirx.net
5
+ * GIT: git.xirx.net
6
+ *
7
+ * License: You can use this code however you like
8
+ * but leave information about the original author.
9
+ * Code is free for non-commercial and commercial use.
10
+ */
11
+
12
+#include "rs232.h"
13
+
14
+/**
15
+ * Constructor for the RS232 class
16
+ *
17
+ * @param parent Parent object
18
+ */
19
+RS232::RS232(QObject *parent) : QObject(parent)
20
+{
21
+	this->reset();
22
+}
23
+
24
+//! Reset the RS232 buffers
25
+void RS232::reset()
26
+{
27
+	this->rxBuffer.clear();
28
+}
29
+
30
+/**
31
+ * Receive function used to emulate input
32
+ *
33
+ * @param text Received text
34
+ */
35
+void RS232::rx(const QString &text)
36
+{
37
+	unsigned int charAdded = 0;
38
+
39
+	for(const QChar &cFor : text)
40
+	{
41
+		unsigned char c = static_cast<unsigned char>(cFor.toLatin1());
42
+
43
+		if((c >= MIN_CODE) && (c <= MAX_CODE))
44
+		{
45
+			this->rxBuffer.append(c);
46
+
47
+			charAdded++;
48
+
49
+			emit updateRxSignal(c);
50
+		}
51
+	}
52
+
53
+	if(charAdded > 0)
54
+	{
55
+		this->rxBuffer.append(0x00);
56
+
57
+		emit updateRxSignal(0x00);
58
+	}
59
+
60
+	if(this->rxBuffer.length() > 0)
61
+	{
62
+		emit dataReadySignal(true);
63
+	}
64
+}
65
+
66
+/**
67
+ * Send char
68
+ *
69
+ * @param c Char to send
70
+ */
71
+void RS232::send(unsigned char c)
72
+{
73
+	emit updateTxSignal(c & SEND_MASK);
74
+}
75
+
76
+/**
77
+ * Read next a char from receive buffer
78
+ *
79
+ * @return Next char from the receive buffer
80
+ */
81
+unsigned char RS232::receive()
82
+{
83
+	if(this->rxBuffer.length() > 0)
84
+	{
85
+		return(this->rxBuffer.takeFirst());
86
+	}
87
+
88
+	emit dataReadySignal(false);
89
+
90
+	return(0);
91
+}
92
+
93
+/**
94
+ * Get free space size in the transmit buffer
95
+ *
96
+ * @return Free space size in the transmit buffer
97
+ */
98
+unsigned char RS232::getSendBufferFree()
99
+{
100
+	return(BUFFER_TX_SIZE);
101
+}