summaryrefslogtreecommitdiff
path: root/tools/atmega324u_usbhid/rawhid_testinterface.c
blob: 48246c338d5b0193c9a91b4a251b680cd4c044ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdint.h>
#if defined(OS_LINUX) || defined(OS_MACOSX)
#include <sys/ioctl.h>
#include <termios.h>
#elif defined(OS_WINDOWS)
#include <conio.h>
#endif

#include "hid.h"


void sendstr(char * tosend)
{
  rawhid_send(0, tosend, strlen(tosend),1000);
}

int mtime_diff(struct timeval high,struct timeval low)
{
  int result=1000*(high.tv_sec-low.tv_sec);
  result+=high.tv_usec/1000-low.tv_usec/1000;
  return result;
}

#define BUF_LEN 64
#define NUM_DEVICE_IDS 3
uint16_t dev_ids[] = {0x0481, 0x0480, 0x0482};

int main (int argc, char *argv[])
{
    int r, num, d;
    char buf[BUF_LEN];
    // C-based example is 16C0:0480:FFAB:0200
    unsigned int attemtps = 100;
    while(attemtps--)
    {
        for (d=0; d<NUM_DEVICE_IDS; d++)
        {
            r = rawhid_open(1, 0x16C0, dev_ids[d], 0xFFAB, 0x0200);
            if (r > 0)
                break;
        }
        if (r > 0)
            break;
        else
            sleep(1.0);
    }
    if (r <= 0)
    {
        printf("no rawhid device found\n");
        return -1;
    }
    printf("found rawhid device\n\n");

    while (1)
    {
        num = rawhid_recv(0, buf, BUF_LEN, 250);
        if (num < 0)
        {
            printf("\nerror reading, device went offline\n");
            rawhid_close(0);
            return 0;
        }
        if (num > 0)
        {
            buf[num]='\0';
            printf("%s\n",buf);
            fflush(0);
        }
    }
}