mirror of
https://github.com/Serial-Studio/Serial-Studio.git
synced 2025-01-31 17:42:55 +08:00
Document code
This commit is contained in:
parent
988e50e60d
commit
71a1013434
@ -287,7 +287,7 @@ void Console::send(const QString &data)
|
|||||||
{
|
{
|
||||||
append(dataToString(bin));
|
append(dataToString(bin));
|
||||||
m_timestampAdded = false;
|
m_timestampAdded = false;
|
||||||
if (m_cursor)
|
if (m_cursor && lineEnding() == LineEnding::NoLineEnding)
|
||||||
m_cursor->insertBlock();
|
m_cursor->insertBlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -356,9 +356,12 @@ void Serial::setBaudRate(const qint32 rate)
|
|||||||
LOG_INFO() << "Baud rate set to" << rate;
|
LOG_INFO() << "Baud rate set to" << rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the port index value, this value is later used by the @c openSerialPort()
|
||||||
|
* function.
|
||||||
|
*/
|
||||||
void Serial::setPortIndex(const quint8 portIndex)
|
void Serial::setPortIndex(const quint8 portIndex)
|
||||||
{
|
{
|
||||||
auto ports = validPorts();
|
|
||||||
auto portId = portIndex - 1;
|
auto portId = portIndex - 1;
|
||||||
if (portId >= 0 && portId < validPorts().count())
|
if (portId >= 0 && portId < validPorts().count())
|
||||||
m_portIndex = portIndex;
|
m_portIndex = portIndex;
|
||||||
|
@ -6,8 +6,14 @@
|
|||||||
|
|
||||||
using namespace IO;
|
using namespace IO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pointer to the only instance of the class.
|
||||||
|
*/
|
||||||
static Manager *INSTANCE = nullptr;
|
static Manager *INSTANCE = nullptr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor function
|
||||||
|
*/
|
||||||
Manager::Manager()
|
Manager::Manager()
|
||||||
: m_writeEnabled(true)
|
: m_writeEnabled(true)
|
||||||
, m_maxBuzzerSize(1024 * 1024)
|
, m_maxBuzzerSize(1024 * 1024)
|
||||||
@ -20,8 +26,14 @@ Manager::Manager()
|
|||||||
setWatchdogInterval(15);
|
setWatchdogInterval(15);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor function
|
||||||
|
*/
|
||||||
Manager::~Manager() { }
|
Manager::~Manager() { }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the only instance of the class
|
||||||
|
*/
|
||||||
Manager *Manager::getInstance()
|
Manager *Manager::getInstance()
|
||||||
{
|
{
|
||||||
if (!INSTANCE)
|
if (!INSTANCE)
|
||||||
@ -30,16 +42,25 @@ Manager *Manager::getInstance()
|
|||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns @c true if a device is connected and its open in read-only mode
|
||||||
|
*/
|
||||||
bool Manager::readOnly()
|
bool Manager::readOnly()
|
||||||
{
|
{
|
||||||
return connected() && !m_writeEnabled;
|
return connected() && !m_writeEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns @c true if a device is connected and its open in read/write mode
|
||||||
|
*/
|
||||||
bool Manager::readWrite()
|
bool Manager::readWrite()
|
||||||
{
|
{
|
||||||
return connected() && m_writeEnabled;
|
return connected() && m_writeEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns @c true if a device is currently selected and opened
|
||||||
|
*/
|
||||||
bool Manager::connected()
|
bool Manager::connected()
|
||||||
{
|
{
|
||||||
if (device())
|
if (device())
|
||||||
@ -48,41 +69,94 @@ bool Manager::connected()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns @c true if a device is currently selected
|
||||||
|
*/
|
||||||
bool Manager::deviceAvailable()
|
bool Manager::deviceAvailable()
|
||||||
{
|
{
|
||||||
return device() != nullptr;
|
return device() != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the interval of the watchdog in milliseconds. The watchdog is used to clear
|
||||||
|
* the data buffer if no data has been received in more than x milliseconds (default
|
||||||
|
* 15 ms, tested on a 300 baud rate MCU).
|
||||||
|
*
|
||||||
|
* This is useful for: a) Dealing when a device suddenly is disconnected
|
||||||
|
* b) Removing extra data between received frames
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* RRRRRRRRR watchdog clears buf. RRRRRRRRR watchdog clear buf. RRRRRRRR
|
||||||
|
* [--------] . - . - . - . - . - . [--------] . - . - . - . - . - . [--------]
|
||||||
|
* Frame A Interval of > 15 ms Frame B Interval of > 15 ms Frame C
|
||||||
|
*
|
||||||
|
* Note: in the representation above "R" means that the watchdog is reset, thus, the
|
||||||
|
* data buffer is not cleared until we wait for another frame or communications
|
||||||
|
* are interrupted.
|
||||||
|
*
|
||||||
|
* TODO: let the JSON project file define the watchdog timeout time.
|
||||||
|
*/
|
||||||
int Manager::watchdogInterval() const
|
int Manager::watchdogInterval() const
|
||||||
{
|
{
|
||||||
return m_watchdog.interval();
|
return m_watchdog.interval();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the maximum size of the buffer. This is useful to avoid consuming to much
|
||||||
|
* memory when a large block of invalid data is received (for example, when the user
|
||||||
|
* selects an invalid baud rate configuration).
|
||||||
|
*/
|
||||||
int Manager::maxBufferSize() const
|
int Manager::maxBufferSize() const
|
||||||
{
|
{
|
||||||
return m_maxBuzzerSize;
|
return m_maxBuzzerSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a pointer to the currently selected device.
|
||||||
|
*
|
||||||
|
* @warning you need to check this pointer before using it, it can have a @c nullptr
|
||||||
|
* value during normal operations.
|
||||||
|
*/
|
||||||
QIODevice *Manager::device()
|
QIODevice *Manager::device()
|
||||||
{
|
{
|
||||||
return m_device;
|
return m_device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the currently selected data source, possible return values:
|
||||||
|
* - @c DataSource::Serial use a serial port as a data source
|
||||||
|
* - @c DataSource::Network use a network port as a data source
|
||||||
|
*/
|
||||||
Manager::DataSource Manager::dataSource() const
|
Manager::DataSource Manager::dataSource() const
|
||||||
{
|
{
|
||||||
return m_dataSource;
|
return m_dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the start sequence string used by the application to know where to consider
|
||||||
|
* that a frame begins. If the start sequence is empty, then the application shall ignore
|
||||||
|
* incoming data. The only thing that wont ignore the incoming data will be the console.
|
||||||
|
*/
|
||||||
QString Manager::startSequence() const
|
QString Manager::startSequence() const
|
||||||
{
|
{
|
||||||
return m_startSequence;
|
return m_startSequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the finish sequence string used by the application to know where to consider
|
||||||
|
* that a frame ends. If the start sequence is empty, then the application shall ignore
|
||||||
|
* incoming data. The only thing that wont ignore the incoming data will be the console.
|
||||||
|
*/
|
||||||
QString Manager::finishSequence() const
|
QString Manager::finishSequence() const
|
||||||
{
|
{
|
||||||
return m_finishSequence;
|
return m_finishSequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the size of the data received (and successfully read) from the device in the
|
||||||
|
* format of a string.
|
||||||
|
*/
|
||||||
QString Manager::receivedDataLength() const
|
QString Manager::receivedDataLength() const
|
||||||
{
|
{
|
||||||
QString value;
|
QString value;
|
||||||
@ -111,6 +185,9 @@ QString Manager::receivedDataLength() const
|
|||||||
return QString("%1 %2").arg(value).arg(units);
|
return QString("%1 %2").arg(value).arg(units);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list with the possible data source options.
|
||||||
|
*/
|
||||||
QStringList Manager::dataSourcesList() const
|
QStringList Manager::dataSourcesList() const
|
||||||
{
|
{
|
||||||
QStringList list;
|
QStringList list;
|
||||||
@ -119,6 +196,12 @@ QStringList Manager::dataSourcesList() const
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to write the given @a data to the current device. Upon data write, the class
|
||||||
|
* emits the @a tx() signal for UI updating.
|
||||||
|
*
|
||||||
|
* @returns the number of bytes written to the target device
|
||||||
|
*/
|
||||||
qint64 Manager::writeData(const QByteArray &data)
|
qint64 Manager::writeData(const QByteArray &data)
|
||||||
{
|
{
|
||||||
// Write data to device
|
// Write data to device
|
||||||
@ -134,6 +217,10 @@ qint64 Manager::writeData(const QByteArray &data)
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects/disconnects the application from the currently selected device. This function
|
||||||
|
* is used as a convenience for the connect/disconnect button.
|
||||||
|
*/
|
||||||
void Manager::toggleConnection()
|
void Manager::toggleConnection()
|
||||||
{
|
{
|
||||||
if (connected())
|
if (connected())
|
||||||
@ -142,12 +229,16 @@ void Manager::toggleConnection()
|
|||||||
connectDevice();
|
connectDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the currently selected device and tries to open & configure a new connection.
|
||||||
|
* A data source must be selected before calling this function.
|
||||||
|
*/
|
||||||
void Manager::connectDevice()
|
void Manager::connectDevice()
|
||||||
{
|
{
|
||||||
// Disconnect previous device (if any)
|
// Disconnect previous device (if any)
|
||||||
disconnectDevice();
|
disconnectDevice();
|
||||||
|
|
||||||
// Set device handler
|
// Try to open a serial port connection
|
||||||
if (dataSource() == DataSource::Serial)
|
if (dataSource() == DataSource::Serial)
|
||||||
setDevice(DataSources::Serial::getInstance()->openSerialPort());
|
setDevice(DataSources::Serial::getInstance()->openSerialPort());
|
||||||
|
|
||||||
@ -178,6 +269,9 @@ void Manager::connectDevice()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnects from the current device and clears temp. data
|
||||||
|
*/
|
||||||
void Manager::disconnectDevice()
|
void Manager::disconnectDevice()
|
||||||
{
|
{
|
||||||
if (deviceAvailable())
|
if (deviceAvailable())
|
||||||
@ -200,12 +294,18 @@ void Manager::disconnectDevice()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables/disables RW mode
|
||||||
|
*/
|
||||||
void Manager::setWriteEnabled(const bool enabled)
|
void Manager::setWriteEnabled(const bool enabled)
|
||||||
{
|
{
|
||||||
m_writeEnabled = enabled;
|
m_writeEnabled = enabled;
|
||||||
emit writeEnabledChanged();
|
emit writeEnabledChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the data source type. Check the @c dataSource() funciton for more information.
|
||||||
|
*/
|
||||||
void Manager::setDataSource(const DataSource source)
|
void Manager::setDataSource(const DataSource source)
|
||||||
{
|
{
|
||||||
// Disconnect current device
|
// Disconnect current device
|
||||||
@ -217,24 +317,40 @@ void Manager::setDataSource(const DataSource source)
|
|||||||
emit dataSourceChanged();
|
emit dataSourceChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the maximum permited buffer size. Check the @c maxBufferSize() function for
|
||||||
|
* more information.
|
||||||
|
*/
|
||||||
void Manager::setMaxBufferSize(const int maxBufferSize)
|
void Manager::setMaxBufferSize(const int maxBufferSize)
|
||||||
{
|
{
|
||||||
m_maxBuzzerSize = maxBufferSize;
|
m_maxBuzzerSize = maxBufferSize;
|
||||||
emit maxBufferSizeChanged();
|
emit maxBufferSizeChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the frame start sequence. Check the @c startSequence() function for more
|
||||||
|
* information.
|
||||||
|
*/
|
||||||
void Manager::setStartSequence(const QString &sequence)
|
void Manager::setStartSequence(const QString &sequence)
|
||||||
{
|
{
|
||||||
m_startSequence = sequence;
|
m_startSequence = sequence;
|
||||||
emit startSequenceChanged();
|
emit startSequenceChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the frame end sequence. Check the @c finishSequence() function for more
|
||||||
|
* information.
|
||||||
|
*/
|
||||||
void Manager::setFinishSequence(const QString &sequence)
|
void Manager::setFinishSequence(const QString &sequence)
|
||||||
{
|
{
|
||||||
m_finishSequence = sequence;
|
m_finishSequence = sequence;
|
||||||
emit finishSequenceChanged();
|
emit finishSequenceChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the expiration interval of the watchdog timer. Check the @c watchdogInterval()
|
||||||
|
* function for more information.
|
||||||
|
*/
|
||||||
void Manager::setWatchdogInterval(const int interval)
|
void Manager::setWatchdogInterval(const int interval)
|
||||||
{
|
{
|
||||||
m_watchdog.setInterval(interval);
|
m_watchdog.setInterval(interval);
|
||||||
@ -242,6 +358,13 @@ void Manager::setWatchdogInterval(const int interval)
|
|||||||
emit watchdogIntervalChanged();
|
emit watchdogIntervalChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read frames from temporary buffer, every frame that contains the appropiate start/end
|
||||||
|
* sequence is removed from the buffer as soon as its read.
|
||||||
|
*
|
||||||
|
* This function also checks that the buffer size does not exceed specified size
|
||||||
|
* limitations.
|
||||||
|
*/
|
||||||
void Manager::readFrames()
|
void Manager::readFrames()
|
||||||
{
|
{
|
||||||
// No device connected, abort
|
// No device connected, abort
|
||||||
@ -279,12 +402,21 @@ void Manager::readFrames()
|
|||||||
clearTempBuffer();
|
clearTempBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the watchdog timer before it expires. Check the @c watchdogInterval() function
|
||||||
|
* for more information.
|
||||||
|
*/
|
||||||
void Manager::feedWatchdog()
|
void Manager::feedWatchdog()
|
||||||
{
|
{
|
||||||
m_watchdog.stop();
|
m_watchdog.stop();
|
||||||
m_watchdog.start();
|
m_watchdog.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads incoming data from the serial device, updates the serial console object,
|
||||||
|
* registers incoming data to temporary buffer & extracts valid data frames from the
|
||||||
|
* buffer using the @c readFrame() function.
|
||||||
|
*/
|
||||||
void Manager::onDataReceived()
|
void Manager::onDataReceived()
|
||||||
{
|
{
|
||||||
// Verify that device is still valid
|
// Verify that device is still valid
|
||||||
@ -313,16 +445,29 @@ void Manager::onDataReceived()
|
|||||||
emit rx();
|
emit rx();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes the contents of the temporary buffer. This function is called automatically by
|
||||||
|
* the class when the temporary buffer size exceeds the limit imposed by the
|
||||||
|
* @c maxBufferSize() function.
|
||||||
|
*/
|
||||||
void Manager::clearTempBuffer()
|
void Manager::clearTempBuffer()
|
||||||
{
|
{
|
||||||
m_dataBuffer.clear();
|
m_dataBuffer.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the watchdog timer expires. For the moment, this function only clears the
|
||||||
|
* temporary data buffer.
|
||||||
|
*/
|
||||||
void Manager::onWatchdogTriggered()
|
void Manager::onWatchdogTriggered()
|
||||||
{
|
{
|
||||||
clearTempBuffer();
|
clearTempBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the target device pointer. Deletion should be handled by the interface
|
||||||
|
* implementation, not by this class.
|
||||||
|
*/
|
||||||
void Manager::setDevice(QIODevice *device)
|
void Manager::setDevice(QIODevice *device)
|
||||||
{
|
{
|
||||||
disconnectDevice();
|
disconnectDevice();
|
||||||
|
@ -24,17 +24,28 @@
|
|||||||
|
|
||||||
using namespace JSON;
|
using namespace JSON;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor function
|
||||||
|
*/
|
||||||
Frame::Frame(QObject *parent)
|
Frame::Frame(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_title("")
|
, m_title("")
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor function, free memory used by the @c Group objects before destroying an
|
||||||
|
* instance of this class.
|
||||||
|
*/
|
||||||
Frame::~Frame()
|
Frame::~Frame()
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the frame title and frees the memory used by the @c Group objects associated
|
||||||
|
* to the instance of the @c Frame object.
|
||||||
|
*/
|
||||||
void Frame::clear()
|
void Frame::clear()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < groupCount(); ++i)
|
for (int i = 0; i < groupCount(); ++i)
|
||||||
@ -44,21 +55,36 @@ void Frame::clear()
|
|||||||
m_groups.clear();
|
m_groups.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the title of the frame.
|
||||||
|
*/
|
||||||
QString Frame::title() const
|
QString Frame::title() const
|
||||||
{
|
{
|
||||||
return m_title;
|
return m_title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of groups contained in the frame.
|
||||||
|
*/
|
||||||
int Frame::groupCount() const
|
int Frame::groupCount() const
|
||||||
{
|
{
|
||||||
return groups().count();
|
return groups().count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a vector of pointers to the @c Group objects associated to this frame.
|
||||||
|
*/
|
||||||
QVector<Group *> Frame::groups() const
|
QVector<Group *> Frame::groups() const
|
||||||
{
|
{
|
||||||
return m_groups;
|
return m_groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the frame information and all its asociated groups (and datatsets) from the given
|
||||||
|
* JSON @c object.
|
||||||
|
*
|
||||||
|
* @return @c true on success, @c false on failure
|
||||||
|
*/
|
||||||
bool Frame::read(const QJsonObject &object)
|
bool Frame::read(const QJsonObject &object)
|
||||||
{
|
{
|
||||||
// Rest frame data
|
// Rest frame data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user