Changed connection delay to something non-blocking

Former-commit-id: b313005a29cb42eb6839060bc4c48e4cbd927d3b
This commit is contained in:
johan 2014-05-04 11:41:55 +02:00
parent 3eb29146dd
commit 1e21602798
5 changed files with 33 additions and 9 deletions

View File

@ -1 +1 @@
ab3338d1164469e008bd9635c817659b6e528a1f e7867d56a269136c5c795a55b831dca58e962139

View File

@ -13,6 +13,7 @@ include_directories(
# Group the headers that go through the MOC compiler # Group the headers that go through the MOC compiler
SET(Leddevice_QT_HEADERS SET(Leddevice_QT_HEADERS
${CURRENT_SOURCE_DIR}/LedRs232Device.h
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.h ${CURRENT_SOURCE_DIR}/LedDeviceAdalight.h
) )
@ -20,8 +21,6 @@ SET(Leddevice_HEADERS
${CURRENT_HEADER_DIR}/LedDevice.h ${CURRENT_HEADER_DIR}/LedDevice.h
${CURRENT_HEADER_DIR}/LedDeviceFactory.h ${CURRENT_HEADER_DIR}/LedDeviceFactory.h
${CURRENT_SOURCE_DIR}/LedRs232Device.h
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.h ${CURRENT_SOURCE_DIR}/LedDeviceLightpack.h
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.h ${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.h
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.h ${CURRENT_SOURCE_DIR}/LedDevicePaintpack.h

View File

@ -12,7 +12,7 @@
/// ///
/// Implementation of the LedDevice interface for writing to an Adalight led device. /// Implementation of the LedDevice interface for writing to an Adalight led device.
/// ///
class LedDeviceAdalight : public QObject, public LedRs232Device class LedDeviceAdalight : public LedRs232Device
{ {
Q_OBJECT Q_OBJECT

View File

@ -4,19 +4,21 @@
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
// Qt includes
#include <QTimer>
// Serial includes // Serial includes
#include <serial/serial.h> #include <serial/serial.h>
// Local Hyperion includes // Local Hyperion includes
#include "LedRs232Device.h" #include "LedRs232Device.h"
#include "utils/Sleep.h"
LedRs232Device::LedRs232Device(const std::string& outputDevice, const unsigned baudrate, int delayAfterConnect_ms) : LedRs232Device::LedRs232Device(const std::string& outputDevice, const unsigned baudrate, int delayAfterConnect_ms) :
_deviceName(outputDevice), _deviceName(outputDevice),
_baudRate_Hz(baudrate), _baudRate_Hz(baudrate),
_delayAfterConnect_ms(delayAfterConnect_ms), _delayAfterConnect_ms(delayAfterConnect_ms),
_rs232Port() _rs232Port(),
_blockedForDelay(false)
{ {
// empty // empty
} }
@ -40,7 +42,9 @@ int LedRs232Device::open()
if (_delayAfterConnect_ms > 0) if (_delayAfterConnect_ms > 0)
{ {
Sleep::msleep(_delayAfterConnect_ms); _blockedForDelay = true;
QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
std::cout << "Device blocked for " << _delayAfterConnect_ms << " ms" << std::endl;
} }
} }
catch (const std::exception& e) catch (const std::exception& e)
@ -54,6 +58,11 @@ int LedRs232Device::open()
int LedRs232Device::writeBytes(const unsigned size, const uint8_t * data) int LedRs232Device::writeBytes(const unsigned size, const uint8_t * data)
{ {
if (_blockedForDelay)
{
return 0;
}
if (!_rs232Port.isOpen()) if (!_rs232Port.isOpen())
{ {
return -1; return -1;
@ -102,3 +111,9 @@ int LedRs232Device::writeBytes(const unsigned size, const uint8_t * data)
return 0; return 0;
} }
void LedRs232Device::unblockAfterDelay()
{
std::cout << "Device unblocked" << std::endl;
_blockedForDelay = false;
}

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <QObject>
// Serial includes // Serial includes
#include <serial/serial.h> #include <serial/serial.h>
@ -9,8 +11,10 @@
/// ///
/// The LedRs232Device implements an abstract base-class for LedDevices using a RS232-device. /// The LedRs232Device implements an abstract base-class for LedDevices using a RS232-device.
/// ///
class LedRs232Device : public LedDevice class LedRs232Device : public QObject, public LedDevice
{ {
Q_OBJECT
public: public:
/// ///
/// Constructs the LedDevice attached to a RS232-device /// Constructs the LedDevice attached to a RS232-device
@ -43,6 +47,10 @@ protected:
*/ */
int writeBytes(const unsigned size, const uint8_t *data); int writeBytes(const unsigned size, const uint8_t *data);
private slots:
/// Unblock the device after a connection delay
void unblockAfterDelay();
private: private:
/// The name of the output device /// The name of the output device
const std::string _deviceName; const std::string _deviceName;
@ -55,4 +63,6 @@ private:
/// The RS232 serial-device /// The RS232 serial-device
serial::Serial _rs232Port; serial::Serial _rs232Port;
bool _blockedForDelay;
}; };