File indexing completed on 2024-05-12 04:41:10

0001 /* AtCore KDE Libary for 3D Printers
0002     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003     SPDX-FileCopyrightText: 2016 Patrick José Pereira <patrickjp@kde.org>
0004     SPDX-FileCopyrightText: 2016-2018, 2020 Chris Rizzitello <rizzitello@kde.org>
0005     SPDX-FileCopyrightText: 2016, 2018 Tomaz Canabrava <tcanabrava@kde.org>
0006     SPDX-FileCopyrightText: 2018 Leandro Santiago <leandrosansilva@gmail.com>
0007 */
0008 
0009 #pragma once
0010 
0011 #include <QSerialPort>
0012 
0013 #include "atcore_export.h"
0014 
0015 /**
0016  * @brief The SerialLayer class.
0017  * Provide the low level serial operations
0018  */
0019 class ATCORE_EXPORT SerialLayer : public QSerialPort
0020 {
0021     Q_OBJECT
0022 
0023 private:
0024     struct SerialLayerPrivate;
0025     SerialLayerPrivate *d;
0026 
0027     /**
0028      * @brief Read all available serial data
0029      *
0030      */
0031     void readAllData();
0032 signals:
0033 
0034     /**
0035      * @brief Emit signal when command is pushed
0036      *
0037      * @param comm : Command
0038      */
0039     void pushedCommand(const QByteArray &comm);
0040 
0041     /**
0042      * @brief Emit signal when command is received
0043      *
0044      * @param comm : Command
0045      */
0046     void receivedCommand(const QByteArray &comm);
0047 
0048     /**
0049      * @brief Emit a signal if an error has happened.
0050      * @param error: the Error
0051      */
0052     void serialError(QSerialPort::SerialPortError error);
0053 
0054 public:
0055     /**
0056      * @brief SerialLayer Class to realize communication
0057      *
0058      * @param port : Port (/dev/ttyUSB ACM)
0059      * @param baud : Baud rate (115200)
0060      * @param parent : Parent
0061      */
0062     SerialLayer(const QString &port, int32_t baud, QObject *parent = nullptr);
0063     ~SerialLayer();
0064 
0065     /**
0066      * @brief Add command to be pushed
0067      *
0068      * @param comm : Command
0069      * @param term : Terminator
0070      */
0071     void add(const QByteArray &comm, const QByteArray &term);
0072 
0073     /**
0074      * @brief Add command to be pushed
0075      *
0076      * @param comm : Command, default terminator will be used
0077      */
0078     void add(const QByteArray &comm);
0079 
0080     /**
0081      * @brief handleError Handle Errors from the serial port
0082      * @param error: The reported error
0083      */
0084     void handleError(QSerialPort::SerialPortError error);
0085 
0086     /**
0087      * @brief Push command directly
0088      *
0089      * @param comm : Command
0090      * @param term : Terminator
0091      */
0092     void pushCommand(const QByteArray &comm, const QByteArray &term);
0093 
0094     /**
0095      * @brief Push command directly
0096      *
0097      * @param comm : Command, default terminator will be used
0098      */
0099     void pushCommand(const QByteArray &comm);
0100 
0101     /**
0102      * @brief Push all commands used in add to serial write
0103      *
0104      */
0105     void push();
0106 
0107     /**
0108      * @brief Check if is a command available
0109      *
0110      * @return bool
0111      */
0112     bool commandAvailable() const;
0113 
0114     /**
0115      * @brief Return a QStringList of valids serial baud rates
0116      *
0117      * @return QStringList
0118      */
0119     QStringList validBaudRates() const;
0120 };