forked from PianetaRadio/CatRadio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinkeyer.cpp
More file actions
125 lines (106 loc) · 3.37 KB
/
Copy pathwinkeyer.cpp
File metadata and controls
125 lines (106 loc) · 3.37 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
** This file is part of the CatRadio project.
** Copyright 2022-2025 Gianfranco Sordetti IZ8EWD <iz8ewd@pianetaradio.it>.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include "winkeyer.h"
#include <QSerialPort>
#include <QDebug>
int WinKeyer::init(QString portName) //Serial port init
{
serial = new QSerialPort();
if (serial && !serial->isOpen())
{
//Set serial port parameters
serial->setPortName(portName);
serial->setBaudRate(QSerialPort::Baud1200);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
//Open the serial port
serial->open(QIODevice::ReadWrite);
return serial->error();
}
return QSerialPort::OpenError;
}
int WinKeyer::open() //Open WK
{
if (serial->isOpen())
{
//Command mode \x00 and Host open \x02
QByteArray dataToSend = QByteArray::fromRawData("\x00\x02", 2);
serial->write(dataToSend);
if (serial->waitForBytesWritten(1000))
{
if (serial->waitForReadyRead(1000))
{
QByteArray receivedData = serial->readAll(); //receive the WK revision code
bool okInt;
int intValue = receivedData.toHex().toInt(&okInt, 16);
if (okInt)
{
isOpen = true;
return intValue;
}
else
{
isOpen = false;
return 0;
}
}
}
}
return 0;
}
void WinKeyer::close() //Close WK and serial port
{
if (serial->isOpen())
{
//Command mode x00 and Host close \x03
QByteArray dataToSend = QByteArray::fromRawData("\x00\x03", 2);
serial->write(dataToSend);
//Close the serial port
serial->close();
delete serial;
serial = nullptr;
isOpen = false;
}
}
void WinKeyer::setWpmSpeed(int wpm)
{
if (serial->isOpen())
{
if (wpm < 5) wpm = 5;
else if (wpm > 99) wpm = 99;
//Set WPM speed \x02 + \xnn WPM
QByteArray dataToSend = QByteArray::fromRawData("\x02", 1);
dataToSend.append(intToByte(wpm));
serial->write(dataToSend);
}
}
void WinKeyer::sendString(QByteArray string)
{
if (serial->isOpen())
{
serial->write(string.toUpper());
}
}
QByteArray WinKeyer::intToByte (int value)
{
QByteArray byteArray;
byteArray.append(static_cast<char>(value & 0xFF));
return byteArray;
}