hyperion.ng/libsrc/leddevice/ProviderRs232.h
redPanther 5aac2be702 Leddevice refactoring the next next part (#263)
* switch rs232 provider to completly async transfer

* start of implementing a seperate init function for leddevices

* rename setconfig to init

* more fixes

* implement missing code

* fix code style

* remove debug  code

* remove  debug stuff

* set loglevel to original state
2016-10-08 08:14:36 +02:00

98 lines
2.0 KiB
C++

#pragma once
#include <QObject>
#include <QSerialPort>
#include <QTimer>
// Leddevice includes
#include <leddevice/LedDevice.h>
///
/// The ProviderRs232 implements an abstract base-class for LedDevices using a RS232-device.
///
class ProviderRs232 : public LedDevice
{
Q_OBJECT
public:
///
/// Constructs specific LedDevice
///
ProviderRs232();
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const Json::Value &deviceConfig);
///
/// Destructor of the LedDevice; closes the output device if it is open
///
virtual ~ProviderRs232();
///
/// Opens and configures the output device
///
/// @return Zero on succes else negative
///
int open();
protected:
/**
* Writes the given bytes to the RS232-device and
*
* @param[in[ size The length of the data
* @param[in] data The data
*
* @return Zero on succes else negative
*/
int writeBytes(const qint64 size, const uint8_t *data);
void closeDevice();
/// The RS232 serial-device
QSerialPort _rs232Port;
private slots:
/// Write the last data to the leds again
int rewriteLeds();
/// Unblock the device after a connection delay
void unblockAfterDelay();
void error(QSerialPort::SerialPortError error);
void bytesWritten(qint64 bytes);
void readyRead();
protected:
// tries to open device if not opened
bool tryOpen(const int delayAfterConnect_ms);
/// The name of the output device
std::string _deviceName;
/// The used baudrate of the output device
qint32 _baudRate_Hz;
/// Sleep after the connect before continuing
int _delayAfterConnect_ms;
/// The RS232 serial-device
// QSerialPort _rs232Port;
bool _blockedForDelay;
bool _stateChanged;
qint64 _bytesToWrite;
qint64 _bytesWritten;
qint64 _frameDropCounter;
QSerialPort::SerialPortError _lastError;
/// Timer object which makes sure that led data is written at a minimum rate
/// e.g. Adalight device will switch off when it does not receive data at least
/// every 15 seconds
QTimer _timer;
};