X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=rf433rcv%2Fpc%2Frawhid_test.c;fp=rf433rcv%2Fpc%2Frawhid_test.c;h=7bea89248fc67ba51844261b45a6516ff835ef06;hb=deb2b278d460a716e0e2d8be370b1ee8f8963e34;hp=0000000000000000000000000000000000000000;hpb=1c4aea8ca6197b857498de2881b8bc594af1df49;p=svn42.git diff --git a/rf433rcv/pc/rawhid_test.c b/rf433rcv/pc/rawhid_test.c new file mode 100644 index 0000000..7bea892 --- /dev/null +++ b/rf433rcv/pc/rawhid_test.c @@ -0,0 +1,100 @@ +#include +#include +#include + +#if defined(OS_LINUX) || defined(OS_MACOSX) +#include +#include +#elif defined(OS_WINDOWS) +#include +#endif + +#include "hid.h" + + +static char get_keystroke(void); + + +int main() +{ + int i, r, num; + char c, buf[64]; + + // C-based example is 16C0:0480:FFAB:0200 + r = rawhid_open(1, 0x16C0, 0x0480, 0xFFAB, 0x0200); + if (r <= 0) { + // Arduino-based example is 16C0:0486:FFAB:0200 + r = rawhid_open(1, 0x16C0, 0x0486, 0xFFAB, 0x0200); + if (r <= 0) { + printf("no rawhid device found\n"); + return -1; + } + } + printf("found rawhid device\n"); + + while (1) { + // check if any Raw HID packet has arrived + num = rawhid_recv(0, buf, 64, 220); + if (num < 0) { + printf("\nerror reading, device went offline\n"); + rawhid_close(0); + return 0; + } + if (num > 0) { + printf("\nrecv %d bytes:\n", num); + for (i=0; i= 32) { + printf("\ngot key '%c', sending...\n", c); + buf[0] = c; + for (i=1; i<64; i++) { + buf[i] = 0; + } + rawhid_send(0, buf, 64, 100); + } + } +} + +#if defined(OS_LINUX) || defined(OS_MACOSX) +// Linux (POSIX) implementation of _kbhit(). +// Morgan McGuire, morgan@cs.brown.edu +static int _kbhit() { + static const int STDIN = 0; + static int initialized = 0; + int bytesWaiting; + + if (!initialized) { + // Use termios to turn off line buffering + struct termios term; + tcgetattr(STDIN, &term); + term.c_lflag &= ~ICANON; + tcsetattr(STDIN, TCSANOW, &term); + setbuf(stdin, NULL); + initialized = 1; + } + ioctl(STDIN, FIONREAD, &bytesWaiting); + return bytesWaiting; +} +static char _getch(void) { + char c; + if (fread(&c, 1, 1, stdin) < 1) return 0; + return c; +} +#endif + + +static char get_keystroke(void) +{ + if (_kbhit()) { + char c = _getch(); + if (c >= 32) return c; + } + return 0; +} + +