#include <QMutexLocker>
#include <QFile>
#include <QTextStream>
#include <QtDebug>
#include <QStringList>
#include <iostream>
#include <QMetaType>
#include <QTime>
#include "messager.h"
using std::cout;
Messager
::Messager(QObject *parent
) {
stopped = false;
abort = false;
ReadDevice = false;
pollRate = 3;
//Constructor
}
Messager::~Messager()
{
//Destructor
mutex.lock();
waitCondition.wakeOne();
mutex.unlock();
wait();
}
void Messager::run()
{
do
{
//This code is for sending a packet
/* if(!packetQueue.isEmpty())
{
if(mutex.tryLock())
{
Packet sendPacket = packetQueue.dequeue();
UINT8 address = addressQueue.dequeue();
UPDATE type = typeQueue.dequeue();
mutex.unlock();
if(SendMessage(sendPacket, address))
emit PacketSent(true, type);
else
emit PacketSent(false, type);
}
} */
msleep(pollRate);
if(ReadDevice)
ReadMessage(0);
if(abort)
stopped = true;
}while(!stopped);
if(stopped)
quit();
else
exec();
}
void Messager::stop()
{
stopped = true;
}
/*void Messager::SendPacket(Packet myPacket, UINT8 address, UPDATE Type)
{
QMutexLocker locker(&mutex);
packetQueue.enqueue(myPacket);
addressQueue.enqueue(address);
typeQueue.enqueue(Type);
}
bool Messager::Config(int port, int bitrate)
{
QMutexLocker locker(&mutex);
// Open the port
ahandle = aa_open(port);
if (ahandle <= 0)
{
return false;
}
aa_configure(ahandle, AA_CONFIG_GPIO_I2C);
aa_i2c_pullup(ahandle, AA_I2C_PULLUP_BOTH);
aa_target_power(ahandle, AA_TARGET_POWER_NONE);
// Set the bitrate
bitrate = aa_i2c_bitrate(ahandle, bitrate);
return true;
}
bool Messager::SendMessage(Packet outgoingPacket, UINT8 addr)
{
QMutexLocker locker(&mutex);
int status = 0;
int retries = 0;
UINT16 aacount;
status = aa_i2c_write_ext(ahandle, addr, AA_I2C_NO_FLAGS, (2*outgoingPacket.GetLength())+4, outgoingPacket.GetData(), &aacount);
if(status != 0) // Try again
{
++retries;
status = aa_i2c_write_ext(ahandle, addr, AA_I2C_NO_FLAGS, (2*outgoingPacket.GetLength())+4, outgoingPacket.GetData(), &aacount);
if(status != 0)
{
qDebug() << "Message Sending Failure!\n";
return false;
}
else
return true;
}
else
return true;
}*/
bool Messager::ReadMessage(int address)
{
int i;
for(i = 0; i < 15; i ++)
packet.append(formatter.number(0xFF));
emit PacketRecieved( packet );
return true;
/* UINT8 buffer[511];
UINT16 actualSize = 0;
UINT8* curBuf = buffer;
UINT16 actualRead = 0;
INT32 status = 0;
UINT8 payloadSzWords = 0;
UINT16 curSize = 4;
UINT16 messageID = 0;
int i = 0;
int retries = 0;
int start = 0;
bool ok;
status = aa_i2c_read_ext(ahandle, address, AA_I2C_NO_FLAGS, curSize, curBuf, &actualRead);
if ((0 != status) )
{
++retries;
status = aa_i2c_read_ext(ahandle, address, AA_I2C_NO_FLAGS, curSize, curBuf, &actualRead);
}
else if ( actualRead != curSize)
{
return false;
}
else // the correct number of bytes were received
{
payloadSzWords = curBuf[0];
actualSize = actualRead;
if ( 0 != payloadSzWords )
{
curSize = 2 * (UINT16(payloadSzWords));
actualSize += curSize;
curBuf = &buffer[actualRead];
status = aa_i2c_read_ext(ahandle, address, AA_I2C_NO_FLAGS, curSize, curBuf, &actualRead);
if ((0 != status))
{
++retries;
status = aa_i2c_read_ext(ahandle, address, AA_I2C_NO_FLAGS, curSize, curBuf, &actualRead);
}
else if ( actualRead != curSize)
{
status = -7;
}
else if(status ==0)
{
for(i = 0; i < actualSize; i++)
{
packet.append(formatter.sprintf("%02x ", buffer[i] & 0xff));
}
emit PacketRecieved( packet );
}
else
return false;
}
else
return false;
}
return true;*/
}
void Messager::EnableDeviceStatus(bool Enabled)
{
mutex.lock();
ReadDevice = Enabled;
mutex.unlock();
}
#include <QMutexLocker>
#include <QFile>
#include <QTextStream>
#include <QtDebug>
#include <QStringList>
#include <iostream>
#include <QMetaType>
#include <QTime>
#include "messager.h"
using std::cout;
Messager::Messager(QObject *parent)
: QThread(parent)
{
stopped = false;
abort = false;
ReadDevice = false;
pollRate = 3;
//Constructor
}
Messager::~Messager()
{
//Destructor
mutex.lock();
waitCondition.wakeOne();
mutex.unlock();
wait();
}
void Messager::run()
{
do
{
//This code is for sending a packet
/* if(!packetQueue.isEmpty())
{
if(mutex.tryLock())
{
Packet sendPacket = packetQueue.dequeue();
UINT8 address = addressQueue.dequeue();
UPDATE type = typeQueue.dequeue();
mutex.unlock();
if(SendMessage(sendPacket, address))
emit PacketSent(true, type);
else
emit PacketSent(false, type);
}
} */
msleep(pollRate);
if(ReadDevice)
ReadMessage(0);
if(abort)
stopped = true;
}while(!stopped);
if(stopped)
quit();
else
exec();
}
void Messager::stop()
{
stopped = true;
}
/*void Messager::SendPacket(Packet myPacket, UINT8 address, UPDATE Type)
{
QMutexLocker locker(&mutex);
packetQueue.enqueue(myPacket);
addressQueue.enqueue(address);
typeQueue.enqueue(Type);
}
bool Messager::Config(int port, int bitrate)
{
QMutexLocker locker(&mutex);
// Open the port
ahandle = aa_open(port);
if (ahandle <= 0)
{
return false;
}
aa_configure(ahandle, AA_CONFIG_GPIO_I2C);
aa_i2c_pullup(ahandle, AA_I2C_PULLUP_BOTH);
aa_target_power(ahandle, AA_TARGET_POWER_NONE);
// Set the bitrate
bitrate = aa_i2c_bitrate(ahandle, bitrate);
return true;
}
bool Messager::SendMessage(Packet outgoingPacket, UINT8 addr)
{
QMutexLocker locker(&mutex);
int status = 0;
int retries = 0;
UINT16 aacount;
status = aa_i2c_write_ext(ahandle, addr, AA_I2C_NO_FLAGS, (2*outgoingPacket.GetLength())+4, outgoingPacket.GetData(), &aacount);
if(status != 0) // Try again
{
++retries;
status = aa_i2c_write_ext(ahandle, addr, AA_I2C_NO_FLAGS, (2*outgoingPacket.GetLength())+4, outgoingPacket.GetData(), &aacount);
if(status != 0)
{
qDebug() << "Message Sending Failure!\n";
return false;
}
else
return true;
}
else
return true;
}*/
bool Messager::ReadMessage(int address)
{
QStringList packet;
QString formatter;
int i;
for(i = 0; i < 15; i ++)
packet.append(formatter.number(0xFF));
emit PacketRecieved( packet );
return true;
/* UINT8 buffer[511];
UINT16 actualSize = 0;
UINT8* curBuf = buffer;
UINT16 actualRead = 0;
INT32 status = 0;
UINT8 payloadSzWords = 0;
UINT16 curSize = 4;
UINT16 messageID = 0;
int i = 0;
int retries = 0;
int start = 0;
bool ok;
status = aa_i2c_read_ext(ahandle, address, AA_I2C_NO_FLAGS, curSize, curBuf, &actualRead);
if ((0 != status) )
{
++retries;
status = aa_i2c_read_ext(ahandle, address, AA_I2C_NO_FLAGS, curSize, curBuf, &actualRead);
}
else if ( actualRead != curSize)
{
return false;
}
else // the correct number of bytes were received
{
payloadSzWords = curBuf[0];
actualSize = actualRead;
if ( 0 != payloadSzWords )
{
curSize = 2 * (UINT16(payloadSzWords));
actualSize += curSize;
curBuf = &buffer[actualRead];
status = aa_i2c_read_ext(ahandle, address, AA_I2C_NO_FLAGS, curSize, curBuf, &actualRead);
if ((0 != status))
{
++retries;
status = aa_i2c_read_ext(ahandle, address, AA_I2C_NO_FLAGS, curSize, curBuf, &actualRead);
}
else if ( actualRead != curSize)
{
status = -7;
}
else if(status ==0)
{
for(i = 0; i < actualSize; i++)
{
packet.append(formatter.sprintf("%02x ", buffer[i] & 0xff));
}
emit PacketRecieved( packet );
}
else
return false;
}
else
return false;
}
return true;*/
}
void Messager::EnableDeviceStatus(bool Enabled)
{
mutex.lock();
ReadDevice = Enabled;
mutex.unlock();
}
To copy to clipboard, switch view to plain text mode
#ifndef MESSAGER_H
#define MESSAGER_H
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QQueue>
{
Q_OBJECT
public:
~Messager();
void stop();
void EnableDeviceStatus(bool Enabled);
signals:
//void PacketSent(bool Success, UPDATE changeUI);
public slots:
//void SendPacket(Packet myPacket, UINT8 address, UPDATE MessageType);
protected:
void run();
private:
//bool SendMessage(Packet outgoingPacket, UINT8 address);
bool ReadMessage(int address);
/* QQueue <Packet> packetQueue;
QQueue <UINT8> addressQueue;
QQueue <UPDATE> typeQueue;
UPDATE MessageType;
UINT8 addr;
Aardvark ahandle;
*/
volatile bool stopped;
bool abort;
int pollRate;
bool ReadDevice;
};
#endif
#ifndef MESSAGER_H
#define MESSAGER_H
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QQueue>
class Messager : public QThread
{
Q_OBJECT
public:
Messager(QObject *parent = 0);
~Messager();
void stop();
void EnableDeviceStatus(bool Enabled);
signals:
//void PacketSent(bool Success, UPDATE changeUI);
void PacketRecieved(const QStringList &packet);
public slots:
//void SendPacket(Packet myPacket, UINT8 address, UPDATE MessageType);
protected:
void run();
private:
//bool SendMessage(Packet outgoingPacket, UINT8 address);
bool ReadMessage(int address);
/* QQueue <Packet> packetQueue;
QQueue <UINT8> addressQueue;
QQueue <UPDATE> typeQueue;
UPDATE MessageType;
UINT8 addr;
Aardvark ahandle;
*/
volatile bool stopped;
bool abort;
int pollRate;
bool ReadDevice;
QMutex mutex;
QWaitCondition waitCondition;
};
#endif
To copy to clipboard, switch view to plain text mode
Bookmarks