#include "Login.h"
#include <qtopia/qpeapplication.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <qvariant.h>
#include <stdio.h>
#include <qthread.h>
#include "b_line_table.h"
#include "define.h"
QPEApplication *a =NULL;
char rx_buf[32];
int fd = 0 ;
//条码枪事件
const QEvent::Type MyEvent = (QEvent::Type)1000 ;
class GeneralDevThread : public QThread
{
public:
virtual void run();
};
void GeneralDevThread::run(void)
{
int j;
int i;
if ((fd=open("/dev/SPI",O_RDWR ))<0){
perror("can not open device!\n");
QThread::exit();
}
while (1)
{
if (read(fd,rx_buf,32)<0)
{
perror("read error!\n");
QThread::exit(); //结束调用线程的执行并且唤醒任何等待它终止的线程。
}
char h1, h2;
char barcode[14];
char idcode[11];
memcpy(&h1, rx_buf, 1);
memcpy(&h2, rx_buf + 1, 1);
printf("h1 is %d, h2 is %d \n", h1, h2);
if ((h1 == 3)&&(h2 == 1)){
memset(barcode, 0, 14);
memcpy(barcode, rx_buf + 2, 13);
printf("Barcode string-- : %s \n", barcode);
if (b_line_tableForm::b_line_table_ptr != NULL){
a->postEvent(b_line_tableForm::b_line_table_ptr, new QCustomEvent(MyEvent, barcode));
a->wakeUpGuiThread ();
}
}else
{
printf("This is not supported event .\n");
}
}
}
void print_str_piece(char * pc)
{
//printf("print content block is %s \n", pc);
//printf("file descriptor is %d \n", fd);
char tx_buf[1024] ;
tx_buf[0] = 0x06 ;
tx_buf[1] = 0x02 ;
tx_buf[2] = '\0' ;
strcat(tx_buf,pc);
//printf("tx buf is %s\n", tx_buf);
int length = strlen(pc)+2;
if (write(fd,tx_buf,length)<0)
{
perror("print write error\n");
}
printf("print one block , \n");
}
void print_str(char * pc)
{
int i = 0;
char *cur = pc;
char *pp[100];
int row = 0;
int prelen = 0;
int index = 0;
int len = 0;
for (i = 0; i < strlen(pc); i++)
{
if (*(cur) == '\n')
{
row++;
if (row < 5) {
//printf("dumy ....... \n");
}
else
{
len = i + 1 - prelen;
pp[index] = (char *)malloc(len + 1);
(pp[index])[len] = '\0';
memcpy(pp[index], pc + prelen, len);
prelen = i + 1;
index++;
row = 0;
}
}
cur++;
}
int elen = strlen(pc) - prelen;
pp[index] = (char *)malloc(elen + 1);
(pp[index])[elen]='\0';
memcpy(pp[index], pc + prelen, elen);
int j = 0;
for (j = 0; j < index + 1; j++)
{
//printf("pp[%d] is %s \n", j, pp[j]);
print_str_piece(pp[j]);
if (j != index)
sleep(1);
}
print_str_piece(" \n \n \n \n");
for (j = 0; j < index + 1; j++)
{
free(pp[j]);
}
printf("print end .\n");
}
FormLogin *frmLogin = NULL;
int main( int argc, char ** argv )
{
printf("Welcome to travel system\n") ;
print_str_piece("");
a = new QPEApplication( argc, argv );
frmLogin = new FormLogin(0, "FormLogin");
GeneralDevThread devTh ;
devTh.start() ;
a->showMainWidget( frmLogin );
return a->exec();
}