Device
int uart_rec_step = 0;
int uart_rec_index = 0;
int nread = 0;
const int _MAX_SIZE = 100;
unsigned char gyro_buf[_MAX_SIZE];
int gyro_index = 0;
typedef void (*DealGyroMsgCb)(unsigned char *buf, unsigned char len, unsigned int reserved, void *usrdata);
class GyroDevice
{
public:
GyroDevice(char *canbusUartName, char *keyDeviceName, McuService *commserver);
~GyroDevice();
void GyroDevice::setGyroMsgCb(DealGyroMsgCb usrCb, void *usrData)
{
int ret = pthread_create(&mThreadID1, NULL, threadRun, (void *)usrCb);
if (ret != 0)
{
fprintf(stderr, "creat thread successfully\n");
}
}
void threadRun(void *arg)
{
unsigned char checksum = 0;
unsigned char buf[_MAX_SIZE] = {0};
DealGyroMsgCb usrCb = (DealGyroMsgCb)arg;
while (uart_rec_index < nread)
{
switch (uart_rec_step)
{
case 0:
{
if (buf[uart_rec_index] == 0x55)
{
uart_rec_step = 1;
}
else
{
uart_rec_step = 0;
}
break;
}
case 1:
{
switch (buf[uart_rec_index])
{
case 0x53:
{
uart_rec_step = 2;
gyro_buf[0] = 0x53;
gyro_index++;
break;
}
case 0x56:
{
uart_rec_step = 2;
gyro_buf[0] = 0x56;
gyro_index++;
break;
}
}
break;
}
case 2:
{
if ((nread - uart_rec_index) > 7)
{
for (i = 0; i < 8; i++, gyro_index++, uart_rec_index++)
{
gyro_buf[gyro_index] = buf[uart_rec_index];
checksum += buf[uart_rec_index];
#ifdef GYRO_DBG
fprintf(stderr, "gyro_buf[%d] = 0x%02x\n", gyro_index, gyro_buf[gyro_index]);
#endif
}
checksum = checksum + 0x55;
checksum = checksum + 0x53;
#ifdef GYRO_DBG
fprintf(stderr, "buf[%d] = 0x%02x\n", uart_rec_index, buf[uart_rec_index]);
fprintf(stderr, "checksum = 0x%02x\n", checksum);
#endif
if (checksum == buf[uart_rec_index])
{
usrCb(gyro_buf, 9, 0, NULL);
}
checksum = 0;
gyro_index = 0;
uart_rec_step = 0;
break;
}
break;
}
}
uart_rec_index++;
}
uart_rec_index = 0;
}
};
Interface
class GyroInterface: public QThread
{
Q_OBJECT
public:
public:
explicit GyroInterface(QObject *parent = 0);
~GyroInterface();
void init(){
QThread::start();
}
protected:
void run(){
mGyroDevice->setGyroMsgCb(&GyroInterface::gyroDealCb, (void *)"xxxx");
QThread::msleep(100);
}
private:
GyroDevice *mGyroDevice;
bool mIsKeepRunning;
static void gyroDealCb(unsigned char *buf, unsigned char len, unsigned int reserved, void *usrdata)
{
int Data0 = buf[0];
switch (Data0)
{
case 0x53:{
}break;
case 0x54:{
}break;
}
}
};