<file_sep>#ifndef DeltaHIDINTERFACE_H
#define DeltaHIDINTERFACE_H

#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <stdlib.h>
#include "hidapi.h"

// Headers needed for sleeping.
#include <unistd.h>

#define hidDataLength 64

//hid data receive callback function
typedef void (*DataArriveCallBackFunc)(unsigned char *recData,unsigned char length);

DataArriveCallBackFunc hidDataArriveCallBack;

hid_device *handle;

unsigned char isOpen;


int hidApi_Init(DataArriveCallBackFunc DataArriveCallBack);

int hidApi_Write(unsigned char *data, unsigned char length);

//when data arrived, the function will be called
void hidApi_DataReceive(unsigned char *recData,unsigned char length);

int hidApi_Close(void);

#endif // HIDINTERFACE_H
<file_sep>/*******************************************************
 Windows HID simplification

 <NAME>
 Signal 11 Software

 8/22/2009

 Copyright 2009, All Rights Reserved.
 
 This contents of this file may be used by anyone
 for any reason without any conditions and may be
 used as a starting point for your own applications
 which use HIDAPI.
********************************************************/


#include "hidinterface.h"
#include <pthread.h>

void hidRead_thread(void);

int hidApi_Init(DataArriveCallBackFunc DataArriveCallBack)
{
    hidDataArriveCallBack = NULL;
    // Open the device using the VID, PID,
    // and optionally the Serial number.
    ////handle = hid_open(0x4d8, 0x3f, L"12345");
    handle = hid_open(0x4d8, 0x3f, NULL);
    if (!handle) {
        printf("unable to open device\n");
        isOpen = 0;
        return -1;
    }

    printf("open device success\n");
    isOpen = 1;
    hidDataArriveCallBack = DataArriveCallBack;
    hidRead_thread();


    // Set the hid_read() function to be non-blocking.
    hid_set_nonblocking(handle, 1);

    return 0;
}

int hidApi_Write(unsigned char *data, unsigned char length)
{
    int res;
    unsigned char realData[length+1];

    realData[0]=length;
    int i;
    for(i=0;i<length;i++)
    {
        realData[i+1]=data[i];
    }

    res = hid_write(handle, realData, length+1);
    if (res < 0) {
        printf("Unable to write()\n");
        printf("Error: %ls\n", hid_error(handle));
        return -1;
    }

    printf("write success\n");
    return 0;
}

void hidApi_DataReceive(unsigned char *recData,unsigned char length)
{
    if(hidDataArriveCallBack==NULL)
        return;

    hidDataArriveCallBack(recData,length);
}

void hidApi_Read()
{
    unsigned char recData[hidDataLength];

    int res;
    while (isOpen==1) {
        res = hid_read(handle, recData, hidDataLength);
        if (res == 0)
            ;//printf("waiting...\n");
        else if (res < 0)
        {
            printf("Unable to read()\n");
            return -1;
        }
        else
        {

            int i;

//            printf("Data read:\n   ");
//            // Print out the returned buffer.
//
//            for (i = 0; i < res; i++)
//                printf("%02hhx ", recData[i]);
//            printf("\n");

            unsigned char length = recData[0];

            unsigned char datas[length];
            for(i=0;i<length;i++)
            {
                datas[i]=recData[i+1];
            }

            hidApi_DataReceive(datas,length);
        }


        usleep(50*1000);

    }

}

void hidRead_thread(void)
{
   pthread_t id;
   int ret, i;
   ret=pthread_create(&id,NULL,(void *) hidApi_Read,NULL); // 成功返回0，错误返回错误编号
   if(ret!=0) {
       printf ("Create pthread error!\n");
       exit (1);
   }


   //pthread_join(id,NULL);

   printf ("Create pthread success!\n");
}

int hidApi_Close(void)
{
    hid_close(handle);
    isOpen = 0;
    /* Free static HIDAPI objects. */
    hid_exit();

    return 0;

}



<file_sep>#include <hidlib/hidinterface.h>

// down 50000 50 : FA 00 00 04 03 01 00 00 00 50 C3 00 00 32 00 00 00 A7 A5
unsigned char bufferDown50[] = {0xFA, 0x00, 0x00, 0x04, 0x03, 0x01, 0x00, 0x00, 0x00, 0x50, 0xC3, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0xA7, 0xA5};
// down 50000 10 : FA 00 00 04 03 01 00 00 00 50 C3 00 00 0A 00 00 00 9F A5
unsigned char bufferDown10[] = {0xFA, 0x00, 0x00, 0x04, 0x03, 0x01, 0x00, 0x00, 0x00, 0x50, 0xC3, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x9F, 0xA5};

// up 50000 50 : FA 00 00 04 03 00 00 00 00 50 C3 00 00 32 00 00 00 A6 A5
unsigned char bufferUp50[] = {0xFA, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x50, 0xC3, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0xA6, 0xA5};
// up 50000 10 : FA 00 00 04 03 00 00 00 00 50 C3 00 00 0A 00 00 00 9E A5
unsigned char bufferUp10[] = {0xFA, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x50, 0xC3, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x9E, 0xA5};


// go home FA 00 02 04 03 00 00 00 00 00 00 00 00 0A 00 00 00 0D A5
unsigned char bufferGoHome[] = { 0xFA, 0x00, 0x02, 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0xA5};

char isReponse = 0;

void datarec(unsigned char *recData,unsigned char length)
{
    printf("Data Receive: ");
    int i;
    for(i=0;i<length;i++)
    {
        printf("%02hhx ", recData[i]);
    }
    printf("\n");
    isReponse = 1;
}

int main(int argc, char* argv[])
{

    hidApi_Init(datarec);

    isReponse = 1;
    while(1)
    {
        while(isReponse==0) usleep(500*1000);
        isReponse =0;
        hidApi_Write(bufferGoHome,0x13);
        usleep(500*1000);

        while(isReponse==0) usleep(500*1000);
        isReponse =0;
        hidApi_Write(bufferDown10,0x13);
        usleep(500*1000);

        while(isReponse==0) usleep(500*1000);
        isReponse =0;
        hidApi_Write(bufferDown50,0x13);

        while(isReponse==0) usleep(500*1000);
        isReponse =0;
        hidApi_Write(bufferDown10,0x13);
        usleep(500*1000);

        usleep(2*1000*1000);
        while(isReponse==0) usleep(500*1000);
        isReponse =0;
        hidApi_Write(bufferGoHome,0x13);

        usleep(2*1000*1000);
    }

    hidApi_Close();

    return 0;
}
